iotaminer commited on
Commit
89d1242
·
verified ·
1 Parent(s): 1100eae

Clone of smile0123/model4@c68d9940

Browse files
Files changed (5) hide show
  1. README.md +3 -0
  2. chute_config.yml +19 -0
  3. class_names.txt +1 -0
  4. miner.py +633 -0
  5. weights.onnx +3 -0
README.md ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ # Cloned model
2
+
3
+ From smile0123/model4@c68d9940
chute_config.yml ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Image:
2
+ from_base: parachutes/python:3.12
3
+ run_command:
4
+ - pip install --upgrade setuptools wheel
5
+ - pip install 'numpy>=1.23' 'onnxruntime-gpu[cuda,cudnn]>=1.16' 'opencv-python>=4.7' 'pillow>=9.5' 'huggingface_hub>=0.19.4' 'pydantic>=2.0' 'pyyaml>=6.0' 'aiohttp>=3.9'
6
+ - pip install torch torchvision
7
+
8
+ NodeSelector:
9
+ gpu_count: 1
10
+ include:
11
+ - pro_6000
12
+
13
+ Chute:
14
+ tee: true
15
+ timeout_seconds: 900
16
+ concurrency: 4
17
+ max_instances: 5
18
+ scaling_threshold: 0.5
19
+ shutdown_after_seconds: 288000
class_names.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ number_plate
miner.py ADDED
@@ -0,0 +1,633 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ import math
3
+
4
+ import cv2
5
+ import numpy as np
6
+ import onnxruntime as ort
7
+ from numpy import ndarray
8
+ from pydantic import BaseModel
9
+
10
+
11
+ class BoundingBox(BaseModel):
12
+ x1: int
13
+ y1: int
14
+ x2: int
15
+ y2: int
16
+ cls_id: int
17
+ conf: float
18
+
19
+
20
+ class TVFrameResult(BaseModel):
21
+ frame_id: int
22
+ boxes: list[BoundingBox]
23
+ keypoints: list[tuple[int, int]]
24
+
25
+
26
+ class Miner:
27
+ def __init__(self, path_hf_repo: Path) -> None:
28
+ model_path = path_hf_repo / "weights.onnx"
29
+
30
+ # Canonical class indices (validator / PGT / manifest); match dataset.yaml names order.
31
+ self.class_names = ['numberplate']
32
+ # ONNX class index order from training export (Ultralytics nc=1 → single id 0).
33
+ model_class_order = ['numberplate']
34
+ self._train_cls_to_canonical = np.array(
35
+ [self.class_names.index(n) for n in model_class_order],
36
+ dtype=np.int32
37
+ )
38
+ print("ORT version:", ort.__version__)
39
+
40
+ try:
41
+ ort.preload_dlls()
42
+ print("✅ onnxruntime.preload_dlls() success")
43
+ except Exception as e:
44
+ print(f"⚠️ preload_dlls failed: {e}")
45
+
46
+ print("ORT available providers BEFORE session:", ort.get_available_providers())
47
+
48
+ sess_options = ort.SessionOptions()
49
+ sess_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
50
+
51
+ try:
52
+ self.session = ort.InferenceSession(
53
+ str(model_path),
54
+ sess_options=sess_options,
55
+ providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
56
+ )
57
+ print("✅ Created ORT session with preferred CUDA provider list")
58
+ except Exception as e:
59
+ print(f"⚠️ CUDA session creation failed, falling back to CPU: {e}")
60
+ self.session = ort.InferenceSession(
61
+ str(model_path),
62
+ sess_options=sess_options,
63
+ providers=["CPUExecutionProvider"],
64
+ )
65
+
66
+ print("ORT session providers:", self.session.get_providers())
67
+
68
+ for inp in self.session.get_inputs():
69
+ print("INPUT:", inp.name, inp.shape, inp.type)
70
+
71
+ for out in self.session.get_outputs():
72
+ print("OUTPUT:", out.name, out.shape, out.type)
73
+
74
+ self.input_name = self.session.get_inputs()[0].name
75
+ self.output_names = [output.name for output in self.session.get_outputs()]
76
+ self.input_shape = self.session.get_inputs()[0].shape
77
+
78
+ self.input_height = self._safe_dim(self.input_shape[2], default=1280)
79
+ self.input_width = self._safe_dim(self.input_shape[3], default=1280)
80
+
81
+ # ---------- Scoring-oriented thresholds (single-class number plates) ----------
82
+ # MUST stay in sync with miner_script/002/miner.py so non-sweep predictions match 002.
83
+ self.conf_thres = 0.15
84
+
85
+ # Below this on orig view, a box needs a flip-view IoU match (TTA) to survive.
86
+ self.conf_high = 0.90
87
+
88
+ self.iou_thres = 0.32
89
+
90
+ # Plates are small; allow a bit of shift between orig / flipped view.
91
+ self.tta_match_iou = 0.01
92
+
93
+ self.max_det = 150
94
+ self.use_tta = True
95
+
96
+ # Box sanity filters (plates can be small and very wide)
97
+ self.min_box_area = 2 * 2
98
+ self.min_w = 1
99
+ self.min_h = 1
100
+ self.max_aspect_ratio = 8.0
101
+ self.max_box_area_ratio = 0.8
102
+
103
+ print(f"✅ ONNX model loaded from: {model_path}")
104
+ print(f"✅ ONNX providers: {self.session.get_providers()}")
105
+ print(f"✅ ONNX input: name={self.input_name}, shape={self.input_shape}")
106
+
107
+ def __repr__(self) -> str:
108
+ return (
109
+ f"ONNXRuntime(session={type(self.session).__name__}, "
110
+ f"providers={self.session.get_providers()})"
111
+ )
112
+
113
+ @staticmethod
114
+ def _safe_dim(value, default: int) -> int:
115
+ return value if isinstance(value, int) and value > 0 else default
116
+
117
+ def _remap_train_cls_ids(self, cls_ids: np.ndarray) -> np.ndarray:
118
+ idx = np.clip(cls_ids.astype(np.int64, copy=False), 0, len(self._train_cls_to_canonical) - 1)
119
+ return self._train_cls_to_canonical[idx]
120
+
121
+ def _letterbox(
122
+ self,
123
+ image: ndarray,
124
+ new_shape: tuple[int, int],
125
+ color=(114, 114, 114),
126
+ ) -> tuple[ndarray, float, tuple[float, float]]:
127
+ h, w = image.shape[:2]
128
+ new_w, new_h = new_shape
129
+
130
+ ratio = min(new_w / w, new_h / h)
131
+ resized_w = int(round(w * ratio))
132
+ resized_h = int(round(h * ratio))
133
+
134
+ if (resized_w, resized_h) != (w, h):
135
+ interp = cv2.INTER_CUBIC if ratio > 1.0 else cv2.INTER_LINEAR
136
+ image = cv2.resize(image, (resized_w, resized_h), interpolation=interp)
137
+
138
+ dw = new_w - resized_w
139
+ dh = new_h - resized_h
140
+ dw /= 2.0
141
+ dh /= 2.0
142
+
143
+ left = int(round(dw - 0.1))
144
+ right = int(round(dw + 0.1))
145
+ top = int(round(dh - 0.1))
146
+ bottom = int(round(dh + 0.1))
147
+
148
+ padded = cv2.copyMakeBorder(
149
+ image,
150
+ top,
151
+ bottom,
152
+ left,
153
+ right,
154
+ borderType=cv2.BORDER_CONSTANT,
155
+ value=color,
156
+ )
157
+ return padded, ratio, (dw, dh)
158
+
159
+ def _preprocess(
160
+ self, image: ndarray
161
+ ) -> tuple[np.ndarray, float, tuple[float, float], tuple[int, int]]:
162
+ orig_h, orig_w = image.shape[:2]
163
+
164
+ img, ratio, pad = self._letterbox(
165
+ image, (self.input_width, self.input_height)
166
+ )
167
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
168
+ img = img.astype(np.float32) / 255.0
169
+ img = np.transpose(img, (2, 0, 1))[None, ...]
170
+ img = np.ascontiguousarray(img, dtype=np.float32)
171
+
172
+ return img, ratio, pad, (orig_w, orig_h)
173
+
174
+ @staticmethod
175
+ def _clip_boxes(boxes: np.ndarray, image_size: tuple[int, int]) -> np.ndarray:
176
+ w, h = image_size
177
+ boxes[:, 0] = np.clip(boxes[:, 0], 0, w - 1)
178
+ boxes[:, 1] = np.clip(boxes[:, 1], 0, h - 1)
179
+ boxes[:, 2] = np.clip(boxes[:, 2], 0, w - 1)
180
+ boxes[:, 3] = np.clip(boxes[:, 3], 0, h - 1)
181
+ return boxes
182
+
183
+ @staticmethod
184
+ def _xywh_to_xyxy(boxes: np.ndarray) -> np.ndarray:
185
+ out = np.empty_like(boxes)
186
+ out[:, 0] = boxes[:, 0] - boxes[:, 2] / 2.0
187
+ out[:, 1] = boxes[:, 1] - boxes[:, 3] / 2.0
188
+ out[:, 2] = boxes[:, 0] + boxes[:, 2] / 2.0
189
+ out[:, 3] = boxes[:, 1] + boxes[:, 3] / 2.0
190
+ return out
191
+
192
+ @staticmethod
193
+ def _hard_nms(
194
+ boxes: np.ndarray,
195
+ scores: np.ndarray,
196
+ iou_thresh: float,
197
+ ) -> np.ndarray:
198
+ if len(boxes) == 0:
199
+ return np.array([], dtype=np.intp)
200
+
201
+ boxes = np.asarray(boxes, dtype=np.float32)
202
+ scores = np.asarray(scores, dtype=np.float32)
203
+ order = np.argsort(scores)[::-1]
204
+ keep = []
205
+
206
+ while len(order) > 0:
207
+ i = order[0]
208
+ keep.append(i)
209
+ if len(order) == 1:
210
+ break
211
+
212
+ rest = order[1:]
213
+
214
+ xx1 = np.maximum(boxes[i, 0], boxes[rest, 0])
215
+ yy1 = np.maximum(boxes[i, 1], boxes[rest, 1])
216
+ xx2 = np.minimum(boxes[i, 2], boxes[rest, 2])
217
+ yy2 = np.minimum(boxes[i, 3], boxes[rest, 3])
218
+
219
+ inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
220
+
221
+ area_i = np.maximum(0.0, (boxes[i, 2] - boxes[i, 0])) * np.maximum(0.0, (boxes[i, 3] - boxes[i, 1]))
222
+ area_r = np.maximum(0.0, (boxes[rest, 2] - boxes[rest, 0])) * np.maximum(0.0, (boxes[rest, 3] - boxes[rest, 1]))
223
+
224
+ iou = inter / (area_i + area_r - inter + 1e-7)
225
+ order = rest[iou <= iou_thresh]
226
+
227
+ return np.array(keep, dtype=np.intp)
228
+
229
+ @classmethod
230
+ def _nms_per_class(
231
+ cls,
232
+ boxes: np.ndarray,
233
+ scores: np.ndarray,
234
+ cls_ids: np.ndarray,
235
+ iou_thresh: float,
236
+ max_det: int,
237
+ ) -> np.ndarray:
238
+ """NMS within each class so overlapping predictions of different classes are not merged away."""
239
+ if len(boxes) == 0:
240
+ return np.array([], dtype=np.intp)
241
+ keep_all: list[int] = []
242
+ for c in np.unique(cls_ids):
243
+ idxs = np.nonzero(cls_ids == c)[0]
244
+ if len(idxs) == 0:
245
+ continue
246
+ local_keep = cls._hard_nms(boxes[idxs], scores[idxs], iou_thresh)
247
+ keep_all.extend(idxs[local_keep].tolist())
248
+ keep_all = np.array(keep_all, dtype=np.intp)
249
+ order = np.argsort(scores[keep_all])[::-1]
250
+ return keep_all[order[:max_det]]
251
+
252
+ @staticmethod
253
+ def _box_iou_one_to_many(box: np.ndarray, boxes: np.ndarray) -> np.ndarray:
254
+ xx1 = np.maximum(box[0], boxes[:, 0])
255
+ yy1 = np.maximum(box[1], boxes[:, 1])
256
+ xx2 = np.minimum(box[2], boxes[:, 2])
257
+ yy2 = np.minimum(box[3], boxes[:, 3])
258
+
259
+ inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
260
+
261
+ area_a = max(0.0, (box[2] - box[0]) * (box[3] - box[1]))
262
+ area_b = np.maximum(0.0, boxes[:, 2] - boxes[:, 0]) * np.maximum(0.0, boxes[:, 3] - boxes[:, 1])
263
+
264
+ return inter / (area_a + area_b - inter + 1e-7)
265
+
266
+ def _filter_sane_boxes(
267
+ self,
268
+ boxes: np.ndarray,
269
+ scores: np.ndarray,
270
+ cls_ids: np.ndarray,
271
+ orig_size: tuple[int, int],
272
+ ) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
273
+ if len(boxes) == 0:
274
+ return boxes, scores, cls_ids
275
+
276
+ orig_w, orig_h = orig_size
277
+ image_area = float(orig_w * orig_h)
278
+
279
+ keep = []
280
+ for i, box in enumerate(boxes):
281
+ x1, y1, x2, y2 = box.tolist()
282
+ bw = x2 - x1
283
+ bh = y2 - y1
284
+
285
+ if bw <= 0 or bh <= 0:
286
+ continue
287
+ if bw < self.min_w or bh < self.min_h:
288
+ continue
289
+
290
+ area = bw * bh
291
+ if area < self.min_box_area:
292
+ continue
293
+ if area > self.max_box_area_ratio * image_area:
294
+ continue
295
+
296
+ ar = max(bw / max(bh, 1e-6), bh / max(bw, 1e-6))
297
+ if ar > self.max_aspect_ratio:
298
+ continue
299
+
300
+ keep.append(i)
301
+
302
+ if not keep:
303
+ return (
304
+ np.empty((0, 4), dtype=np.float32),
305
+ np.empty((0,), dtype=np.float32),
306
+ np.empty((0,), dtype=np.int32),
307
+ )
308
+
309
+ keep = np.array(keep, dtype=np.intp)
310
+ return boxes[keep], scores[keep], cls_ids[keep]
311
+
312
+ def _decode_final_dets(
313
+ self,
314
+ preds: np.ndarray,
315
+ ratio: float,
316
+ pad: tuple[float, float],
317
+ orig_size: tuple[int, int],
318
+ ) -> list[BoundingBox]:
319
+ if preds.ndim == 3 and preds.shape[0] == 1:
320
+ preds = preds[0]
321
+
322
+ if preds.ndim != 2 or preds.shape[1] < 6:
323
+ raise ValueError(f"Unexpected ONNX final-det output shape: {preds.shape}")
324
+
325
+ boxes = preds[:, :4].astype(np.float32)
326
+ scores = preds[:, 4].astype(np.float32)
327
+ cls_ids = self._remap_train_cls_ids(preds[:, 5].astype(np.int32))
328
+ # Single class: numberplate (see self.class_names).
329
+
330
+ # candidate threshold
331
+ keep = scores >= self.conf_thres
332
+ boxes = boxes[keep]
333
+ scores = scores[keep]
334
+ cls_ids = cls_ids[keep]
335
+
336
+ if len(boxes) == 0:
337
+ return []
338
+
339
+ pad_w, pad_h = pad
340
+ orig_w, orig_h = orig_size
341
+
342
+ boxes[:, [0, 2]] -= pad_w
343
+ boxes[:, [1, 3]] -= pad_h
344
+ boxes /= ratio
345
+ boxes = self._clip_boxes(boxes, (orig_w, orig_h))
346
+
347
+ boxes, scores, cls_ids = self._filter_sane_boxes(boxes, scores, cls_ids, orig_size)
348
+ if len(boxes) == 0:
349
+ return []
350
+
351
+ keep_idx = self._nms_per_class(
352
+ boxes, scores, cls_ids, self.iou_thres, self.max_det
353
+ )
354
+
355
+ boxes = boxes[keep_idx]
356
+ scores = scores[keep_idx]
357
+ cls_ids = cls_ids[keep_idx]
358
+
359
+ return [
360
+ BoundingBox(
361
+ x1=int(math.floor(box[0])),
362
+ y1=int(math.floor(box[1])),
363
+ x2=int(math.ceil(box[2])),
364
+ y2=int(math.ceil(box[3])),
365
+ cls_id=int(cls_id),
366
+ conf=float(conf),
367
+ )
368
+ for box, conf, cls_id in zip(boxes, scores, cls_ids)
369
+ if box[2] > box[0] and box[3] > box[1]
370
+ ]
371
+
372
+ def _decode_raw_yolo(
373
+ self,
374
+ preds: np.ndarray,
375
+ ratio: float,
376
+ pad: tuple[float, float],
377
+ orig_size: tuple[int, int],
378
+ ) -> list[BoundingBox]:
379
+ if preds.ndim != 3:
380
+ raise ValueError(f"Unexpected raw ONNX output shape: {preds.shape}")
381
+ if preds.shape[0] != 1:
382
+ raise ValueError(f"Unexpected batch dimension in raw output: {preds.shape}")
383
+
384
+ preds = preds[0]
385
+
386
+ # Normalize to [N, C]
387
+ if preds.shape[0] <= 16 and preds.shape[1] > preds.shape[0]:
388
+ preds = preds.T
389
+
390
+ if preds.ndim != 2 or preds.shape[1] < 5:
391
+ raise ValueError(f"Unexpected normalized raw output shape: {preds.shape}")
392
+
393
+ boxes_xywh = preds[:, :4].astype(np.float32)
394
+ tail = preds[:, 4:].astype(np.float32)
395
+
396
+ # Supports:
397
+ # [x,y,w,h,score] single-class
398
+ # [x,y,w,h,obj,cls] YOLO standard single-class
399
+ # [x,y,w,h,obj,cls1,cls2,...] multi-class
400
+ if tail.shape[1] == 1:
401
+ scores = tail[:, 0]
402
+ cls_ids = np.zeros(len(scores), dtype=np.int32)
403
+ elif tail.shape[1] == 2:
404
+ obj = tail[:, 0]
405
+ cls_prob = tail[:, 1]
406
+ scores = obj * cls_prob
407
+ cls_ids = np.zeros(len(scores), dtype=np.int32)
408
+ else:
409
+ obj = tail[:, 0]
410
+ class_probs = tail[:, 1:]
411
+ cls_ids = np.argmax(class_probs, axis=1).astype(np.int32)
412
+ cls_scores = class_probs[np.arange(len(class_probs)), cls_ids]
413
+ scores = obj * cls_scores
414
+
415
+ cls_ids = self._remap_train_cls_ids(cls_ids)
416
+
417
+ keep = scores >= self.conf_thres
418
+ boxes_xywh = boxes_xywh[keep]
419
+ scores = scores[keep]
420
+ cls_ids = cls_ids[keep]
421
+
422
+ if len(boxes_xywh) == 0:
423
+ return []
424
+
425
+ boxes = self._xywh_to_xyxy(boxes_xywh)
426
+
427
+ pad_w, pad_h = pad
428
+ orig_w, orig_h = orig_size
429
+
430
+ boxes[:, [0, 2]] -= pad_w
431
+ boxes[:, [1, 3]] -= pad_h
432
+ boxes /= ratio
433
+ boxes = self._clip_boxes(boxes, (orig_w, orig_h))
434
+
435
+ boxes, scores, cls_ids = self._filter_sane_boxes(boxes, scores, cls_ids, orig_size)
436
+ if len(boxes) == 0:
437
+ return []
438
+
439
+ keep_idx = self._nms_per_class(
440
+ boxes, scores, cls_ids, self.iou_thres, self.max_det
441
+ )
442
+
443
+ boxes = boxes[keep_idx]
444
+ scores = scores[keep_idx]
445
+ cls_ids = cls_ids[keep_idx]
446
+
447
+ return [
448
+ BoundingBox(
449
+ x1=int(math.floor(box[0])),
450
+ y1=int(math.floor(box[1])),
451
+ x2=int(math.ceil(box[2])),
452
+ y2=int(math.ceil(box[3])),
453
+ cls_id=int(cls_id),
454
+ conf=float(conf),
455
+ )
456
+ for box, conf, cls_id in zip(boxes, scores, cls_ids)
457
+ if box[2] > box[0] and box[3] > box[1]
458
+ ]
459
+
460
+ def _postprocess(
461
+ self,
462
+ output: np.ndarray,
463
+ ratio: float,
464
+ pad: tuple[float, float],
465
+ orig_size: tuple[int, int],
466
+ ) -> list[BoundingBox]:
467
+ if output.ndim == 2 and output.shape[1] >= 6:
468
+ return self._decode_final_dets(output, ratio, pad, orig_size)
469
+
470
+ if output.ndim == 3 and output.shape[0] == 1 and output.shape[2] >= 6:
471
+ return self._decode_final_dets(output, ratio, pad, orig_size)
472
+
473
+ return self._decode_raw_yolo(output, ratio, pad, orig_size)
474
+
475
+ def _predict_single(self, image: np.ndarray) -> list[BoundingBox]:
476
+ if image is None:
477
+ raise ValueError("Input image is None")
478
+ if not isinstance(image, np.ndarray):
479
+ raise TypeError(f"Input is not numpy array: {type(image)}")
480
+ if image.ndim != 3:
481
+ raise ValueError(f"Expected HWC image, got shape={image.shape}")
482
+ if image.shape[0] <= 0 or image.shape[1] <= 0:
483
+ raise ValueError(f"Invalid image shape={image.shape}")
484
+ if image.shape[2] != 3:
485
+ raise ValueError(f"Expected 3 channels, got shape={image.shape}")
486
+
487
+ if image.dtype != np.uint8:
488
+ image = image.astype(np.uint8)
489
+
490
+ input_tensor, ratio, pad, orig_size = self._preprocess(image)
491
+
492
+ expected_shape = (1, 3, self.input_height, self.input_width)
493
+ if input_tensor.shape != expected_shape:
494
+ raise ValueError(
495
+ f"Bad input tensor shape={input_tensor.shape}, expected={expected_shape}"
496
+ )
497
+
498
+ outputs = self.session.run(self.output_names, {self.input_name: input_tensor})
499
+ det_output = outputs[0]
500
+ return self._postprocess(det_output, ratio, pad, orig_size)
501
+
502
+ def _merge_tta_consensus(
503
+ self,
504
+ boxes_orig: list[BoundingBox],
505
+ boxes_flip: list[BoundingBox],
506
+ ) -> list[BoundingBox]:
507
+ """
508
+ Keep:
509
+ - any box with conf >= conf_high
510
+ - low/medium-conf boxes only if confirmed across TTA views
511
+ Then run final hard NMS.
512
+ """
513
+ if not boxes_orig and not boxes_flip:
514
+ return []
515
+
516
+ coords_o = np.array([[b.x1, b.y1, b.x2, b.y2] for b in boxes_orig], dtype=np.float32) if boxes_orig else np.empty((0, 4), dtype=np.float32)
517
+ scores_o = np.array([b.conf for b in boxes_orig], dtype=np.float32) if boxes_orig else np.empty((0,), dtype=np.float32)
518
+ cls_o = np.array([b.cls_id for b in boxes_orig], dtype=np.int32) if boxes_orig else np.empty((0,), dtype=np.int32)
519
+
520
+ coords_f = np.array([[b.x1, b.y1, b.x2, b.y2] for b in boxes_flip], dtype=np.float32) if boxes_flip else np.empty((0, 4), dtype=np.float32)
521
+ scores_f = np.array([b.conf for b in boxes_flip], dtype=np.float32) if boxes_flip else np.empty((0,), dtype=np.float32)
522
+ cls_f = np.array([b.cls_id for b in boxes_flip], dtype=np.int32) if boxes_flip else np.empty((0,), dtype=np.int32)
523
+
524
+ accepted_boxes = []
525
+ accepted_scores = []
526
+ accepted_cls = []
527
+
528
+ # Original view candidates
529
+ for i in range(len(coords_o)):
530
+ score = scores_o[i]
531
+ if score >= self.conf_high:
532
+ accepted_boxes.append(coords_o[i])
533
+ accepted_scores.append(score)
534
+ accepted_cls.append(int(cls_o[i]))
535
+ elif len(coords_f) > 0:
536
+ ious = self._box_iou_one_to_many(coords_o[i], coords_f)
537
+ j = int(np.argmax(ious))
538
+ if ious[j] >= self.tta_match_iou:
539
+ fused_score = max(score, scores_f[j])
540
+ accepted_boxes.append(coords_o[i])
541
+ accepted_scores.append(fused_score)
542
+ accepted_cls.append(int(cls_o[i]))
543
+
544
+ # Flipped-view high-confidence boxes that original missed
545
+ for i in range(len(coords_f)):
546
+ score = scores_f[i]
547
+ if score < self.conf_high:
548
+ continue
549
+
550
+ if len(coords_o) == 0:
551
+ accepted_boxes.append(coords_f[i])
552
+ accepted_scores.append(score)
553
+ accepted_cls.append(int(cls_f[i]))
554
+ continue
555
+
556
+ ious = self._box_iou_one_to_many(coords_f[i], coords_o)
557
+ if np.max(ious) < self.tta_match_iou:
558
+ accepted_boxes.append(coords_f[i])
559
+ accepted_scores.append(score)
560
+ accepted_cls.append(int(cls_f[i]))
561
+
562
+ if not accepted_boxes:
563
+ return []
564
+
565
+ boxes = np.array(accepted_boxes, dtype=np.float32)
566
+ scores = np.array(accepted_scores, dtype=np.float32)
567
+ cls_ids = np.array(accepted_cls, dtype=np.int32)
568
+
569
+ keep = self._nms_per_class(boxes, scores, cls_ids, self.iou_thres, self.max_det)
570
+
571
+ out = []
572
+ for idx in keep:
573
+ x1, y1, x2, y2 = boxes[idx].tolist()
574
+ out.append(
575
+ BoundingBox(
576
+ x1=int(math.floor(x1)),
577
+ y1=int(math.floor(y1)),
578
+ x2=int(math.ceil(x2)),
579
+ y2=int(math.ceil(y2)),
580
+ cls_id=int(cls_ids[idx]),
581
+ conf=float(scores[idx]),
582
+ )
583
+ )
584
+ return out
585
+
586
+ def _predict_tta(self, image: np.ndarray) -> list[BoundingBox]:
587
+ boxes_orig = self._predict_single(image)
588
+
589
+ flipped = cv2.flip(image, 1)
590
+ boxes_flip_raw = self._predict_single(flipped)
591
+
592
+ w = image.shape[1]
593
+ boxes_flip = [
594
+ BoundingBox(
595
+ x1=w - b.x2,
596
+ y1=b.y1,
597
+ x2=w - b.x1,
598
+ y2=b.y2,
599
+ cls_id=b.cls_id,
600
+ conf=b.conf,
601
+ )
602
+ for b in boxes_flip_raw
603
+ ]
604
+
605
+ return self._merge_tta_consensus(boxes_orig, boxes_flip)
606
+
607
+ def predict_batch(
608
+ self,
609
+ batch_images: list[ndarray],
610
+ offset: int,
611
+ n_keypoints: int,
612
+ ) -> list[TVFrameResult]:
613
+ results: list[TVFrameResult] = []
614
+
615
+ for frame_number_in_batch, image in enumerate(batch_images):
616
+ try:
617
+ if self.use_tta:
618
+ boxes = self._predict_tta(image)
619
+ else:
620
+ boxes = self._predict_single(image)
621
+ except Exception as e:
622
+ print(f"⚠️ Inference failed for frame {offset + frame_number_in_batch}: {e}")
623
+ boxes = []
624
+
625
+ results.append(
626
+ TVFrameResult(
627
+ frame_id=offset + frame_number_in_batch,
628
+ boxes=boxes,
629
+ keypoints=[(0, 0) for _ in range(max(0, int(n_keypoints)))],
630
+ )
631
+ )
632
+
633
+ return results
weights.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d9cc7776c2b1faa46755065a4e8f782d0ec408fe2e18eeebac45eb17403bdb6e
3
+ size 19424645