-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlink_Eye_Detect.py
More file actions
57 lines (48 loc) · 1.93 KB
/
Blink_Eye_Detect.py
File metadata and controls
57 lines (48 loc) · 1.93 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
import cv2
import os
import playsound
#Initializing the face and eye cascade classifiers from xml files
face_cascade = cv2.CascadeClassifier('Input/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('Input/haarcascade_eye_tree_eyeglasses.xml')
#Variable store execution state
first_read = True
#Starting the video capture
cap = cv2.VideoCapture(0)
# cap = cv2.VideoCapture('Detect_Eyes2.mp4')
ret,img = cap.read()
while(ret):
ret,img = cap.read()
#Coverting the recorded image to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#Applying filter to remove impurities
gray = cv2.bilateralFilter(gray,5,1,1)
#Detecting the face for region of image to be fed to eye classifier
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
if(len(faces)>0):
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
#roi_face is face which is input to eye classifier
roi_face = gray[y:y+h,x:x+w]
roi_face_clr = img[y:y+h,x:x+w]
eyes = eye_cascade.detectMultiScale(roi_face,1.287,4) # 1.293,4
print(eyes)
#Examining the length of eyes object for eyes
if(len(eyes)>=2):
#Check if program is running for detection
if(first_read):
cv2.putText(img, "Eyes Open", (70,70), cv2.FONT_HERSHEY_COMPLEX,1,(255,0,0),2)
else:
if(first_read):
cv2.putText(img, "Eyes Closed", (70,70), cv2.FONT_HERSHEY_COMPLEX, 1,(0,0,255),2)
# playsound.playsound("Input/sound/warning-sound-6686.mp3")
else:
cv2.putText(img,"No face detected",(100,100),cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0),2)
#Controlling the algorithm with keys
cv2.imshow('img',img)
a = cv2.waitKey(20)
if(a>0):
break
cap.release()
cv2.destroyAllWindows()
cv2.waitKey(100)
os.system("python home.py")