iamramzan commited on
Commit
a7846ca
·
verified ·
1 Parent(s): ca3f6ee

Update inference.py

Browse files
Files changed (1) hide show
  1. 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, = self.process_output(outputs, conf_threshold)
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
- yolov8_detector = YOLOv10(model_file)
129
 
130
- with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as f:
131
- f.write(
132
- requests.get(
133
- "https://live.staticflickr.com/13/19041780_d6fd803de0_3k.jpg"
134
- ).content
135
- )
136
- f.seek(0)
137
- img = cv2.imread(f.name)
 
 
 
 
 
 
138
 
139
- # # Detect Objects
140
- combined_image = yolov8_detector.detect_objects(img)
141
 
 
 
 
142
 
143
- # Draw detections
144
- cv2.namedWindow("Output", cv2.WINDOW_NORMAL)
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()