Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import mediapipe as mp
|
| 4 |
-
import
|
| 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
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
"""
|
| 32 |
-
if frame is None:
|
| 33 |
-
return None
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
mp_drawing.draw_landmarks(bgr_draw, hand_landmarks, mp_hands.HAND_CONNECTIONS)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
if pose_results.pose_landmarks:
|
| 59 |
-
mp_drawing.draw_landmarks(bgr_draw, pose_results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
|
|
|
| 63 |
|
| 64 |
-
|
| 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=
|
| 73 |
inputs=[
|
| 74 |
-
gr.
|
| 75 |
-
gr.
|
| 76 |
-
gr.inputs.Checkbox(default=False, label="Return at original webcam resolution")
|
| 77 |
],
|
| 78 |
-
outputs=gr.
|
| 79 |
-
|
| 80 |
-
|
| 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__":
|