Code: Select all
#!/usr/bin/python
import numpy as np
import picamera
import picamera.array
import datetime
minimum_still_interval = 5
motion_detected = False
camera = picamera.PiCamera()
last_still_capture_time = datetime.datetime.now()
# The 'analyse' method gets called on every frame processed while picamera
# is recording h264 video. It gets an array (a) of motion vectors from the GPU.
class DetectMotion(picamera.array.PiMotionAnalysis):
def analyse(self, a):
if datetime.datetime.now() > last_still_capture_time + \
datetime.timedelta(seconds=minimum_still_interval):
a = np.sqrt(
np.square(a['x'].astype(np.float)) +
np.square(a['y'].astype(np.float))
).clip(0, 255).astype(np.uint8)
# If there're more than 10 vectors with a magnitude greater
# than 60, then say we've detected motion
if (a > 60).sum() > 10:
print('movement!')
motion_detected = True
with DetectMotion(camera) as output:
try:
camera.resolution = (640, 480)
camera.framerate= 10
camera.start_recording('/dev/null', format='h264', motion_output=output)
while True: # loop forever
while not motion_detected: # this never fails the test! wth?! :(
print('waiting')
camera.wait_recording(1)
print ('stopping recording') # we never reach this point :evil:
camera.stop_recording()
motion_detected = False
filename = '/home/pi/picam/img' + \
datetime.datetime.now().strftime('%Y-%m-%dT%H.%M.%S.%f') + '.jpg'
print('capturing %s' % filename)
camera.capture(filename, format='jpeg', use_video_port=True)
camera.start_recording('/dev/null', format='h264', motion_output=output)
finally:
camera.close()
Code: Select all
waiting
waiting
waiting
movement!
waiting
waiting
movement!
waiting
movement!
waiting
movement!
.
.
.To simplify, I'm trying to do the following pseudocode with Python, but failing to terminate the loop:
Code: Select all
exit = false
function processStuff() {
...
exit = true
}
while (exit == false) {
invoke (processStuff)
}
print ('done') // if this were Python. we'd never get here.