Created
May 16, 2013 06:37
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root = Tk() | |
frame=[] | |
for i in range(1,9): | |
fname=str(i)+".png" | |
frame+=[PhotoImage(file=fname)] | |
wrap = Canvas(root, width=180, height=50) | |
wrap.pack() | |
def diferencia(imagen1, imagen2): | |
pixeles1 = imagen1.load() | |
pixeles2 = imagen2.load() | |
x, y = imagen1.size | |
imagen_nueva_prom = Image.new("RGB", (x, y)) | |
cor = [] | |
for a in range(x): | |
for b in range(y): | |
try: | |
dif = pixeles1[a, b][0]-pixeles2[a,b][0] | |
if dif > 255: | |
dif = 255 | |
if dif < 0: | |
dif = 0 | |
cor.append((a,b)) | |
imagen_nueva_prom.putpixel((a, b), (dif, dif, dif)) | |
except: | |
pass | |
return imagen_nueva_prom, cor | |
def do_animation(currentframe): | |
def do_image(): | |
wrap.create_image(0,0, anchor=NW, image=frame[currentframe], tag='ani') | |
i1 = cambiar_agrises(str(currentframe)+".png") | |
i2 = cambiar_agrises(str(currentframe+1)+".png") | |
diff, cor = diferencia(i1, i2) | |
histo = diff.histogram() | |
lenH = sum(histo) | |
prob = [] | |
for i in histo: | |
prob.append(float(i)/lenH) | |
entropia = [] | |
for i in prob: | |
if i != 0: | |
entropia.append(i * log(i, 2)) | |
entropia = - sum(entropia) | |
diff, cor = diferencia(i1, i2) | |
if entropia > 2.0: | |
print "Movimiento detectado" | |
wrap.create_line(cor[0][0], cor[0][1], cor[len(cor)-1][0], cor[len(cor)-1][1], arrow="last") | |
else: | |
print "No hay movimiento" | |
wrap.delete('ani') | |
try: | |
do_image() | |
except IndexError: | |
currentframe = 1 | |
do_image() | |
wrap.update_idletasks() | |
currentframe = currentframe + 1 | |
root.after(500, do_animation, currentframe) | |
root.after(5, do_animation, 0) | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment