Spaces:
Sleeping
Sleeping
David Driscoll
commited on
Commit
·
d539f74
1
Parent(s):
568b799
Update
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ import mediapipe as mp
|
|
3 |
import numpy as np
|
4 |
import gradio as gr
|
5 |
|
6 |
-
# Initialize MediaPipe Pose
|
7 |
mp_pose = mp.solutions.pose
|
8 |
pose = mp_pose.Pose(
|
9 |
static_image_mode=False,
|
@@ -13,7 +13,7 @@ pose = mp_pose.Pose(
|
|
13 |
min_tracking_confidence=0.5
|
14 |
)
|
15 |
|
16 |
-
# Initialize MediaPipe Face Mesh
|
17 |
mp_face_mesh = mp.solutions.face_mesh
|
18 |
face_mesh = mp_face_mesh.FaceMesh(
|
19 |
static_image_mode=False,
|
@@ -25,96 +25,80 @@ face_mesh = mp_face_mesh.FaceMesh(
|
|
25 |
|
26 |
def process_frame(image):
|
27 |
"""
|
28 |
-
Processes
|
29 |
-
1. Converting
|
30 |
2. Flipping the frame for a mirror view.
|
31 |
3. Creating a black background.
|
32 |
-
4.
|
33 |
-
5.
|
34 |
-
6. Drawing a neck line from
|
35 |
-
7. Converting the result back to RGB
|
36 |
"""
|
37 |
-
# Convert the input image from RGB (Gradio default) to BGR.
|
38 |
frame = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
39 |
-
|
40 |
-
# Flip the frame horizontally for a mirror view.
|
41 |
frame = cv2.flip(frame, 1)
|
42 |
-
|
43 |
-
# Create a black background image of the same size.
|
44 |
output = np.zeros_like(frame)
|
45 |
-
|
46 |
-
# Convert frame to RGB for MediaPipe processing.
|
47 |
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
48 |
-
|
49 |
-
# --- Body Posture Analysis
|
50 |
pose_results = pose.process(rgb_frame)
|
51 |
shoulder_center = None
|
52 |
if pose_results.pose_landmarks:
|
53 |
h, w, _ = frame.shape
|
54 |
landmarks = [(int(lm.x * w), int(lm.y * h)) for lm in pose_results.pose_landmarks.landmark]
|
55 |
-
|
56 |
-
# Draw body skeleton (only drawing non-facial landmarks, i.e. indices 11 and above).
|
57 |
for connection in mp_pose.POSE_CONNECTIONS:
|
58 |
start_idx, end_idx = connection
|
59 |
if start_idx >= 11 and end_idx >= 11:
|
60 |
if start_idx < len(landmarks) and end_idx < len(landmarks):
|
61 |
cv2.line(output, landmarks[start_idx], landmarks[end_idx], (255, 255, 0), 2)
|
62 |
-
|
63 |
-
# Draw landmarks as yellow circles.
|
64 |
for i, pt in enumerate(landmarks):
|
65 |
if i >= 11:
|
66 |
cv2.circle(output, pt, 3, (255, 255, 0), -1)
|
67 |
-
|
68 |
-
# Calculate shoulder center using landmarks 11 (left shoulder) and 12 (right shoulder).
|
69 |
if len(landmarks) > 12:
|
70 |
-
left_shoulder = landmarks[11]
|
71 |
-
right_shoulder = landmarks[12]
|
72 |
shoulder_center = ((left_shoulder[0] + right_shoulder[0]) // 2,
|
73 |
(left_shoulder[1] + right_shoulder[1]) // 2)
|
74 |
cv2.circle(output, shoulder_center, 4, (0, 255, 255), -1)
|
75 |
-
|
76 |
-
# ---
|
77 |
chin_point = None
|
78 |
fm_results = face_mesh.process(rgb_frame)
|
79 |
if fm_results.multi_face_landmarks:
|
80 |
for face_landmarks in fm_results.multi_face_landmarks:
|
81 |
h, w, _ = frame.shape
|
82 |
fm_points = [(int(lm.x * w), int(lm.y * h)) for lm in face_landmarks.landmark]
|
83 |
-
|
84 |
-
# Draw red lines connecting facial landmarks.
|
85 |
for connection in mp_face_mesh.FACEMESH_TESSELATION:
|
86 |
start_idx, end_idx = connection
|
87 |
if start_idx < len(fm_points) and end_idx < len(fm_points):
|
88 |
cv2.line(output, fm_points[start_idx], fm_points[end_idx], (0, 0, 255), 1)
|
89 |
-
|
90 |
-
# Draw green dots for each facial landmark.
|
91 |
for pt in fm_points:
|
92 |
cv2.circle(output, pt, 2, (0, 255, 0), -1)
|
93 |
-
|
94 |
-
# Extract the chin point (landmark 152 is generally at the bottom of the chin).
|
95 |
if len(face_landmarks.landmark) > 152:
|
96 |
lm = face_landmarks.landmark[152]
|
97 |
chin_point = (int(lm.x * w), int(lm.y * h))
|
98 |
cv2.circle(output, chin_point, 4, (0, 0, 255), -1)
|
99 |
break # Process only the first detected face.
|
100 |
-
|
101 |
-
# --- Draw
|
102 |
if shoulder_center and chin_point:
|
103 |
cv2.line(output, shoulder_center, chin_point, (0, 255, 255), 2)
|
104 |
-
|
105 |
-
# Convert the processed image back to RGB for display.
|
106 |
-
output_rgb = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
|
107 |
-
return output_rgb
|
108 |
|
109 |
-
|
|
|
|
|
|
|
110 |
iface = gr.Interface(
|
111 |
fn=process_frame,
|
112 |
-
inputs=gr.Image(
|
113 |
outputs=gr.Image(type="numpy", label="Processed Output"),
|
114 |
live=True,
|
115 |
-
title="Body Posture & Neck Analysis (No Face Pose)",
|
116 |
-
description="
|
117 |
)
|
118 |
|
119 |
-
# Launch the Gradio app.
|
120 |
iface.launch()
|
|
|
3 |
import numpy as np
|
4 |
import gradio as gr
|
5 |
|
6 |
+
# Initialize MediaPipe Pose
|
7 |
mp_pose = mp.solutions.pose
|
8 |
pose = mp_pose.Pose(
|
9 |
static_image_mode=False,
|
|
|
13 |
min_tracking_confidence=0.5
|
14 |
)
|
15 |
|
16 |
+
# Initialize MediaPipe Face Mesh
|
17 |
mp_face_mesh = mp.solutions.face_mesh
|
18 |
face_mesh = mp_face_mesh.FaceMesh(
|
19 |
static_image_mode=False,
|
|
|
25 |
|
26 |
def process_frame(image):
|
27 |
"""
|
28 |
+
Processes a frame by:
|
29 |
+
1. Converting RGB to BGR for OpenCV.
|
30 |
2. Flipping the frame for a mirror view.
|
31 |
3. Creating a black background.
|
32 |
+
4. Drawing body landmarks and computing shoulder center.
|
33 |
+
5. Drawing facial mesh and extracting chin point.
|
34 |
+
6. Drawing a neck line from shoulder center to chin.
|
35 |
+
7. Converting the result back to RGB.
|
36 |
"""
|
|
|
37 |
frame = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
|
|
|
|
38 |
frame = cv2.flip(frame, 1)
|
|
|
|
|
39 |
output = np.zeros_like(frame)
|
|
|
|
|
40 |
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
41 |
+
|
42 |
+
# --- Body Posture Analysis ---
|
43 |
pose_results = pose.process(rgb_frame)
|
44 |
shoulder_center = None
|
45 |
if pose_results.pose_landmarks:
|
46 |
h, w, _ = frame.shape
|
47 |
landmarks = [(int(lm.x * w), int(lm.y * h)) for lm in pose_results.pose_landmarks.landmark]
|
48 |
+
|
|
|
49 |
for connection in mp_pose.POSE_CONNECTIONS:
|
50 |
start_idx, end_idx = connection
|
51 |
if start_idx >= 11 and end_idx >= 11:
|
52 |
if start_idx < len(landmarks) and end_idx < len(landmarks):
|
53 |
cv2.line(output, landmarks[start_idx], landmarks[end_idx], (255, 255, 0), 2)
|
54 |
+
|
|
|
55 |
for i, pt in enumerate(landmarks):
|
56 |
if i >= 11:
|
57 |
cv2.circle(output, pt, 3, (255, 255, 0), -1)
|
58 |
+
|
|
|
59 |
if len(landmarks) > 12:
|
60 |
+
left_shoulder, right_shoulder = landmarks[11], landmarks[12]
|
|
|
61 |
shoulder_center = ((left_shoulder[0] + right_shoulder[0]) // 2,
|
62 |
(left_shoulder[1] + right_shoulder[1]) // 2)
|
63 |
cv2.circle(output, shoulder_center, 4, (0, 255, 255), -1)
|
64 |
+
|
65 |
+
# --- Facial Mesh Analysis ---
|
66 |
chin_point = None
|
67 |
fm_results = face_mesh.process(rgb_frame)
|
68 |
if fm_results.multi_face_landmarks:
|
69 |
for face_landmarks in fm_results.multi_face_landmarks:
|
70 |
h, w, _ = frame.shape
|
71 |
fm_points = [(int(lm.x * w), int(lm.y * h)) for lm in face_landmarks.landmark]
|
72 |
+
|
|
|
73 |
for connection in mp_face_mesh.FACEMESH_TESSELATION:
|
74 |
start_idx, end_idx = connection
|
75 |
if start_idx < len(fm_points) and end_idx < len(fm_points):
|
76 |
cv2.line(output, fm_points[start_idx], fm_points[end_idx], (0, 0, 255), 1)
|
77 |
+
|
|
|
78 |
for pt in fm_points:
|
79 |
cv2.circle(output, pt, 2, (0, 255, 0), -1)
|
80 |
+
|
|
|
81 |
if len(face_landmarks.landmark) > 152:
|
82 |
lm = face_landmarks.landmark[152]
|
83 |
chin_point = (int(lm.x * w), int(lm.y * h))
|
84 |
cv2.circle(output, chin_point, 4, (0, 0, 255), -1)
|
85 |
break # Process only the first detected face.
|
86 |
+
|
87 |
+
# --- Draw Neck Line ---
|
88 |
if shoulder_center and chin_point:
|
89 |
cv2.line(output, shoulder_center, chin_point, (0, 255, 255), 2)
|
|
|
|
|
|
|
|
|
90 |
|
91 |
+
return cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
|
92 |
+
|
93 |
+
|
94 |
+
# --- Gradio Interface for Live Webcam Inference ---
|
95 |
iface = gr.Interface(
|
96 |
fn=process_frame,
|
97 |
+
inputs=gr.Image(sources=["webcam"], streaming=True, label="Webcam Input"), # Live webcam stream
|
98 |
outputs=gr.Image(type="numpy", label="Processed Output"),
|
99 |
live=True,
|
100 |
+
title="Live Body Posture & Neck Analysis (No Face Pose)",
|
101 |
+
description="Real-time webcam analysis using MediaPipe Pose and Face Mesh with live inference."
|
102 |
)
|
103 |
|
|
|
104 |
iface.launch()
|