-
-
Save eWizardII/6757364 to your computer and use it in GitHub Desktop.
Full Program
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
from PIL import Image | |
import numpy as np | |
from PIL import ImageChops | |
import subprocess | |
import math | |
import datetime | |
from PIL import ImageFont | |
from PIL import ImageDraw | |
from collections import deque | |
def image_entropy(prev, curr, smasumm, values): | |
im1 = Image.open(prev) | |
im2 = Image.open(curr) | |
img = ImageChops.difference(im1, im2) | |
w, h = img.size | |
a = np.array(img.convert('RGB')).reshape((w * h, 3)) | |
h, e = np.histogramdd(a, bins=(16,) * 3, range=((0, 256),) * 3) | |
prob = h / np.sum(h) # normalize | |
prob = prob[prob > 0] # remove zeros | |
comp = -np.sum(prob * np.log2(prob)) | |
framsd = SD(values, smasumm) | |
information = 'ENT: ' + str('{0:.2f}'.format(comp)) + ' SMA: ' + str('{0:.2f}'.format(smasumm)) + ' LB: ' + str( | |
'{0:.2f}'.format(smasumm - framsd)) + ' UB: ' + str('{0:.2f}'.format(smasumm + framsd)) | |
cimg = Image.open(curr) | |
draw = ImageDraw.Draw(cimg) | |
font = ImageFont.truetype("arial.ttf", 24) | |
draw.text((0, 0), information, (0, 255, 0), font=font) | |
cimg.save(curr) | |
return comp | |
def SD(values, mean): | |
size = len(values) | |
sumsd = 0.0 | |
for n in range(0, size): | |
sumsd += math.sqrt((values[n] - mean) ** 2) | |
return math.sqrt((1.0 / (size - 1)) * (sumsd / size)) | |
try: | |
print "Initial Image ..." | |
time = datetime.datetime.now() | |
filename = "image_" | |
subprocess.call( | |
"raspistill -t 1000 -ex night -awb auto -w 720 -h 480 -o %s" % filename + '1.jpg', | |
shell=True) | |
prev = filename + '1.jpg' | |
i = 1 | |
summ = smasumm = 0.0 | |
period = n = 10 | |
values = deque([0.0] * period) | |
while True: | |
time = datetime.datetime.now() | |
filename = "image_" + str(i) + ".jpg" | |
subprocess.call( | |
"raspistill -t 1000 -ex night -awb auto -w 720 -h 480 -o %s" % filename, | |
shell=True) | |
i += 1 | |
curr = filename | |
sma = image_entropy(prev, curr, smasumm, values) | |
values.append(sma) | |
summ += sma - values.popleft() | |
smasumm = summ / n | |
prev = curr | |
print(datetime.datetime.now() - time) | |
except KeyboardInterrupt: | |
print " Quit" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment