David Driscoll
commited on
Commit
·
91863a8
1
Parent(s):
0792145
FaceMesh
Browse files
app.py
CHANGED
@@ -28,7 +28,7 @@ faces_cache = {"boxes": None, "text": "Initializing...", "counter": 0}
|
|
28 |
# -----------------------------
|
29 |
# Initialize Models and Helpers
|
30 |
# -----------------------------
|
31 |
-
# MediaPipe Pose and Face
|
32 |
mp_pose = mp.solutions.pose
|
33 |
pose = mp_pose.Pose()
|
34 |
mp_drawing = mp.solutions.drawing_utils
|
@@ -51,6 +51,7 @@ object_categories = FasterRCNN_ResNet50_FPN_Weights.DEFAULT.meta["categories"]
|
|
51 |
|
52 |
# -----------------------------
|
53 |
# Facial Recognition Model (Marltgap/FaceTransformerOctupletLoss ONNX)
|
|
|
54 |
# -----------------------------
|
55 |
facial_recognition_onnx = rt.InferenceSession("FaceTransformerOctupletLoss.onnx", providers=rt.get_available_providers())
|
56 |
|
@@ -149,6 +150,39 @@ def compute_faces_overlay(image):
|
|
149 |
text = "No faces detected"
|
150 |
return boxes, text
|
151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
def compute_facial_recognition_vector(image):
|
153 |
"""
|
154 |
Detects a face using MediaPipe, crops and resizes it to 112x112, then computes its embedding
|
@@ -232,6 +266,7 @@ def analyze_faces_current(image):
|
|
232 |
output = draw_boxes_overlay(output, faces_cache["boxes"], (0, 0, 255))
|
233 |
return output, f"<div style='color: lime !important;'>Face Detection: {faces_cache['text']}</div>"
|
234 |
|
|
|
235 |
def analyze_facial_recognition(image):
|
236 |
# Compute and return the facial vector (and the cropped face)
|
237 |
face_crop, vector_str = compute_facial_recognition_vector(image)
|
@@ -333,12 +368,15 @@ faces_interface = gr.Interface(
|
|
333 |
live=False
|
334 |
)
|
335 |
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
|
|
|
|
|
|
342 |
live=False
|
343 |
)
|
344 |
|
@@ -357,7 +395,7 @@ tabbed_interface = gr.TabbedInterface(
|
|
357 |
emotion_interface,
|
358 |
objects_interface,
|
359 |
faces_interface,
|
360 |
-
|
361 |
all_interface
|
362 |
],
|
363 |
tab_names=[
|
@@ -365,7 +403,7 @@ tabbed_interface = gr.TabbedInterface(
|
|
365 |
"Emotion",
|
366 |
"Objects",
|
367 |
"Faces",
|
368 |
-
"
|
369 |
"All Inferences"
|
370 |
]
|
371 |
)
|
@@ -376,7 +414,7 @@ tabbed_interface = gr.TabbedInterface(
|
|
376 |
demo = gr.Blocks(css=custom_css)
|
377 |
with demo:
|
378 |
gr.Markdown("<h1 class='gradio-title' style='color: #32CD32;'>Multi-Analysis Image App</h1>")
|
379 |
-
gr.Markdown("<p class='gradio-description' style='color: #32CD32;'>Upload an image to run high-tech analysis for posture, emotions, objects, faces, and
|
380 |
tabbed_interface.render()
|
381 |
|
382 |
if __name__ == "__main__":
|
|
|
28 |
# -----------------------------
|
29 |
# Initialize Models and Helpers
|
30 |
# -----------------------------
|
31 |
+
# MediaPipe Pose, Face Detection, and Face Mesh
|
32 |
mp_pose = mp.solutions.pose
|
33 |
pose = mp_pose.Pose()
|
34 |
mp_drawing = mp.solutions.drawing_utils
|
|
|
51 |
|
52 |
# -----------------------------
|
53 |
# Facial Recognition Model (Marltgap/FaceTransformerOctupletLoss ONNX)
|
54 |
+
# (No longer used in the UI; kept here for reference)
|
55 |
# -----------------------------
|
56 |
facial_recognition_onnx = rt.InferenceSession("FaceTransformerOctupletLoss.onnx", providers=rt.get_available_providers())
|
57 |
|
|
|
150 |
text = "No faces detected"
|
151 |
return boxes, text
|
152 |
|
153 |
+
# -----------------------------
|
154 |
+
# New Facemesh Functions
|
155 |
+
# -----------------------------
|
156 |
+
def compute_facemesh_overlay(image):
|
157 |
+
"""
|
158 |
+
Uses MediaPipe Face Mesh to detect and draw facial landmarks.
|
159 |
+
"""
|
160 |
+
frame_bgr = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
161 |
+
h, w, _ = frame_bgr.shape
|
162 |
+
# Initialize Face Mesh in static mode
|
163 |
+
face_mesh = mp.solutions.face_mesh.FaceMesh(
|
164 |
+
static_image_mode=True, max_num_faces=1, refine_landmarks=True, min_detection_confidence=0.5
|
165 |
+
)
|
166 |
+
results = face_mesh.process(cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB))
|
167 |
+
if results.multi_face_landmarks:
|
168 |
+
for face_landmarks in results.multi_face_landmarks:
|
169 |
+
for landmark in face_landmarks.landmark:
|
170 |
+
x = int(landmark.x * w)
|
171 |
+
y = int(landmark.y * h)
|
172 |
+
cv2.circle(frame_bgr, (x, y), 1, (0, 255, 0), -1)
|
173 |
+
text = "Facemesh detected"
|
174 |
+
else:
|
175 |
+
text = "No facemesh detected"
|
176 |
+
face_mesh.close()
|
177 |
+
return frame_bgr, text
|
178 |
+
|
179 |
+
def analyze_facemesh(image):
|
180 |
+
annotated_image, text = compute_facemesh_overlay(image)
|
181 |
+
return annotated_image, f"<div style='color: lime !important;'>Facemesh Analysis: {text}</div>"
|
182 |
+
|
183 |
+
# -----------------------------
|
184 |
+
# (Retained) Facial Recognition Function (Not used in UI anymore)
|
185 |
+
# -----------------------------
|
186 |
def compute_facial_recognition_vector(image):
|
187 |
"""
|
188 |
Detects a face using MediaPipe, crops and resizes it to 112x112, then computes its embedding
|
|
|
266 |
output = draw_boxes_overlay(output, faces_cache["boxes"], (0, 0, 255))
|
267 |
return output, f"<div style='color: lime !important;'>Face Detection: {faces_cache['text']}</div>"
|
268 |
|
269 |
+
# (The old facial recognition analysis function is retained below but not linked to any UI tab)
|
270 |
def analyze_facial_recognition(image):
|
271 |
# Compute and return the facial vector (and the cropped face)
|
272 |
face_crop, vector_str = compute_facial_recognition_vector(image)
|
|
|
368 |
live=False
|
369 |
)
|
370 |
|
371 |
+
# -----------------------------
|
372 |
+
# New Facemesh Interface (Replaces the old Facial Recognition tab)
|
373 |
+
# -----------------------------
|
374 |
+
facemesh_interface = gr.Interface(
|
375 |
+
fn=analyze_facemesh,
|
376 |
+
inputs=gr.Image(label="Upload an Image for Facemesh"),
|
377 |
+
outputs=[gr.Image(type="numpy", label="Annotated Output"), gr.HTML(label="Facemesh Analysis")],
|
378 |
+
title="Facemesh",
|
379 |
+
description="Detects facial landmarks using MediaPipe Face Mesh.",
|
380 |
live=False
|
381 |
)
|
382 |
|
|
|
395 |
emotion_interface,
|
396 |
objects_interface,
|
397 |
faces_interface,
|
398 |
+
facemesh_interface,
|
399 |
all_interface
|
400 |
],
|
401 |
tab_names=[
|
|
|
403 |
"Emotion",
|
404 |
"Objects",
|
405 |
"Faces",
|
406 |
+
"Facemesh",
|
407 |
"All Inferences"
|
408 |
]
|
409 |
)
|
|
|
414 |
demo = gr.Blocks(css=custom_css)
|
415 |
with demo:
|
416 |
gr.Markdown("<h1 class='gradio-title' style='color: #32CD32;'>Multi-Analysis Image App</h1>")
|
417 |
+
gr.Markdown("<p class='gradio-description' style='color: #32CD32;'>Upload an image to run high-tech analysis for posture, emotions, objects, faces, and facemesh landmarks.</p>")
|
418 |
tabbed_interface.render()
|
419 |
|
420 |
if __name__ == "__main__":
|