Spaces:
Sleeping
Sleeping
Update yolo_detect.py
Browse files- yolo_detect.py +9 -41
yolo_detect.py
CHANGED
|
@@ -1,54 +1,22 @@
|
|
| 1 |
import sys
|
| 2 |
from ultralytics import YOLO
|
| 3 |
from pathlib import Path
|
| 4 |
-
import cv2
|
| 5 |
|
| 6 |
if len(sys.argv) < 3:
|
| 7 |
-
print("Usage: python yolo_detect.py <weights_path> <
|
| 8 |
sys.exit(1)
|
| 9 |
|
| 10 |
weights_path = sys.argv[1]
|
| 11 |
-
|
| 12 |
|
| 13 |
print("Using weights file:", weights_path)
|
| 14 |
|
| 15 |
model = YOLO(weights_path)
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 23 |
-
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 24 |
-
out = cv2.VideoWriter(str(mp4_path), fourcc, fps, (width, height))
|
| 25 |
-
while True:
|
| 26 |
-
ret, frame = cap.read()
|
| 27 |
-
if not ret:
|
| 28 |
-
break
|
| 29 |
-
out.write(frame)
|
| 30 |
-
cap.release()
|
| 31 |
-
out.release()
|
| 32 |
-
return mp4_path
|
| 33 |
-
|
| 34 |
-
# Check if the file is a video or image
|
| 35 |
-
if media_path.lower().endswith(('.mp4', '.avi', '.mov', '.mkv', '.webm')):
|
| 36 |
-
# Video detection
|
| 37 |
-
results = model(media_path, save=True, project='/data/runs/detect', exist_ok=True)
|
| 38 |
-
found = False
|
| 39 |
-
for r in results:
|
| 40 |
-
for avi_path in Path(r.save_dir).glob("*.avi"):
|
| 41 |
-
if avi_path.is_file():
|
| 42 |
-
mp4_path = convert_avi_to_mp4(avi_path)
|
| 43 |
-
print(str(mp4_path))
|
| 44 |
-
found = True
|
| 45 |
-
if not found:
|
| 46 |
-
print("ERROR: No .avi file found for conversion.", file=sys.stderr)
|
| 47 |
-
sys.exit(1)
|
| 48 |
-
else:
|
| 49 |
-
# Image detection
|
| 50 |
-
results = model(media_path, save=True, project='/data/runs/detect', exist_ok=True)
|
| 51 |
-
for r in results:
|
| 52 |
-
for path in Path(r.save_dir).glob("*"):
|
| 53 |
-
if path.is_file():
|
| 54 |
-
print(str(path))
|
|
|
|
| 1 |
import sys
|
| 2 |
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> <image_path>")
|
| 7 |
sys.exit(1)
|
| 8 |
|
| 9 |
weights_path = sys.argv[1]
|
| 10 |
+
image_path = sys.argv[2]
|
| 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 |
+
# Print the path to the saved image
|
| 19 |
+
for r in results:
|
| 20 |
+
for path in Path(r.save_dir).glob("*"):
|
| 21 |
+
if path.is_file():
|
| 22 |
+
print(str(path))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|