Medvira commited on
Commit
d204b0e
·
verified ·
1 Parent(s): 13f67e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -70
app.py CHANGED
@@ -16,62 +16,50 @@ sys.excepthook = custom_excepthook
16
  def list_overlay_images(directory):
17
  return [f for f in os.listdir(directory) if f.endswith('.png')]
18
 
19
- def process_frame(frame, overlay, LEFT_EYE, RIGHT_EYE, LEFT_IRIS, RIGHT_IRIS,
20
- min_detection_confidence, min_tracking_confidence, alpha):
21
- try:
22
- mp_face_mesh = mp.solutions.face_mesh
23
- with mp_face_mesh.FaceMesh(
24
- max_num_faces=1,
25
- refine_landmarks=True,
26
- min_detection_confidence=min_detection_confidence,
27
- min_tracking_confidence=min_tracking_confidence
28
- ) as face_mesh:
29
- rgb_frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
30
- rgba_frame = cv.cvtColor(frame, cv.COLOR_BGR2RGBA)
31
- height, width = rgba_frame.shape[:2]
32
- results = face_mesh.process(rgb_frame)
33
- if results.multi_face_landmarks:
34
- zero_overlay = np.zeros_like(rgba_frame)
35
- mesh_points = np.array([np.multiply([p.x, p.y],
36
- [width, height]).astype(int) for p in results.multi_face_landmarks[0].landmark])
37
- iris_mask_left = np.zeros(rgba_frame.shape, dtype=np.uint8)
38
- iris_mask_right = np.zeros(rgba_frame.shape, dtype=np.uint8)
39
- _, re_ratio, le_ratio = blinkRatio(rgb_frame, mesh_points, RIGHT_EYE, LEFT_EYE)
40
- (l_cx, l_cy), l_radius = cv.minEnclosingCircle(mesh_points[LEFT_IRIS])
41
- (r_cx, r_cy), r_radius = cv.minEnclosingCircle(mesh_points[RIGHT_IRIS])
42
- center_left = (int(l_cx), int(l_cy))
43
- center_right = (int(r_cx), int(r_cy))
44
- cv.circle(iris_mask_left, center_left, int(l_radius), (255, 0, 0, 255), -1, cv.LINE_AA)
45
- cv.circle(iris_mask_right, center_right, int(r_radius), (255, 0, 0, 255), -1, cv.LINE_AA)
46
- bbx_size_l = int((l_radius * 2) / 2)
47
- bbx_size_r = int((r_radius * 2) / 2)
48
- resized_overlay_l = cv.resize(overlay, (bbx_size_l * 2, bbx_size_l * 2), interpolation=cv.INTER_CUBIC)
49
- resized_overlay_r = cv.resize(overlay, (bbx_size_r * 2, bbx_size_r * 2), interpolation=cv.INTER_CUBIC)
50
- y1_r = center_right[1] - bbx_size_r
51
- y2_r = center_right[1] + bbx_size_r
52
- x1_r = center_right[0] - bbx_size_r
53
- x2_r = center_right[0] + bbx_size_r
54
- y1_l = center_left[1] - bbx_size_l
55
- y2_l = center_left[1] + bbx_size_l
56
- x1_l = center_left[0] - bbx_size_l
57
- x2_l = center_left[0] + bbx_size_l
58
- if (resized_overlay_l.shape == zero_overlay[y1_l:y2_l, x1_l:x2_l].shape) & (le_ratio < 5.0) & (le_ratio > 2.0):
59
- zero_overlay[y1_l:y2_l, x1_l:x2_l] = resized_overlay_l
60
- if (resized_overlay_r.shape == zero_overlay[y1_r:y2_r, x1_r:x2_r].shape) & (re_ratio < 5.0) & (re_ratio > 2.0):
61
- zero_overlay[y1_r:y2_r, x1_r:x2_r] = resized_overlay_r
62
- eye_mask_left = np.zeros(rgba_frame.shape, dtype=np.uint8)
63
- eye_mask_right = np.zeros(rgba_frame.shape, dtype=np.uint8)
64
- cv.fillPoly(eye_mask_left, [mesh_points[LEFT_EYE]], (255, 0, 0, 255))
65
- cv.fillPoly(eye_mask_right, [mesh_points[RIGHT_EYE]], (255, 0, 0, 255))
66
- zero_overlay[np.where((iris_mask_left[:, :, 3] > 0) & (eye_mask_left[:, :, 3] == 0))] = 0
67
- zero_overlay[np.where((iris_mask_right[:, :, 3] > 0) & (eye_mask_right[:, :, 3] == 0))] = 0
68
- rgba_frame = cv.addWeighted(rgba_frame, 1, zero_overlay, alpha, 0)
69
- return rgba_frame
70
- except Exception as e:
71
- print(f"Error in process_frame: {e}")
72
- traceback.print_exc()
73
 
