coolroman commited on
Commit
a9aeb5e
·
verified ·
1 Parent(s): 70f2888

scorevision: push artifact

Browse files
Files changed (1) hide show
  1. miner.py +368 -144
miner.py CHANGED
@@ -1,17 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from pathlib import Path
2
  import math
3
- import os
4
- import glob
5
- import site
6
- import ctypes
7
-
8
- # Preload pip-installed NVIDIA cuDNN so onnxruntime can use CUDAExecutionProvider
9
- for sp in site.getsitepackages():
10
- for d in glob.glob(os.path.join(sp, 'nvidia', '*', 'lib')):
11
- os.environ['LD_LIBRARY_PATH'] = d + ':' + os.environ.get('LD_LIBRARY_PATH', '')
12
- _cudnn = os.path.join(d, 'libcudnn.so.9')
13
- if os.path.exists(_cudnn):
14
- ctypes.CDLL(_cudnn, mode=ctypes.RTLD_GLOBAL)
15
 
16
  import cv2
17
  import numpy as np
@@ -35,153 +37,372 @@ class TVFrameResult(BaseModel):
35
  keypoints: list[tuple[int, int]]
36
 
37
 
38
- class Miner:
39
- """
40
- Auto-generated by subnet_bridge from a Manako element repo.
41
- This miner is intentionally self-contained for chute import restrictions.
42
- """
43
 
 
44
  def __init__(self, path_hf_repo: Path) -> None:
45
- self.path_hf_repo = path_hf_repo
46
- self.class_names = ['numberplate']
47
- self.session = ort.InferenceSession(
48
- str(path_hf_repo / "weights.onnx"),
49
- providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
50
- )
51
- self.input_name = self.session.get_inputs()[0].name
52
- input_shape = self.session.get_inputs()[0].shape
53
- # expected [N, C, H, W]
54
- self.input_h = int(input_shape[2])
55
- self.input_w = int(input_shape[3])
56
- self.conf_threshold = 0.15
57
- self.iou_threshold = 0.3
58
- self.use_tta = True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
- def __repr__(self) -> str:
61
- return f"ONNX Miner session={type(self.session).__name__} classes={len(self.class_names)}"
62
-
63
- def _preprocess(self, image_bgr: ndarray) -> tuple[np.ndarray, tuple[int, int]]:
64
- h, w = image_bgr.shape[:2]
65
- rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
66
- resized = cv2.resize(rgb, (self.input_w, self.input_h))
67
- x = resized.astype(np.float32) / 255.0
68
- x = np.transpose(x, (2, 0, 1))[None, ...]
69
- return x, (h, w)
70
-
71
- def _normalize_predictions(self, raw: np.ndarray) -> np.ndarray:
72
- pred = raw[0]
73
- if pred.ndim != 2:
74
- raise ValueError(f"Unexpected prediction shape: {raw.shape}")
75
- if pred.shape[0] < pred.shape[1]:
76
- pred = pred.transpose(1, 0)
77
- return pred
78
-
79
- def _nms(self, dets: list[tuple[float, float, float, float, float, int]]) -> list[tuple[float, float, float, float, float, int]]:
80
- if not dets:
81
- return []
82
 
83
- boxes = np.array([[d[0], d[1], d[2], d[3]] for d in dets], dtype=np.float32)
84
- scores = np.array([d[4] for d in dets], dtype=np.float32)
85
- order = scores.argsort()[::-1]
86
- keep = []
87
 
88
- while order.size > 0:
89
- i = order[0]
90
- keep.append(i)
91
 
92
- xx1 = np.maximum(boxes[i, 0], boxes[order[1:], 0])
93
- yy1 = np.maximum(boxes[i, 1], boxes[order[1:], 1])
94
- xx2 = np.minimum(boxes[i, 2], boxes[order[1:], 2])
95
- yy2 = np.minimum(boxes[i, 3], boxes[order[1:], 3])
96
 
97
- w = np.maximum(0.0, xx2 - xx1)
98
- h = np.maximum(0.0, yy2 - yy1)
99
- inter = w * h
 
 
100
 
