-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
72 lines (60 loc) · 2.74 KB
/
Copy pathapp.py
File metadata and controls
72 lines (60 loc) · 2.74 KB
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
import os, time
import cv2
import numpy as np
def run():
while True:
time.sleep(1)
cmd = "ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=(pop @F)/1000000000; print $idle}'"
result = os.popen(cmd)
str = result.read()
idle_time = int(str.split(".")[0])
print('user idle time', idle_time)
seconds_face_not_detected = 0
if idle_time >= 5:
while True:
front_face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
profile_face_cascade = cv2.CascadeClassifier('haarcascade_profileface.xml')
cap = cv2.VideoCapture(0)
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
front_face = front_face_cascade.detectMultiScale(gray, 1.3, 5)
profile_face = profile_face_cascade.detectMultiScale(gray, 1.3, 5)
face_detected = False
for (x, y, w, h) in front_face:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
face_detected = True
print('face_detected front face', face_detected)
seconds_face_not_detected = 0
cap.release()
cv2.destroyAllWindows()
print('waiting 5 seconds tobe called again if no activity')
time.sleep(5)
run()
break
for (x, y, w, h) in profile_face:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
face_detected = True
print('face_detected profile face', face_detected)
seconds_face_not_detected = 0
cap.release()
cv2.destroyAllWindows()
print('waiting 5 seconds tobe called again if no activity')
time.sleep(5)
run()
break
if not face_detected:
seconds_face_not_detected += 1
if seconds_face_not_detected > 10:
print('LOCK SCREEN')
cap.release()
cv2.destroyAllWindows()
sleep_cmd = """osascript -e 'ignoring application responses' -e 'tell application "Finder" to sleep' -e end"""
os.system(sleep_cmd)
break
cv2.imshow('img', img)
print('FACE_DETECTED', face_detected)
print('seconds_face_not_detected', seconds_face_not_detected)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
run()