mkfallah commited on
Commit
6e8f7ef
·
verified ·
1 Parent(s): 18f84de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -44
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  import cv2
3
  import mediapipe as mp
4
- import numpy as np
5
 
6
  # Initialize MediaPipe once
7
  mp_hands = mp.solutions.hands
@@ -23,62 +23,58 @@ pose = mp_pose.Pose(
23
  min_tracking_confidence=0.5
24
  )
25
 
26
- def process_frame(frame, target_width=640, return_original_size=False):
27
- """
28
- frame: RGB image from webcam
29
- target_width: width to resize before processing
30
- return_original_size: upscale output back to original size
31
- """
32
- if frame is None:
33
- return None
34
 
35
- h0, w0 = frame.shape[:2]
 
 
 
36
 
37
- # Compute resize keeping aspect ratio
38
- target_width = max(160, int(target_width))
39
- scale = target_width / float(w0)
40
- target_height = int(round(h0 * scale))
 
41
 
42
- # Resize for processing
43
- rgb_small = cv2.resize(frame, (target_width, target_height), interpolation=cv2.INTER_AREA)
44
 
45
- # Process with MediaPipe
46
- hand_results = hands.process(rgb_small)
47
- pose_results = pose.process(rgb_small)
 
 
48
 
49
- # Convert to BGR for drawing
50
- bgr_draw = cv2.cvtColor(rgb_small, cv2.COLOR_RGB2BGR)
 
 
51
 
52
- # Draw hands
53
- if hand_results.multi_hand_landmarks:
54
- for hand_landmarks in hand_results.multi_hand_landmarks:
55
- mp_drawing.draw_landmarks(bgr_draw, hand_landmarks, mp_hands.HAND_CONNECTIONS)
56
 
57
- # Draw pose
58
- if pose_results.pose_landmarks:
59
- mp_drawing.draw_landmarks(bgr_draw, pose_results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
60
 
61
- # Convert back to RGB for Gradio
62
- rgb_out_small = cv2.cvtColor(bgr_draw, cv2.COLOR_BGR2RGB)
 
63
 
64
- if return_original_size:
65
- rgb_out = cv2.resize(rgb_out_small, (w0, h0), interpolation=cv2.INTER_LINEAR)
66
- return rgb_out
67
- else:
68
- return rgb_out_small
69
 
70
- # Gradio Interface
71
  iface = gr.Interface(
72
- fn=process_frame,
73
  inputs=[
74
- gr.inputs.Image(shape=(640,480), webcam=True, label="Webcam Input"),
75
- gr.inputs.Slider(minimum=160, maximum=1280, default=640, step=20, label="Processing width"),
76
- gr.inputs.Checkbox(default=False, label="Return at original webcam resolution")
77
  ],
78
- outputs=gr.outputs.Image(label="Processed Output"),
79
- live=True,
80
- title="Live Hand & Body Pose Detection",
81
- description="Real-time MediaPipe Hand and Body Pose detection from webcam."
82
  )
83
 
84
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  import cv2
3
  import mediapipe as mp
4
+ import tempfile
5
 
6
  # Initialize MediaPipe once
7
  mp_hands = mp.solutions.hands
 
23
  min_tracking_confidence=0.5
24
  )
25
 
26
+ def process_video(video_path, target_width=640):
27
+ cap = cv2.VideoCapture(video_path)
28
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
29
+ temp_output = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
30
+ out = None
 
 
 
31
 
32
+ while cap.isOpened():
33
+ ret, frame = cap.read()
34
+ if not ret:
35
+ break
36
 
37
+ h0, w0 = frame.shape[:2]
38
+ # Resize frame keeping aspect ratio
39
+ scale = target_width / float(w0)
40
+ target_height = int(round(h0 * scale))
41
+ frame_small = cv2.resize(frame, (target_width, target_height), interpolation=cv2.INTER_AREA)
42
 
43
+ # Convert BGR to RGB
44
+ rgb_small = cv2.cvtColor(frame_small, cv2.COLOR_BGR2RGB)
45
 
46
+ # Hand detection
47
+ hand_results = hands.process(rgb_small)
48
+ if hand_results.multi_hand_landmarks:
49
+ for hand_landmarks in hand_results.multi_hand_landmarks:
50
+ mp_drawing.draw_landmarks(frame_small, hand_landmarks, mp_hands.HAND_CONNECTIONS)
51
 
52
+ # Pose detection
53
+ pose_results = pose.process(rgb_small)
54
+ if pose_results.pose_landmarks:
55
+ mp_drawing.draw_landmarks(frame_small, pose_results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
56
 
57
+ # Initialize video writer
58
+ if out is None:
59
+ out = cv2.VideoWriter(temp_output.name, fourcc, cap.get(cv2.CAP_PROP_FPS), (frame_small.shape[1], frame_small.shape[0]))
 
60
 
61
+ out.write(frame_small)
 
 
62
 
63
+ cap.release()
64
+ if out:
65
+ out.release()
66
 
67
+ return temp_output.name
 
 
 
 
68
 
 
69
  iface = gr.Interface(
70
+ fn=process_video,
71
  inputs=[
72
+ gr.Video(label="Upload a video"),
73
+ gr.Slider(minimum=160, maximum=1280, value=640, step=20, label="Processing width")
 
74
  ],
75
+ outputs=gr.Video(label="Processed video"),
76
+ title="Hand & Body Pose Detection from Video",
77
+ description="Upload a video and see MediaPipe detect hands and body pose."
 
78
  )
79
 
80
  if __name__ == "__main__":