101
- area_i = (boxes[i, 2] - boxes[i, 0]) * (boxes[i, 3] - boxes[i, 1])
102
- area_rest = (boxes[order[1:], 2] - boxes[order[1:], 0]) * (boxes[order[1:], 3] - boxes[order[1:], 1])
103
- union = np.maximum(area_i + area_rest - inter, 1e-6)
104
- iou = inter / union
 
 
105
 
106
- remaining = np.where(iou <= self.iou_threshold)[0]
107
- order = order[remaining + 1]
108
 
109
- return [dets[idx] for idx in keep]
 
 
110
 
111
- def _decode(self, image_bgr: ndarray) -> list[tuple[float, float, float, float, float, int]]:
112
- """Run model and return raw detections before NMS."""
113
- inp, (orig_h, orig_w) = self._preprocess(image_bgr)
114
- out = self.session.run(None, {self.input_name: inp})[0]
115
- pred = self._normalize_predictions(out)
116
 
117
- if pred.shape[1] < 5:
118
- return []
 
119
 
120
- boxes = pred[:, :4]
121
- cls_scores = pred[:, 4:]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
 
123
- if cls_scores.shape[1] == 0:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  return []
125
-
126
- cls_ids = np.argmax(cls_scores, axis=1)
127
- confs = np.max(cls_scores, axis=1)
128
- keep = confs >= self.conf_threshold
129
-
130
- boxes = boxes[keep]
131
- confs = confs[keep]
132
- cls_ids = cls_ids[keep]
133
-
134
- if boxes.shape[0] == 0:
135
  return []
136
-
137
- sx = orig_w / float(self.input_w)
138
- sy = orig_h / float(self.input_h)
139
-
140
- dets: list[tuple[float, float, float, float, float, int]] = []
141
- for i in range(boxes.shape[0]):
142
- cx, cy, bw, bh = boxes[i].tolist()
143
- x1 = (cx - bw / 2.0) * sx
144
- y1 = (cy - bh / 2.0) * sy
145
- x2 = (cx + bw / 2.0) * sx
146
- y2 = (cy + bh / 2.0) * sy
147
- dets.append((x1, y1, x2, y2, float(confs[i]), int(cls_ids[i])))
148
-
149
- return dets
150
-
151
- def _infer_single(self, image_bgr: ndarray) -> list[BoundingBox]:
152
- orig_h, orig_w = image_bgr.shape[:2]
153
-
154
- # Original pass
155
- all_dets = self._decode(image_bgr)
156
-
157
- # TTA: horizontal flip pass
158
- if self.use_tta:
159
- flipped = cv2.flip(image_bgr, 1)
160
- flip_dets = self._decode(flipped)
161
- for x1, y1, x2, y2, conf, cls_id in flip_dets:
162
- all_dets.append((orig_w - x2, y1, orig_w - x1, y2, conf, cls_id))
163
-
164
- # NMS
165
- all_dets = self._nms(all_dets)
166
-
167
- out_boxes: list[BoundingBox] = []
168
- for x1, y1, x2, y2, conf, cls_id in all_dets:
169
- ix1 = max(0, min(orig_w, math.floor(x1)))
170
- iy1 = max(0, min(orig_h, math.floor(y1)))
171
- ix2 = max(0, min(orig_w, math.ceil(x2)))
172
- iy2 = max(0, min(orig_h, math.ceil(y2)))
173
- out_boxes.append(
174
  BoundingBox(
175
- x1=ix1,
176
- y1=iy1,
177
- x2=ix2,
178
- y2=iy2,
179
- cls_id=cls_id,
180
- conf=max(0.0, min(1.0, conf)),
181
  )
182
  )
183
- return out_boxes
184
 
 
185
  def predict_batch(
186
  self,
187
  batch_images: list[ndarray],
@@ -189,14 +410,17 @@ class Miner:
189
  n_keypoints: int,
190
  ) -> list[TVFrameResult]:
191
  results: list[TVFrameResult] = []
192
- for idx, image in enumerate(batch_images):
193
- boxes = self._infer_single(image)
194
- keypoints = [(0, 0) for _ in range(max(0, int(n_keypoints)))]
 
 
 