74
- def process_image(input_image, overlay_file, alpha=0.3, min_detection_confidence=0.5, min_tracking_confidence=0.5):
75
  overlay_file = overlay_file + '.png'
76
  overlay_path = os.path.join(os.getcwd(),'overlays', overlay_file)
77
  overlay = cv.imread(overlay_path, cv.IMREAD_UNCHANGED)
@@ -79,13 +67,11 @@ def process_image(input_image, overlay_file, alpha=0.3, min_detection_confidence
79
  w,h,_ = frame.shape
80
  new_h = 500
81
  new_w = int((w/h)*new_h)
82
- frame = cv.resize(frame, (new_h,new_w), interpolation=cv.INTER_CUBIC)
83
- processed_frame = process_frame(frame, overlay, LEFT_EYE, RIGHT_EYE, LEFT_IRIS, RIGHT_IRIS,
84
- min_detection_confidence, min_tracking_confidence, alpha)
85
  return cv.cvtColor(processed_frame, cv.COLOR_BGR2RGB)
86
 
87
- def process_video(input_video, overlay_file, alpha=0.3, output_format='mp4', output_frame_rate=30,
88
- min_detection_confidence=0.5, min_tracking_confidence=0.5):
89
  overlay_file = overlay_file + '.png'
90
  overlay_path = os.path.join(os.getcwd(),'overlays', overlay_file)
91
  overlay = cv.imread(overlay_path, cv.IMREAD_UNCHANGED)
@@ -107,9 +93,7 @@ def process_video(input_video, overlay_file, alpha=0.3, output_format='mp4', out
107
  ret, frame = cap.read()
108
  if ret == True:
109
  frame = cv.resize(frame, (new_w,new_h), interpolation=cv.INTER_NEAREST)
110
- processed_frame = process_frame(frame,overlay,LEFT_EYE, RIGHT_EYE, LEFT_IRIS, RIGHT_IRIS,
111
- float(min_detection_confidence),
112
- float(min_tracking_confidence), float(alpha)) # Assuming process_frame is a function that processes a single frame
113
  processed_frame = cv.cvtColor(processed_frame, cv.COLOR_RGBA2BGR)
114
  out.write(processed_frame)
115
  else:
@@ -119,13 +103,15 @@ def process_video(input_video, overlay_file, alpha=0.3, output_format='mp4', out
119
  return output_path
120
 
121
 
122
- def process_webcam(frame, overlay_file, alpha=0.3, min_detection_confidence=0.5, min_tracking_confidence=0.5):
123
- overlay_file = overlay_file + '.png'
124
- overlay_path = os.path.join(os.getcwd(), overlay_file)
125
- overlay = cv.imread(overlay_path, cv.IMREAD_UNCHANGED)
126
- processed_frame = process_frame(frame, overlay, LEFT_EYE, RIGHT_EYE, LEFT_IRIS, RIGHT_IRIS,
127
- min_detection_confidence, min_tracking_confidence, alpha)
128
- return processed_frame
 
 
129
 
130
  LEFT_EYE = [362, 382, 381, 380, 374, 373, 390, 249, 263, 466, 388, 387, 386, 385, 384, 398]
131
  RIGHT_EYE = [33, 7, 163, 144, 145, 153, 154, 155, 133, 173, 157, 158, 159, 160, 161, 246]
 
16
  def list_overlay_images(directory):
17
  return [f for f in os.listdir(directory) if f.endswith('.png')]
18
 
19
+ def process_frame(frame, overlay, alpha, LEFT_EYE, RIGHT_EYE, LEFT_IRIS, RIGHT_IRIS):
20
+ rgb_frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
21
+ rgba_frame = cv.cvtColor(frame, cv.COLOR_BGR2RGBA)
22
+ height, width = rgba_frame.shape[:2]
23
+ results = face_mesh.process(rgb_frame)
24
+ if results.multi_face_landmarks:
25
+ zero_overlay = np.zeros_like(rgba_frame)
26
+ mesh_points = np.array([np.multiply([p.x, p.y],
27
+ [width, height]).astype(int) for p in results.multi_face_landmarks[0].landmark])
28
+ iris_mask_left = np.zeros(rgba_frame.shape, dtype=np.uint8)
29
+ iris_mask_right = np.zeros(rgba_frame.shape, dtype=np.uint8)
30
+ _, re_ratio, le_ratio = blinkRatio(rgb_frame, mesh_points, RIGHT_EYE, LEFT_EYE)
31
+ (l_cx, l_cy), l_radius = cv.minEnclosingCircle(mesh_points[LEFT_IRIS])
32
+ (r_cx, r_cy), r_radius = cv.minEnclosingCircle(mesh_points[RIGHT_IRIS])
33
+ center_left = (int(l_cx), int(l_cy))
34
+ center_right = (int(r_cx), int(r_cy))
35
+ cv.circle(iris_mask_left, center_left, int(l_radius), (255, 0, 0, 255), -1, cv.LINE_AA)
36
+ cv.circle(iris_mask_right, center_right, int(r_radius), (255, 0, 0, 255), -1, cv.LINE_AA)
37
+ bbx_size_l = int((l_radius * 2) / 2)
38
+ bbx_size_r = int((r_radius * 2) / 2)
39
+ resized_overlay_l = cv.resize(overlay, (bbx_size_l * 2, bbx_size_l * 2), interpolation=cv.INTER_CUBIC)
40
+ resized_overlay_r = cv.resize(overlay, (bbx_size_r * 2, bbx_size_r * 2), interpolation=cv.INTER_CUBIC)
41
+ y1_r = center_right[1] - bbx_size_r
42
+ y2_r = center_right[1] + bbx_size_r
43
+ x1_r = center_right[0] - bbx_size_r
44
+ x2_r = center_right[0] + bbx_size_r
45
+ y1_l = center_left[1] - bbx_size_l
46
+ y2_l = center_left[1] + bbx_size_l
47
+ x1_l = center_left[0] - bbx_size_l
48
+ x2_l = center_left[0] + bbx_size_l
49
+ if (resized_overlay_l.shape == zero_overlay[y1_l:y2_l, x1_l:x2_l].shape) & (le_ratio < 5.0) & (le_ratio > 2.0):
50
+ zero_overlay[y1_l:y2_l, x1_l:x2_l] = resized_overlay_l
51
+ if (resized_overlay_r.shape == zero_overlay[y1_r:y2_r, x1_r:x2_r].shape) & (re_ratio < 5.0) & (re_ratio > 2.0):
52
+ zero_overlay[y1_r:y2_r, x1_r:x2_r] = resized_overlay_r
53
+ eye_mask_left = np.zeros(rgba_frame.shape, dtype=np.uint8)
54
+ eye_mask_right = np.zeros(rgba_frame.shape, dtype=np.uint8)
55
+ cv.fillPoly(eye_mask_left, [mesh_points[LEFT_EYE]], (255, 0, 0, 255))
56
+ cv.fillPoly(eye_mask_right, [mesh_points[RIGHT_EYE]], (255, 0, 0, 255))
57
+ zero_overlay[np.where((iris_mask_left[:, :, 3] > 0) & (eye_mask_left[:, :, 3] == 0))] = 0
58
+ zero_overlay[np.where((iris_mask_right[:, :, 3] > 0) & (eye_mask_right[:, :, 3] == 0))] = 0
59
+ rgba_frame = cv.addWeighted(rgba_frame, 1, zero_overlay, alpha, 0)
60
+ return rgba_frame
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
+ def process_image(input_image, overlay_file, alpha=0.3):
63
  overlay_file = overlay_file + '.png'
64
  overlay_path = os.path.join(os.getcwd(),'overlays', overlay_file)
65
  overlay = cv.imread(overlay_path, cv.IMREAD_UNCHANGED)
 
67
  w,h,_ = frame.shape
68
  new_h = 500
69
  new_w = int((w/h)*new_h)
70
+ frame = cv.resize(frame, (new_h,new_w), interpolation=cv.INTER_NEAREST)
71
+ processed_frame = process_frame(frame, overlay, alpha, LEFT_EYE, RIGHT_EYE, LEFT_IRIS, RIGHT_IRIS)
 
72
  return cv.cvtColor(processed_frame, cv.COLOR_BGR2RGB)
73
 
74
+ def process_video(input_video, overlay_file, alpha=0.3, output_format='mp4', output_frame_rate=30):
 
75
  overlay_file = overlay_file + '.png'
76
  overlay_path = os.path.join(os.getcwd(),'overlays', overlay_file)
77
  overlay = cv.imread(overlay_path, cv.IMREAD_UNCHANGED)
 
93
  ret, frame = cap.read()
94
  if ret == True:
95
  frame = cv.resize(frame, (new_w,new_h), interpolation=cv.INTER_NEAREST)
96
+ processed_frame = process_frame(frame,overlay,alpha,LEFT_EYE, RIGHT_EYE, LEFT_IRIS, RIGHT_IRIS) # Assuming process_frame is a function that processes a single frame
 
 
97
  processed_frame = cv.cvtColor(processed_frame, cv.COLOR_RGBA2BGR)
98
  out.write(processed_frame)
99
  else:
 
103
  return output_path
104
 
105
 
106
+
107
+ # Initialize face mesh once and reuse it
108
+ mp_face_mesh = mp.solutions.face_mesh
109
+ face_mesh = mp_face_mesh.FaceMesh(
110
+ max_num_faces=1,
111
+ refine_landmarks=True,
112
+ min_detection_confidence=0.5,
113
+ min_tracking_confidence=0.5
114
+ )
115
 
116
  LEFT_EYE = [362, 382, 381, 380, 374, 373, 390, 249, 263, 466, 388, 387, 386, 385, 384, 398]
117
  RIGHT_EYE = [33, 7, 163, 144, 145, 153, 154, 155, 133, 173, 157, 158, 159, 160, 161, 246]