bishalrauniyar commited on
Commit
7fabf20
1 Parent(s): c5ff6e0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +318 -0
app.py ADDED
@@ -0,0 +1,318 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import gradio as gr
3
+ import mediapipe as mp
4
+ import dlib
5
+ import imutils
6
+ import numpy as np
7
+
8
+
9
+ mp_drawing = mp.solutions.drawing_utils
10
+ mp_drawing_styles = mp.solutions.drawing_styles
11
+ mp_face_mesh = mp.solutions.face_mesh
12
+ mp_face_detection = mp.solutions.face_detection
13
+
14
+
15
+ def apply_media_pipe_face_detection(image):
16
+ with mp_face_detection.FaceDetection(
17
+ model_selection=1, min_detection_confidence=0.5) as face_detection:
18
+ results = face_detection.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
19
+ if not results.detections:
20
+ return image
21
+ annotated_image = image.copy()
22
+ for detection in results.detections:
23
+ mp_drawing.draw_detection(annotated_image, detection)
24
+ return annotated_image
25
+
26
+
27
+ def apply_media_pipe_facemesh(image):
28
+ with mp_face_mesh.FaceMesh(
29
+ static_image_mode=True,
30
+ max_num_faces=1,
31
+ refine_landmarks=True,
32
+ min_detection_confidence=0.5) as face_mesh:
33
+ results = face_mesh.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
34
+ if not results.multi_face_landmarks:
35
+ return image
36
+ annotated_image = image.copy()
37
+ for face_landmarks in results.multi_face_landmarks:
38
+ mp_drawing.draw_landmarks(
39
+ image=annotated_image,
40
+ landmark_list=face_landmarks,
41
+ connections=mp_face_mesh.FACEMESH_TESSELATION,
42
+ landmark_drawing_spec=None,
43
+ connection_drawing_spec=mp_drawing_styles
44
+ .get_default_face_mesh_tesselation_style())
45
+ mp_drawing.draw_landmarks(
46
+ image=annotated_image,
47
+ landmark_list=face_landmarks,
48
+ connections=mp_face_mesh.FACEMESH_CONTOURS,
49
+ landmark_drawing_spec=None,
50
+ connection_drawing_spec=mp_drawing_styles
51
+ .get_default_face_mesh_contours_style())
52
+ mp_drawing.draw_landmarks(
53
+ image=annotated_image,
54
+ landmark_list=face_landmarks,
55
+ connections=mp_face_mesh.FACEMESH_IRISES,
56
+ landmark_drawing_spec=None,
57
+ connection_drawing_spec=mp_drawing_styles
58
+ .get_default_face_mesh_iris_connections_style())
59
+ return annotated_image
60
+
61
+
62
+ class FaceOrientation(object):
63
+ def __init__(self):
64
+ self.detect = dlib.get_frontal_face_detector()
65
+ self.predict = dlib.shape_predictor("model/shape_predictor_68_face_landmarks.dat")
66
+
67
+ def create_orientation(self, frame):
68
+ draw_rect1 = True
69
+ draw_rect2 = True
70
+ draw_lines = True
71
+
72
+ frame = imutils.resize(frame, width=800)
73
+ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
74
+ subjects = self.detect(gray, 0)
75
+
76
+ for subject in subjects:
77
+ landmarks = self.predict(gray, subject)
78
+ size = frame.shape
79
+
80
+ # 2D image points. If you change the image, you need to change vector
81
+ image_points = np.array([
82
+ (landmarks.part(33).x, landmarks.part(33).y), # Nose tip
83
+ (landmarks.part(8).x, landmarks.part(8).y), # Chin
84
+ (landmarks.part(36).x, landmarks.part(36).y), # Left eye left corner
85
+ (landmarks.part(45).x, landmarks.part(45).y), # Right eye right corne
86
+ (landmarks.part(48).x, landmarks.part(48).y), # Left Mouth corner
87
+ (landmarks.part(54).x, landmarks.part(54).y) # Right mouth corner
88
+ ], dtype="double")
89
+
90
+ # 3D model points.
91
+ model_points = np.array([
92
+ (0.0, 0.0, 0.0), # Nose tip
93
+ (0.0, -330.0, -65.0), # Chin
94
+ (-225.0, 170.0, -135.0), # Left eye left corner
95
+ (225.0, 170.0, -135.0), # Right eye right corne
96
+ (-150.0, -150.0, -125.0), # Left Mouth corner
97
+ (150.0, -150.0, -125.0) # Right mouth corner
98
+
99
+ ])
100
+ # Camera internals
101
+ focal_length = size[1]
102
+ center = (size[1] / 2, size[0] / 2)
103
+ camera_matrix = np.array(
104
+ [[focal_length, 0, center[0]],
105
+ [0, focal_length, center[1]],
106
+ [0, 0, 1]], dtype="double"
107
+ )
108
+
109
+ dist_coeffs = np.zeros((4, 1)) # Assuming no lens distortion
110
+ (success, rotation_vector, translation_vector) = cv2.solvePnP(model_points, image_points, camera_matrix,
111
+ dist_coeffs)
112
+
113
+ (b1, jacobian) = cv2.projectPoints(np.array([(350.0, 270.0, 0.0)]), rotation_vector, translation_vector,
114
+ camera_matrix, dist_coeffs)
115
+ (b2, jacobian) = cv2.projectPoints(np.array([(-350.0, -270.0, 0.0)]), rotation_vector,
116
+ translation_vector, camera_matrix, dist_coeffs)
117
+ (b3, jacobian) = cv2.projectPoints(np.array([(-350.0, 270, 0.0)]), rotation_vector, translation_vector,
118
+ camera_matrix, dist_coeffs)
119
+ (b4, jacobian) = cv2.projectPoints(np.array([(350.0, -270.0, 0.0)]), rotation_vector,
120
+ translation_vector, camera_matrix, dist_coeffs)
121
+
122
+ (b11, jacobian) = cv2.projectPoints(np.array([(450.0, 350.0, 400.0)]), rotation_vector,
123
+ translation_vector, camera_matrix, dist_coeffs)
124
+ (b12, jacobian) = cv2.projectPoints(np.array([(-450.0, -350.0, 400.0)]), rotation_vector,
125
+ translation_vector, camera_matrix, dist_coeffs)
126
+ (b13, jacobian) = cv2.projectPoints(np.array([(-450.0, 350, 400.0)]), rotation_vector,
127
+ translation_vector, camera_matrix, dist_coeffs)
128
+ (b14, jacobian) = cv2.projectPoints(np.array([(450.0, -350.0, 400.0)]), rotation_vector,
129
+ translation_vector, camera_matrix, dist_coeffs)
130
+
131
+ b1 = (int(b1[0][0][0]), int(b1[0][0][1]))
132
+ b2 = (int(b2[0][0][0]), int(b2[0][0][1]))
133
+ b3 = (int(b3[0][0][0]), int(b3[0][0][1]))
134
+ b4 = (int(b4[0][0][0]), int(b4[0][0][1]))
135
+
136
+ b11 = (int(b11[0][0][0]), int(b11[0][0][1]))
137
+ b12 = (int(b12[0][0][0]), int(b12[0][0][1]))
138
+ b13 = (int(b13[0][0][0]), int(b13[0][0][1]))
139
+ b14 = (int(b14[0][0][0]), int(b14[0][0][1]))
140
+
141
+ if draw_rect1 == True:
142
+ cv2.line(frame, b1, b3, (255, 255, 0), 10)
143
+ cv2.line(frame, b3, b2, (255, 255, 0), 10)
144
+ cv2.line(frame, b2, b4, (255, 255, 0), 10)
145
+ cv2.line(frame, b4, b1, (255, 255, 0), 10)
146
+
147
+ if draw_rect2 == True:
148
+ cv2.line(frame, b11, b13, (255, 255, 0), 10)
149
+ cv2.line(frame, b13, b12, (255, 255, 0), 10)
150
+ cv2.line(frame, b12, b14, (255, 255, 0), 10)
151
+ cv2.line(frame, b14, b11, (255, 255, 0), 10)
152
+
153
+ if draw_lines == True:
154
+ cv2.line(frame, b11, b1, (0, 255, 0), 10)
155
+ cv2.line(frame, b13, b3, (0, 255, 0), 10)
156
+ cv2.line(frame, b12, b2, (0, 255, 0), 10)
157
+ cv2.line(frame, b14, b4, (0, 255, 0), 10)
158
+
159
+ return frame
160
+
161
+
162
+ face_orientation_obj = FaceOrientation()
163
+
164
+
165
+ class FaceProcessing(object):
166
+ def __init__(self, ui_obj):
167
+ self.name = "Face Image Processing"
168
+ self.description = "Call for Face Image and video Processing"
169
+ self.ui_obj = ui_obj
170
+
171
+ def take_webcam_photo(self, image):
172
+ return image
173
+
174
+ def take_webcam_video(self, images):
175
+ return images
176
+
177
+ def mp_webcam_photo(self, image):
178
+ return image
179
+
180
+ def mp_webcam_face_mesh(self, image):
181
+ face_mesh_image = apply_media_pipe_facemesh(image)
182
+ return face_mesh_image
183
+
184
+ def mp_webcam_face_detection(self, image):
185
+ face_detection_img = apply_media_pipe_face_detection(image)
186
+ return face_detection_img
187
+
188
+ def dlib_apply_face_orientation(self, image):
189
+ image = face_orientation_obj.create_orientation(image)
190
+ return image
191
+
192
+ def webcam_stream_update(self, video_frame):
193
+ video_out = face_orientation_obj.create_orientation(video_frame)
194
+ return video_out
195
+
196
+ def create_ui(self):
197
+ with self.ui_obj:
198
+ gr.Markdown("""
199
+ ### 👨‍💻Made By Bishal Kumar Rauniyar👨‍💻
200
+ ## Project: Face Processing based on DLIB Shape Predictor Model
201
+ """)
202
+ with gr.Tabs():
203
+ with gr.TabItem("Processing Webcam"):
204
+ with gr.Row():
205
+ webcam_image_in = gr.Image(label="Webcam Image Input")
206
+ webcam_video_in = gr.Video(label="Webcam Video Input")
207
+ with gr.Row():
208
+ webcam_photo_action = gr.Button("Take the Photo")
209
+ webcam_video_action = gr.Button("Take the Video")
210
+ with gr.Row():
211
+ webcam_photo_out = gr.Image(label="Webcam Photo Output")
212
+ webcam_video_out = gr.Video(label="Webcam Video")
213
+ with gr.TabItem("Mediapipe Facemesh with Webcam"):
214
+ with gr.Row():
215
+ with gr.Column():
216
+ mp_image_in = gr.Image(label="Webcam Image Input")
217
+ with gr.Column():
218
+ mp_photo_action = gr.Button("Take the Photo")
219
+ mp_apply_fm_action = gr.Button("Apply Face Mesh the Photo")
220
+ mp_apply_landmarks_action = gr.Button("Apply Face Landmarks the Photo")
221
+ with gr.Row():
222
+ mp_photo_out = gr.Image(label="Webcam Photo Output")
223
+ mp_fm_photo_out = gr.Image(label="Face Mesh Photo Output")
224
+ mp_lm_photo_out = gr.Image(label="Face Landmarks Photo Output")
225
+ with gr.TabItem("DLib Model Based Face Orientation"):
226
+ with gr.Row():
227
+ with gr.Column():
228
+ dlib_image_in = gr.Image(label="Webcam Image Input")
229
+ with gr.Column():
230
+ dlib_photo_action = gr.Button("Take the Photo")
231
+ dlib_apply_orientation_action = gr.Button("Apply Face Mesh the Photo")
232
+ with gr.Row():
233
+ dlib_photo_out = gr.Image(label="Webcam Photo Output")
234
+ dlib_orientation_photo_out = gr.Image(label="Face Mesh Photo Output")
235
+ with gr.TabItem("Face Orientation on Live Webcam Stream"):
236
+ with gr.Row():
237
+ webcam_stream_in = gr.Image(label="Webcam Stream Input",
238
+ streaming=True)
239
+ webcam_stream_out = gr.Image(label="Webcam Stream Output")
240
+ webcam_stream_in.change(
241
+ self.webcam_stream_update,
242
+ inputs=webcam_stream_in,
243
+ outputs=webcam_stream_out
244
+ )
245
+
246
+ dlib_photo_action.click(
247
+ self.mp_webcam_photo,
248
+ [
249
+ dlib_image_in
250
+ ],
251
+ [
252
+ dlib_photo_out
253
+ ]
254
+ )
255
+ dlib_apply_orientation_action.click(
256
+ self.dlib_apply_face_orientation,
257
+ [
258
+ dlib_image_in
259
+ ],
260
+ [
261
+ dlib_orientation_photo_out
262
+ ]
263
+ )
264
+ mp_photo_action.click(
265
+ self.mp_webcam_photo,
266
+ [
267
+ mp_image_in
268
+ ],
269
+ [
270
+ mp_photo_out
271
+ ]
272
+ )
273
+ mp_apply_fm_action.click(
274
+ self.mp_webcam_face_mesh,
275
+ [
276
+ mp_image_in
277
+ ],
278
+ [
279
+ mp_fm_photo_out
280
+ ]
281
+ )
282
+ mp_apply_landmarks_action.click(
283
+ self.mp_webcam_face_detection,
284
+ [
285
+ mp_image_in
286
+ ],
287
+ [
288
+ mp_lm_photo_out
289
+ ]
290
+ )
291
+ webcam_photo_action.click(
292
+ self.take_webcam_photo,
293
+ [
294
+ webcam_image_in
295
+ ],
296
+ [
297
+ webcam_photo_out
298
+ ]
299
+ )
300
+ webcam_video_action.click(
301
+ self.take_webcam_video,
302
+ [
303
+ webcam_video_in
304
+ ],
305
+ [
306
+ webcam_video_out
307
+ ]
308
+ )
309
+
310
+ def launch_ui(self):
311
+ self.ui_obj.launch()
312
+
313
+
314
+ if __name__ == '__main__':
315
+ my_app = gr.Blocks()
316
+ face_ui = FaceProcessing(my_app)
317
+ face_ui.create_ui()
318
+ face_ui.launch_ui()