Spaces:
Runtime error
Runtime error
Upload inference.py
Browse files- inference.py +77 -0
inference.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import onnxruntime
|
5 |
+
|
6 |
+
try:
|
7 |
+
from demo.object_detection.utils import draw_detections
|
8 |
+
except (ImportError, ModuleNotFoundError):
|
9 |
+
from utils import draw_detections
|
10 |
+
|
11 |
+
class YOLOv10:
|
12 |
+
def __init__(self, path):
|
13 |
+
self.initialize_model(path)
|
14 |
+
|
15 |
+
def __call__(self, image):
|
16 |
+
return self.detect_objects(image)
|
17 |
+
|
18 |
+
def initialize_model(self, path):
|
19 |
+
self.session = onnxruntime.InferenceSession(path, providers=['CPUExecutionProvider'])
|
20 |
+
self.get_input_details()
|
21 |
+
self.get_output_details()
|
22 |
+
|
23 |
+
def detect_objects(self, image, conf_threshold=0.3):
|
24 |
+
input_tensor = self.prepare_input(image)
|
25 |
+
return self.inference(image, input_tensor, conf_threshold)
|
26 |
+
|
27 |
+
def prepare_input(self, image):
|
28 |
+
self.img_height, self.img_width = image.shape[:2]
|
29 |
+
input_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
30 |
+
input_img = cv2.resize(input_img, (self.input_width, self.input_height))
|
31 |
+
input_img = input_img / 255.0
|
32 |
+
input_img = input_img.transpose(2, 0, 1)
|
33 |
+
input_tensor = input_img[np.newaxis, :, :, :].astype(np.float32)
|
34 |
+
return input_tensor
|
35 |
+
|
36 |
+
def inference(self, image, input_tensor, conf_threshold=0.3):
|
37 |
+
start = time.perf_counter()
|
38 |
+
outputs = self.session.run(self.output_names, {self.input_names[0]: input_tensor})
|
39 |
+
print(f"Inference time: {(time.perf_counter() - start) * 1000:.2f} ms")
|
40 |
+
boxes, scores, class_ids = self.process_output(outputs, conf_threshold)
|
41 |
+
return self.draw_detections(image, boxes, scores, class_ids)
|
42 |
+
|
43 |
+
def process_output(self, output, conf_threshold=0.3):
|
44 |
+
predictions = np.squeeze(output[0])
|
45 |
+
scores = predictions[:, 4]
|
46 |
+
predictions = predictions[scores > conf_threshold, :]
|
47 |
+
scores = scores[scores > conf_threshold]
|
48 |
+
if len(scores) == 0:
|
49 |
+
return [], [], []
|
50 |
+
class_ids = predictions[:, 5].astype(int)
|
51 |
+
boxes = self.extract_boxes(predictions)
|
52 |
+
return boxes, scores, class_ids
|
53 |
+
|
54 |
+
def extract_boxes(self, predictions):
|
55 |
+
boxes = predictions[:, :4]
|
56 |
+
boxes = self.rescale_boxes(boxes)
|
57 |
+
return boxes
|
58 |
+
|
59 |
+
def rescale_boxes(self, boxes):
|
60 |
+
input_shape = np.array([self.input_width, self.input_height, self.input_width, self.input_height])
|
61 |
+
boxes = np.divide(boxes, input_shape, dtype=np.float32)
|
62 |
+
boxes *= np.array([self.img_width, self.img_height, self.img_width, self.img_height])
|
63 |
+
return boxes
|
64 |
+
|
65 |
+
def draw_detections(self, image, boxes, scores, class_ids, draw_scores=True, mask_alpha=0.4):
|
66 |
+
return draw_detections(image, boxes, scores, class_ids, mask_alpha)
|
67 |
+
|
68 |
+
def get_input_details(self):
|
69 |
+
model_inputs = self.session.get_inputs()
|
70 |
+
self.input_names = [model_inputs[i].name for i in range(len(model_inputs))]
|
71 |
+
self.input_shape = model_inputs[0].shape
|
72 |
+
self.input_height = self.input_shape[2]
|
73 |
+
self.input_width = self.input_shape[3]
|
74 |
+
|
75 |
+
def get_output_details(self):
|
76 |
+
model_outputs = self.session.get_outputs()
|
77 |
+
self.output_names = [model_outputs[i].name for i in range(len(model_outputs))]
|