-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
80 lines (61 loc) · 2.9 KB
/
main.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
79
80
import cv2
from face_recognition import FaceRecognition
from body_recognition import BodyRecognition
from serial_connection import create_connection, get_port_name
from arduino import Arduino
import time
import json
if __name__ == '__main__':
# getting the names of the available arduino ports and creating a serial connection
# conn = create_connection(port_name)
board = Arduino(get_port_name())
# video cap.
cap = cv2.VideoCapture(0)
# instantiating the different classification libraris
face_detector = FaceRecognition()
body_detector = BodyRecognition()
while True:
# getting feed from webcam
success, img = cap.read()
ih, iw, ic = img.shape
# cross-air
xmid, ymid = (int(iw/2), int(ih/2))
# creates the landmarks for the face and the body
body_recs = body_detector.get_recognitions(img = img)
face_recs = face_detector.get_recognitions(img = img)
# updates the image to draw landmarks
body_detector.draw_landmarks(img=img)
face_detector.draw_landmarks(img = img)
# cross hair
cv2.putText(img, "+", (xmid, ymid), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
if (face_recs != None):
# if there is a face found
if(len(face_recs) >= 1):
index = 0
current = face_recs[0].location_data.relative_bounding_box
xmin, ymin, xmax, ymax = int(current.xmin * iw), int(current.ymin * ih), int((current.xmin + current.width)*iw), int((current.ymin + current.height)*ih)
x_face_mid, y_face_mid = xmin + int(xmax-xmin)/2, ymin + int(ymax - ymin) / 2
cv2.putText(img, "+", (int(x_face_mid), int(y_face_mid)), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
# checking to make sure the cross air is on the head
# if ((xmin < xmid and xmid < xmax) and (ymin < ymid and ymid < ymax)):
# board.shoot()
# else:
# board.stop_shooting()
# logic to check wether the sero is pointed to the right angle
if(xmin < xmid < xmax):
print("shoot")
else:
# logic to turn servo
if(xmid > xmin):
cv2.putText(img, "left", (100,100), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
print("dont shoot")
board.turn(board.servo_angle+1.5)
if(xmid < xmax):
cv2.putText(img, "right", (100,100), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
print('dont shoot')
board.turn(board.servo_angle-1.5)
else:
# board.stop_shooting()
pass
cv2.imshow("Image", img)
cv2.waitKey(1)