-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDetectWink2.py
126 lines (105 loc) · 3.83 KB
/
DetectWink2.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import numpy as np
import cv2
import os
from os import listdir
from os.path import isfile, join
import sys
def detectWink(frame, location, ROI, cascade):
ROI = cv2.medianBlur(ROI, 3)
scaleFactor = 1.1 # range is from 1 to ..
minNeighbors = 8 # range is from 0 to ..
flag = 0 | cv2.CASCADE_SCALE_IMAGE # either 0 or 0|cv2.CASCADE_SCALE_IMAGE
minSize = (10, 10) # range is from (0,0) to ..
eyes = cascade.detectMultiScale(
ROI,
scaleFactor,
minNeighbors,
flag,
minSize
)
for e in eyes:
e[0] += location[0]
e[1] += location[1]
x, y, w, h = e[0], e[1], e[2], e[3]
cv2.rectangle(frame, (x,y), (x+w,y+h), (0, 0, 255), 2)
return len(eyes) == 1 # number of eyes is one
def detect(frame, faceCascade, eyesCascade):
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# possible frame pre-processing:
#gray_frame = cv2.medianBlur(gray_frame, 3)
gray_frame = cv2.equalizeHist(gray_frame)
# Detect Face
scaleFactor = 1.1 #1.15 # range is from 1 to ..
minNeighbors = 3 # range is from 0 to ..
flag = 0|cv2.CASCADE_SCALE_IMAGE # either 0 or 0|cv2.CASCADE_SCALE_IMAGE
minSize = (30, 30) #(30,30) # range is from (0,0) to ..
faces = faceCascade.detectMultiScale(
gray_frame,
scaleFactor,
minNeighbors,
flag,
minSize)
detected = 0
for f in faces:
x, y, w, h = f[0], f[1], f[2], f[3]
faceROI = gray_frame[y:y+h, x:x+w]
if detectWink(frame, (x, y), faceROI, eyesCascade):
detected += 1
cv2.rectangle(frame, (x,y), (x+w,y+h), (255, 0, 0), 2)
else:
cv2.rectangle(frame, (x,y), (x+w,y+h), (0, 255, 0), 2)
return detected
def run_on_folder(cascade1, cascade2, folder):
if(folder[-1] != "/"):
folder = folder + "/"
files = [join(folder,f) for f in listdir(folder) if isfile(join(folder,f))]
windowName = None
totalCount = 0
for f in files:
img = cv2.imread(f)
if type(img) is np.ndarray:
lCnt = detect(img, cascade1, cascade2)
totalCount += lCnt
if windowName != None:
cv2.destroyWindow(windowName)
windowName = f
cv2.namedWindow(windowName, cv2.WINDOW_AUTOSIZE)
cv2.imshow(windowName, img)
cv2.waitKey(0)
return totalCount
def runonVideo(face_cascade, eyes_cascade):
videocapture = cv2.VideoCapture(0)
if not videocapture.isOpened():
print("Can't open default video camera!")
exit()
windowName = "Live Video"
showlive = True
while(showlive):
ret, frame = videocapture.read()
if not ret:
print("Can't capture frame")
exit()
detect(frame, face_cascade, eyes_cascade)
cv2.imshow(windowName, frame)
if cv2.waitKey(30) >= 0:
showlive = False
# outside the while loop
videocapture.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
# check command line arguments: nothing or a folderpath
if len(sys.argv) != 1 and len(sys.argv) != 2:
print(sys.argv[0] + ": got " + len(sys.argv) - 1
+ "arguments. Expecting 0 or 1:[image-folder]")
exit()
# load pretrained cascades
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades
+ 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades
+ 'haarcascade_eye.xml')
if(len(sys.argv) == 2): # one argument
folderName = sys.argv[1]
detections = run_on_folder(face_cascade, eye_cascade, folderName)
print("Total of ", detections, "detections")
else: # no arguments
runonVideo(face_cascade, eye_cascade)