195
  results.append(
196
  TVFrameResult(
197
- frame_id=offset + idx,
198
  boxes=boxes,
199
- keypoints=keypoints,
200
  )
201
  )
202
  return results
 
1
+ """Plate-detection miner — v1 "sparse conditional tile-aug".
2
+
3
+ Base weights: alfred8995/arabic002 (YOLO26s @ 1280, fp16, ~19 MB).
4
+
5
+ Inference pipeline:
6
+ 1) Full-image primary pass with arabic002's strict tuning
7
+ (conf=0.27, iou=0.444, sigma=0.5, soft-NMS + hflip TTA, max_det=18).
8
+ 2) If the primary returned fewer than SPARSE_THRESHOLD (5) boxes,
9
+ run a 2x2 overlapping tile pass with higher conf (tile_conf=0.40)
10
+ and novelty-merge: keep a tile box only when it does not overlap
11
+ any primary box at IoU >= 0.10. Tile augmentation is skipped
12
+ entirely on challenges where the primary already has enough
13
+ detections, so the FP score stays intact on dense scenes.
14
+ """
15
  from pathlib import Path
16
  import math
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  import cv2
19
  import numpy as np
 
37
  keypoints: list[tuple[int, int]]
38
 
39
 
40
+ SIZE = 1280
41
+
 
 
 
42
 
43
+ class Miner:
44
  def __init__(self, path_hf_repo: Path) -> None:
45
+ model_path = path_hf_repo / "weights.onnx"
46
+ cn_path = model_path.with_name("class_names.txt")
47
+ if cn_path.is_file():
48
+ lines = cn_path.read_text(encoding="utf-8").splitlines()
49
+ self.class_names = [
50
+ ln.strip()
51
+ for ln in lines
52
+ if ln.strip() and not ln.strip().startswith("#")
53
+ ]
54
+ else:
55
+ self.class_names = ["numberplate"]
56
+ print("ORT version:", ort.__version__)
57
+
58
+ try:
59
+ ort.preload_dlls()
60
+ print("onnxruntime.preload_dlls() success")
61
+ except Exception as e:
62
+ print(f"preload_dlls failed: {e}")
63
+
64
+ print("ORT available providers BEFORE session:", ort.get_available_providers())
65
+
66
+ sess_options = ort.SessionOptions()
67
+ sess_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
68
+
69
+ try:
70
+ self.session = ort.InferenceSession(
71
+ str(model_path),
72
+ sess_options=sess_options,
73
+ providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
74
+ )
75
+ print("Created ORT session with preferred CUDA provider list")
76
+ except Exception as e:
77
+ print(f"CUDA session creation failed, falling back to CPU: {e}")
78
+ self.session = ort.InferenceSession(
79
+ str(model_path),
80
+ sess_options=sess_options,
81
+ providers=["CPUExecutionProvider"],
82
+ )
83
 
84
+ print("ORT session providers:", self.session.get_providers())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
+ for inp in self.session.get_inputs():
87
+ print("INPUT:", inp.name, inp.shape, inp.type)
88
+ for out in self.session.get_outputs():
89
+ print("OUTPUT:", out.name, out.shape, out.type)
90
 
91
+ self.input_name = self.session.get_inputs()[0].name
92
+ self.output_names = [o.name for o in self.session.get_outputs()]
93
+ self.input_shape = self.session.get_inputs()[0].shape
94
 
95
+ self.input_height = self._safe_dim(self.input_shape[2], default=SIZE)
96
+ self.input_width = self._safe_dim(self.input_shape[3], default=SIZE)
 
 
97
 
98
+ # Primary pass: arabic002 strict tuning
99
+ self.conf_thres = 0.27
100
+ self.iou_thres = 0.444
101
+ self.sigma = 0.5
102
+ self.max_det = 18
103
 
104
+ # Conditional tile-pass
105
+ self.sparse_threshold = 5 # fire tiles only if primary returns < this
106
+ self.tile_conf = 0.40
107
+ self.tile_overlap = 0.20
108
+ self.novelty_iou = 0.10
109
+ self.final_max_det = 22
110
 
