snapshot_on_movement.py
import csi
import random
import os
import machine
csi0 = csi.CSI()
csi0.reset()
csi0.pixformat(csi.RGB565)
csi0.framesize(csi.QVGA)
csi0.snapshot(time=2000)
csi0.auto_whitebal(False)
led = machine.LED("LED_RED")
if not "temp" in os.listdir():
os.mkdir("temp")
while True:
print("About to save background image...")
csi0.snapshot(time=2000)
csi0.snapshot().save("temp/bg.bmp")
print("Saved background image - Now detecting motion!")
diff = 10
while diff:
img = csi0.snapshot()
img.difference("temp/bg.bmp")
stats = img.statistics()
if stats[5] > 20:
diff -= 1
led.on()
print("Movement detected! Saving image...")
csi0.snapshot().save("temp/snapshot-%d.jpg" % random.getrandbits(32))
led.off()