-
Notifications
You must be signed in to change notification settings - Fork 0
/
webcam.py
executable file
·59 lines (52 loc) · 2.09 KB
/
webcam.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
import os
import cv2
import dlib
from model import predict
cap = cv2.VideoCapture(0)
# initialize hog + svm based face detector
hog_face_detector = dlib.get_frontal_face_detector()
boss_count = 0
while True:
ret, frame = cap.read()
if ret:
# Resize window
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
cv2.resizeWindow('frame', 1200, 800)
cv2.moveWindow('frame', 100, 100)
# Resize frame for decrease predict time
scale = 0.5
resize_frame = cv2.resize(frame, (int(frame.shape[1]*scale), int(frame.shape[0]*scale)))
faces_hog = hog_face_detector(resize_frame)
frame_h, frame_w, _ = frame.shape
reindex_x = lambda x: max(min(x, frame_w), 1)
reindex_y = lambda x: max(min(x, frame_h), 1)
# loop over detected faces
for face in faces_hog:
x = reindex_x(int(face.left() / scale))
y = reindex_y(int(face.top() / scale))
r = reindex_x(int(face.right() / scale))
b = reindex_y(int(face.bottom() / scale))
# draw box over face
padding = 0
crop_face = frame[y - padding:b + padding, x - padding:r + padding]
is_boss = predict(crop_face)
if is_boss:
cv2.putText(frame, "BOSS", (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 2,
(0, 0, 255), 4)
cv2.rectangle(frame, (x, y), (r, b), (0, 0, 255), 2)
boss_count += 1
# Open something
if boss_count > 3:
print ("Switch============================")
# os.system('open -n -b "com.microsoft.VSCode"')
boss_count = 0
else:
cv2.putText(frame, "NORMAL", (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 2,
(0, 255, 0), 4)
cv2.rectangle(frame, (x, y), (r, b), (0, 255, 0), 2)
cv2.imshow("frame", frame)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()