111
+ self.use_tta = True
 
112
 
113
+ print(f"ONNX model loaded from: {model_path}")
114
+ print(f"ONNX providers: {self.session.get_providers()}")
115
+ print(f"ONNX input: name={self.input_name}, shape={self.input_shape}")
116
 
117
+ def __repr__(self) -> str:
118
+ return (
119
+ f"ONNXRuntime(session={type(self.session).__name__}, "
120
+ f"providers={self.session.get_providers()})"
121
+ )
122
 
123
+ @staticmethod
124
+ def _safe_dim(value, default: int) -> int:
125
+ return value if isinstance(value, int) and value > 0 else default
126
 
127
+ # ---------- image preprocessing ----------
128
+ def _letterbox(
129
+ self,
130
+ image: ndarray,
131
+ new_shape: tuple[int, int],
132
+ color=(114, 114, 114),
133
+ ) -> tuple[ndarray, float, tuple[float, float]]:
134
+ h, w = image.shape[:2]
135
+ new_w, new_h = new_shape
136
+ ratio = min(new_w / w, new_h / h)
137
+ resized_w = int(round(w * ratio))
138
+ resized_h = int(round(h * ratio))
139
+ if (resized_w, resized_h) != (w, h):
140
+ interp = cv2.INTER_CUBIC if ratio > 1.0 else cv2.INTER_LINEAR
141
+ image = cv2.resize(image, (resized_w, resized_h), interpolation=interp)
142
+ dw = (new_w - resized_w) / 2.0
143
+ dh = (new_h - resized_h) / 2.0
144
+ left = int(round(dw - 0.1))
145
+ right = int(round(dw + 0.1))
146
+ top = int(round(dh - 0.1))
147
+ bottom = int(round(dh + 0.1))
148
+ padded = cv2.copyMakeBorder(
149
+ image, top, bottom, left, right,
150
+ borderType=cv2.BORDER_CONSTANT, value=color,
151
+ )
152
+ return padded, ratio, (dw, dh)
153
+
154
+ def _preprocess(self, image: ndarray):
155
+ img, ratio, pad = self._letterbox(image, (self.input_width, self.input_height))
156
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
157
+ img = np.transpose(img, (2, 0, 1))[None, ...]
158
+ return np.ascontiguousarray(img, dtype=np.float32), ratio, pad
159
+
160
+ @staticmethod
161
+ def _clip_boxes(boxes: np.ndarray, image_size: tuple[int, int]) -> np.ndarray:
162
+ w, h = image_size
163
+ boxes[:, 0] = np.clip(boxes[:, 0], 0, w - 1)
164
+ boxes[:, 1] = np.clip(boxes[:, 1], 0, h - 1)
165
+ boxes[:, 2] = np.clip(boxes[:, 2], 0, w - 1)
166
+ boxes[:, 3] = np.clip(boxes[:, 3], 0, h - 1)
167
+ return boxes
168
+
169
+ # ---------- NMS primitives ----------
170
+ @staticmethod
171
+ def _hard_nms(boxes: np.ndarray, scores: np.ndarray, iou_thresh: float) -> np.ndarray:
172
+ N = len(boxes)
173
+ if N == 0:
174
+ return np.array([], dtype=np.intp)
175
+ boxes = np.asarray(boxes, dtype=np.float32)
176
+ scores = np.asarray(scores, dtype=np.float32)
177
+ order = np.argsort(-scores)
178
+ keep: list[int] = []
179
+ while len(order):
180
+ i = int(order[0])
181
+ keep.append(i)
182
+ if len(order) == 1:
183
+ break
184
+ rest = order[1:]
185
+ xx1 = np.maximum(boxes[i, 0], boxes[rest, 0])
186
+ yy1 = np.maximum(boxes[i, 1], boxes[rest, 1])
187
+ xx2 = np.minimum(boxes[i, 2], boxes[rest, 2])
188
+ yy2 = np.minimum(boxes[i, 3], boxes[rest, 3])
189
+ inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
190
+ area_i = (boxes[i, 2] - boxes[i, 0]) * (boxes[i, 3] - boxes[i, 1])
191
+ area_r = (boxes[rest, 2] - boxes[rest, 0]) * (boxes[rest, 3] - boxes[rest, 1])
192
+ iou = inter / (area_i + area_r - inter + 1e-7)
193
+ order = rest[iou <= iou_thresh]
194
+ return np.array(keep, dtype=np.intp)
195
 
