Spaces:
Runtime error
Runtime error
Changed from yolov8n-world.pt to yolov8s-world.pt, add error handling
Browse files
app.py
CHANGED
@@ -5,14 +5,50 @@ import torch
|
|
5 |
from ultralytics import SAM, YOLOWorld
|
6 |
import os
|
7 |
|
8 |
-
# Initialize models
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
def detect_motorcycles(first_frame, prompt="motorcycle"):
|
13 |
"""Detect motorcycles in the first frame using YOLO-World and return bounding boxes."""
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
boxes = []
|
17 |
for result in results:
|
18 |
if result.boxes is not None and len(result.boxes.xyxy) > 0:
|
@@ -20,8 +56,10 @@ def detect_motorcycles(first_frame, prompt="motorcycle"):
|
|
20 |
|
21 |
if len(boxes) > 0:
|
22 |
boxes = np.vstack(boxes)
|
|
|
23 |
else:
|
24 |
boxes = np.array([])
|
|
|
25 |
return boxes
|
26 |
|
27 |
def segment_and_highlight_video(video_path, prompt="motorcycle", highlight_color="red"):
|
@@ -131,8 +169,8 @@ iface = gr.Interface(
|
|
131 |
gr.Dropdown(choices=["red", "green", "blue"], label="Highlight Color", value="red")
|
132 |
],
|
133 |
outputs=gr.Video(label="Highlighted Video"),
|
134 |
-
title="Video Segmentation with MobileSAM and YOLO
|
135 |
-
description="Upload a short video (5-10 seconds), specify a text prompt (e.g., 'motorcycle'), and choose a highlight color.
|
136 |
examples=[
|
137 |
[None, "motorcycle", "red"],
|
138 |
[None, "car", "green"],
|
|
|
5 |
from ultralytics import SAM, YOLOWorld
|
6 |
import os
|
7 |
|
8 |
+
# Initialize models with proper error handling and auto-download
|
9 |
+
def initialize_models():
|
10 |
+
"""Initialize models with proper error handling."""
|
11 |
+
try:
|
12 |
+
sam_model = SAM("mobile_sam.pt") # This auto-downloads
|
13 |
+
print("✅ SAM model loaded successfully")
|
14 |
+
except Exception as e:
|
15 |
+
print(f"❌ Error loading SAM model: {e}")
|
16 |
+
raise
|
17 |
+
|
18 |
+
try:
|
19 |
+
# Try different YOLO-World model names that auto-download
|
20 |
+
yolo_model = YOLOWorld("yolov8s-world.pt") # Small world model (auto-downloads)
|
21 |
+
print("✅ YOLO-World model loaded successfully")
|
22 |
+
return sam_model, yolo_model
|
23 |
+
except Exception as e:
|
24 |
+
print(f"❌ Error loading YOLO-World model: {e}")
|
25 |
+
try:
|
26 |
+
# Fallback to regular YOLO if YOLO-World fails
|
27 |
+
from ultralytics import YOLO
|
28 |
+
yolo_model = YOLO("yolov8n.pt") # Regular YOLO nano model
|
29 |
+
print("⚠️ Using regular YOLO model as fallback")
|
30 |
+
return sam_model, yolo_model
|
31 |
+
except Exception as e2:
|
32 |
+
print(f"❌ Fallback YOLO model also failed: {e2}")
|
33 |
+
raise
|
34 |
+
|
35 |
+
sam_model, yolo_model = initialize_models()
|
36 |
|
37 |
def detect_motorcycles(first_frame, prompt="motorcycle"):
|
38 |
"""Detect motorcycles in the first frame using YOLO-World and return bounding boxes."""
|
39 |
+
try:
|
40 |
+
# Check if it's YOLO-World model
|
41 |
+
if hasattr(yolo_model, 'set_classes'):
|
42 |
+
yolo_model.set_classes([prompt])
|
43 |
+
results = yolo_model.predict(first_frame, device="cpu", max_det=2, imgsz=320, verbose=False)
|
44 |
+
else:
|
45 |
+
# Regular YOLO model - can't set custom classes, will detect all objects
|
46 |
+
results = yolo_model.predict(first_frame, device="cpu", max_det=5, imgsz=320, verbose=False)
|
47 |
+
print("⚠️ Using regular YOLO - detecting all objects, not just the specified prompt")
|
48 |
+
except Exception as e:
|
49 |
+
print(f"Error in YOLO prediction: {e}")
|
50 |
+
return np.array([])
|
51 |
+
|
52 |
boxes = []
|
53 |
for result in results:
|
54 |
if result.boxes is not None and len(result.boxes.xyxy) > 0:
|
|
|
56 |
|
57 |
if len(boxes) > 0:
|
58 |
boxes = np.vstack(boxes)
|
59 |
+
print(f"Detected {len(boxes)} objects")
|
60 |
else:
|
61 |
boxes = np.array([])
|
62 |
+
print("No objects detected")
|
63 |
return boxes
|
64 |
|
65 |
def segment_and_highlight_video(video_path, prompt="motorcycle", highlight_color="red"):
|
|
|
169 |
gr.Dropdown(choices=["red", "green", "blue"], label="Highlight Color", value="red")
|
170 |
],
|
171 |
outputs=gr.Video(label="Highlighted Video"),
|
172 |
+
title="Video Segmentation with MobileSAM and YOLO (CPU Optimized)",
|
173 |
+
description="Upload a short video (5-10 seconds), specify a text prompt (e.g., 'motorcycle'), and choose a highlight color. Uses MobileSAM + YOLO for CPU processing at 320x180 resolution.",
|
174 |
examples=[
|
175 |
[None, "motorcycle", "red"],
|
176 |
[None, "car", "green"],
|