Spaces:
Sleeping
Sleeping
File size: 1,837 Bytes
6cb50ff a03d512 a668c53 a03d512 6cb50ff a03d512 33ab799 6cb50ff a03d512 6cb50ff a03d512 6cb50ff a03d512 6cb50ff a03d512 6cb50ff efee8a7 6cb50ff a03d512 6cb50ff a03d512 6cb50ff a03d512 6cb50ff a03d512 9b73d76 a03d512 6cb50ff a03d512 |
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 |
import cv2
import torch
import gradio as gr
from ultralytics import YOLO
# Load YOLOv8 model
model = YOLO('./data/model.pt') # Path to your model
# Define the function that processes the uploaded video
def process_video(video):
# video is now the file path string, not a file object
input_video = cv2.VideoCapture(video) # Directly pass the path to cv2.VideoCapture
# Get frame width, height, and fps from input video
frame_width = int(input_video.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(input_video.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = input_video.get(cv2.CAP_PROP_FPS)
# Define output video writer
output_video_path = "/mnt/data/output_video.mp4" # Path to save the output video
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
output_video = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))
while True:
# Read a frame from the video
ret, frame = input_video.read()
if not ret:
break # End of video
# Perform inference on the frame
results = model(frame)
# The results object contains annotations for the frame
annotated_frame = results[0].plot() # Plot the frame with bounding boxes
# Write the annotated frame to the output video
output_video.write(annotated_frame)
# Release resources
input_video.release()
output_video.release()
return output_video_path
# Create a Gradio interface for video upload
iface = gr.Interface(fn=process_video,
inputs=gr.Video(label="Upload Video"), # Updated line
outputs="file",
title="YOLOv8 Object Detection on Video",
description="Upload a video for object detection using YOLOv8")
# Launch the interface
iface.launch()
|