Spaces:
Sleeping
Sleeping
Update inference.py
Browse files- inference.py +23 -23
inference.py
CHANGED
|
@@ -2,13 +2,11 @@ import time
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
import onnxruntime
|
| 5 |
-
|
| 6 |
from utils import draw_detections
|
| 7 |
|
| 8 |
|
| 9 |
class YOLOv10:
|
| 10 |
def __init__(self, path):
|
| 11 |
-
|
| 12 |
# Initialize model
|
| 13 |
self.initialize_model(path)
|
| 14 |
|
|
@@ -53,7 +51,7 @@ class YOLOv10:
|
|
| 53 |
)
|
| 54 |
|
| 55 |
print(f"Inference time: {(time.perf_counter() - start)*1000:.2f} ms")
|
| 56 |
-
boxes, scores, class_ids
|
| 57 |
return self.draw_detections(image, boxes, scores, class_ids)
|
| 58 |
|
| 59 |
def process_output(self, output, conf_threshold=0.3):
|
|
@@ -82,9 +80,6 @@ class YOLOv10:
|
|
| 82 |
# Scale boxes to original image dimensions
|
| 83 |
boxes = self.rescale_boxes(boxes)
|
| 84 |
|
| 85 |
-
# Convert boxes to xyxy format
|
| 86 |
-
#boxes = xywh2xyxy(boxes)
|
| 87 |
-
|
| 88 |
return boxes
|
| 89 |
|
| 90 |
def rescale_boxes(self, boxes):
|
|
@@ -117,30 +112,35 @@ class YOLOv10:
|
|
| 117 |
|
| 118 |
|
| 119 |
if __name__ == "__main__":
|
| 120 |
-
import requests
|
| 121 |
-
import tempfile
|
| 122 |
from huggingface_hub import hf_hub_download
|
| 123 |
|
| 124 |
model_file = hf_hub_download(
|
| 125 |
repo_id="onnx-community/yolov10s", filename="onnx/model.onnx"
|
| 126 |
)
|
| 127 |
|
| 128 |
-
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
|
| 144 |
-
cv2.
|
| 145 |
-
cv2.imshow("Output", combined_image)
|
| 146 |
-
cv2.waitKey(0)
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
import onnxruntime
|
|
|
|
| 5 |
from utils import draw_detections
|
| 6 |
|
| 7 |
|
| 8 |
class YOLOv10:
|
| 9 |
def __init__(self, path):
|
|
|
|
| 10 |
# Initialize model
|
| 11 |
self.initialize_model(path)
|
| 12 |
|
|
|
|
| 51 |
)
|
| 52 |
|
| 53 |
print(f"Inference time: {(time.perf_counter() - start)*1000:.2f} ms")
|
| 54 |
+
boxes, scores, class_ids = self.process_output(outputs, conf_threshold)
|
| 55 |
return self.draw_detections(image, boxes, scores, class_ids)
|
| 56 |
|
| 57 |
def process_output(self, output, conf_threshold=0.3):
|
|
|
|
| 80 |
# Scale boxes to original image dimensions
|
| 81 |
boxes = self.rescale_boxes(boxes)
|
| 82 |
|
|
|
|
|
|
|
|
|
|
| 83 |
return boxes
|
| 84 |
|
| 85 |
def rescale_boxes(self, boxes):
|
|
|
|
| 112 |
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 115 |
from huggingface_hub import hf_hub_download
|
| 116 |
|
| 117 |
model_file = hf_hub_download(
|
| 118 |
repo_id="onnx-community/yolov10s", filename="onnx/model.onnx"
|
| 119 |
)
|
| 120 |
|
| 121 |
+
yolov10_detector = YOLOv10(model_file)
|
| 122 |
|
| 123 |
+
cap = cv2.VideoCapture(1)
|
| 124 |
+
|
| 125 |
+
if not cap.isOpened():
|
| 126 |
+
print("Error: Could not open video stream.")
|
| 127 |
+
exit()
|
| 128 |
+
|
| 129 |
+
while True:
|
| 130 |
+
ret, frame = cap.read()
|
| 131 |
+
if not ret:
|
| 132 |
+
print("Failed to grab frame. Exiting...")
|
| 133 |
+
break
|
| 134 |
+
|
| 135 |
+
# Perform object detection on the frame
|
| 136 |
+
detected_frame = yolov10_detector.detect_objects(frame)
|
| 137 |
|
| 138 |
+
# Display the frame with detections
|
| 139 |
+
cv2.imshow("Mobile Camera Feed with YOLOv10", detected_frame)
|
| 140 |
|
| 141 |
+
# Press 'q' to quit
|
| 142 |
+
if cv2.waitKey(1) & 0xFF == ord("q"):
|
| 143 |
+
break
|
| 144 |
|
| 145 |
+
cap.release()
|
| 146 |
+
cv2.destroyAllWindows()
|
|
|
|
|
|