in_memory_advanced_frame_differencing.py
import csi
import image
import time
TRIGGER_THRESHOLD = 5
BG_UPDATE_FRAMES = 50
BG_UPDATE_BLEND = 128
csi0 = csi.CSI()
csi0.reset()
csi0.pixformat(csi.RGB565)
csi0.framesize(csi.QVGA)
csi0.snapshot(time=2000)
csi0.auto_whitebal(False)
clock = time.clock()
extra_fb = image.Image(csi0.width(), csi0.height(), csi0.pixformat())
print("About to save background image...")
csi0.snapshot(time=2000)
extra_fb.draw_image(csi0.snapshot())
print("Saved background image - Now frame differencing!")
triggered = False
frame_count = 0
while True:
clock.tick()
img = csi0.snapshot()
frame_count += 1
if frame_count > BG_UPDATE_FRAMES:
frame_count = 0
img.blend(extra_fb, alpha=(255 - BG_UPDATE_BLEND))
extra_fb.draw_image(img)
img.difference(extra_fb)
hist = img.get_histogram()
diff = hist.get_percentile(0.99).l_value - hist.get_percentile(0.90).l_value
triggered = diff > TRIGGER_THRESHOLD
print(clock.fps(), triggered)