mjpeg_on_movement.py
import csi
import time
import mjpeg
import os
import machine
import random
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()
m = mjpeg.Mjpeg("example-%d.mjpeg" % random.getrandbits(32))
clock = time.clock()
for i in range(200):
clock.tick()
m.write(csi0.snapshot())
print(clock.fps())
m.close()
led.off()
print("Restarting...")