Spaces:
Sleeping
Sleeping
Update yolo_detect.py
Browse files- yolo_detect.py +19 -9
yolo_detect.py
CHANGED
|
@@ -3,20 +3,30 @@ from ultralytics import YOLO
|
|
| 3 |
from pathlib import Path
|
| 4 |
|
| 5 |
if len(sys.argv) < 3:
|
| 6 |
-
print("Usage: python yolo_detect.py <weights_path> <
|
| 7 |
sys.exit(1)
|
| 8 |
|
| 9 |
weights_path = sys.argv[1]
|
| 10 |
-
|
| 11 |
|
| 12 |
print("Using weights file:", weights_path)
|
| 13 |
|
| 14 |
model = YOLO(weights_path)
|
| 15 |
-
# Use /data/runs/detect as the project directory (writable in Hugging Face Spaces)
|
| 16 |
-
results = model(image_path, save=True, project='/data/runs/detect', exist_ok=True)
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from pathlib import Path
|
| 4 |
|
| 5 |
if len(sys.argv) < 3:
|
| 6 |
+
print("Usage: python yolo_detect.py <weights_path> <media_path>")
|
| 7 |
sys.exit(1)
|
| 8 |
|
| 9 |
weights_path = sys.argv[1]
|
| 10 |
+
media_path = sys.argv[2]
|
| 11 |
|
| 12 |
print("Using weights file:", weights_path)
|
| 13 |
|
| 14 |
model = YOLO(weights_path)
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Check if the file is a video or image
|
| 17 |
+
if media_path.lower().endswith(('.mp4', '.avi', '.mov', '.mkv', '.webm')):
|
| 18 |
+
# Video detection
|
| 19 |
+
results = model(media_path, save=True, project='/data/runs/detect', exist_ok=True)
|
| 20 |
+
# Find the output video file
|
| 21 |
+
for r in results:
|
| 22 |
+
for path in Path(r.save_dir).glob("*.mp4"):
|
| 23 |
+
if path.is_file():
|
| 24 |
+
print(str(path))
|
| 25 |
+
else:
|
| 26 |
+
# Image detection
|
| 27 |
+
results = model(media_path, save=True, project='/data/runs/detect', exist_ok=True)
|
| 28 |
+
# Find the output image file
|
| 29 |
+
for r in results:
|
| 30 |
+
for path in Path(r.save_dir).glob("*"):
|
| 31 |
+
if path.is_file():
|
| 32 |
+
print(str(path))
|