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