-
Notifications
You must be signed in to change notification settings - Fork 0
/
move_async.py
78 lines (69 loc) · 2.13 KB
/
move_async.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import freenect
import cv2
import frame_convert2
import random
import time
cv2.namedWindow("RGB", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("RGB",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
keep_running = True
last_time = 0
faceFound = False
last_x = 0
last_y = 0
last_w = 0
last_h = 0
tilt = 0
timeout = 0
def display_depth(dev, data, timestamp):
global keep_running
def display_rgb(dev, data, timestamp):
global keep_running, tilt, faceFound, timeout
image = cv2.cvtColor(data, cv2.COLOR_RGB2BGR)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 4)
if len(faces) != 0:
faceFound = True
if timeout < 20:
timeout += 1
for (x,y,w,h) in faces:
last_x = x
last_y = y
last_w = w
last_h = h
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
image_height = image.shape[1::-1][1]
steps = image_height / 30
tilt = 30 - round(y / steps)
if len(faces) == 0:
faceFound = False
if timeout > 14:
timeout -= 1
tilt = 0
if timeout >= 15:
cv2.rectangle(image,(last_x,last_y),(last_x+last_w,last_y+last_h),(255,0,0),2)
image_height = image.shape[1::-1][1]
steps = image_height / 30
tilt = 30 - round(y / steps)
cv2.imshow('RGB', image)
if cv2.waitKey(10) == 27:
keep_running = False
def body(dev, ctx):
global last_time, tilt0
if not keep_running:
raise freenect.Kill
if time.time() - last_time < 1:
return
last_time = time.time()
led = 2
if faceFound and abs(tilt - tilt0) > 0:
led = 3
if faceFound and abs(tilt - tilt0) == 0:
led = 4
tilt0 = tilt
freenect.set_led(dev, led)
freenect.set_tilt_degs(dev, tilt)
print('led[%d] tilt[%d] accel[%s]' % (led, tilt, freenect.get_accel(dev)))
if not keep_running:
raise freenect.Kill
freenect.runloop(depth=display_depth, video=display_rgb, body=body)