Spaces:
Runtime error
Runtime error
File size: 1,632 Bytes
c48d766 e754390 a833460 c48d766 1e4a4a4 c48d766 1e4a4a4 5f2ef5e 1e4a4a4 5f2ef5e c48d766 e754390 a833460 e754390 271aaa9 e754390 271aaa9 e754390 271aaa9 e754390 271aaa9 e754390 271aaa9 e754390 271aaa9 e754390 271aaa9 e754390 271aaa9 e754390 271aaa9 e754390 |
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 59 |
import torch
import cv2
import tempfile
import gradio as gr
from ultralytics import YOLO
from ultralytics.nn.tasks import DetectionModel
from ultralytics.nn.modules.conv import Conv
# Add all the classes we've seen so far to the safe globals list
torch.serialization.add_safe_globals([
DetectionModel,
torch.nn.modules.container.Sequential,
Conv
])
# Load the YOLO model
model = YOLO("yolov8n.pt")
# Object tracking function
def track_objects(video_input):
# Read uploaded video
cap = cv2.VideoCapture(video_input)
# Create a temporary output video file
tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
out = cv2.VideoWriter(tmp_file.name, fourcc, fps, (width, height))
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Run YOLOv8 tracking
results = model.track(frame, persist=True, tracker="bytetrack.yaml")[0]
# Get annotated frame
annotated_frame = results.plot()
out.write(annotated_frame)
cap.release()
out.release()
return tmp_file.name
# Gradio interface
demo = gr.Interface(
fn=track_objects,
inputs=gr.Video(label="Upload a video to track people"),
outputs=gr.Video(label="Tracked Output"),
title="People Tracking with YOLOv8",
description="Upload a video and track people with YOLOv8 and ByteTrack"
)
demo.launch() |