Update app.py
Browse files
app.py
CHANGED
|
@@ -1,81 +1,63 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import mediapipe as mp
|
|
|
|
| 4 |
import tempfile
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
mp_hands = mp.solutions.hands
|
| 8 |
-
mp_pose = mp.solutions.pose
|
| 9 |
mp_drawing = mp.solutions.drawing_utils
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
static_image_mode=False,
|
| 13 |
-
max_num_hands=2,
|
| 14 |
-
min_detection_confidence=0.5,
|
| 15 |
-
min_tracking_confidence=0.5
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
-
pose = mp_pose.Pose(
|
| 19 |
-
static_image_mode=False,
|
| 20 |
-
model_complexity=1,
|
| 21 |
-
enable_segmentation=False,
|
| 22 |
-
min_detection_confidence=0.5,
|
| 23 |
-
min_tracking_confidence=0.5
|
| 24 |
-
)
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 29 |
-
temp_output = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 30 |
-
out = None
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 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 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 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 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
-
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 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 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import mediapipe as mp
|
| 3 |
+
import streamlit as st
|
| 4 |
import tempfile
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# mediapipe pose setup
|
|
|
|
|
|
|
| 8 |
mp_drawing = mp.solutions.drawing_utils
|
| 9 |
+
mp_pose = mp.solutions.pose
|
| 10 |
|
| 11 |
+
st.title("MediaPipe Body Pose Detection")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# select input source
|
| 14 |
+
option = st.radio("choose input source", ("webcam", "upload video"))
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
video_path = None
|
| 17 |
+
if option == "upload video":
|
| 18 |
+
uploaded_file = st.file_uploader("upload a video file", type=["mp4", "mov", "avi"])
|
| 19 |
+
if uploaded_file:
|
| 20 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False)
|
| 21 |
+
temp_file.write(uploaded_file.read())
|
| 22 |
+
video_path = temp_file.name
|
| 23 |
|
| 24 |
+
elif option == "webcam":
|
| 25 |
+
video_path = 0 # 0 for default webcam
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
if video_path is not None:
|
| 28 |
+
stframe = st.empty()
|
| 29 |
|
| 30 |
+
cap = cv2.VideoCapture(video_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
|
| 33 |
+
while cap.isOpened():
|
| 34 |
+
ret, frame = cap.read()
|
| 35 |
+
if not ret:
|
| 36 |
+
break
|
| 37 |
|
| 38 |
+
# convert to rgb for processing
|
| 39 |
+
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 40 |
+
image.flags.writeable = False
|
| 41 |
|
| 42 |
+
results = pose.process(image)
|
| 43 |
|
| 44 |
+
# convert back to BGR for display
|
| 45 |
+
image.flags.writeable = True
|
| 46 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 47 |
|
| 48 |
+
# draw landmarks and connections if detected
|
| 49 |
+
if results.pose_landmarks:
|
| 50 |
+
mp_drawing.draw_landmarks(
|
| 51 |
+
image,
|
| 52 |
+
results.pose_landmarks,
|
| 53 |
+
mp_pose.POSE_CONNECTIONS,
|
| 54 |
+
mp_drawing.DrawingSpec(color=(0,0,255), thickness=2, circle_radius=2), # landmarks
|
| 55 |
+
mp_drawing.DrawingSpec(color=(0,255,0), thickness=2, circle_radius=2) # connections
|
| 56 |
+
)
|
| 57 |
|
| 58 |
+
# display frame in streamlit
|
| 59 |
+
stframe.image(image, channels="BGR")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
cap.release()
|
| 62 |
+
if option == "upload video":
|
| 63 |
+
os.remove(video_path)
|