Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,6 @@ pose = mp_pose.Pose(static_image_mode=False,
|
|
| 13 |
min_detection_confidence=0.5,
|
| 14 |
min_tracking_confidence=0.5)
|
| 15 |
|
| 16 |
-
# Function to calculate joint angles
|
| 17 |
def calculate_angle(a, b, c):
|
| 18 |
a = np.array(a)
|
| 19 |
b = np.array(b)
|
|
@@ -24,13 +23,12 @@ def calculate_angle(a, b, c):
|
|
| 24 |
angle = np.arccos(np.clip(cosine_angle, -1.0, 1.0))
|
| 25 |
return np.degrees(angle)
|
| 26 |
|
| 27 |
-
# Main video processing function
|
| 28 |
def detect_pose_video(video_path, max_duration=20):
|
| 29 |
try:
|
| 30 |
if not os.path.exists(video_path):
|
| 31 |
return None, "Error: Video file does not exist."
|
| 32 |
|
| 33 |
-
# Copy
|
| 34 |
temp_input = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 35 |
temp_input.close()
|
| 36 |
shutil.copy(video_path, temp_input.name)
|
|
@@ -44,7 +42,6 @@ def detect_pose_video(video_path, max_duration=20):
|
|
| 44 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) or 480)
|
| 45 |
max_frames = int(fps * max_duration)
|
| 46 |
|
| 47 |
-
# Output temporary file
|
| 48 |
temp_output = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 49 |
temp_output.close()
|
| 50 |
out_path = temp_output.name
|
|
@@ -57,7 +54,7 @@ def detect_pose_video(video_path, max_duration=20):
|
|
| 57 |
if not ret:
|
| 58 |
break
|
| 59 |
|
| 60 |
-
# Resize if
|
| 61 |
max_dim = 640
|
| 62 |
h, w, _ = frame.shape
|
| 63 |
if max(h, w) > max_dim:
|
|
@@ -76,7 +73,7 @@ def detect_pose_video(video_path, max_duration=20):
|
|
| 76 |
mp_drawing.DrawingSpec(color=(0,0,255), thickness=2)
|
| 77 |
)
|
| 78 |
|
| 79 |
-
#
|
| 80 |
landmarks = results.pose_landmarks.landmark
|
| 81 |
shoulder = [landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x * frame.shape[1],
|
| 82 |
landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y * frame.shape[0]]
|
|
@@ -94,14 +91,14 @@ def detect_pose_video(video_path, max_duration=20):
|
|
| 94 |
|
| 95 |
cap.release()
|
| 96 |
out.release()
|
| 97 |
-
os.remove(temp_input.name) # Clean up input
|
| 98 |
|
| 99 |
return out_path, "Video processed successfully."
|
| 100 |
|
| 101 |
except Exception as e:
|
| 102 |
return None, f"Runtime Error: {str(e)}"
|
| 103 |
|
| 104 |
-
# Gradio interface
|
| 105 |
with gr.Blocks() as demo:
|
| 106 |
gr.Markdown("## Human Pose Estimation on Video\nUpload a video (max 20 seconds). Submit is enabled only after upload.")
|
| 107 |
|
|
@@ -110,9 +107,9 @@ with gr.Blocks() as demo:
|
|
| 110 |
video_output = gr.Video(label="Annotated Video")
|
| 111 |
submit_btn = gr.Button("Submit", interactive=False)
|
| 112 |
|
| 113 |
-
#
|
| 114 |
def enable_submit(video):
|
| 115 |
-
return True if video else False
|
| 116 |
|
| 117 |
video_input.change(enable_submit, inputs=[video_input], outputs=[submit_btn])
|
| 118 |
submit_btn.click(detect_pose_video, inputs=[video_input], outputs=[video_output, status_output])
|
|
|
|
| 13 |
min_detection_confidence=0.5,
|
| 14 |
min_tracking_confidence=0.5)
|
| 15 |
|
|
|
|
| 16 |
def calculate_angle(a, b, c):
|
| 17 |
a = np.array(a)
|
| 18 |
b = np.array(b)
|
|
|
|
| 23 |
angle = np.arccos(np.clip(cosine_angle, -1.0, 1.0))
|
| 24 |
return np.degrees(angle)
|
| 25 |
|
|
|
|
| 26 |
def detect_pose_video(video_path, max_duration=20):
|
| 27 |
try:
|
| 28 |
if not os.path.exists(video_path):
|
| 29 |
return None, "Error: Video file does not exist."
|
| 30 |
|
| 31 |
+
# Copy video to temp file
|
| 32 |
temp_input = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 33 |
temp_input.close()
|
| 34 |
shutil.copy(video_path, temp_input.name)
|
|
|
|
| 42 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) or 480)
|
| 43 |
max_frames = int(fps * max_duration)
|
| 44 |
|
|
|
|
| 45 |
temp_output = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 46 |
temp_output.close()
|
| 47 |
out_path = temp_output.name
|
|
|
|
| 54 |
if not ret:
|
| 55 |
break
|
| 56 |
|
| 57 |
+
# Resize if too large
|
| 58 |
max_dim = 640
|
| 59 |
h, w, _ = frame.shape
|
| 60 |
if max(h, w) > max_dim:
|
|
|
|
| 73 |
mp_drawing.DrawingSpec(color=(0,0,255), thickness=2)
|
| 74 |
)
|
| 75 |
|
| 76 |
+
# Left elbow angle
|
| 77 |
landmarks = results.pose_landmarks.landmark
|
| 78 |
shoulder = [landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x * frame.shape[1],
|
| 79 |
landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y * frame.shape[0]]
|
|
|
|
| 91 |
|
| 92 |
cap.release()
|
| 93 |
out.release()
|
| 94 |
+
os.remove(temp_input.name) # Clean up input
|
| 95 |
|
| 96 |
return out_path, "Video processed successfully."
|
| 97 |
|
| 98 |
except Exception as e:
|
| 99 |
return None, f"Runtime Error: {str(e)}"
|
| 100 |
|
| 101 |
+
# Gradio interface
|
| 102 |
with gr.Blocks() as demo:
|
| 103 |
gr.Markdown("## Human Pose Estimation on Video\nUpload a video (max 20 seconds). Submit is enabled only after upload.")
|
| 104 |
|
|
|
|
| 107 |
video_output = gr.Video(label="Annotated Video")
|
| 108 |
submit_btn = gr.Button("Submit", interactive=False)
|
| 109 |
|
| 110 |
+
# Correct: enable button using .update()
|
| 111 |
def enable_submit(video):
|
| 112 |
+
return submit_btn.update(interactive=True) if video else submit_btn.update(interactive=False)
|
| 113 |
|
| 114 |
video_input.change(enable_submit, inputs=[video_input], outputs=[submit_btn])
|
| 115 |
submit_btn.click(detect_pose_video, inputs=[video_input], outputs=[video_output, status_output])
|