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

open eyes detector

Browse files
Files changed (1) hide show
  1. app.py +55 -11
app.py CHANGED
@@ -2,7 +2,8 @@ import gradio as gr
2
  import mediapipe as mp
3
  import cv2
4
  import pandas as pd
5
- from statistics import mean
 
6
 
7
  # Run simple face mesh
8
  mp_face_mesh = mp.solutions.face_mesh
@@ -17,6 +18,16 @@ movementRight = pd.DataFrame(index=['Up', 'Center', 'Down'], columns=['Left', 'C
17
  # TO DO:
18
  # 1. Calibration screen
19
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  def findIris(input_img1, input_img2, input_img3, input_img4, input_img5):
22
  global pupilLocation
@@ -49,13 +60,13 @@ def findIris(input_img1, input_img2, input_img3, input_img4, input_img5):
49
  for p in leftIrisPoints:
50
  point = [int(face_landmarks.landmark[p].x * width), int(face_landmarks.landmark[p].y * height)]
51
  left_iris.append(point)
52
- cv2.circle(annotated_image, point, 1, (255, 0, 255), 2)
53
 
54
  right_iris = []
55
  for p in rightIrisPoints:
56
  point = [int(face_landmarks.landmark[p].x * width), int(face_landmarks.landmark[p].y * height)]
57
  right_iris.append(point)
58
- cv2.circle(annotated_image, point, 1, (255, 0, 255), 2)
59
 
60
  leftIris_leftside = (int(left_iris[2][0]), int(left_iris[2][1]))
61
  leftIris_rightside = (int(left_iris[0][0]), int(left_iris[0][1]))
@@ -66,6 +77,37 @@ def findIris(input_img1, input_img2, input_img3, input_img4, input_img5):
66
  rightIris_top = (int(right_iris[1][0]), int(right_iris[1][1]))
67
  rightIris_bottom = (int(right_iris[3][0]), int(right_iris[3][1]))
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  '''
70
  cv2.circle(annotated_image,
71
  (int((leftIris_leftside[0] + leftIris_rightside[0]) / 2),
@@ -82,11 +124,7 @@ def findIris(input_img1, input_img2, input_img3, input_img4, input_img5):
82
  (0, 255, 255), 2)
83
  '''
84
 
85
- left = leftIris_leftside[0] - 150
86
- right = rightIris_rightside[0] + 150
87
- up = leftIris_top[1] - 50
88
- down = leftIris_bottom[1] + 50
89
- annotated_image = annotated_image[up:down, left:right]
90
 
91
  name = 'TBD'
92
  newRow = pd.Series([name,
@@ -105,15 +143,21 @@ def findIris(input_img1, input_img2, input_img3, input_img4, input_img5):
105
  #filename = directoy_name + 'Analysis/' + name[0:-4] + '-analysis.jpg'
106
  #cv2.imwrite(filename, annotated_image)
107
 
 
 
 
 
 
 
108
  x1 = (leftIris_leftside[0] - nose[0] + leftIris_rightside[0] - nose[0]) / 2
109
  y1 = (leftIris_top[1] - nose[1] + leftIris_bottom[1] - nose[1]) / 2
110
  x2 = (rightIris_leftside[0] - nose[0] + rightIris_rightside[0] - nose[0]) / 2
111
  y2 = (rightIris_top[1] - nose[1] + rightIris_bottom[1] - nose[1]) / 2
112
  print("Slope=", (y2 - y1) / (x2 - x1))
113
  text = "Slope=" + str(round((y2 - y1) / (x2 - x1), 2))
114
- cv2.putText(annotated_image, text,
115
- (5, 110), cv2.FONT_HERSHEY_SIMPLEX,
116
- 1, (255, 255, 0), 1, cv2.LINE_AA)
117
 
118
  print("left iris size in pixels = ", abs(leftIris_leftside[0] - leftIris_rightside[0]))
119
  print("Right iris size in pixels = ", abs(rightIris_leftside[0] - rightIris_rightside[0]))
 
2
  import mediapipe as mp
3
  import cv2
4
  import pandas as pd
5
+ from statistics import mean, stdev
6
+ import numpy as np
7
 
8
  # Run simple face mesh
9
  mp_face_mesh = mp.solutions.face_mesh
 
18
  # TO DO:
19
  # 1. Calibration screen
20
 
21
+ def isEyeOpen(image):
22
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
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:
29
+ return False
30
+
31
 
32
  def findIris(input_img1, input_img2, input_img3, input_img4, input_img5):
33
  global pupilLocation
 
60
  for p in leftIrisPoints:
61
  point = [int(face_landmarks.landmark[p].x * width), int(face_landmarks.landmark[p].y * height)]
62
  left_iris.append(point)
63
+ #cv2.circle(annotated_image, point, 1, (255, 0, 255), 2)
64
 
65
  right_iris = []
66
  for p in rightIrisPoints:
67
  point = [int(face_landmarks.landmark[p].x * width), int(face_landmarks.landmark[p].y * height)]
68
  right_iris.append(point)
69
+ #cv2.circle(annotated_image, point, 1, (255, 0, 255), 2)
70
 
71
  leftIris_leftside = (int(left_iris[2][0]), int(left_iris[2][1]))
72
  leftIris_rightside = (int(left_iris[0][0]), int(left_iris[0][1]))
 
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]
86
+ leftEyeOpen = isEyeOpen (leftEye)
87
+
88
+ rightEye = annotated_image[rightIris_top[1] - sizeIncrease: rightIris_bottom[1] + sizeIncrease,
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,
113
  (int((leftIris_leftside[0] + leftIris_rightside[0]) / 2),
 
124
  (0, 255, 255), 2)
125
  '''
126
 
127
+
 
 
 
 
128
 
129
  name = 'TBD'
130
  newRow = pd.Series([name,
 
143
  #filename = directoy_name + 'Analysis/' + name[0:-4] + '-analysis.jpg'
144
  #cv2.imwrite(filename, annotated_image)
145
 
146
+ left = leftIris_leftside[0] - 150
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
155
  y2 = (rightIris_top[1] - nose[1] + rightIris_bottom[1] - nose[1]) / 2
156
  print("Slope=", (y2 - y1) / (x2 - x1))
157
  text = "Slope=" + str(round((y2 - y1) / (x2 - x1), 2))
158
+ #cv2.putText(annotated_image, text,
159
+ # (5, 110), cv2.FONT_HERSHEY_SIMPLEX,
160
+ # 1, (255, 255, 0), 1, cv2.LINE_AA)
161
 
162
  print("left iris size in pixels = ", abs(leftIris_leftside[0] - leftIris_rightside[0]))
163
  print("Right iris size in pixels = ", abs(rightIris_leftside[0] - rightIris_rightside[0]))