-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupperbody.py
69 lines (50 loc) · 1.38 KB
/
upperbody.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
import cv2
import pigpio
import time
#Setting up the PIO
pi = pigpio.pi()
pi.set_mode(4, pigpio.OUTPUT)
pi.set_servo_pulsewidth(4, 1500)
# Create a CascadeClassifier Object
cascade = cv2.CascadeClassifier("haarcascade_upperbody.xml")
# Setting up the camera frame
width = 160
height = 120
cap = cv2.VideoCapture(0)
cap.set(3, width)
cap.set(4, height)
if cap.isOpened():
ret, frame = cap.read()
else:
ret = False
current_angle = 90
center_thresh = 8
rate = 2
while ret:
# Reading the image as gray scale image
gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Search the co-ordinates of the image
upperbody = cascade.detectMultiScale(gray_img, scaleFactor = 1.05, minNeighbors=5)
# Sorting the faces on basis of area
upperbody = sorted(upperbody, key=lambda x:x[3]*x[2])
if len(upperbody)>0:
x,y,w,h = upperbody[-1]
x_medium = int((x+x+w)//2)
# Drawing the line on center of face
cv2.rectangle(frame, (x, y),(x+w, y+h), (0, 255, 0), 2)
# Moving the servo
if abs(x_medium - width//2)>center_thresh:
if x_medium > width//2:
current_angle += rate
if x_medium < width//2:
current_angle -=rate
dc = (1+current_angle/180)
pi.set_servo_pulsewidth(4, dc*1000)
cv2.imshow("Camera", frame)
if cv2.waitKey(1) == 27: # exit on ESC
break
ret, frame = cap.read()
# Flipping the frame
frame = cv2.flip(frame, 0)
cv2.destroyAllWindows()
cap.release()