Spaces:
Running
Running
Tashi Kuenga Phuntsho
commited on
Commit
·
d334106
1
Parent(s):
c5a82b4
app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,12 @@
|
|
| 1 |
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 |
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 |
-
#
|
| 14 |
results = model(frame)
|
| 15 |
return results
|
| 16 |
|
|
@@ -25,14 +21,18 @@ while cap.isOpened():
|
|
| 25 |
# Perform inference on the current frame
|
| 26 |
results = detect(frame)
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|
|
|
|
| 1 |
import torch
|
|
|
|
|
|
|
|
|
|
| 2 |
import cv2
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
# Load your YOLO model
|
| 6 |
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt')
|
| 7 |
|
| 8 |
def detect(frame):
|
| 9 |
+
# Perform inference on the current frame
|
| 10 |
results = model(frame)
|
| 11 |
return results
|
| 12 |
|
|
|
|
| 21 |
# Perform inference on the current frame
|
| 22 |
results = detect(frame)
|
| 23 |
|
| 24 |
+
# Render predictions on the frame (draw bounding boxes)
|
| 25 |
+
frame_with_boxes = np.array(results.render()[0]) # render predictions on the frame
|
| 26 |
+
|
| 27 |
+
# Convert from RGB to BGR for OpenCV (since OpenCV uses BGR)
|
| 28 |
+
frame_with_boxes = cv2.cvtColor(frame_with_boxes, cv2.COLOR_RGB2BGR)
|
| 29 |
+
|
| 30 |
+
# Display the frame with results
|
| 31 |
+
cv2.imshow("Detection", frame_with_boxes)
|
| 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()
|