Spaces:
Sleeping
Sleeping
File size: 2,430 Bytes
6cb50ff a03d512 fc635eb 177b569 7a25fd2 99d616d 7a25fd2 99d616d b739bed a952223 c0cf5bc 99d616d 6cb50ff a03d512 99d616d 7a25fd2 99d616d 7a25fd2 99d616d 7a25fd2 99d616d 7a25fd2 99d616d 7a25fd2 99d616d a03d512 99d616d 7a25fd2 99d616d 7a25fd2 99d616d 7a25fd2 99d616d 7a25fd2 99d616d a952223 7a25fd2 99d616d 7a25fd2 99d616d |
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
import cv2
import torch
import gradio as gr
import numpy as np
from ultralytics import YOLO, __version__ as ultralytics_version
# Debug: Check environment
print(f"Torch version: {torch.__version__}")
print(f"Gradio version: {gr.__version__}")
print(f"Ultralytics version: {ultralytics_version}")
print(f"CUDA available: {torch.cuda.is_available()}")
# Load YOLOv8 model
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device: {device}")
model = YOLO('./data/best.pt').to(device)
def process_video(video):
if video is None:
return "Error: No video uploaded"
# Open the input video
cap = cv2.VideoCapture(video)
if not cap.isOpened():
return "Error: Could not open video file"
# Get input video properties
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# Use original resolution to avoid resizing issues (optional: keep 320x240 if needed)
# frame_width, frame_height = 320, 240
print(f"Input video: {frame_width}x{frame_height}, {fps} FPS, {total_frames} frames")
# Set up video writer
output_path = "processed_output.mp4"
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
frame_count = 0
while True:
ret, frame = cap.read()
if not ret:
break
frame_count += 1
print(f"Processing frame {frame_count}/{total_frames}")
# Optional: Resize if needed (remove if using original resolution)
# frame = cv2.resize(frame, (frame_width, frame_height))
# Run YOLOv8 inference
results = model(frame)
annotated_frame = results[0].plot()
# Write the annotated frame to the output video
out.write(annotated_frame)
# Release resources
cap.release()
out.release()
print(f"Output video saved as {output_path}")
return output_path
# Gradio interface
iface = gr.Interface(
fn=process_video,
inputs=gr.Video(label="Upload Video"),
outputs=gr.Video(label="Processed Video"),
title="YOLOv8 Object Detection",
description="Upload a short video for object detection"
)
if __name__ == "__main__":
iface.launch() |