-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
80 lines (61 loc) · 2.55 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import cv2
import numpy as np
from tensorflow.keras.models import load_model
import openvino as ov
# Load OpenVINO Core
core = ov.Core()
# Load OpenVINO Model
ir_model = core.read_model('blink/blink_CNN.xml')
compiled_model = core.compile_model(model=ir_model)
output_key = compiled_model.output(0)
# Load Keras Model
model = load_model('blink/eye.h5', compile=False)
# Function to preprocess eye image
def preprocess_eye(eye):
eye = cv2.resize(eye, (80, 80))
eye = eye / 255.0
eye = np.expand_dims(eye, axis=0)
return eye
# Load Haarcascades for face and eyes detection
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_eye.xml")
# Open webcam
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, minNeighbors=15, scaleFactor=1.2, minSize=(25, 25))
for (x, y, w, h) in faces:
# Draw rectangular box around the detected face
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
# Extract face region
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
# Detect eyes in the face region
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex, ey, ew, eh) in eyes:
# Draw rectangular box around the detected eyes
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (255, 0, 0), 2)
# Preprocess eye for prediction
eye_input = preprocess_eye(roi_color[ey:ey + eh, ex:ex + ew])
# Run inference using OpenVINO model
prediction = compiled_model(eye_input)[output_key]
probability_open = prediction[0][1]
probability_open_percent = probability_open * 100
# Determine eye status
if probability_open > 0.5:
eye_status = "Open"
else:
eye_status = "Closed"
# Display eye status on the frame
cv2.putText(roi_color, f"Eye Status: {eye_status}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
# Display the frame
cv2.imshow('Eye Status Detection', frame)
# Break the loop if 'q' key is pressed
key = cv2.waitKey(1)
if key in {ord('q'), ord('Q'), 27}:
break
# Release the camera and close all OpenCV windows
cap.release()
cv2.destroyAllWindows()