Ziv Pollak commited on
Commit
15b346a
1 Parent(s): 01d96f4

open eyes detector

Browse files
Files changed (1) hide show
  1. app.py +61 -9
app.py CHANGED
@@ -23,6 +23,8 @@ def isEyeOpen(image):
23
  hist = cv2.calcHist([image], [0], None, [256], [0, 256])
24
  colors = np.where(hist > 10)
25
 
 
 
26
  if np.mean(colors) < 25:
27
  return True
28
  else:
@@ -38,7 +40,9 @@ def findIris(input_img1, input_img2, input_img3, input_img4, input_img5):
38
  with mp_face_mesh.FaceMesh(max_num_faces=1, refine_landmarks=True,
39
  static_image_mode=True,
40
  min_detection_confidence=0.45) as face_mesh:
41
- for image in images:
 
 
42
  if image is None:
43
  continue
44
 
@@ -77,9 +81,6 @@ def findIris(input_img1, input_img2, input_img3, input_img4, input_img5):
77
  rightIris_top = (int(right_iris[1][0]), int(right_iris[1][1]))
78
  rightIris_bottom = (int(right_iris[3][0]), int(right_iris[3][1]))
79
 
80
-
81
-
82
-
83
  sizeIncrease = 0
84
  leftEye = annotated_image[leftIris_top[1] - sizeIncrease : leftIris_bottom[1] + sizeIncrease,
85
  leftIris_leftside[0] - sizeIncrease : leftIris_rightside[0] + sizeIncrease]
@@ -89,24 +90,33 @@ def findIris(input_img1, input_img2, input_img3, input_img4, input_img5):
89
  rightIris_leftside[0] - sizeIncrease: rightIris_rightside[0] + sizeIncrease]
90
  rightEyeOpen = isEyeOpen(rightEye)
91
 
 
 
 
 
92
  if leftEyeOpen:
93
- cv2.putText(annotated_image, "Left Open",
94
  (rightIris_leftside[0] - 20, leftIris_top[1] - 10), cv2.FONT_HERSHEY_SIMPLEX,
95
  1, (255, 255, 0), 1, cv2.LINE_AA)
96
  else:
97
- cv2.putText(annotated_image, "Left Closed",
98
  (rightIris_leftside[0] - 20, leftIris_top[1] - 10), cv2.FONT_HERSHEY_SIMPLEX,
99
  1, (255, 255, 0), 1, cv2.LINE_AA)
100
-
 
 
 
 
101
  if rightEyeOpen:
102
- cv2.putText(annotated_image, "Right Open",
103
  (rightIris_leftside[0] - 20, rightIris_top[1] + 50), cv2.FONT_HERSHEY_SIMPLEX,
104
  1, (255, 255, 0), 1, cv2.LINE_AA)
105
  else:
106
- cv2.putText(annotated_image, "Right Closed",
107
  (rightIris_leftside[0] - 20, rightIris_top[1] + 50), cv2.FONT_HERSHEY_SIMPLEX,
108
  1, (255, 255, 0), 1, cv2.LINE_AA)
109
  #leftEye = cv2.cvtColor(leftEye, cv2.COLOR_BGR2GRAY)
 
110
 
111
  '''
112
  cv2.circle(annotated_image,
@@ -147,8 +157,50 @@ def findIris(input_img1, input_img2, input_img3, input_img4, input_img5):
147
  right = rightIris_rightside[0] + 150
148
  up = leftIris_top[1] - 50
149
  down = leftIris_bottom[1] + 50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  annotated_image = annotated_image[up:down, left:right]
151
 
 
 
152
  x1 = (leftIris_leftside[0] - nose[0] + leftIris_rightside[0] - nose[0]) / 2
153
  y1 = (leftIris_top[1] - nose[1] + leftIris_bottom[1] - nose[1]) / 2
154
  x2 = (rightIris_leftside[0] - nose[0] + rightIris_rightside[0] - nose[0]) / 2
 
23
  hist = cv2.calcHist([image], [0], None, [256], [0, 256])
24
  colors = np.where(hist > 10)
25
 
26
+ return np.mean(colors)
27
+
28
  if np.mean(colors) < 25:
29
  return True
30
  else:
 
40
  with mp_face_mesh.FaceMesh(max_num_faces=1, refine_landmarks=True,
41
  static_image_mode=True,
42
  min_detection_confidence=0.45) as face_mesh:
43
+ #for image in images:
44
+ for id, image in enumerate(images):
45
+
46
  if image is None:
47
  continue
48
 
 
81
  rightIris_top = (int(right_iris[1][0]), int(right_iris[1][1]))
82
  rightIris_bottom = (int(right_iris[3][0]), int(right_iris[3][1]))
83
 
 
 
 
84
  sizeIncrease = 0
85
  leftEye = annotated_image[leftIris_top[1] - sizeIncrease : leftIris_bottom[1] + sizeIncrease,
86
  leftIris_leftside[0] - sizeIncrease : leftIris_rightside[0] + sizeIncrease]
 
90
  rightIris_leftside[0] - sizeIncrease: rightIris_rightside[0] + sizeIncrease]
91
  rightEyeOpen = isEyeOpen(rightEye)
92
 
93
+ cv2.putText(annotated_image, "Left " + str(int(leftEyeOpen)),
94
+ (rightIris_leftside[0] - 20, leftIris_top[1] - 10), cv2.FONT_HERSHEY_SIMPLEX,
95
+ 1, (255, 255, 0), 1, cv2.LINE_AA)
96
+ '''
97
  if leftEyeOpen:
