Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,73 +19,77 @@ def calculate_angle(a, b, c):
|
|
| 19 |
return np.degrees(angle)
|
| 20 |
|
| 21 |
def detect_pose_video(video_path, max_duration=20):
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
# Gradio interface
|
| 85 |
demo = gr.Interface(
|
| 86 |
fn=detect_pose_video,
|
| 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 |
)
|
|
|
|
| 19 |
return np.degrees(angle)
|
| 20 |
|
| 21 |
def detect_pose_video(video_path, max_duration=20):
|
| 22 |
+
try:
|
| 23 |
+
if not os.path.exists(video_path):
|
| 24 |
+
return None, "Error: Video file does not exist."
|
| 25 |
+
|
| 26 |
+
cap = cv2.VideoCapture(video_path)
|
| 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 |
+
out_path = "annotated_video.mp4"
|
| 37 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 38 |
+
out = cv2.VideoWriter(out_path, fourcc, fps, (width, height))
|
| 39 |
+
|
| 40 |
+
frame_count = 0
|
| 41 |
+
while frame_count < max_frames:
|
| 42 |
+
ret, frame = cap.read()
|
| 43 |
+
if not ret:
|
| 44 |
+
break
|
| 45 |
+
|
| 46 |
+
# Resize frame if too big
|
| 47 |
+
max_dim = 640
|
| 48 |
+
h, w, _ = frame.shape
|
| 49 |
+
if max(h, w) > max_dim:
|
| 50 |
+
scale = max_dim / max(h, w)
|
| 51 |
+
frame = cv2.resize(frame, (int(w*scale), int(h*scale)))
|
| 52 |
+
|
| 53 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 54 |
+
results = pose.process(frame_rgb)
|
| 55 |
+
|
| 56 |
+
if results.pose_landmarks:
|
| 57 |
+
mp_drawing.draw_landmarks(
|
| 58 |
+
frame,
|
| 59 |
+
results.pose_landmarks,
|
| 60 |
+
mp_pose.POSE_CONNECTIONS,
|
| 61 |
+
mp_drawing.DrawingSpec(color=(0,255,0), thickness=2, circle_radius=2),
|
| 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]]
|
| 69 |
+
elbow = [landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].x * frame.shape[1],
|
| 70 |
+
landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].y * frame.shape[0]]
|
| 71 |
+
wrist = [landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x * frame.shape[1],
|
| 72 |
+
landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y * frame.shape[0]]
|
| 73 |
+
|
| 74 |
+
angle = calculate_angle(shoulder, elbow, wrist)
|
| 75 |
+
cv2.putText(frame, f"Left Elbow: {int(angle)} deg", (20,40),
|
| 76 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,0), 2)
|
| 77 |
+
|
| 78 |
+
out.write(frame)
|
| 79 |
+
frame_count += 1
|
| 80 |
+
|
| 81 |
+
cap.release()
|
| 82 |
+
out.release()
|
| 83 |
+
return out_path, "Video processed successfully."
|
| 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 |
)
|