<s> from __future__ import division <EOL> import time <EOL> import os <EOL> import subprocess <EOL> import datetime <EOL> import picamera <EOL> import picamera . array <EOL> import numpy as np <EOL> class CaptureHandler : <EOL> def __init__ ( self , camera , post_capture_callback = None ) : <EOL> self . camera = camera <EOL> self . callback = post_capture_callback <EOL> self . detected = False <EOL> self . working = False <EOL> self . i = <NUM_LIT:0> <EOL> def motion_detected ( self ) : <EOL> if not self . working : <EOL> self . detected = True <EOL> def tick ( self ) : <EOL> if self . detected : <EOL> print "<STR_LIT>" <EOL> self . working = True <EOL> self . detected = False <EOL> self . i += <NUM_LIT:1> <EOL> path = "<STR_LIT>" % datetime . datetime . now ( ) . isoformat ( ) <EOL> os . makedirs ( path ) <EOL> self . camera . start_preview ( ) <EOL> for x in range ( <NUM_LIT:1> , <NUM_LIT:16> ) : <EOL> filename = "<STR_LIT>" % x <EOL> self . camera . capture ( path + filename , use_video_port = True ) <EOL> print "<STR_LIT>" + filename <EOL> self . camera . stop_preview ( ) <EOL> print "<STR_LIT>" <EOL> montage_file = path + '<STR_LIT>' <EOL> subprocess . call ( "<STR_LIT>" + path + "<STR_LIT>" + montage_file , shell = True ) <EOL> print "<STR_LIT>" <EOL> if self . callback : <EOL> self . callback ( montage_file ) <EOL> self . working = False <EOL> class MyMotionDetector ( picamera . array . PiMotionAnalysis ) : <EOL> def __init__ ( self , camera , handler ) : <EOL> super ( MyMotionDetector , self ) . __init__ ( camera ) <EOL> self . handler = handler <EOL> self . first = True <EOL> def analyse ( self , a ) : <EOL> a = np . sqrt ( <EOL> np . square ( a [ '<STR_LIT:x>' ] . astype ( np . float ) ) + <EOL> np . square ( a [ '<STR_LIT:y>' ] . astype ( np . float ) ) <EOL> ) . clip ( <NUM_LIT:0> , <NUM_LIT:255> ) . astype ( np . uint8 ) <EOL> if ( a > <NUM_LIT> ) . sum ( ) > <NUM_LIT:50> : <EOL> if self . first : <EOL> self . first = False <EOL> return <EOL> self . handler . motion_detected ( ) <EOL> class PiMotion : <EOL> def __init__ ( self , verbose = False , post_capture_callback = None ) : <EOL> self . verbose = verbose <EOL> self . post_capture_callback = post_capture_callback <EOL> def __print ( self , str ) : <EOL> if self . verbose : <EOL> print str <EOL> def start ( self ) : <EOL> with picamera . PiCamera ( ) as camera : <EOL> camera . resolution = ( <NUM_LIT> , <NUM_LIT> ) <EOL> camera . framerate = <NUM_LIT:10> <EOL> handler = CaptureHandler ( camera , self . post_capture_callback ) <EOL> self . __print ( '<STR_LIT>' ) <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> try : <EOL> self . __print ( '<STR_LIT>' ) <EOL> camera . start_recording ( <EOL> '<STR_LIT>' , format = '<STR_LIT>' , <EOL> motion_output = MyMotionDetector ( camera , handler ) <EOL> ) <EOL> while True : <EOL> handler . tick ( ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> finally : <EOL> camera . stop_recording ( ) <EOL> self . __print ( '<STR_LIT>' ) </s>
