vk commited on
Commit
3b86909
·
1 Parent(s): db5fd2f

square input handling

Browse files
Files changed (1) hide show
  1. yolox_onnx.py +36 -2
yolox_onnx.py CHANGED
@@ -4,6 +4,7 @@ import numpy as np
4
  import onnxruntime
5
 
6
 
 
7
  class YOLOX_ONNX:
8
 
9
  def __init__(self, model_path):
@@ -13,12 +14,28 @@ class YOLOX_ONNX:
13
  # print(self.model.get_outputs()[0].name)
14
  # print(self.image_size)
15
  self.labels_map=['pedestrian']
16
-
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
 
19
 
20
  def __preprocess_image(self, img, swap=(2, 0, 1)):
21
 
 
 
 
 
22
  padded_img = np.ones((self.image_size[0], self.image_size[1], 3), dtype=np.uint8) * 114
23
  r = min(self.image_size[0] / img.shape[0], self.image_size[1] / img.shape[1])
24
  resized_img = cv2.resize(img, (int(img.shape[1] * r), int(img.shape[0] * r)), interpolation=cv2.INTER_LINEAR).astype(np.uint8)
@@ -102,6 +119,13 @@ class YOLOX_ONNX:
102
 
103
  #valid_boxes_xyxy, valid_scores, valid_classes = self.__remove_duplicates(valid_boxes_xyxy, valid_scores, valid_classes)
104
 
 
 
 
 
 
 
 
105
 
106
  return valid_boxes_xyxy, valid_scores, valid_classes
107
 
@@ -136,7 +160,8 @@ class YOLOX_ONNX:
136
  # lineType=cv2.LINE_AA)
137
  return img
138
 
139
- def predict(self, image, score_thresh=0.5, iou_thresh=0.4):
 
140
  h,w = image.shape[:2]
141
  origin_img=np.copy(image)
142
  model_input = np.copy(image)
@@ -155,3 +180,12 @@ class YOLOX_ONNX:
155
  return d_boxes, d_scores, d_classes
156
 
157
 
 
 
 
 
 
 
 
 
 
 
4
  import onnxruntime
5
 
6
 
7
+
8
  class YOLOX_ONNX:
9
 
10
  def __init__(self, model_path):
 
14
  # print(self.model.get_outputs()[0].name)
15
  # print(self.image_size)
16
  self.labels_map=['pedestrian']
17
+ self.pad_to_square_flag=False
18
+
19
+ def pad_to_square(self,image):
20
+ height, width = image.shape[:2]
21
+ size = max(height, width)
22
+ delta_w = size - width
23
+ delta_h = size - height
24
+ self.top, self.bottom = delta_h // 2, delta_h - (delta_h // 2)
25
+ self.left, self.right = delta_w // 2, delta_w - (delta_w // 2)
26
+ print(self.top, self.bottom,self.left, self.right)
27
+ color = [114,114,114] # padding
28
+ padded_image = cv2.copyMakeBorder(image, self.top, self.bottom, self.left, self.right, cv2.BORDER_CONSTANT, value=color)
29
+ return padded_image
30
 
31
 
32
 
33
  def __preprocess_image(self, img, swap=(2, 0, 1)):
34
 
35
+ if (img.shape[1]/img.shape[0]) > 1.2:
36
+ self.pad_to_square_flag=True
37
+ img = self.pad_to_square(img) # training aspect ratio is 1:1
38
+
39
  padded_img = np.ones((self.image_size[0], self.image_size[1], 3), dtype=np.uint8) * 114
40
  r = min(self.image_size[0] / img.shape[0], self.image_size[1] / img.shape[1])
41
  resized_img = cv2.resize(img, (int(img.shape[1] * r), int(img.shape[0] * r)), interpolation=cv2.INTER_LINEAR).astype(np.uint8)
 
119
 
120
  #valid_boxes_xyxy, valid_scores, valid_classes = self.__remove_duplicates(valid_boxes_xyxy, valid_scores, valid_classes)
121
 
122
+ if self.pad_to_square_flag:
123
+
124
+ for i,offset in enumerate([self.left,self.top,self.right,self.bottom]):
125
+ valid_boxes_xyxy[:, i] = valid_boxes_xyxy[:,i] - offset #remove pad offsets from boundingbox(xmin,ymin,xmax,ymax)
126
+
127
+ self.pad_to_square_flag=False
128
+
129
 
130
  return valid_boxes_xyxy, valid_scores, valid_classes
131
 
 
160
  # lineType=cv2.LINE_AA)
161
  return img
162
 
163
+ def predict(self, image, score_thresh=0.4, iou_thresh=0.4):
164
+
165
  h,w = image.shape[:2]
166
  origin_img=np.copy(image)
167
  model_input = np.copy(image)
 
180
  return d_boxes, d_scores, d_classes
181
 
182
 
183
+
184
+ # if __name__=="__main__":
185
+ # from matplotlib import pyplot as plt
186
+ # path='test-images/test1.jpg'
187
+ # yolox_nano_onnx=YOLOX_ONNX('models/pedestrian-detection-best95.onnx')
188
+ # yolox_nano_onnx.predict(cv2.imread(path))
189
+ # plt.title('Predicted')
190
+ # plt.imshow(cv2.cvtColor(yolox_nano_onnx.output_img,cv2.COLOR_BGR2RGB))
191
+ # plt.show()