forked from waveform80/picamera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgesture_detect.py
51 lines (47 loc) · 1.83 KB
/
gesture_detect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
import numpy as np
import picamera
from picamera.array import PiMotionAnalysis
class GestureDetector(PiMotionAnalysis):
QUEUE_SIZE = 10 # the number of consecutive frames to analyze
THRESHOLD = 4.0 # the minimum average motion required in either axis
def __init__(self, camera):
super(GestureDetector, self).__init__(camera)
self.x_queue = np.zeros(self.QUEUE_SIZE, dtype=np.float)
self.y_queue = np.zeros(self.QUEUE_SIZE, dtype=np.float)
self.last_move = ''
def analyze(self, a):
# Roll the queues and overwrite the first element with a new
# mean (equivalent to pop and append, but faster)
self.x_queue[1:] = self.x_queue[:-1]
self.y_queue[1:] = self.y_queue[:-1]
self.x_queue[0] = a['x'].mean()
self.y_queue[0] = a['y'].mean()
# Calculate the mean of both queues
x_mean = self.x_queue.mean()
y_mean = self.y_queue.mean()
# Convert left/up to -1, right/down to 1, and movement below
# the threshold to 0
x_move = (
'' if abs(x_mean) < self.THRESHOLD else
'left' if x_mean < 0.0 else
'right')
y_move = (
'' if abs(y_mean) < self.THRESHOLD else
'down' if y_mean < 0.0 else
'up')
# Update the display
movement = ('%s %s' % (x_move, y_move)).strip()
if movement != self.last_move:
self.last_move = movement
if movement:
print(movement)
with picamera.PiCamera(resolution='VGA', framerate=24) as camera:
with GestureDetector(camera) as detector:
camera.start_recording(
os.devnull, format='h264', motion_output=detector)
try:
while True:
camera.wait_recording(1)
finally:
camera.stop_recording()