98
+ cv2.putText(annotated_image, "Left Open " + str(leftEyeOpen),
99
  (rightIris_leftside[0] - 20, leftIris_top[1] - 10), cv2.FONT_HERSHEY_SIMPLEX,
100
  1, (255, 255, 0), 1, cv2.LINE_AA)
101
  else:
102
+ cv2.putText(annotated_image, "Left Closed " + str(leftEyeOpen),
103
  (rightIris_leftside[0] - 20, leftIris_top[1] - 10), cv2.FONT_HERSHEY_SIMPLEX,
104
  1, (255, 255, 0), 1, cv2.LINE_AA)
105
+ '''
106
+ cv2.putText(annotated_image, "Right " + str(int(rightEyeOpen)),
107
+ (rightIris_leftside[0] - 20, rightIris_top[1] + 50), cv2.FONT_HERSHEY_SIMPLEX,
108
+ 1, (255, 255, 0), 1, cv2.LINE_AA)
109
+ '''
110
  if rightEyeOpen:
111
+ cv2.putText(annotated_image, "Right Open " + str(rightEyeOpen),
112
  (rightIris_leftside[0] - 20, rightIris_top[1] + 50), cv2.FONT_HERSHEY_SIMPLEX,
113
  1, (255, 255, 0), 1, cv2.LINE_AA)
114
  else:
115
+ cv2.putText(annotated_image, "Right Closed " + str(rightEyeOpen),
116
  (rightIris_leftside[0] - 20, rightIris_top[1] + 50), cv2.FONT_HERSHEY_SIMPLEX,
117
  1, (255, 255, 0), 1, cv2.LINE_AA)
118
  #leftEye = cv2.cvtColor(leftEye, cv2.COLOR_BGR2GRAY)
119
+ '''
120
 
121
  '''
122
  cv2.circle(annotated_image,
 
157
  right = rightIris_rightside[0] + 150
158
  up = leftIris_top[1] - 50
159
  down = leftIris_bottom[1] + 50
160
+
161
+ boundryIncrease = 10
162
+ print("leftside = ", leftIris_leftside[0])
163
+ print("rightside =", leftIris_rightside[0])
164
+ #print("left = ", left)
165
+ #print("right = ", right)
166
+
167
+ '''
168
+ if (id == 1): # Looking Left so we want the right limbus
169
+ print("Id == 1")
170
+ annotated_image = annotated_image[
171
+ leftIris_top[1] : leftIris_bottom[1],
172
+ leftIris_rightside[0] - boundryIncrease : leftIris_rightside[0] + boundryIncrease
173
+ ]
174
+ annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2GRAY)
175
+ #annotated_image = cv2.GaussianBlur(annotated_image, (3, 3), sigmaX=0, sigmaY=0)
176
+ annotated_image = cv2.Canny(annotated_image, 50, 125)
177
+ #annotated_image = cv2.Sobel(src=annotated_image, ddepth=cv2.CV_64F, dx=0, dy=1,
178
+ # ksize=5) # Sobel Edge Detection on the Y axis
179
+
180
+ contours = cv2.findContours(image, cv2.RETR_LIST, cv2.RETR_EXTERNAL)
181
+ contours = sorted(contours, key=cv2.contourArea, reverse=True)
182
+ for i in range(len(contours)):
183
+ contour = contours[i]
184
+ random.seed(i)
185
+ color = (255 * random.random(), 255 * random.random(), 255 * random.random())
186
+ cv2.drawContours(img, [contour], -1, color, 3)
187
+
188
+
189
+
190
+ elif (id == 2): # Looking Right so we want the left limbus
191
+ annotated_image = annotated_image[
192
+ leftIris_top[1]: leftIris_bottom[1],
193
+ leftIris_leftside[0] - boundryIncrease: leftIris_leftside[0] + boundryIncrease
194
+ ]
195
+ annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2GRAY)
196
+ # annotated_image = cv2.GaussianBlur(annotated_image, (3, 3), sigmaX=0, sigmaY=0)
197
+ annotated_image = cv2.Canny(annotated_image, 50, 125)
198
+ else:
199
+ '''
200
  annotated_image = annotated_image[up:down, left:right]
201
 
202
+
203
+
204
  x1 = (leftIris_leftside[0] - nose[0] + leftIris_rightside[0] - nose[0]) / 2
205
  y1 = (leftIris_top[1] - nose[1] + leftIris_bottom[1] - nose[1]) / 2
206
  x2 = (rightIris_leftside[0] - nose[0] + rightIris_rightside[0] - nose[0]) / 2