-
Notifications
You must be signed in to change notification settings - Fork 0
/
sleep.py
52 lines (46 loc) · 1.98 KB
/
sleep.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
import cv2 as cv
from math import cos, sin, pi
import numpy as np
from scipy.spatial import distance
def eye_aspect_ratio(eye):
A = distance.euclidean(eye[1], eye[5])
B = distance.euclidean(eye[2], eye[4])
C = distance.euclidean(eye[0], eye[3])
ear = (A + B) / (2.0 * C)
return ear
winName = 'OpenVINO on Raspberry Pi'
cv.namedWindow(winName, cv.WINDOW_NORMAL)
faceDetectionNet = cv.dnn.readNet('models/face-detection-retail-0004.xml',
'models/face-detection-retail-0004.bin')
landmarkNet = cv.dnn.readNet('models/facial-landmarks-35-adas-0002.xml',
'models/facial-landmarks-35-adas-0002.bin')
faceDetectionNet.setPreferableTarget(cv.dnn.DNN_TARGET_MYRIAD)
landmarkNet.setPreferableTarget(cv.dnn.DNN_TARGET_MYRIAD)
cap = cv.VideoCapture(0)
while cv.waitKey(1) != 27:
hasFrame, frame = cap.read()
if not hasFrame:
break
frameHeight, frameWidth = frame.shape[0], frame.shape[1]
# Detect faces on the image.
blob = cv.dnn.blobFromImage(frame, size=(300, 300), ddepth=cv.CV_8U)
faceDetectionNet.setInput(blob)
detections = faceDetectionNet.forward()
for detection in detections.reshape(-1, 7):
confidence = float(detection[2])
if confidence > 0.5:
xmin = int(detection[3] * frameWidth)
ymin = int(detection[4] * frameHeight)
xmax = int(detection[5] * frameWidth)
ymax = int(detection[6] * frameHeight)
xmax = max(1, min(xmax, frameWidth - 1))
ymax = max(1, min(ymax, frameHeight - 1))
xmin = max(0, min(xmin, xmax - 1))
ymin = max(0, min(ymin, ymax - 1))
# Run head pose estimation network.
face = frame[ymin:ymax+1, xmin:xmax+1]
blob = cv.dnn.blobFromImage(face, size=(60, 60), ddepth=cv.CV_8U)
landmarkNet.setInput(blob)
landmarkOp = landmarkNet.forward()
print(len(landmarkOp[0]))
cv.imshow(winName, frame)