196
+ def _soft_nms(
197
+ self,
198
+ boxes: np.ndarray,
199
+ scores: np.ndarray,
200
+ sigma: float,
201
+ score_thresh: float = 0.01,
202
+ ) -> tuple[np.ndarray, np.ndarray]:
203
+ N = len(boxes)
204
+ if N == 0:
205
+ return np.array([], dtype=np.intp), np.array([], dtype=np.float32)
206
+ boxes = boxes.astype(np.float32, copy=True)
207
+ scores = scores.astype(np.float32, copy=True)
208
+ order = np.arange(N)
209
+ for i in range(N):
210
+ max_pos = i + int(np.argmax(scores[i:]))
211
+ boxes[[i, max_pos]] = boxes[[max_pos, i]]
212
+ scores[[i, max_pos]] = scores[[max_pos, i]]
213
+ order[[i, max_pos]] = order[[max_pos, i]]
214
+ if i + 1 >= N:
215
+ break
216
+ xx1 = np.maximum(boxes[i, 0], boxes[i + 1:, 0])
217
+ yy1 = np.maximum(boxes[i, 1], boxes[i + 1:, 1])
218
+ xx2 = np.minimum(boxes[i, 2], boxes[i + 1:, 2])
219
+ yy2 = np.minimum(boxes[i, 3], boxes[i + 1:, 3])
220
+ inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
221
+ area_i = float(
222
+ (boxes[i, 2] - boxes[i, 0]) * (boxes[i, 3] - boxes[i, 1])
223
+ )
224
+ areas_j = (
225
+ np.maximum(0.0, boxes[i + 1:, 2] - boxes[i + 1:, 0])
226
+ * np.maximum(0.0, boxes[i + 1:, 3] - boxes[i + 1:, 1])
227
+ )
228
+ iou = inter / (area_i + areas_j - inter + 1e-7)
229
+ scores[i + 1:] *= np.exp(-(iou ** 2) / sigma)
230
+ mask = scores > score_thresh
231
+ return order[mask], scores[mask]
232
+
233
+ @staticmethod
234
+ def _box_iou_one_to_many(box: np.ndarray, boxes: np.ndarray) -> np.ndarray:
235
+ if len(boxes) == 0:
236
+ return np.zeros(0, dtype=np.float32)
237
+ xx1 = np.maximum(box[0], boxes[:, 0])
238
+ yy1 = np.maximum(box[1], boxes[:, 1])
239
+ xx2 = np.minimum(box[2], boxes[:, 2])
240
+ yy2 = np.minimum(box[3], boxes[:, 3])
241
+ inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
242
+ area_a = max(0.0, (box[2] - box[0]) * (box[3] - box[1]))
243
+ area_b = np.maximum(0.0, boxes[:, 2] - boxes[:, 0]) * np.maximum(0.0, boxes[:, 3] - boxes[:, 1])
244
+ return inter / (area_a + area_b - inter + 1e-7)
245
+
246
+ # ---------- raw-dets helper ----------
247
+ def _raw_dets(self, image: ndarray, conf: float) -> np.ndarray:
248
+ """Run a single forward pass and return [N, 5] dets in ORIGINAL image coords."""
249
+ x, ratio, (dw, dh) = self._preprocess(image)
250
+ out = self.session.run(self.output_names, {self.input_name: x})[0]
251
+ if out.ndim == 3:
252
+ out = out[0]
253
+ if out.shape[1] < 5:
254
+ return np.zeros((0, 5), dtype=np.float32)
255
+ boxes = out[:, :4].astype(np.float32)
256
+ scores = out[:, 4].astype(np.float32)
257
+ keep = scores >= conf
258
+ boxes, scores = boxes[keep], scores[keep]
259
+ if len(boxes) == 0:
260
+ return np.zeros((0, 5), dtype=np.float32)
261
+ boxes[:, [0, 2]] -= dw
262
+ boxes[:, [1, 3]] -= dh
263
+ boxes /= ratio
264
+ oh, ow = image.shape[:2]
265
+ boxes = self._clip_boxes(boxes, (ow, oh))
266
+ return np.concatenate([boxes, scores[:, None]], axis=1)
267
+
268
+ # ---------- primary pass: soft-NMS + hflip TTA ----------
269
+ def _primary(self, image: ndarray) -> np.ndarray:
270
+ d1 = self._raw_dets(image, self.conf_thres)
271
+ flipped = cv2.flip(image, 1)
272
+ d2 = self._raw_dets(flipped, self.conf_thres)
273
+ if len(d2):
274
+ w = image.shape[1]
275
+ x1 = w - d2[:, 2]
276
+ x2 = w - d2[:, 0]
277
+ d2 = np.stack([x1, d2[:, 1], x2, d2[:, 3], d2[:, 4]], axis=1)
278
+ all_d = np.concatenate([d1, d2], axis=0) if len(d2) else d1
279
+ if len(all_d) == 0:
280
+ return np.zeros((0, 5), dtype=np.float32)
281
+ # soft-NMS, then hard-NMS
282
+ keep_idx, scores = self._soft_nms(all_d[:, :4].copy(), all_d[:, 4].copy(), sigma=self.sigma)
283
+ if len(keep_idx) == 0:
284
+ return np.zeros((0, 5), dtype=np.float32)
285
+ merged = np.concatenate([all_d[keep_idx, :4], scores[:, None]], axis=1)
286
+ keep = self._hard_nms(merged[:, :4], merged[:, 4], self.iou_thres)
287
+ merged = merged[keep]
288
+ if len(merged) > self.max_det:
289
+ merged = merged[np.argsort(-merged[:, 4])[: self.max_det]]
290
+ return merged
291
+
292
+ # ---------- conditional tile pass ----------
293
+ def _tile_augment(self, image: ndarray, primary: np.ndarray) -> np.ndarray:
294
+ """Run 2x2 overlapping tiles + hflip, novelty-merge into primary."""
295
+ oh, ow = image.shape[:2]
296
+ tw, th = ow // 2, oh // 2
297
+ ox, oy = int(tw * self.tile_overlap), int(th * self.tile_overlap)
298
+ tiles = [
299
+ (0, 0, min(ow, tw + ox), min(oh, th + oy)),
300
+ (max(0, tw - ox), 0, ow, min(oh, th + oy)),
301
+ (0, max(0, th - oy), min(ow, tw + ox), oh),
302
+ (max(0, tw - ox), max(0, th - oy), ow, oh),
303
+ ]
304
+ collected: list[np.ndarray] = []
305
+ for x1, y1, x2, y2 in tiles:
306
+ crop = image[y1:y2, x1:x2]
307
+ if crop.size == 0:
308
+ continue
309
+ d = self._raw_dets(crop, self.tile_conf)
310
+ if len(d):
311
+ d[:, 0] += x1
312
+ d[:, 1] += y1
313
+ d[:, 2] += x1
314
+ d[:, 3] += y1
315
+ collected.append(d)
316
+
317
+ # hflip tile pass
318
+ flipped = cv2.flip(image, 1)
319
+ for x1, y1, x2, y2 in tiles:
320
+ fx1 = ow - x2
321
+ fx2 = ow - x1
322
+ if fx2 <= fx1:
323
+ continue
324
+ crop = flipped[y1:y2, fx1:fx2]
325
+ if crop.size == 0:
326
+ continue
327
+ d = self._raw_dets(crop, self.tile_conf)
328
+ if len(d):
329
+ d_un = d.copy()
330
+ d_un[:, 0] = (ow - (d[:, 2] + fx1))
331
+ d_un[:, 2] = (ow - (d[:, 0] + fx1))
332
+ d_un[:, 1] = d[:, 1] + y1
333
+ d_un[:, 3] = d[:, 3] + y1
334
+ collected.append(d_un)
335
+
336
+ if not collected:
337
+ return primary
338
+
339
+ tile_dets = np.concatenate(collected, axis=0)
340
+ keep = self._hard_nms(tile_dets[:, :4], tile_dets[:, 4], 0.5)
341
+ tile_dets = tile_dets[keep]
342
+
343
+ # Novelty: drop tile boxes that overlap any primary box at IoU >= novelty_iou
344
+ if len(primary) > 0 and len(tile_dets) > 0:
345
+ mask = np.ones(len(tile_dets), dtype=bool)
346
+ for i in range(len(tile_dets)):
347
+ ious = self._box_iou_one_to_many(tile_dets[i, :4], primary[:, :4])
348
+ if len(ious) and np.max(ious) >= self.novelty_iou:
349
+ mask[i] = False
350
+ tile_dets = tile_dets[mask]
351
+
352
+ if len(tile_dets) == 0:
353
+ return primary
354
+
355
+ # Sanity filter: min/max size, aspect ratio
356
+ w = tile_dets[:, 2] - tile_dets[:, 0]
357
+ h = tile_dets[:, 3] - tile_dets[:, 1]
358
+ area = w * h
359
+ ar = np.maximum(w / np.maximum(h, 1e-6), h / np.maximum(w, 1e-6))
360
+ img_area = float(ow * oh)
361
+ ok = (w >= 6) & (h >= 6) & (area >= 36) & (area <= 0.5 * img_area) & (ar <= 10.0)
362
+ tile_dets = tile_dets[ok]
363
+ if len(tile_dets) == 0:
364
+ return primary
365
+
366
+ merged = np.concatenate([primary, tile_dets], axis=0)
367
+ keep = self._hard_nms(merged[:, :4], merged[:, 4], self.iou_thres)
368
+ merged = merged[keep]
369
+ if len(merged) > self.final_max_det:
370
+ merged = merged[np.argsort(-merged[:, 4])[: self.final_max_det]]
371
+ return merged
372
+
373
+ # ---------- single-image predict ----------
374
+ def _predict_single(self, image: ndarray) -> list[BoundingBox]:
375
+ if image is None or not isinstance(image, np.ndarray) or image.ndim != 3:
376
  return []
