vu0018 commited on
Commit
a55051b
·
verified ·
1 Parent(s): f09e80a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -62
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
- 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
-
55
- if results.pose_landmarks:
56
- mp_drawing.draw_landmarks(
57
- frame,
58
- results.pose_landmarks,
59
- mp_pose.POSE_CONNECTIONS,
60
- mp_drawing.DrawingSpec(color=(0,255,0), thickness=2, circle_radius=2),
61
- mp_drawing.DrawingSpec(color=(0,0,255), thickness=2)
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
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
  )