File size: 1,561 Bytes
f3f589c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | import cv2
from cvzone.PoseModule import PoseDetector
import gradio as gr
# Initialize pose detector
poseDetector = PoseDetector()
# Function to process video and detect poses
def process_video(video_path):
cap = cv2.VideoCapture(video_path)
posList = []
output_frames = []
while True:
success, img = cap.read()
if not success:
break
# Detect pose
img = poseDetector.findPose(img)
lmList, bboxInfo = poseDetector.findPosition(img)
if bboxInfo:
lmString = ''
for lm in lmList:
lmString += f'{lm[0]},{img.shape[0]-lm[1]},{lm[2]},'
posList.append(lmString)
# Convert frame to RGB for Gradio display
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
output_frames.append(img_rgb)
# Release video capture
cap.release()
# Save pose data to a file
with open("AnimationFile.txt", "w") as f:
f.writelines(["%s\n" % item for item in posList])
# Return the processed frames as a video
return output_frames
# Gradio interface
def gradio_interface(video):
frames = process_video(video)
return frames
# Create Gradio app
iface = gr.Interface(
fn=gradio_interface,
inputs=gr.Video(label="Input Video"),
outputs=gr.Video(label="Processed Video"),
title="Pose Detection with MediaPipe",
description="Upload a video to detect human poses using MediaPipe and OpenCV.",
)
# Launch the app
iface.launch() |