Spaces:
Running
Running
Tashi Kuenga Phuntsho
commited on
Commit
·
c5a82b4
1
Parent(s):
e550449
App.py
Browse files
app.py
CHANGED
|
@@ -2,16 +2,37 @@ import torch
|
|
| 2 |
from yolov5.utils.general import non_max_suppression, scale_coords
|
| 3 |
from yolov5.utils.plots import plot_boxes
|
| 4 |
from yolov5.models.common import DetectMultiBackend
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
|
|
|
|
| 8 |
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt')
|
| 9 |
|
| 10 |
-
def detect(
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from yolov5.utils.general import non_max_suppression, scale_coords
|
| 3 |
from yolov5.utils.plots import plot_boxes
|
| 4 |
from yolov5.models.common import DetectMultiBackend
|
| 5 |
+
import cv2
|
| 6 |
from PIL import Image
|
| 7 |
import numpy as np
|
| 8 |
|
| 9 |
+
# Load your YOLO model
|
| 10 |
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt')
|
| 11 |
|
| 12 |
+
def detect(frame):
|
| 13 |
+
# Convert the frame to an image format YOLOv5 can process
|
| 14 |
+
results = model(frame)
|
| 15 |
+
return results
|
| 16 |
|
| 17 |
+
# Open the video file (or camera)
|
| 18 |
+
cap = cv2.VideoCapture("test_video.mp4") # replace with your video path or camera index (0 for webcam)
|
| 19 |
+
|
| 20 |
+
while cap.isOpened():
|
| 21 |
+
ret, frame = cap.read()
|
| 22 |
+
if not ret:
|
| 23 |
+
break
|
| 24 |
+
|
| 25 |
+
# Perform inference on the current frame
|
| 26 |
+
results = detect(frame)
|
| 27 |
+
|
| 28 |
+
# Display the frame with results (optional)
|
| 29 |
+
frame = np.array(results.render()[0]) # render predictions on the frame
|
| 30 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) # Convert from RGB to BGR for OpenCV
|
| 31 |
+
cv2.imshow("Detection", frame)
|
| 32 |
+
|
| 33 |
+
# Break loop if 'q' is pressed
|
| 34 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 35 |
+
break
|
| 36 |
+
|
| 37 |
+
cap.release()
|
| 38 |
+
cv2.destroyAllWindows()
|