-
Notifications
You must be signed in to change notification settings - Fork 12
/
low_face_mode.py
96 lines (67 loc) · 2.36 KB
/
low_face_mode.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# dim the computer screen when not looking at it with opencv
from cv2 import cv2
import osascript
import argparse
import platform
from subprocess import call
from custom_recognition.recognize_faces_video import get_name
# if running on Linux, load linux scripts
if platform.system() == 'Linux':
with open("linux_scripts/brighten.sh") as f:
brighten_script = f.read()
with open("linux_scripts/dim.sh") as f:
dim_script = f.read()
# if running on MacOS, load apple scripts
elif platform.system() == 'Darwin':
with open("scripts/brighten.applescript") as brighten:
brighten_script = brighten.read()
with open("scripts/dim.applescript") as dim:
dim_script = dim.read()
# TODO: Add support on windows
def dim():
if platform.system() == 'Linux':
call(dim_script, shell=True)
elif platform.system() == 'Darwin':
osascript.osascript(dim_script)
def brighten():
if platform.system() == 'Linux':
call(brighten_script, shell=True)
elif platform.system() == 'Darwin':
osascript.osascript(brighten_script)
face_cascade = cv2.CascadeClassifier("data/haarcascade_frontalface_default.xml")
capture = cv2.VideoCapture(0)
face_counter = []
frames = 0
dimmed = False
while True:
_, img = capture.read()
frames += 1
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
face_counter.append(len(faces))
users, verified_users, rec = get_name(img)
if frames > 20:
if dimmed == False:
if 1 not in face_counter[frames - 20 : frames] or (
(bool(set(users) & set(verified_users)) == False) and rec == True
):
dim()
dimmed = True
if dimmed == True:
if rec == True:
if set(users) & set(verified_users):
if 1 in face_counter[frames - 20 : frames]:
brighten()
dimmed = False
else:
if 1 in face_counter[frames - 20 : frames]:
brighten()
dimmed = False
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.imshow("Low Face Mode On", img)
k = cv2.waitKey(30) & 0xFF
if k == 27 or k == 113:
break
capture.release()
cv2.destroyAllWindows()