vu0018 commited on
Commit
956cfce
·
verified ·
1 Parent(s): 8a40a91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -9
app.py CHANGED
@@ -3,12 +3,17 @@ 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
9
  mp_drawing = mp.solutions.drawing_utils
10
- pose = mp_pose.Pose(static_image_mode=False, min_detection_confidence=0.5, min_tracking_confidence=0.5)
 
 
11
 
 
12
  def calculate_angle(a, b, c):
13
  a = np.array(a)
14
  b = np.array(b)
@@ -19,12 +24,18 @@ def calculate_angle(a, b, c):
19
  angle = np.arccos(np.clip(cosine_angle, -1.0, 1.0))
20
  return np.degrees(angle)
21
 
 
22
  def detect_pose_video(video_path, max_duration=20):
23
  try:
24
  if not os.path.exists(video_path):
25
  return None, "Error: Video file does not exist."
26
 
27
- cap = cv2.VideoCapture(video_path)
 
 
 
 
 
28
  if not cap.isOpened():
29
  return None, "Error: Cannot open video file."
30
 
@@ -33,11 +44,10 @@ def detect_pose_video(video_path, max_duration=20):
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,7 +57,7 @@ def detect_pose_video(video_path, max_duration=20):
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,6 +76,7 @@ def detect_pose_video(video_path, max_duration=20):
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]]
@@ -83,7 +94,27 @@ def detect_pose_video(video_path, max_duration=20):
83
 
84
  cap.release()
85
  out.release()
 
 
86
  return out_path, "Video processed successfully."
87
 
88
  except Exception as e:
89
- return None, f"Error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import numpy as np
4
  import gradio as gr
5
  import tempfile
6
+ import shutil
7
  import os
8
 
9
+ # Initialize MediaPipe Pose
10
  mp_pose = mp.solutions.pose
11
  mp_drawing = mp.solutions.drawing_utils
12
+ 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
  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 uploaded video to a temporary safe path
34
+ temp_input = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
35
+ temp_input.close()
36
+ shutil.copy(video_path, temp_input.name)
37
+
38
+ cap = cv2.VideoCapture(temp_input.name)
39
  if not cap.isOpened():
40
  return None, "Error: Cannot open video file."
41
 
 
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
 
51
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
52
  out = cv2.VideoWriter(out_path, fourcc, fps, (width, height))
53
 
 
57
  if not ret:
58
  break
59
 
60
+ # Resize if frame is too large
61
  max_dim = 640
62
  h, w, _ = frame.shape
63
  if max(h, w) > max_dim:
 
76
  mp_drawing.DrawingSpec(color=(0,0,255), thickness=2)
77
  )
78
 
79
+ # Example: left elbow angle
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
 
95
  cap.release()
96
  out.release()
97
+ os.remove(temp_input.name) # Clean up input temp file
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 with submit button enabled only after video upload
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
+
108
+ video_input = gr.Video(label="Upload Video")
109
+ status_output = gr.Textbox(label="Status")
110
+ video_output = gr.Video(label="Annotated Video")
111
+ submit_btn = gr.Button("Submit", interactive=False)
112
+
113
+ # Enable submit button only if a video is uploaded
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])
119
+
120
+ demo.launch()