Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import cv2
|
|
| 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
|
|
@@ -27,13 +28,16 @@ def detect_pose_video(video_path, max_duration=20):
|
|
| 27 |
if not cap.isOpened():
|
| 28 |
return None, "Error: Cannot open video file."
|
| 29 |
|
| 30 |
-
# Video properties
|
| 31 |
fps = cap.get(cv2.CAP_PROP_FPS) or 20.0
|
| 32 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) or 640)
|
| 33 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) or 480)
|
| 34 |
max_frames = int(fps * max_duration)
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 38 |
out = cv2.VideoWriter(out_path, fourcc, fps, (width, height))
|
| 39 |
|
|
@@ -43,7 +47,7 @@ def detect_pose_video(video_path, max_duration=20):
|
|
| 43 |
if not ret:
|
| 44 |
break
|
| 45 |
|
| 46 |
-
# Resize
|
| 47 |
max_dim = 640
|
| 48 |
h, w, _ = frame.shape
|
| 49 |
if max(h, w) > max_dim:
|
|
@@ -62,7 +66,6 @@ def detect_pose_video(video_path, max_duration=20):
|
|
| 62 |
mp_drawing.DrawingSpec(color=(0,0,255), thickness=2)
|
| 63 |
)
|
| 64 |
|
| 65 |
-
# Example: left elbow angle
|
| 66 |
landmarks = results.pose_landmarks.landmark
|
| 67 |
shoulder = [landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x * frame.shape[1],
|
| 68 |
landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y * frame.shape[0]]
|
|
@@ -84,15 +87,3 @@ def detect_pose_video(video_path, max_duration=20):
|
|
| 84 |
|
| 85 |
except Exception as e:
|
| 86 |
return None, f"Error: {str(e)}"
|
| 87 |
-
|
| 88 |
-
# Gradio interface
|
| 89 |
-
demo = gr.Interface(
|
| 90 |
-
fn=detect_pose_video,
|
| 91 |
-
inputs=gr.Video(label="Upload Video"),
|
| 92 |
-
outputs=[gr.Video(label="Annotated Video"), gr.Textbox(label="Status")],
|
| 93 |
-
title="Human Pose Estimation on Video",
|
| 94 |
-
description="Upload a video (max 20 seconds will be processed) and see pose landmarks & joint angles."
|
| 95 |
-
)
|
| 96 |
-
|
| 97 |
-
if __name__ == "__main__":
|
| 98 |
-
demo.launch()
|
|
|
|
| 2 |
import mediapipe as mp
|
| 3 |
import numpy as np
|
| 4 |
import gradio as gr
|
| 5 |
+
import tempfile
|
| 6 |
import os
|
| 7 |
|
| 8 |
mp_pose = mp.solutions.pose
|
|
|
|
| 28 |
if not cap.isOpened():
|
| 29 |
return None, "Error: Cannot open video file."
|
| 30 |
|
|
|
|
| 31 |
fps = cap.get(cv2.CAP_PROP_FPS) or 20.0
|
| 32 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) or 640)
|
| 33 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) or 480)
|
| 34 |
max_frames = int(fps * max_duration)
|
| 35 |
|
| 36 |
+
# Use tempfile for a safe path Hugging Face can access
|
| 37 |
+
tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 38 |
+
out_path = tmp_file.name
|
| 39 |
+
tmp_file.close()
|
| 40 |
+
|
| 41 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 42 |
out = cv2.VideoWriter(out_path, fourcc, fps, (width, height))
|
| 43 |
|
|
|
|
| 47 |
if not ret:
|
| 48 |
break
|
| 49 |
|
| 50 |
+
# Resize if too big
|
| 51 |
max_dim = 640
|
| 52 |
h, w, _ = frame.shape
|
| 53 |
if max(h, w) > max_dim:
|
|
|
|
| 66 |
mp_drawing.DrawingSpec(color=(0,0,255), thickness=2)
|
| 67 |
)
|
| 68 |
|
|
|
|
| 69 |
landmarks = results.pose_landmarks.landmark
|
| 70 |
shoulder = [landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x * frame.shape[1],
|
| 71 |
landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y * frame.shape[0]]
|
|
|
|
| 87 |
|
| 88 |
except Exception as e:
|
| 89 |
return None, f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|