SuperBitDev commited on
Commit
8e2911a
·
verified ·
1 Parent(s): fde9b9f

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. chute_config.yml +0 -1
  2. miner.py +597 -641
chute_config.yml CHANGED
@@ -14,7 +14,6 @@ NodeSelector:
14
  - b200
15
  - h200
16
  - mi300x
17
- - pro_6000
18
 
19
  Chute:
20
  timeout_seconds: 900
 
14
  - b200
15
  - h200
16
  - mi300x
 
17
 
18
  Chute:
19
  timeout_seconds: 900
miner.py CHANGED
@@ -1,642 +1,598 @@
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
- self.class_names = ['person']
30
- print("ORT version:", ort.__version__)
31
-
32
- try:
33
- ort.preload_dlls()
34
- print("✅ onnxruntime.preload_dlls() success")
35
- except Exception as e:
36
- print(f"⚠️ preload_dlls failed: {e}")
37
-
38
- print("ORT available providers BEFORE session:", ort.get_available_providers())
39
-
40
- sess_options = ort.SessionOptions()
41
- sess_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
42
-
43
- try:
44
- self.session = ort.InferenceSession(
45
- str(model_path),
46
- sess_options=sess_options,
47
- providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
48
- )
49
- print("✅ Created ORT session with preferred CUDA provider list")
50
- except Exception as e:
51
- print(f"⚠️ CUDA session creation failed, falling back to CPU: {e}")
52
- self.session = ort.InferenceSession(
53
- str(model_path),
54
- sess_options=sess_options,
55
- providers=["CPUExecutionProvider"],
56
- )
57
-
58
- print("ORT session providers:", self.session.get_providers())
59
-
60
- for inp in self.session.get_inputs():
61
- print("INPUT:", inp.name, inp.shape, inp.type)
62
-
63
- for out in self.session.get_outputs():
64
- print("OUTPUT:", out.name, out.shape, out.type)
65
-
66
- self.input_name = self.session.get_inputs()[0].name
67
- self.output_names = [output.name for output in self.session.get_outputs()]
68
- self.input_shape = self.session.get_inputs()[0].shape
69
-
70
- # Your export is fixed-size 1280, but we still read actual ONNX input shape first.
71
- self.input_height = self._safe_dim(self.input_shape[2], default=960)
72
- self.input_width = self._safe_dim(self.input_shape[3], default=960)
73
-
74
- # Tuned for validator scoring: reduce FP (FALSE_POSITIVE pillar),
75
- # preserve recall (MAP50, RECALL), improve precision.
76
- self.conf_thres = 0.32 # Higher = fewer FP, slightly lower recall
77
- self.iou_thres = 0.55 # Lower = suppress duplicate detections (FP)
78
- self.max_det = 150 # Cap detections; sports ~20-30 persons
79
- self.use_tta = True
80
-
81
- # Box sanity: filter tiny/spurious detections (common FP source)
82
- self.min_box_area = 12 * 12 # ~144 px²
83
- self.min_side = 8
84
- self.max_aspect_ratio = 10.0
85
-
86
- print(f"✅ ONNX model loaded from: {model_path}")
87
- print(f"✅ ONNX providers: {self.session.get_providers()}")
88
- print(f"✅ ONNX input: name={self.input_name}, shape={self.input_shape}")
89
-
90
- def __repr__(self) -> str:
91
- return (
92
- f"ONNXRuntime(session={type(self.session).__name__}, "
93
- f"providers={self.session.get_providers()})"
94
- )
95
-
96
- @staticmethod
97
- def _safe_dim(value, default: int) -> int:
98
- return value if isinstance(value, int) and value > 0 else default
99
-
100
- def _letterbox(
101
- self,
102
- image: ndarray,
103
- new_shape: tuple[int, int],
104
- color=(114, 114, 114),
105
- ) -> tuple[ndarray, float, tuple[float, float]]:
106
- """
107
- Resize with unchanged aspect ratio and pad to target shape.
108
- Returns:
109
- padded_image,
110
- ratio,
111
- (pad_w, pad_h) # half-padding
112
- """
113
- h, w = image.shape[:2]
114
- new_w, new_h = new_shape
115
-
116
- ratio = min(new_w / w, new_h / h)
117
- resized_w = int(round(w * ratio))
118
- resized_h = int(round(h * ratio))
119
-
120
- if (resized_w, resized_h) != (w, h):
121
- interp = cv2.INTER_CUBIC if ratio > 1.0 else cv2.INTER_LINEAR
122
- image = cv2.resize(image, (resized_w, resized_h), interpolation=interp)
123
-
124
- dw = new_w - resized_w
125
- dh = new_h - resized_h
126
- dw /= 2.0
127
- dh /= 2.0
128
-
129
- left = int(round(dw - 0.1))
130
- right = int(round(dw + 0.1))
131
- top = int(round(dh - 0.1))
132
- bottom = int(round(dh + 0.1))
133
-
134
- padded = cv2.copyMakeBorder(
135
- image,
136
- top,
137
- bottom,
138
- left,
139
- right,
140
- borderType=cv2.BORDER_CONSTANT,
141
- value=color,
142
- )
143
- return padded, ratio, (dw, dh)
144
-
145
- def _preprocess(
146
- self, image: ndarray
147
- ) -> tuple[np.ndarray, float, tuple[float, float], tuple[int, int]]:
148
- """
149
- Preprocess for fixed-size ONNX export:
150
- - enhance image quality (CLAHE, denoise, sharpen)
151
- - letterbox to model input size
152
- - BGR -> RGB
153
- - normalize to [0,1]
154
- - HWC -> NCHW float32
155
- """
156
- orig_h, orig_w = image.shape[:2]
157
-
158
- img, ratio, pad = self._letterbox(
159
- image, (self.input_width, self.input_height)
160
- )
161
- img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
162
- img = img.astype(np.float32) / 255.0
163
- img = np.transpose(img, (2, 0, 1))[None, ...]
164
- img = np.ascontiguousarray(img, dtype=np.float32)
165
-
166
- return img, ratio, pad, (orig_w, orig_h)
167
-
168
- @staticmethod
169
- def _clip_boxes(boxes: np.ndarray, image_size: tuple[int, int]) -> np.ndarray:
170
- w, h = image_size
171
- boxes[:, 0] = np.clip(boxes[:, 0], 0, w - 1)
172
- boxes[:, 1] = np.clip(boxes[:, 1], 0, h - 1)
173
- boxes[:, 2] = np.clip(boxes[:, 2], 0, w - 1)
174
- boxes[:, 3] = np.clip(boxes[:, 3], 0, h - 1)
175
- return boxes
176
-
177
- @staticmethod
178
- def _xywh_to_xyxy(boxes: np.ndarray) -> np.ndarray:
179
- out = np.empty_like(boxes)
180
- out[:, 0] = boxes[:, 0] - boxes[:, 2] / 2.0
181
- out[:, 1] = boxes[:, 1] - boxes[:, 3] / 2.0
182
- out[:, 2] = boxes[:, 0] + boxes[:, 2] / 2.0
183
- out[:, 3] = boxes[:, 1] + boxes[:, 3] / 2.0
184
- return out
185
-
186
- def _soft_nms(
187
- self,
188
- boxes: np.ndarray,
189
- scores: np.ndarray,
190
- sigma: float = 0.5,
191
- score_thresh: float = 0.2,
192
- ) -> tuple[np.ndarray, np.ndarray]:
193
- """
194
- Soft-NMS: Gaussian decay of overlapping scores instead of hard removal.
195
- Returns (kept_original_indices, updated_scores).
196
- """
197
- N = len(boxes)
198
- if N == 0:
199
- return np.array([], dtype=np.intp), np.array([], dtype=np.float32)
200
-
201
- sigma = max(float(sigma), 1e-6)
202
-
203
- boxes = boxes.astype(np.float32, copy=True)
204
- scores = scores.astype(np.float32, copy=True)
205
- order = np.arange(N)
206
-
207
- for i in range(N):
208
- max_pos = i + int(np.argmax(scores[i:]))
209
- boxes[[i, max_pos]] = boxes[[max_pos, i]]
210
- scores[[i, max_pos]] = scores[[max_pos, i]]
211
- order[[i, max_pos]] = order[[max_pos, i]]
212
-
213
- if i + 1 >= N:
214
- break
215
-
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
-
222
- area_i = max(0.0, float(
223
- (boxes[i, 2] - boxes[i, 0]) * (boxes[i, 3] - boxes[i, 1])
224
- ))
225
- areas_j = (
226
- np.maximum(0.0, boxes[i + 1:, 2] - boxes[i + 1:, 0])
227
- * np.maximum(0.0, boxes[i + 1:, 3] - boxes[i + 1:, 1])
228
- )
229
- iou = inter / (area_i + areas_j - inter + 1e-7)
230
- scores[i + 1:] *= np.exp(-(iou ** 2) / sigma)
231
-
232
- mask = scores > score_thresh
233
- return order[mask], scores[mask]
234
-
235
- @staticmethod
236
- def _hard_nms(
237
- boxes: np.ndarray,
238
- scores: np.ndarray,
239
- iou_thresh: float,
240
- ) -> np.ndarray:
241
- """
242
- Standard NMS: keep one box per overlapping cluster (the one with highest score).
243
- Returns indices of kept boxes (into the boxes/scores arrays).
244
- """
245
- N = len(boxes)
246
- if N == 0:
247
- return np.array([], dtype=np.intp)
248
- boxes = np.asarray(boxes, dtype=np.float32)
249
- scores = np.asarray(scores, dtype=np.float32)
250
- order = np.argsort(scores)[::-1]
251
- keep: list[int] = []
252
- suppressed = np.zeros(N, dtype=bool)
253
- for i in range(N):
254
- idx = order[i]
255
- if suppressed[idx]:
256
- continue
257
- keep.append(idx)
258
- bi = boxes[idx]
259
- for k in range(i + 1, N):
260
- jdx = order[k]
261
- if suppressed[jdx]:
262
- continue
263
- bj = boxes[jdx]
264
- xx1 = max(bi[0], bj[0])
265
- yy1 = max(bi[1], bj[1])
266
- xx2 = min(bi[2], bj[2])
267
- yy2 = min(bi[3], bj[3])
268
- inter = max(0.0, xx2 - xx1) * max(0.0, yy2 - yy1)
269
- area_i = (bi[2] - bi[0]) * (bi[3] - bi[1])
270
- area_j = (bj[2] - bj[0]) * (bj[3] - bj[1])
271
- iou = inter / (area_i + area_j - inter + 1e-7)
272
- if iou > iou_thresh:
273
- suppressed[jdx] = True
274
- return np.array(keep)
275
-
276
- def _filter_sane_boxes(
277
- self,
278
- boxes: np.ndarray,
279
- scores: np.ndarray,
280
- cls_ids: np.ndarray,
281
- orig_size: tuple[int, int],
282
- ) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
283
- """Filter out tiny, degenerate, or implausible boxes (common FP)."""
284
- if len(boxes) == 0:
285
- return boxes, scores, cls_ids
286
- orig_w, orig_h = orig_size
287
- image_area = float(orig_w * orig_h)
288
- keep = []
289
- for i, box in enumerate(boxes):
290
- x1, y1, x2, y2 = box.tolist()
291
- bw = x2 - x1
292
- bh = y2 - y1
293
- if bw <= 0 or bh <= 0:
294
- continue
295
- if bw < self.min_side or bh < self.min_side:
296
- continue
297
- area = bw * bh
298
- if area < self.min_box_area:
299
- continue
300
- if area > 0.95 * image_area:
301
- continue
302
- ar = max(bw / max(bh, 1e-6), bh / max(bw, 1e-6))
303
- if ar > self.max_aspect_ratio:
304
- continue
305
- keep.append(i)
306
- if not keep:
307
- return (
308
- np.empty((0, 4), dtype=np.float32),
309
- np.empty((0,), dtype=np.float32),
310
- np.empty((0,), dtype=np.int32),
311
- )
312
- k = np.array(keep, dtype=np.intp)
313
- return boxes[k], scores[k], cls_ids[k]
314
-
315
- @staticmethod
316
- def _max_score_per_cluster(
317
- coords: np.ndarray,
318
- scores: np.ndarray,
319
- keep_indices: np.ndarray,
320
- iou_thresh: float,
321
- ) -> np.ndarray:
322
- """
323
- For each kept box, return the max original score among itself and any
324
- box that overlaps it with IOU >= iou_thresh (so TTA cluster keeps best conf).
325
- """
326
- n_keep = len(keep_indices)
327
- if n_keep == 0:
328
- return np.array([], dtype=np.float32)
329
- out = np.empty(n_keep, dtype=np.float32)
330
- coords = np.asarray(coords, dtype=np.float32)
331
- scores = np.asarray(scores, dtype=np.float32)
332
- for i in range(n_keep):
333
- idx = keep_indices[i]
334
- bi = coords[idx]
335
- xx1 = np.maximum(bi[0], coords[:, 0])
336
- yy1 = np.maximum(bi[1], coords[:, 1])
337
- xx2 = np.minimum(bi[2], coords[:, 2])
338
- yy2 = np.minimum(bi[3], coords[:, 3])
339
- inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
340
- area_i = (bi[2] - bi[0]) * (bi[3] - bi[1])
341
- areas_j = (coords[:, 2] - coords[:, 0]) * (coords[:, 3] - coords[:, 1])
342
- iou = inter / (area_i + areas_j - inter + 1e-7)
343
- in_cluster = iou >= iou_thresh
344
- out[i] = float(np.max(scores[in_cluster]))
345
- return out
346
-
347
- def _decode_final_dets(
348
- self,
349
- preds: np.ndarray,
350
- ratio: float,
351
- pad: tuple[float, float],
352
- orig_size: tuple[int, int],
353
- apply_optional_dedup: bool = False,
354
- ) -> list[BoundingBox]:
355
- """
356
- Primary path:
357
- expected output rows like [x1, y1, x2, y2, conf, cls_id]
358
- in letterboxed input coordinates.
359
- """
360
- if preds.ndim == 3 and preds.shape[0] == 1:
361
- preds = preds[0]
362
-
363
- if preds.ndim != 2 or preds.shape[1] < 6:
364
- raise ValueError(f"Unexpected ONNX final-det output shape: {preds.shape}")
365
-
366
- boxes = preds[:, :4].astype(np.float32)
367
- scores = preds[:, 4].astype(np.float32)
368
- cls_ids = preds[:, 5].astype(np.int32)
369
-
370
- keep = scores >= self.conf_thres
371
- boxes = boxes[keep]
372
- scores = scores[keep]
373
- cls_ids = cls_ids[keep]
374
-
375
- if len(boxes) == 0:
376
- return []
377
-
378
- pad_w, pad_h = pad
379
- orig_w, orig_h = orig_size
380
-
381
- # reverse letterbox
382
- boxes[:, [0, 2]] -= pad_w
383
- boxes[:, [1, 3]] -= pad_h
384
- boxes /= ratio
385
- boxes = self._clip_boxes(boxes, (orig_w, orig_h))
386
-
387
- # Box sanity filter (reduces FP)
388
- boxes, scores, cls_ids = self._filter_sane_boxes(
389
- boxes, scores, cls_ids, orig_size
390
- )
391
- if len(boxes) == 0:
392
- return []
393
-
394
- # NMS to remove duplicates (model may output overlapping boxes)
395
- if len(boxes) > 1:
396
- if apply_optional_dedup:
397
- keep_idx, scores = self._soft_nms(boxes, scores)
398
- boxes = boxes[keep_idx]
399
- cls_ids = cls_ids[keep_idx]
400
- else:
401
- keep_idx = self._hard_nms(boxes, scores, self.iou_thres)
402
- keep_idx = keep_idx[: self.max_det]
403
- boxes = boxes[keep_idx]
404
- scores = scores[keep_idx]
405
- cls_ids = cls_ids[keep_idx]
406
-
407
- results: list[BoundingBox] = []
408
- for box, conf, cls_id in zip(boxes, scores, cls_ids):
409
- x1, y1, x2, y2 = box.tolist()
410
-
411
- if x2 <= x1 or y2 <= y1:
412
- continue
413
-
414
- results.append(
415
- BoundingBox(
416
- x1=int(math.floor(x1)),
417
- y1=int(math.floor(y1)),
418
- x2=int(math.ceil(x2)),
419
- y2=int(math.ceil(y2)),
420
- cls_id=int(cls_id),
421
- conf=float(conf),
422
- )
423
- )
424
-
425
- return results
426
-
427
- def _decode_raw_yolo(
428
- self,
429
- preds: np.ndarray,
430
- ratio: float,
431
- pad: tuple[float, float],
432
- orig_size: tuple[int, int],
433
- ) -> list[BoundingBox]:
434
- """
435
- Fallback path for raw YOLO predictions.
436
- Supports common layouts:
437
- - [1, C, N]
438
- - [1, N, C]
439
- """
440
- if preds.ndim != 3:
441
- raise ValueError(f"Unexpected raw ONNX output shape: {preds.shape}")
442
-
443
- if preds.shape[0] != 1:
444
- raise ValueError(f"Unexpected batch dimension in raw output: {preds.shape}")
445
-
446
- preds = preds[0]
447
-
448
- # Normalize to [N, C]
449
- if preds.shape[0] <= 16 and preds.shape[1] > preds.shape[0]:
450
- preds = preds.T
451
-
452
- if preds.ndim != 2 or preds.shape[1] < 5:
453
- raise ValueError(f"Unexpected normalized raw output shape: {preds.shape}")
454
-
455
- boxes_xywh = preds[:, :4].astype(np.float32)
456
- cls_part = preds[:, 4:].astype(np.float32)
457
-
458
- if cls_part.shape[1] == 1:
459
- scores = cls_part[:, 0]
460
- cls_ids = np.zeros(len(scores), dtype=np.int32)
461
- else:
462
- cls_ids = np.argmax(cls_part, axis=1).astype(np.int32)
463
- scores = cls_part[np.arange(len(cls_part)), cls_ids]
464
-
465
- keep = scores >= self.conf_thres
466
- boxes_xywh = boxes_xywh[keep]
467
- scores = scores[keep]
468
- cls_ids = cls_ids[keep]
469
-
470
- if len(boxes_xywh) == 0:
471
- return []
472
-
473
- boxes = self._xywh_to_xyxy(boxes_xywh)
474
-
475
- keep_idx = self._hard_nms(boxes, scores, self.iou_thres)
476
- keep_idx = keep_idx[: self.max_det]
477
- boxes = boxes[keep_idx]
478
- scores = scores[keep_idx]
479
- cls_ids = cls_ids[keep_idx]
480
-
481
- pad_w, pad_h = pad
482
- orig_w, orig_h = orig_size
483
-
484
- boxes[:, [0, 2]] -= pad_w
485
- boxes[:, [1, 3]] -= pad_h
486
- boxes /= ratio
487
- boxes = self._clip_boxes(boxes, (orig_w, orig_h))
488
-
489
- boxes, scores, cls_ids = self._filter_sane_boxes(
490
- boxes, scores, cls_ids, (orig_w, orig_h)
491
- )
492
- if len(boxes) == 0:
493
- return []
494
-
495
- results: list[BoundingBox] = []
496
- for box, conf, cls_id in zip(boxes, scores, cls_ids):
497
- x1, y1, x2, y2 = box.tolist()
498
-
499
- if x2 <= x1 or y2 <= y1:
500
- continue
501
-
502
- results.append(
503
- BoundingBox(
504
- x1=int(math.floor(x1)),
505
- y1=int(math.floor(y1)),
506
- x2=int(math.ceil(x2)),
507
- y2=int(math.ceil(y2)),
508
- cls_id=int(cls_id),
509
- conf=float(conf),
510
- )
511
- )
512
-
513
- return results
514
-
515
- def _postprocess(
516
- self,
517
- output: np.ndarray,
518
- ratio: float,
519
- pad: tuple[float, float],
520
- orig_size: tuple[int, int],
521
- ) -> list[BoundingBox]:
522
- """
523
- Prefer final detections first.
524
- Fallback to raw decode only if needed.
525
- """
526
- # final detections: [N,6]
527
- if output.ndim == 2 and output.shape[1] >= 6:
528
- return self._decode_final_dets(output, ratio, pad, orig_size)
529
-
530
- # final detections: [1,N,6]
531
- if output.ndim == 3 and output.shape[0] == 1 and output.shape[2] == 6:
532
- return self._decode_final_dets(output, ratio, pad, orig_size)
533
-
534
- # fallback raw decode
535
- return self._decode_raw_yolo(output, ratio, pad, orig_size)
536
-
537
- def _predict_single(self, image: np.ndarray) -> list[BoundingBox]:
538
- if image is None:
539
- raise ValueError("Input image is None")
540
- if not isinstance(image, np.ndarray):
541
- raise TypeError(f"Input is not numpy array: {type(image)}")
542
- if image.ndim != 3:
543
- raise ValueError(f"Expected HWC image, got shape={image.shape}")
544
- if image.shape[0] <= 0 or image.shape[1] <= 0:
545
- raise ValueError(f"Invalid image shape={image.shape}")
546
- if image.shape[2] != 3:
547
- raise ValueError(f"Expected 3 channels, got shape={image.shape}")
548
-
549
- if image.dtype != np.uint8:
550
- image = image.astype(np.uint8)
551
-
552
- input_tensor, ratio, pad, orig_size = self._preprocess(image)
553
-
554
- expected_shape = (1, 3, self.input_height, self.input_width)
555
- if input_tensor.shape != expected_shape:
556
- raise ValueError(
557
- f"Bad input tensor shape={input_tensor.shape}, expected={expected_shape}"
558
- )
559
-
560
- outputs = self.session.run(self.output_names, {self.input_name: input_tensor})
561
- det_output = outputs[0]
562
- return self._postprocess(det_output, ratio, pad, orig_size)
563
-
564
- def _predict_tta(self, image: np.ndarray) -> list[BoundingBox]:
565
- """
566
- Horizontal-flip TTA: merge original + flipped via hard NMS.
567
- Boost confidence for consensus detections (both views agree) to improve
568
- mAP: validator sorts by confidence, so higher conf for TP helps PR curve.
569
- """
570
- boxes_orig = self._predict_single(image)
571
-
572
- flipped = cv2.flip(image, 1)
573
- boxes_flip = self._predict_single(flipped)
574
-
575
- w = image.shape[1]
576
- boxes_flip = [
577
- BoundingBox(
578
- x1=w - b.x2, y1=b.y1, x2=w - b.x1, y2=b.y2,
579
- cls_id=b.cls_id, conf=b.conf,
580
- )
581
- for b in boxes_flip
582
- ]
583
-
584
- all_boxes = boxes_orig + boxes_flip
585
- if len(all_boxes) == 0:
586
- return []
587
-
588
- coords = np.array(
589
- [[b.x1, b.y1, b.x2, b.y2] for b in all_boxes], dtype=np.float32
590
- )
591
- scores = np.array([b.conf for b in all_boxes], dtype=np.float32)
592
-
593
- hard_keep = self._hard_nms(coords, scores, self.iou_thres)
594
- if len(hard_keep) == 0:
595
- return []
596
-
597
- hard_keep = hard_keep[: self.max_det]
598
-
599
- # Boost confidence when both views agree (overlapping detections)
600
- boosted = self._max_score_per_cluster(
601
- coords, scores, hard_keep, self.iou_thres
602
- )
603
-
604
- return [
605
- BoundingBox(
606
- x1=all_boxes[i].x1,
607
- y1=all_boxes[i].y1,
608
- x2=all_boxes[i].x2,
609
- y2=all_boxes[i].y2,
610
- cls_id=all_boxes[i].cls_id,
611
- conf=float(boosted[j]),
612
- )
613
- for j, i in enumerate(hard_keep)
614
- ]
615
-
616
- def predict_batch(
617
- self,
618
- batch_images: list[ndarray],
619
- offset: int,
620
- n_keypoints: int,
621
- ) -> list[TVFrameResult]:
622
- results: list[TVFrameResult] = []
623
-
624
- for frame_number_in_batch, image in enumerate(batch_images):
625
- try:
626
- if self.use_tta:
627
- boxes = self._predict_tta(image)
628
- else:
629
- boxes = self._predict_single(image)
630
- except Exception as e:
631
- print(f"⚠️ Inference failed for frame {offset + frame_number_in_batch}: {e}")
632
- boxes = []
633
-
634
- results.append(
635
- TVFrameResult(
636
- frame_id=offset + frame_number_in_batch,
637
- boxes=boxes,
638
- keypoints=[(0, 0) for _ in range(max(0, int(n_keypoints)))],
639
- )
640
- )
641
-
642
  return results
 
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
+ self.class_names = ["person"]
30
+ print("ORT version:", ort.__version__)
31
+
32
+ try:
33
+ ort.preload_dlls()
34
+ print("✅ onnxruntime.preload_dlls() success")
35
+ except Exception as e:
36
+ print(f"⚠️ preload_dlls failed: {e}")
37
+
38
+ print("ORT available providers BEFORE session:", ort.get_available_providers())
39
+
40
+ sess_options = ort.SessionOptions()
41
+ sess_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
42
+
43
+ try:
44
+ self.session = ort.InferenceSession(
45
+ str(model_path),
46
+ sess_options=sess_options,
47
+ providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
48
+ )
49
+ print("✅ Created ORT session with preferred CUDA provider list")
50
+ except Exception as e:
51
+ print(f"⚠️ CUDA session creation failed, falling back to CPU: {e}")
52
+ self.session = ort.InferenceSession(
53
+ str(model_path),
54
+ sess_options=sess_options,
55
+ providers=["CPUExecutionProvider"],
56
+ )
57
+
58
+ print("ORT session providers:", self.session.get_providers())
59
+
60
+ for inp in self.session.get_inputs():
61
+ print("INPUT:", inp.name, inp.shape, inp.type)
62
+
63
+ for out in self.session.get_outputs():
64
+ print("OUTPUT:", out.name, out.shape, out.type)
65
+
66
+ self.input_name = self.session.get_inputs()[0].name
67
+ self.output_names = [output.name for output in self.session.get_outputs()]
68
+ self.input_shape = self.session.get_inputs()[0].shape
69
+
70
+ self.input_height = self._safe_dim(self.input_shape[2], default=960)
71
+ self.input_width = self._safe_dim(self.input_shape[3], default=960)
72
+
73
+ # ---------- Scoring-oriented thresholds ----------
74
+ # Low threshold for candidate generation
75
+ self.conf_thres = 0.24
76
+
77
+ # High-confidence boxes can survive without TTA confirmation
78
+ self.conf_high = 0.56
79
+
80
+ # NMS threshold
81
+ self.iou_thres = 0.50
82
+
83
+ # TTA confirmation IoU
84
+ self.tta_match_iou = 0.62
85
+
86
+ self.max_det = 150
87
+ self.use_tta = True
88
+
89
+ # Box sanity filters
90
+ self.min_box_area = 14 * 14
91
+ self.min_w = 8
92
+ self.min_h = 8
93
+ self.max_aspect_ratio = 8.0
94
+ self.max_box_area_ratio = 0.8
95
+
96
+ print(f"✅ ONNX model loaded from: {model_path}")
97
+ print(f"✅ ONNX providers: {self.session.get_providers()}")
98
+ print(f"✅ ONNX input: name={self.input_name}, shape={self.input_shape}")
99
+
100
+ def __repr__(self) -> str:
101
+ return (
102
+ f"ONNXRuntime(session={type(self.session).__name__}, "
103
+ f"providers={self.session.get_providers()})"
104
+ )
105
+
106
+ @staticmethod
107
+ def _safe_dim(value, default: int) -> int:
108
+ return value if isinstance(value, int) and value > 0 else default
109
+
110
+ def _letterbox(
111
+ self,
112
+ image: ndarray,
113
+ new_shape: tuple[int, int],
114
+ color=(114, 114, 114),
115
+ ) -> tuple[ndarray, float, tuple[float, float]]:
116
+ h, w = image.shape[:2]
117
+ new_w, new_h = new_shape
118
+
119
+ ratio = min(new_w / w, new_h / h)
120
+ resized_w = int(round(w * ratio))
121
+ resized_h = int(round(h * ratio))
122
+
123
+ if (resized_w, resized_h) != (w, h):
124
+ interp = cv2.INTER_CUBIC if ratio > 1.0 else cv2.INTER_LINEAR
125
+ image = cv2.resize(image, (resized_w, resized_h), interpolation=interp)
126
+
127
+ dw = new_w - resized_w
128
+ dh = new_h - resized_h
129
+ dw /= 2.0
130
+ dh /= 2.0
131
+
132
+ left = int(round(dw - 0.1))
133
+ right = int(round(dw + 0.1))
134
+ top = int(round(dh - 0.1))
135
+ bottom = int(round(dh + 0.1))
136
+
137
+ padded = cv2.copyMakeBorder(
138
+ image,
139
+ top,
140
+ bottom,
141
+ left,
142
+ right,
143
+ borderType=cv2.BORDER_CONSTANT,
144
+ value=color,
145
+ )
146
+ return padded, ratio, (dw, dh)
147
+
148
+ def _preprocess(
149
+ self, image: ndarray
150
+ ) -> tuple[np.ndarray, float, tuple[float, float], tuple[int, int]]:
151
+ orig_h, orig_w = image.shape[:2]
152
+
153
+ img, ratio, pad = self._letterbox(
154
+ image, (self.input_width, self.input_height)
155
+ )
156
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
157
+ img = img.astype(np.float32) / 255.0
158
+ img = np.transpose(img, (2, 0, 1))[None, ...]
159
+ img = np.ascontiguousarray(img, dtype=np.float32)
160
+
161
+ return img, ratio, pad, (orig_w, orig_h)
162
+
163
+ @staticmethod
164
+ def _clip_boxes(boxes: np.ndarray, image_size: tuple[int, int]) -> np.ndarray:
165
+ w, h = image_size
166
+ boxes[:, 0] = np.clip(boxes[:, 0], 0, w - 1)
167
+ boxes[:, 1] = np.clip(boxes[:, 1], 0, h - 1)
168
+ boxes[:, 2] = np.clip(boxes[:, 2], 0, w - 1)
169
+ boxes[:, 3] = np.clip(boxes[:, 3], 0, h - 1)
170
+ return boxes
171
+
172
+ @staticmethod
173
+ def _xywh_to_xyxy(boxes: np.ndarray) -> np.ndarray:
174
+ out = np.empty_like(boxes)
175
+ out[:, 0] = boxes[:, 0] - boxes[:, 2] / 2.0
176
+ out[:, 1] = boxes[:, 1] - boxes[:, 3] / 2.0
177
+ out[:, 2] = boxes[:, 0] + boxes[:, 2] / 2.0
178
+ out[:, 3] = boxes[:, 1] + boxes[:, 3] / 2.0
179
+ return out
180
+
181
+ @staticmethod
182
+ def _hard_nms(
183
+ boxes: np.ndarray,
184
+ scores: np.ndarray,
185
+ iou_thresh: float,
186
+ ) -> np.ndarray:
187
+ if len(boxes) == 0:
188
+ return np.array([], dtype=np.intp)
189
+
190
+ boxes = np.asarray(boxes, dtype=np.float32)
191
+ scores = np.asarray(scores, dtype=np.float32)
192
+ order = np.argsort(scores)[::-1]
193
+ keep = []
194
+
195
+ while len(order) > 0:
196
+ i = order[0]
197
+ keep.append(i)
198
+ if len(order) == 1:
199
+ break
200
+
201
+ rest = order[1:]
202
+
203
+ xx1 = np.maximum(boxes[i, 0], boxes[rest, 0])
204
+ yy1 = np.maximum(boxes[i, 1], boxes[rest, 1])
205
+ xx2 = np.minimum(boxes[i, 2], boxes[rest, 2])
206
+ yy2 = np.minimum(boxes[i, 3], boxes[rest, 3])
207
+
208
+ inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
209
+
210
+ area_i = np.maximum(0.0, (boxes[i, 2] - boxes[i, 0])) * np.maximum(0.0, (boxes[i, 3] - boxes[i, 1]))
211
+ area_r = np.maximum(0.0, (boxes[rest, 2] - boxes[rest, 0])) * np.maximum(0.0, (boxes[rest, 3] - boxes[rest, 1]))
212
+
213
+ iou = inter / (area_i + area_r - inter + 1e-7)
214
+ order = rest[iou <= iou_thresh]
215
+
216
+ return np.array(keep, dtype=np.intp)
217
+
218
+ @staticmethod
219
+ def _box_iou_one_to_many(box: np.ndarray, boxes: np.ndarray) -> np.ndarray:
220
+ xx1 = np.maximum(box[0], boxes[:, 0])
221
+ yy1 = np.maximum(box[1], boxes[:, 1])
222
+ xx2 = np.minimum(box[2], boxes[:, 2])
223
+ yy2 = np.minimum(box[3], boxes[:, 3])
224
+
225
+ inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
226
+
227
+ area_a = max(0.0, (box[2] - box[0]) * (box[3] - box[1]))
228
+ area_b = np.maximum(0.0, boxes[:, 2] - boxes[:, 0]) * np.maximum(0.0, boxes[:, 3] - boxes[:, 1])
229
+
230
+ return inter / (area_a + area_b - inter + 1e-7)
231
+
232
+ def _filter_sane_boxes(
233
+ self,
234
+ boxes: np.ndarray,
235
+ scores: np.ndarray,
236
+ cls_ids: np.ndarray,
237
+ orig_size: tuple[int, int],
238
+ ) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
239
+ if len(boxes) == 0:
240
+ return boxes, scores, cls_ids
241
+
242
+ orig_w, orig_h = orig_size
243
+ image_area = float(orig_w * orig_h)
244
+
245
+ keep = []
246
+ for i, box in enumerate(boxes):
247
+ x1, y1, x2, y2 = box.tolist()
248
+ bw = x2 - x1
249
+ bh = y2 - y1
250
+
251
+ if bw <= 0 or bh <= 0:
252
+ continue
253
+ if bw < self.min_w or bh < self.min_h:
254
+ continue
255
+
256
+ area = bw * bh
257
+ if area < self.min_box_area:
258
+ continue
259
+ if area > self.max_box_area_ratio * image_area:
260
+ continue
261
+
262
+ ar = max(bw / max(bh, 1e-6), bh / max(bw, 1e-6))
263
+ if ar > self.max_aspect_ratio:
264
+ continue
265
+
266
+ keep.append(i)
267
+
268
+ if not keep:
269
+ return (
270
+ np.empty((0, 4), dtype=np.float32),
271
+ np.empty((0,), dtype=np.float32),
272
+ np.empty((0,), dtype=np.int32),
273
+ )
274
+
275
+ keep = np.array(keep, dtype=np.intp)
276
+ return boxes[keep], scores[keep], cls_ids[keep]
277
+
278
+ def _decode_final_dets(
279
+ self,
280
+ preds: np.ndarray,
281
+ ratio: float,
282
+ pad: tuple[float, float],
283
+ orig_size: tuple[int, int],
284
+ ) -> list[BoundingBox]:
285
+ if preds.ndim == 3 and preds.shape[0] == 1:
286
+ preds = preds[0]
287
+
288
+ if preds.ndim != 2 or preds.shape[1] < 6:
289
+ raise ValueError(f"Unexpected ONNX final-det output shape: {preds.shape}")
290
+
291
+ boxes = preds[:, :4].astype(np.float32)
292
+ scores = preds[:, 4].astype(np.float32)
293
+ cls_ids = preds[:, 5].astype(np.int32)
294
+
295
+ # person only
296
+ keep = cls_ids == 0
297
+ boxes = boxes[keep]
298
+ scores = scores[keep]
299
+ cls_ids = cls_ids[keep]
300
+
301
+ # candidate threshold
302
+ keep = scores >= self.conf_thres
303
+ boxes = boxes[keep]
304
+ scores = scores[keep]
305
+ cls_ids = cls_ids[keep]
306
+
307
+ if len(boxes) == 0:
308
+ return []
309
+
310
+ pad_w, pad_h = pad
311
+ orig_w, orig_h = orig_size
312
+
313
+ boxes[:, [0, 2]] -= pad_w
314
+ boxes[:, [1, 3]] -= pad_h
315
+ boxes /= ratio
316
+ boxes = self._clip_boxes(boxes, (orig_w, orig_h))
317
+
318
+ boxes, scores, cls_ids = self._filter_sane_boxes(boxes, scores, cls_ids, orig_size)
319
+ if len(boxes) == 0:
320
+ return []
321
+
322
+ keep_idx = self._hard_nms(boxes, scores, self.iou_thres)
323
+ keep_idx = keep_idx[: self.max_det]
324
+
325
+ boxes = boxes[keep_idx]
326
+ scores = scores[keep_idx]
327
+ cls_ids = cls_ids[keep_idx]
328
+
329
+ return [
330
+ BoundingBox(
331
+ x1=int(math.floor(box[0])),
332
+ y1=int(math.floor(box[1])),
333
+ x2=int(math.ceil(box[2])),
334
+ y2=int(math.ceil(box[3])),
335
+ cls_id=int(cls_id),
336
+ conf=float(conf),
337
+ )
338
+ for box, conf, cls_id in zip(boxes, scores, cls_ids)
339
+ if box[2] > box[0] and box[3] > box[1]
340
+ ]
341
+
342
+ def _decode_raw_yolo(
343
+ self,
344
+ preds: np.ndarray,
345
+ ratio: float,
346
+ pad: tuple[float, float],
347
+ orig_size: tuple[int, int],
348
+ ) -> list[BoundingBox]:
349
+ if preds.ndim != 3:
350
+ raise ValueError(f"Unexpected raw ONNX output shape: {preds.shape}")
351
+ if preds.shape[0] != 1:
352
+ raise ValueError(f"Unexpected batch dimension in raw output: {preds.shape}")
353
+
354
+ preds = preds[0]
355
+
356
+ # Normalize to [N, C]
357
+ if preds.shape[0] <= 16 and preds.shape[1] > preds.shape[0]:
358
+ preds = preds.T
359
+
360
+ if preds.ndim != 2 or preds.shape[1] < 5:
361
+ raise ValueError(f"Unexpected normalized raw output shape: {preds.shape}")
362
+
363
+ boxes_xywh = preds[:, :4].astype(np.float32)
364
+ tail = preds[:, 4:].astype(np.float32)
365
+
366
+ # Supports:
367
+ # [x,y,w,h,score] single-class
368
+ # [x,y,w,h,obj,cls] YOLO standard single-class
369
+ # [x,y,w,h,obj,cls1,cls2,...] multi-class
370
+ if tail.shape[1] == 1:
371
+ scores = tail[:, 0]
372
+ cls_ids = np.zeros(len(scores), dtype=np.int32)
373
+ elif tail.shape[1] == 2:
374
+ obj = tail[:, 0]
375
+ cls_prob = tail[:, 1]
376
+ scores = obj * cls_prob
377
+ cls_ids = np.zeros(len(scores), dtype=np.int32)
378
+ else:
379
+ obj = tail[:, 0]
380
+ class_probs = tail[:, 1:]
381
+ cls_ids = np.argmax(class_probs, axis=1).astype(np.int32)
382
+ cls_scores = class_probs[np.arange(len(class_probs)), cls_ids]
383
+ scores = obj * cls_scores
384
+
385
+ keep = cls_ids == 0
386
+ boxes_xywh = boxes_xywh[keep]
387
+ scores = scores[keep]
388
+ cls_ids = cls_ids[keep]
389
+
390
+ keep = scores >= self.conf_thres
391
+ boxes_xywh = boxes_xywh[keep]
392
+ scores = scores[keep]
393
+ cls_ids = cls_ids[keep]
394
+
395
+ if len(boxes_xywh) == 0:
396
+ return []
397
+
398
+ boxes = self._xywh_to_xyxy(boxes_xywh)
399
+
400
+ pad_w, pad_h = pad
401
+ orig_w, orig_h = orig_size
402
+
403
+ boxes[:, [0, 2]] -= pad_w
404
+ boxes[:, [1, 3]] -= pad_h
405
+ boxes /= ratio
406
+ boxes = self._clip_boxes(boxes, (orig_w, orig_h))
407
+
408
+ boxes, scores, cls_ids = self._filter_sane_boxes(boxes, scores, cls_ids, orig_size)
409
+ if len(boxes) == 0:
410
+ return []
411
+
412
+ keep_idx = self._hard_nms(boxes, scores, self.iou_thres)
413
+ keep_idx = keep_idx[: self.max_det]
414
+
415
+ boxes = boxes[keep_idx]
416
+ scores = scores[keep_idx]
417
+ cls_ids = cls_ids[keep_idx]
418
+
419
+ return [
420
+ BoundingBox(
421
+ x1=int(math.floor(box[0])),
422
+ y1=int(math.floor(box[1])),
423
+ x2=int(math.ceil(box[2])),
424
+ y2=int(math.ceil(box[3])),
425
+ cls_id=int(cls_id),
426
+ conf=float(conf),
427
+ )
428
+ for box, conf, cls_id in zip(boxes, scores, cls_ids)
429
+ if box[2] > box[0] and box[3] > box[1]
430
+ ]
431
+
432
+ def _postprocess(
433
+ self,
434
+ output: np.ndarray,
435
+ ratio: float,
436
+ pad: tuple[float, float],
437
+ orig_size: tuple[int, int],
438
+ ) -> list[BoundingBox]:
439
+ if output.ndim == 2 and output.shape[1] >= 6:
440
+ return self._decode_final_dets(output, ratio, pad, orig_size)
441
+
442
+ if output.ndim == 3 and output.shape[0] == 1 and output.shape[2] >= 6:
443
+ return self._decode_final_dets(output, ratio, pad, orig_size)
444
+
445
+ return self._decode_raw_yolo(output, ratio, pad, orig_size)
446
+
447
+ def _predict_single(self, image: np.ndarray) -> list[BoundingBox]:
448
+ if image is None:
449
+ raise ValueError("Input image is None")
450
+ if not isinstance(image, np.ndarray):
451
+ raise TypeError(f"Input is not numpy array: {type(image)}")
452
+ if image.ndim != 3:
453
+ raise ValueError(f"Expected HWC image, got shape={image.shape}")
454
+ if image.shape[0] <= 0 or image.shape[1] <= 0:
455
+ raise ValueError(f"Invalid image shape={image.shape}")
456
+ if image.shape[2] != 3:
457
+ raise ValueError(f"Expected 3 channels, got shape={image.shape}")
458
+
459
+ if image.dtype != np.uint8:
460
+ image = image.astype(np.uint8)
461
+
462
+ input_tensor, ratio, pad, orig_size = self._preprocess(image)
463
+
464
+ expected_shape = (1, 3, self.input_height, self.input_width)
465
+ if input_tensor.shape != expected_shape:
466
+ raise ValueError(
467
+ f"Bad input tensor shape={input_tensor.shape}, expected={expected_shape}"
468
+ )
469
+
470
+ outputs = self.session.run(self.output_names, {self.input_name: input_tensor})
471
+ det_output = outputs[0]
472
+ return self._postprocess(det_output, ratio, pad, orig_size)
473
+
474
+ def _merge_tta_consensus(
475
+ self,
476
+ boxes_orig: list[BoundingBox],
477
+ boxes_flip: list[BoundingBox],
478
+ ) -> list[BoundingBox]:
479
+ """
480
+ Keep:
481
+ - any box with conf >= conf_high
482
+ - low/medium-conf boxes only if confirmed across TTA views
483
+ Then run final hard NMS.
484
+ """
485
+ if not boxes_orig and not boxes_flip:
486
+ return []
487
+
488
+ 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)
489
+ scores_o = np.array([b.conf for b in boxes_orig], dtype=np.float32) if boxes_orig else np.empty((0,), dtype=np.float32)
490
+
491
+ 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)
492
+ scores_f = np.array([b.conf for b in boxes_flip], dtype=np.float32) if boxes_flip else np.empty((0,), dtype=np.float32)
493
+
494
+ accepted_boxes = []
495
+ accepted_scores = []
496
+
497
+ # Original view candidates
498
+ for i in range(len(coords_o)):
499
+ score = scores_o[i]
500
+ if score >= self.conf_high:
501
+ accepted_boxes.append(coords_o[i])
502
+ accepted_scores.append(score)
503
+ elif len(coords_f) > 0:
504
+ ious = self._box_iou_one_to_many(coords_o[i], coords_f)
505
+ j = int(np.argmax(ious))
506
+ if ious[j] >= self.tta_match_iou:
507
+ fused_score = max(score, scores_f[j])
508
+ accepted_boxes.append(coords_o[i])
509
+ accepted_scores.append(fused_score)
510
+
511
+ # Flipped-view high-confidence boxes that original missed
512
+ for i in range(len(coords_f)):
513
+ score = scores_f[i]
514
+ if score < self.conf_high:
515
+ continue
516
+
517
+ if len(coords_o) == 0:
518
+ accepted_boxes.append(coords_f[i])
519
+ accepted_scores.append(score)
520
+ continue
521
+
522
+ ious = self._box_iou_one_to_many(coords_f[i], coords_o)
523
+ if np.max(ious) < self.tta_match_iou:
524
+ accepted_boxes.append(coords_f[i])
525
+ accepted_scores.append(score)
526
+
527
+ if not accepted_boxes:
528
+ return []
529
+
530
+ boxes = np.array(accepted_boxes, dtype=np.float32)
531
+ scores = np.array(accepted_scores, dtype=np.float32)
532
+
533
+ keep = self._hard_nms(boxes, scores, self.iou_thres)
534
+ keep = keep[: self.max_det]
535
+
536
+ out = []
537
+ for idx in keep:
538
+ x1, y1, x2, y2 = boxes[idx].tolist()
539
+ out.append(
540
+ BoundingBox(
541
+ x1=int(math.floor(x1)),
542
+ y1=int(math.floor(y1)),
543
+ x2=int(math.ceil(x2)),
544
+ y2=int(math.ceil(y2)),
545
+ cls_id=0,
546
+ conf=float(scores[idx]),
547
+ )
548
+ )
549
+ return out
550
+
551
+ def _predict_tta(self, image: np.ndarray) -> list[BoundingBox]:
552
+ boxes_orig = self._predict_single(image)
553
+
554
+ flipped = cv2.flip(image, 1)
555
+ boxes_flip_raw = self._predict_single(flipped)
556
+
557
+ w = image.shape[1]
558
+ boxes_flip = [
559
+ BoundingBox(
560
+ x1=w - b.x2,
561
+ y1=b.y1,
562
+ x2=w - b.x1,
563
+ y2=b.y2,
564
+ cls_id=b.cls_id,
565
+ conf=b.conf,
566
+ )
567
+ for b in boxes_flip_raw
568
+ ]
569
+
570
+ return self._merge_tta_consensus(boxes_orig, boxes_flip)
571
+
572
+ def predict_batch(
573
+ self,
574
+ batch_images: list[ndarray],
575
+ offset: int,
576
+ n_keypoints: int,
577
+ ) -> list[TVFrameResult]:
578
+ results: list[TVFrameResult] = []
579
+
580
+ for frame_number_in_batch, image in enumerate(batch_images):
581
+ try:
582
+ if self.use_tta:
583
+ boxes = self._predict_tta(image)
584
+ else:
585
+ boxes = self._predict_single(image)
586
+ except Exception as e:
587
+ print(f"⚠️ Inference failed for frame {offset + frame_number_in_batch}: {e}")
588
+ boxes = []
589
+
590
+ results.append(
591
+ TVFrameResult(
592
+ frame_id=offset + frame_number_in_batch,
593
+ boxes=boxes,
594
+ keypoints=[(0, 0) for _ in range(max(0, int(n_keypoints)))],
595
+ )
596
+ )
597
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
598
  return results