Spaces:
Running
Running
import gradio as gr | |
import os | |
from ultralytics import YOLO | |
# Load the YOLO11-pose model (auto-downloads if not present) | |
model = YOLO("yolo11n-pose.pt") | |
def process_input(uploaded_file, youtube_link): | |
""" | |
Process an uploaded file or a YouTube link to perform pose detection. | |
Returns the path to the annotated output. | |
""" | |
if youtube_link and youtube_link.strip(): | |
try: | |
from pytube import YouTube | |
yt = YouTube(youtube_link) | |
stream = yt.streams.filter(file_extension='mp4', progressive=True)\ | |
.order_by("resolution").desc().first() | |
if stream is None: | |
return "No suitable mp4 stream found." | |
input_path = stream.download() | |
except Exception as e: | |
return f"Error downloading video: {e}" | |
elif uploaded_file is not None: | |
input_path = uploaded_file.name | |
else: | |
return "Please provide an uploaded file or a YouTube link." | |
# Run pose detection and save the annotated output. | |
results = model.predict(source=input_path, save=True) | |
try: | |
output_path = results[0].save_path | |
except Exception as e: | |
return f"Error processing the file: {e}" | |
# Optionally remove the downloaded video if applicable. | |
if youtube_link and os.path.exists(input_path): | |
os.remove(input_path) | |
return output_path | |
# Define the Gradio Blocks interface as a global variable. | |
demo = gr.Blocks() | |
with demo: | |
gr.Markdown("# Pose Detection with YOLO11-pose") | |
gr.Markdown("Upload an image/video or provide a YouTube link to detect human poses.") | |
with gr.Row(): | |
file_input = gr.File(label="Upload Image/Video") | |
youtube_input = gr.Textbox(label="Or enter a YouTube link", placeholder="https://...") | |
output_file = gr.File(label="Download Annotated Output") | |
run_button = gr.Button("Run Pose Detection") | |
run_button.click(process_input, inputs=[file_input, youtube_input], outputs=output_file) | |
# Only launch the interface if this file is executed directly. | |
if __name__ == "__main__": | |
demo.launch() | |