Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,10 @@ import cv2
|
|
| 2 |
import mediapipe as mp
|
| 3 |
import numpy as np
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
mp_pose = mp.solutions.pose
|
| 7 |
mp_drawing = mp.solutions.drawing_utils
|
| 8 |
-
|
| 9 |
pose = mp_pose.Pose(static_image_mode=False, min_detection_confidence=0.5, min_tracking_confidence=0.5)
|
| 10 |
|
| 11 |
def calculate_angle(a, b, c):
|
|
@@ -15,18 +15,40 @@ def calculate_angle(a, b, c):
|
|
| 15 |
ba = a - b
|
| 16 |
bc = c - b
|
| 17 |
cosine_angle = np.dot(ba, bc) / (np.linalg.norm(ba) * np.linalg.norm(bc))
|
| 18 |
-
angle = np.arccos(cosine_angle)
|
| 19 |
return np.degrees(angle)
|
| 20 |
|
| 21 |
-
def detect_pose_video(video_path):
|
|
|
|
|
|
|
|
|
|
| 22 |
cap = cv2.VideoCapture(video_path)
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
| 26 |
ret, frame = cap.read()
|
| 27 |
if not ret:
|
| 28 |
break
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 31 |
results = pose.process(frame_rgb)
|
| 32 |
|
|
@@ -40,31 +62,23 @@ def detect_pose_video(video_path):
|
|
| 40 |
)
|
| 41 |
|
| 42 |
# Example: left elbow angle
|
| 43 |
-
h, w, _ = frame.shape
|
| 44 |
landmarks = results.pose_landmarks.landmark
|
| 45 |
-
shoulder = [landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x *
|
| 46 |
-
landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y *
|
| 47 |
-
elbow = [landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].x *
|
| 48 |
-
landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].y *
|
| 49 |
-
wrist = [landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x *
|
| 50 |
-
landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y *
|
| 51 |
|
| 52 |
angle = calculate_angle(shoulder, elbow, wrist)
|
| 53 |
cv2.putText(frame, f"Left Elbow: {int(angle)} deg", (20,40),
|
| 54 |
cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,0), 2)
|
| 55 |
|
| 56 |
-
|
|
|
|
| 57 |
|
| 58 |
cap.release()
|
| 59 |
-
|
| 60 |
-
# Convert frames to video file
|
| 61 |
-
out_path = "annotated_video.mp4"
|
| 62 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 63 |
-
out = cv2.VideoWriter(out_path, fourcc, 20.0, (frame.shape[1], frame.shape[0]))
|
| 64 |
-
for f in output_frames:
|
| 65 |
-
out.write(f)
|
| 66 |
out.release()
|
| 67 |
-
|
| 68 |
return out_path
|
| 69 |
|
| 70 |
# Gradio interface
|
|
@@ -73,7 +87,7 @@ demo = gr.Interface(
|
|
| 73 |
inputs=gr.Video(label="Upload Video"),
|
| 74 |
outputs=gr.Video(label="Annotated Video"),
|
| 75 |
title="Human Pose Estimation on Video",
|
| 76 |
-
description="Upload a video and see pose landmarks & joint angles
|
| 77 |
)
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import mediapipe as mp
|
| 3 |
import numpy as np
|
| 4 |
import gradio as gr
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
mp_pose = mp.solutions.pose
|
| 8 |
mp_drawing = mp.solutions.drawing_utils
|
|
|
|
| 9 |
pose = mp_pose.Pose(static_image_mode=False, min_detection_confidence=0.5, min_tracking_confidence=0.5)
|
| 10 |
|
| 11 |
def calculate_angle(a, b, c):
|
|
|
|
| 15 |
ba = a - b
|
| 16 |
bc = c - b
|
| 17 |
cosine_angle = np.dot(ba, bc) / (np.linalg.norm(ba) * np.linalg.norm(bc))
|
| 18 |
+
angle = np.arccos(np.clip(cosine_angle, -1.0, 1.0))
|
| 19 |
return np.degrees(angle)
|
| 20 |
|
| 21 |
+
def detect_pose_video(video_path, max_duration=20):
|
| 22 |
+
if not os.path.exists(video_path):
|
| 23 |
+
return None
|
| 24 |
+
|
| 25 |
cap = cv2.VideoCapture(video_path)
|
| 26 |
+
if not cap.isOpened():
|
| 27 |
+
raise ValueError("Cannot open video file")
|
| 28 |
+
|
| 29 |
+
# Video properties
|
| 30 |
+
fps = cap.get(cv2.CAP_PROP_FPS) or 20.0
|
| 31 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) or 640)
|
| 32 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) or 480)
|
| 33 |
+
max_frames = int(fps * max_duration) # Limit to 20 seconds
|
| 34 |
+
|
| 35 |
+
out_path = "annotated_video.mp4"
|
| 36 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 37 |
+
out = cv2.VideoWriter(out_path, fourcc, fps, (width, height))
|
| 38 |
|
| 39 |
+
frame_count = 0
|
| 40 |
+
while frame_count < max_frames:
|
| 41 |
ret, frame = cap.read()
|
| 42 |
if not ret:
|
| 43 |
break
|
| 44 |
|
| 45 |
+
# Resize if too big
|
| 46 |
+
max_dim = 640
|
| 47 |
+
h, w, _ = frame.shape
|
| 48 |
+
if max(h, w) > max_dim:
|
| 49 |
+
scale = max_dim / max(h, w)
|
| 50 |
+
frame = cv2.resize(frame, (int(w*scale), int(h*scale)))
|
| 51 |
+
|
| 52 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 53 |
results = pose.process(frame_rgb)
|
| 54 |
|
|
|
|
| 62 |
)
|
| 63 |
|
| 64 |
# Example: left elbow angle
|
|
|
|
| 65 |
landmarks = results.pose_landmarks.landmark
|
| 66 |
+
shoulder = [landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x * frame.shape[1],
|
| 67 |
+
landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y * frame.shape[0]]
|
| 68 |
+
elbow = [landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].x * frame.shape[1],
|
| 69 |
+
landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].y * frame.shape[0]]
|
| 70 |
+
wrist = [landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x * frame.shape[1],
|
| 71 |
+
landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y * frame.shape[0]]
|
| 72 |
|
| 73 |
angle = calculate_angle(shoulder, elbow, wrist)
|
| 74 |
cv2.putText(frame, f"Left Elbow: {int(angle)} deg", (20,40),
|
| 75 |
cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,0), 2)
|
| 76 |
|
| 77 |
+
out.write(frame)
|
| 78 |
+
frame_count += 1
|
| 79 |
|
| 80 |
cap.release()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
out.release()
|
|
|
|
| 82 |
return out_path
|
| 83 |
|
| 84 |
# Gradio interface
|
|
|
|
| 87 |
inputs=gr.Video(label="Upload Video"),
|
| 88 |
outputs=gr.Video(label="Annotated Video"),
|
| 89 |
title="Human Pose Estimation on Video",
|
| 90 |
+
description="Upload a video (max 20 seconds will be processed) and see pose landmarks & joint angles."
|
| 91 |
)
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|