Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,38 @@
|
|
| 1 |
import cv2
|
| 2 |
import torch
|
| 3 |
-
import gradio
|
| 4 |
import numpy as np
|
| 5 |
from ultralytics import YOLO
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# Load YOLOv8 model
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
-
print(f"Using device: {device}")
|
| 10 |
model = YOLO('./data/best.pt').to(device)
|
| 11 |
|
| 12 |
def process_video(video):
|
|
|
|
|
|
|
|
|
|
| 13 |
cap = cv2.VideoCapture(video)
|
|
|
|
|
|
|
|
|
|
| 14 |
frame_width, frame_height = 320, 240 # Smaller resolution
|
| 15 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 16 |
|
| 17 |
output_path = "processed_output.mp4"
|
| 18 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 19 |
-
out = cv2.VideoWriter(output_path, fourcc,
|
| 20 |
|
| 21 |
frame_count = 0
|
| 22 |
frame_skip = 5 # Process every 5th frame
|
| 23 |
-
max_frames = 100 # Limit
|
| 24 |
|
| 25 |
while True:
|
| 26 |
ret, frame = cap.read()
|
|
@@ -32,13 +44,10 @@ def process_video(video):
|
|
| 32 |
continue
|
| 33 |
|
| 34 |
frame = cv2.resize(frame, (frame_width, frame_height))
|
| 35 |
-
print(f"Processing frame {frame_count}")
|
| 36 |
|
| 37 |
-
# Inference
|
| 38 |
results = model(frame)
|
| 39 |
annotated_frame = results[0].plot()
|
| 40 |
-
|
| 41 |
-
# Write frame
|
| 42 |
out.write(annotated_frame)
|
| 43 |
|
| 44 |
cap.release()
|
|
@@ -51,7 +60,8 @@ iface = gr.Interface(
|
|
| 51 |
inputs=gr.Video(label="Upload Video"),
|
| 52 |
outputs=gr.Video(label="Processed Video"),
|
| 53 |
title="YOLOv8 Object Detection",
|
| 54 |
-
description="Upload a short video for
|
| 55 |
)
|
| 56 |
|
| 57 |
-
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import torch
|
| 3 |
+
import gradio as gr # Ensure correct import with alias
|
| 4 |
import numpy as np
|
| 5 |
from ultralytics import YOLO
|
| 6 |
|
| 7 |
+
# Debug: Check environment
|
| 8 |
+
print(f"Torch version: {torch.__version__}")
|
| 9 |
+
print(f"Gradio version: {gr.__version__}")
|
| 10 |
+
print(f"Ultralytics version: {YOLO.__version__}")
|
| 11 |
+
print(f"CUDA available: {torch.cuda.is_available()}")
|
| 12 |
+
|
| 13 |
# Load YOLOv8 model
|
| 14 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
print(f"Using device: {device}")
|
| 16 |
model = YOLO('./data/best.pt').to(device)
|
| 17 |
|
| 18 |
def process_video(video):
|
| 19 |
+
if video is None:
|
| 20 |
+
return "Error: No video uploaded"
|
| 21 |
+
|
| 22 |
cap = cv2.VideoCapture(video)
|
| 23 |
+
if not cap.isOpened():
|
| 24 |
+
return "Error: Could not open video file"
|
| 25 |
+
|
| 26 |
frame_width, frame_height = 320, 240 # Smaller resolution
|
| 27 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 28 |
|
| 29 |
output_path = "processed_output.mp4"
|
| 30 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 31 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
|
| 32 |
|
| 33 |
frame_count = 0
|
| 34 |
frame_skip = 5 # Process every 5th frame
|
| 35 |
+
max_frames = 100 # Limit for testing
|
| 36 |
|
| 37 |
while True:
|
| 38 |
ret, frame = cap.read()
|
|
|
|
| 44 |
continue
|
| 45 |
|
| 46 |
frame = cv2.resize(frame, (frame_width, frame_height))
|
| 47 |
+
print(f"Processing frame {frame_count}")
|
| 48 |
|
|
|
|
| 49 |
results = model(frame)
|
| 50 |
annotated_frame = results[0].plot()
|
|
|
|
|
|
|
| 51 |
out.write(annotated_frame)
|
| 52 |
|
| 53 |
cap.release()
|
|
|
|
| 60 |
inputs=gr.Video(label="Upload Video"),
|
| 61 |
outputs=gr.Video(label="Processed Video"),
|
| 62 |
title="YOLOv8 Object Detection",
|
| 63 |
+
description="Upload a short video for object detection"
|
| 64 |
)
|
| 65 |
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
iface.launch()
|