377
+ if image.shape[0] <= 0 or image.shape[1] <= 0 or image.shape[2] != 3:
 
 
 
 
 
 
 
 
 
378
  return []
379
+ if image.dtype != np.uint8:
380
+ image = image.astype(np.uint8)
381
+
382
+ primary = self._primary(image)
383
+ if len(primary) < self.sparse_threshold:
384
+ dets = self._tile_augment(image, primary)
385
+ else:
386
+ dets = primary
387
+
388
+ results: list[BoundingBox] = []
389
+ for row in dets:
390
+ x1, y1, x2, y2, conf = row.tolist()
391
+ if x2 <= x1 or y2 <= y1:
392
+ continue
393
+ results.append(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
394
  BoundingBox(
395
+ x1=int(math.floor(x1)),
396
+ y1=int(math.floor(y1)),
397
+ x2=int(math.ceil(x2)),
398
+ y2=int(math.ceil(y2)),
399
+ cls_id=0,
400
+ conf=float(conf),
401
  )
402
  )
403
+ return results
404
 
405
+ # ---------- chute entrypoint ----------
406
  def predict_batch(
407
  self,
408
  batch_images: list[ndarray],
 
410
  n_keypoints: int,
411
  ) -> list[TVFrameResult]:
412
  results: list[TVFrameResult] = []
413
+ for frame_number_in_batch, image in enumerate(batch_images):
414
+ try:
415
+ boxes = self._predict_single(image)
416
+ except Exception as e:
417
+ print(f"Inference failed for frame {offset + frame_number_in_batch}: {e}")
418
+ boxes = []
419
  results.append(
420
  TVFrameResult(
421
+ frame_id=offset + frame_number_in_batch,
422
  boxes=boxes,
423
+ keypoints=[(0, 0) for _ in range(max(0, int(n_keypoints)))],
424
  )
425
  )
426
  return results