on_disk_advanced_frame_differencing.py
import csi
import os
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()
if "temp" not in os.listdir():
os.mkdir("temp")
print("About to save background image...")
csi0.snapshot(time=2000)
csi0.snapshot().save("temp/bg.bmp")
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("temp/bg.bmp", alpha=(255 - BG_UPDATE_BLEND))
img.save("temp/bg.bmp")
img.difference("temp/bg.bmp")
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)