-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
63 lines (49 loc) · 1.74 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
from neural import predict as PredictOpennes
from facedetection import getFace
from eyes_detection import getEyesBB, extractROI
import numpy as np
import cv2
import winsound
import sys
import time
print("[INFO] Loading camera")
cam = cv2.VideoCapture(0)
time.sleep(2)
print("[INFO] complete")
lCount = 0
rCount = 0
closedCount = 0
alarmRunning = False
while True:
retval, image = cam.read()
(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(
image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))
for box in getFace(blob, w, h):
left, right = getEyesBB(image, box)
lROI, rROI = extractROI(image, left, right)
lResult = PredictOpennes(lROI)[0][0]
rResult = PredictOpennes(rROI)[0][0]
lCount = lCount + 1 if lResult != 0 else lCount
rCount = rCount + 1 if rResult != 0 else rCount
threshold = 0.01
if lResult > threshold or rResult > threshold:
closedCount = 0
if alarmRunning:
alarmRunning = False
winsound.PlaySound(None, winsound.SND_ASYNC)
if lResult <= threshold and rResult <= threshold and not alarmRunning:
closedCount += 1
if closedCount > 10 and not alarmRunning:
alarmRunning = True
winsound.PlaySound(
"Alarm.wav", winsound.SND_ASYNC | winsound.SND_FILENAME | winsound.SND_ALIAS)
cv2.rectangle(image, left[0], left[1], (0, 0, 255))
cv2.rectangle(image, right[0], right[1], (0, 0, 255))
cv2.rectangle(image, (box[0], box[1]),
(box[2], box[3]), (255, 0, 0,))
cv2.imshow("Output", image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cam.release()
cv2.destroyAllWindows()