alfred8995 commited on
Commit
d0c689b
·
verified ·
1 Parent(s): ebd9e36

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. chute_config.yml +22 -0
  2. class_names.txt +4 -0
  3. miner.py +659 -0
  4. weights.onnx +3 -0
chute_config.yml ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Image:
2
+ from_base: parachutes/python:3.12
3
+ run_command:
4
+ - pip install --upgrade setuptools wheel
5
+ - pip install huggingface_hub==0.19.4 ultralytics==8.2.40 'torch<2.6' opencv-python-headless onnxruntime-gpu
6
+ set_workdir: /app
7
+
8
+ NodeSelector:
9
+ gpu_count: 1
10
+ min_vram_gb_per_gpu: 16
11
+ exclude:
12
+ - b200
13
+ - h200
14
+ - h100
15
+ - a100
16
+ - mi300x
17
+
18
+ Chute:
19
+ shutdown_after_seconds: 300000
20
+ concurrency: 4
21
+ max_instances: 1
22
+ scaling_threshold: 0.5
class_names.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ petrol hose
2
+ petrol pump
3
+ price board
4
+ roof canopy
miner.py ADDED
@@ -0,0 +1,659 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ SIZE = 1280
26
+ MODEL_CLASS_ORDER = ["petrol pump", "petrol hose", "roof canopy", "price board"]
27
+
28
+
29
+ class Miner:
30
+ def __init__(self, path_hf_repo: Path) -> None:
31
+ model_path = path_hf_repo / "weights.onnx"
32
+ cn_path = model_path.with_name("class_names.txt")
33
+ if cn_path.is_file():
34
+ lines = cn_path.read_text(encoding="utf-8").splitlines()
35
+ self.class_names = [
36
+ ln.strip()
37
+ for ln in lines
38
+ if ln.strip() and not ln.strip().startswith("#")
39
+ ]
40
+ else:
41
+ self.class_names = ["person"]
42
+ self.model_class_order = MODEL_CLASS_ORDER
43
+ self.class_id_remap = self._build_class_id_remap(
44
+ self.model_class_order, self.class_names
45
+ )
46
+ if self.class_id_remap:
47
+ print("Class ID remap (model->class_names):", self.class_id_remap)
48
+ print("ORT version:", ort.__version__)
49
+
50
+ try:
51
+ ort.preload_dlls()
52
+ print("✅ onnxruntime.preload_dlls() success")
53
+ except Exception as e:
54
+ print(f"⚠️ preload_dlls failed: {e}")
55
+
56
+ print("ORT available providers BEFORE session:", ort.get_available_providers())
57
+
58
+ sess_options = ort.SessionOptions()
59
+ sess_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
60
+
61
+ try:
62
+ self.session = ort.InferenceSession(
63
+ str(model_path),
64
+ sess_options=sess_options,
65
+ providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
66
+ )
67
+ print("✅ Created ORT session with preferred CUDA provider list")
68
+ except Exception as e:
69
+ print(f"⚠️ CUDA session creation failed, falling back to CPU: {e}")
70
+ self.session = ort.InferenceSession(
71
+ str(model_path),
72
+ sess_options=sess_options,
73
+ providers=["CPUExecutionProvider"],
74
+ )
75
+
76
+ print("ORT session providers:", self.session.get_providers())
77
+
78
+ for inp in self.session.get_inputs():
79
+ print("INPUT:", inp.name, inp.shape, inp.type)
80
+
81
+ for out in self.session.get_outputs():
82
+ print("OUTPUT:", out.name, out.shape, out.type)
83
+
84
+ self.input_name = self.session.get_inputs()[0].name
85
+ self.output_names = [output.name for output in self.session.get_outputs()]
86
+ self.input_shape = self.session.get_inputs()[0].shape
87
+
88
+ self.input_height = self._safe_dim(self.input_shape[2], default=SIZE)
89
+ self.input_width = self._safe_dim(self.input_shape[3], default=SIZE)
90
+
91
+ self.conf_thres = 0.41
92
+ self.iou_thres = 0.6
93
+ self.max_det = 600
94
+ self.use_tta = True
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
+ @staticmethod
111
+ def _build_class_id_remap(
112
+ model_order: list[str], target_order: list[str]
113
+ ) -> dict[int, int]:
114
+ """
115
+ Build class index remap from model native order -> class_names order.
116
+ """
117
+ target_index = {name: idx for idx, name in enumerate(target_order)}
118
+ remap: dict[int, int] = {}
119
+ for model_idx, class_name in enumerate(model_order):
120
+ if class_name in target_index:
121
+ remap[model_idx] = target_index[class_name]
122
+ return remap
123
+
124
+ def _remap_cls_id(self, cls_id: int) -> int:
125
+ """
126
+ Remap model class id to class_names id; keep original id if unknown.
127
+ """
128
+ return self.class_id_remap.get(int(cls_id), int(cls_id))
129
+
130
+ def _letterbox(
131
+ self,
132
+ image: ndarray,
133
+ new_shape: tuple[int, int],
134
+ color=(114, 114, 114),
135
+ ) -> tuple[ndarray, float, tuple[float, float]]:
136
+ """
137
+ Resize with unchanged aspect ratio and pad to target shape.
138
+ Returns:
139
+ padded_image,
140
+ ratio,
141
+ (pad_w, pad_h) # half-padding
142
+ """
143
+ h, w = image.shape[:2]
144
+ new_w, new_h = new_shape
145
+
146
+ ratio = min(new_w / w, new_h / h)
147
+ resized_w = int(round(w * ratio))
148
+ resized_h = int(round(h * ratio))
149
+
150
+ if (resized_w, resized_h) != (w, h):
151
+ interp = cv2.INTER_CUBIC if ratio > 1.0 else cv2.INTER_LINEAR
152
+ image = cv2.resize(image, (resized_w, resized_h), interpolation=interp)
153
+
154
+ dw = new_w - resized_w
155
+ dh = new_h - resized_h
156
+ dw /= 2.0
157
+ dh /= 2.0
158
+
159
+ left = int(round(dw - 0.1))
160
+ right = int(round(dw + 0.1))
161
+ top = int(round(dh - 0.1))
162
+ bottom = int(round(dh + 0.1))
163
+
164
+ padded = cv2.copyMakeBorder(
165
+ image,
166
+ top,
167
+ bottom,
168
+ left,
169
+ right,
170
+ borderType=cv2.BORDER_CONSTANT,
171
+ value=color,
172
+ )
173
+ return padded, ratio, (dw, dh)
174
+
175
+ def _preprocess(
176
+ self, image: ndarray
177
+ ) -> tuple[np.ndarray, float, tuple[float, float], tuple[int, int]]:
178
+ """
179
+ Preprocess for fixed-size ONNX export:
180
+ - enhance image quality (CLAHE, denoise, sharpen)
181
+ - letterbox to model input size
182
+ - BGR -> RGB
183
+ - normalize to [0,1]
184
+ - HWC -> NCHW float32
185
+ """
186
+ orig_h, orig_w = image.shape[:2]
187
+
188
+ img, ratio, pad = self._letterbox(
189
+ image, (self.input_width, self.input_height)
190
+ )
191
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
192
+ img = img.astype(np.float32) / 255.0
193
+ img = np.transpose(img, (2, 0, 1))[None, ...]
194
+ img = np.ascontiguousarray(img, dtype=np.float32)
195
+
196
+ return img, ratio, pad, (orig_w, orig_h)
197
+
198
+ @staticmethod
199
+ def _clip_boxes(boxes: np.ndarray, image_size: tuple[int, int]) -> np.ndarray:
200
+ w, h = image_size
201
+ boxes[:, 0] = np.clip(boxes[:, 0], 0, w - 1)
202
+ boxes[:, 1] = np.clip(boxes[:, 1], 0, h - 1)
203
+ boxes[:, 2] = np.clip(boxes[:, 2], 0, w - 1)
204
+ boxes[:, 3] = np.clip(boxes[:, 3], 0, h - 1)
205
+ return boxes
206
+
207
+ @staticmethod
208
+ def _xywh_to_xyxy(boxes: np.ndarray) -> np.ndarray:
209
+ out = np.empty_like(boxes)
210
+ out[:, 0] = boxes[:, 0] - boxes[:, 2] / 2.0
211
+ out[:, 1] = boxes[:, 1] - boxes[:, 3] / 2.0
212
+ out[:, 2] = boxes[:, 0] + boxes[:, 2] / 2.0
213
+ out[:, 3] = boxes[:, 1] + boxes[:, 3] / 2.0
214
+ return out
215
+
216
+ def _soft_nms(
217
+ self,
218
+ boxes: np.ndarray,
219
+ scores: np.ndarray,
220
+ sigma: float = 0.5,
221
+ score_thresh: float = 0.01,
222
+ ) -> tuple[np.ndarray, np.ndarray]:
223
+ """
224
+ Soft-NMS: Gaussian decay of overlapping scores instead of hard removal.
225
+ Returns (kept_original_indices, updated_scores).
226
+ """
227
+ N = len(boxes)
228
+ if N == 0:
229
+ return np.array([], dtype=np.intp), np.array([], dtype=np.float32)
230
+
231
+ boxes = boxes.astype(np.float32, copy=True)
232
+ scores = scores.astype(np.float32, copy=True)
233
+ order = np.arange(N)
234
+
235
+ for i in range(N):
236
+ max_pos = i + int(np.argmax(scores[i:]))
237
+ boxes[[i, max_pos]] = boxes[[max_pos, i]]
238
+ scores[[i, max_pos]] = scores[[max_pos, i]]
239
+ order[[i, max_pos]] = order[[max_pos, i]]
240
+
241
+ if i + 1 >= N:
242
+ break
243
+
244
+ xx1 = np.maximum(boxes[i, 0], boxes[i + 1:, 0])
245
+ yy1 = np.maximum(boxes[i, 1], boxes[i + 1:, 1])
246
+ xx2 = np.minimum(boxes[i, 2], boxes[i + 1:, 2])
247
+ yy2 = np.minimum(boxes[i, 3], boxes[i + 1:, 3])
248
+ inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
249
+
250
+ area_i = max(0.0, float(
251
+ (boxes[i, 2] - boxes[i, 0]) * (boxes[i, 3] - boxes[i, 1])
252
+ ))
253
+ areas_j = (
254
+ np.maximum(0.0, boxes[i + 1:, 2] - boxes[i + 1:, 0])
255
+ * np.maximum(0.0, boxes[i + 1:, 3] - boxes[i + 1:, 1])
256
+ )
257
+ iou = inter / (area_i + areas_j - inter + 1e-7)
258
+ scores[i + 1:] *= np.exp(-(iou ** 2) / sigma)
259
+
260
+ mask = scores > score_thresh
261
+ return order[mask], scores[mask]
262
+
263
+ @staticmethod
264
+ def _hard_nms(
265
+ boxes: np.ndarray,
266
+ scores: np.ndarray,
267
+ iou_thresh: float,
268
+ ) -> np.ndarray:
269
+ """
270
+ Standard NMS: keep one box per overlapping cluster (the one with highest score).
271
+ Returns indices of kept boxes (into the boxes/scores arrays).
272
+ """
273
+ N = len(boxes)
274
+ if N == 0:
275
+ return np.array([], dtype=np.intp)
276
+ boxes = np.asarray(boxes, dtype=np.float32)
277
+ scores = np.asarray(scores, dtype=np.float32)
278
+ order = np.argsort(scores)[::-1]
279
+ keep: list[int] = []
280
+ suppressed = np.zeros(N, dtype=bool)
281
+ for i in range(N):
282
+ idx = order[i]
283
+ if suppressed[idx]:
284
+ continue
285
+ keep.append(idx)
286
+ bi = boxes[idx]
287
+ for k in range(i + 1, N):
288
+ jdx = order[k]
289
+ if suppressed[jdx]:
290
+ continue
291
+ bj = boxes[jdx]
292
+ xx1 = max(bi[0], bj[0])
293
+ yy1 = max(bi[1], bj[1])
294
+ xx2 = min(bi[2], bj[2])
295
+ yy2 = min(bi[3], bj[3])
296
+ inter = max(0.0, xx2 - xx1) * max(0.0, yy2 - yy1)
297
+ area_i = (bi[2] - bi[0]) * (bi[3] - bi[1])
298
+ area_j = (bj[2] - bj[0]) * (bj[3] - bj[1])
299
+ iou = inter / (area_i + area_j - inter + 1e-7)
300
+ if iou > iou_thresh:
301
+ suppressed[jdx] = True
302
+ return np.array(keep)
303
+
304
+ @staticmethod
305
+ def _max_score_per_cluster(
306
+ coords: np.ndarray,
307
+ scores: np.ndarray,
308
+ keep_indices: np.ndarray,
309
+ iou_thresh: float,
310
+ ) -> np.ndarray:
311
+ """
312
+ For each kept box, return the max original score among itself and any
313
+ box that overlaps it with IOU >= iou_thresh (so TTA cluster keeps best conf).
314
+ """
315
+ n_keep = len(keep_indices)
316
+ if n_keep == 0:
317
+ return np.array([], dtype=np.float32)
318
+ out = np.empty(n_keep, dtype=np.float32)
319
+ coords = np.asarray(coords, dtype=np.float32)
320
+ scores = np.asarray(scores, dtype=np.float32)
321
+ for i in range(n_keep):
322
+ idx = keep_indices[i]
323
+ bi = coords[idx]
324
+ xx1 = np.maximum(bi[0], coords[:, 0])
325
+ yy1 = np.maximum(bi[1], coords[:, 1])
326
+ xx2 = np.minimum(bi[2], coords[:, 2])
327
+ yy2 = np.minimum(bi[3], coords[:, 3])
328
+ inter = np.maximum(0.0, xx2 - xx1) * np.maximum(0.0, yy2 - yy1)
329
+ area_i = (bi[2] - bi[0]) * (bi[3] - bi[1])
330
+ areas_j = (coords[:, 2] - coords[:, 0]) * (coords[:, 3] - coords[:, 1])
331
+ iou = inter / (area_i + areas_j - inter + 1e-7)
332
+ in_cluster = iou >= iou_thresh
333
+ out[i] = float(np.max(scores[in_cluster]))
334
+ return out
335
+
336
+ def _decode_final_dets(
337
+ self,
338
+ preds: np.ndarray,
339
+ ratio: float,
340
+ pad: tuple[float, float],
341
+ orig_size: tuple[int, int],
342
+ apply_optional_dedup: bool = False,
343
+ ) -> list[BoundingBox]:
344
+ """
345
+ Primary path:
346
+ expected output rows like [x1, y1, x2, y2, conf, cls_id]
347
+ in letterboxed input coordinates.
348
+ """
349
+ if preds.ndim == 3 and preds.shape[0] == 1:
350
+ preds = preds[0]
351
+
352
+ if preds.ndim != 2 or preds.shape[1] < 6:
353
+ raise ValueError(f"Unexpected ONNX final-det output shape: {preds.shape}")
354
+
355
+ boxes = preds[:, :4].astype(np.float32)
356
+ scores = preds[:, 4].astype(np.float32)
357
+ cls_ids = preds[:, 5].astype(np.int32)
358
+
359
+ keep = scores >= self.conf_thres
360
+ boxes = boxes[keep]
361
+ scores = scores[keep]
362
+ cls_ids = cls_ids[keep]
363
+
364
+ if len(boxes) == 0:
365
+ return []
366
+
367
+ pad_w, pad_h = pad
368
+ orig_w, orig_h = orig_size
369
+
370
+ # reverse letterbox
371
+ boxes[:, [0, 2]] -= pad_w
372
+ boxes[:, [1, 3]] -= pad_h
373
+ boxes /= ratio
374
+ boxes = self._clip_boxes(boxes, (orig_w, orig_h))
375
+
376
+ if apply_optional_dedup and len(boxes) > 1:
377
+ keep_idx, scores = self._soft_nms(boxes, scores)
378
+ boxes = boxes[keep_idx]
379
+ cls_ids = cls_ids[keep_idx]
380
+
381
+ results: list[BoundingBox] = []
382
+ for box, conf, cls_id in zip(boxes, scores, cls_ids):
383
+ x1, y1, x2, y2 = box.tolist()
384
+
385
+ if x2 <= x1 or y2 <= y1:
386
+ continue
387
+
388
+ results.append(
389
+ BoundingBox(
390
+ x1=int(math.floor(x1)),
391
+ y1=int(math.floor(y1)),
392
+ x2=int(math.ceil(x2)),
393
+ y2=int(math.ceil(y2)),
394
+ cls_id=self._remap_cls_id(int(cls_id)),
395
+ conf=float(conf),
396
+ )
397
+ )
398
+
399
+ return results
400
+
401
+ def _decode_raw_yolo(
402
+ self,
403
+ preds: np.ndarray,
404
+ ratio: float,
405
+ pad: tuple[float, float],
406
+ orig_size: tuple[int, int],
407
+ ) -> list[BoundingBox]:
408
+ """
409
+ Fallback path for raw YOLO predictions.
410
+ Supports common layouts:
411
+ - [1, C, N]
412
+ - [1, N, C]
413
+ """
414
+ if preds.ndim != 3:
415
+ raise ValueError(f"Unexpected raw ONNX output shape: {preds.shape}")
416
+
417
+ if preds.shape[0] != 1:
418
+ raise ValueError(f"Unexpected batch dimension in raw output: {preds.shape}")
419
+
420
+ preds = preds[0]
421
+
422
+ # Normalize to [N, C]
423
+ if preds.shape[0] <= 16 and preds.shape[1] > preds.shape[0]:
424
+ preds = preds.T
425
+
426
+ if preds.ndim != 2 or preds.shape[1] < 5:
427
+ raise ValueError(f"Unexpected normalized raw output shape: {preds.shape}")
428
+
429
+ boxes_xywh = preds[:, :4].astype(np.float32)
430
+ cls_part = preds[:, 4:].astype(np.float32)
431
+
432
+ if cls_part.shape[1] == 1:
433
+ scores = cls_part[:, 0]
434
+ cls_ids = np.zeros(len(scores), dtype=np.int32)
435
+ else:
436
+ cls_ids = np.argmax(cls_part, axis=1).astype(np.int32)
437
+ scores = cls_part[np.arange(len(cls_part)), cls_ids]
438
+
439
+ keep = scores >= self.conf_thres
440
+ boxes_xywh = boxes_xywh[keep]
441
+ scores = scores[keep]
442
+ cls_ids = cls_ids[keep]
443
+
444
+ if len(boxes_xywh) == 0:
445
+ return []
446
+
447
+ boxes = self._xywh_to_xyxy(boxes_xywh)
448
+ keep_idx, scores = self._soft_nms(boxes, scores)
449
+ keep_idx = keep_idx[: self.max_det]
450
+ scores = scores[: self.max_det]
451
+
452
+ boxes = boxes[keep_idx]
453
+ cls_ids = cls_ids[keep_idx]
454
+
455
+ pad_w, pad_h = pad
456
+ orig_w, orig_h = orig_size
457
+
458
+ boxes[:, [0, 2]] -= pad_w
459
+ boxes[:, [1, 3]] -= pad_h
460
+ boxes /= ratio
461
+ boxes = self._clip_boxes(boxes, (orig_w, orig_h))
462
+
463
+ results: list[BoundingBox] = []
464
+ for box, conf, cls_id in zip(boxes, scores, cls_ids):
465
+ x1, y1, x2, y2 = box.tolist()
466
+
467
+ if x2 <= x1 or y2 <= y1:
468
+ continue
469
+
470
+ results.append(
471
+ BoundingBox(
472
+ x1=int(math.floor(x1)),
473
+ y1=int(math.floor(y1)),
474
+ x2=int(math.ceil(x2)),
475
+ y2=int(math.ceil(y2)),
476
+ cls_id=self._remap_cls_id(int(cls_id)),
477
+ conf=float(conf),
478
+ )
479
+ )
480
+
481
+ return results
482
+
483
+ def _postprocess(
484
+ self,
485
+ output: np.ndarray,
486
+ ratio: float,
487
+ pad: tuple[float, float],
488
+ orig_size: tuple[int, int],
489
+ ) -> list[BoundingBox]:
490
+ """
491
+ Prefer final detections first.
492
+ Fallback to raw decode only if needed.
493
+ """
494
+ # final detections: [N,6]
495
+ if output.ndim == 2 and output.shape[1] >= 6:
496
+ return self._decode_final_dets(output, ratio, pad, orig_size)
497
+
498
+ # final detections: [1,N,6]
499
+ if output.ndim == 3 and output.shape[0] == 1 and output.shape[2] == 6:
500
+ return self._decode_final_dets(output, ratio, pad, orig_size)
501
+
502
+ # fallback raw decode
503
+ return self._decode_raw_yolo(output, ratio, pad, orig_size)
504
+
505
+ def _predict_single(self, image: np.ndarray) -> list[BoundingBox]:
506
+ if image is None:
507
+ raise ValueError("Input image is None")
508
+ if not isinstance(image, np.ndarray):
509
+ raise TypeError(f"Input is not numpy array: {type(image)}")
510
+ if image.ndim != 3:
511
+ raise ValueError(f"Expected HWC image, got shape={image.shape}")
512
+ if image.shape[0] <= 0 or image.shape[1] <= 0:
513
+ raise ValueError(f"Invalid image shape={image.shape}")
514
+ if image.shape[2] != 3:
515
+ raise ValueError(f"Expected 3 channels, got shape={image.shape}")
516
+
517
+ if image.dtype != np.uint8:
518
+ image = image.astype(np.uint8)
519
+
520
+ input_tensor, ratio, pad, orig_size = self._preprocess(image)
521
+
522
+ expected_shape = (1, 3, self.input_height, self.input_width)
523
+ if input_tensor.shape != expected_shape:
524
+ raise ValueError(
525
+ f"Bad input tensor shape={input_tensor.shape}, expected={expected_shape}"
526
+ )
527
+
528
+ outputs = self.session.run(self.output_names, {self.input_name: input_tensor})
529
+ det_output = outputs[0]
530
+ return self._postprocess(det_output, ratio, pad, orig_size)
531
+
532
+ def _predict_tta(self, image: np.ndarray) -> list[BoundingBox]:
533
+ """Horizontal-flip TTA: merge original + flipped via hard NMS."""
534
+ boxes_orig = self._predict_single(image)
535
+
536
+ flipped = cv2.flip(image, 1)
537
+ boxes_flip = self._predict_single(flipped)
538
+
539
+ w = image.shape[1]
540
+ boxes_flip = [
541
+ BoundingBox(
542
+ x1=w - b.x2, y1=b.y1, x2=w - b.x1, y2=b.y2,
543
+ cls_id=b.cls_id, conf=b.conf,
544
+ )
545
+ for b in boxes_flip
546
+ ]
547
+
548
+ all_boxes = boxes_orig + boxes_flip
549
+ if len(all_boxes) == 0:
550
+ return []
551
+
552
+ coords = np.array(
553
+ [[b.x1, b.y1, b.x2, b.y2] for b in all_boxes], dtype=np.float32
554
+ )
555
+ scores = np.array([b.conf for b in all_boxes], dtype=np.float32)
556
+
557
+ hard_keep = self._hard_nms(coords, scores, self.iou_thres)
558
+ if len(hard_keep) == 0:
559
+ return []
560
+
561
+ # _hard_nms already orders kept indices by descending score.
562
+ hard_keep = hard_keep[: self.max_det]
563
+
564
+ return [
565
+ BoundingBox(
566
+ x1=all_boxes[i].x1,
567
+ y1=all_boxes[i].y1,
568
+ x2=all_boxes[i].x2,
569
+ y2=all_boxes[i].y2,
570
+ cls_id=all_boxes[i].cls_id,
571
+ conf=float(scores[i]),
572
+ )
573
+ for i in hard_keep
574
+ ]
575
+
576
+ def predict_batch(
577
+ self,
578
+ batch_images: list[ndarray],
579
+ offset: int,
580
+ n_keypoints: int,
581
+ ) -> list[TVFrameResult]:
582
+ results: list[TVFrameResult] = []
583
+
584
+ for frame_number_in_batch, image in enumerate(batch_images):
585
+ try:
586
+ if self.use_tta:
587
+ boxes = self._predict_tta(image)
588
+ else:
589
+ boxes = self._predict_single(image)
590
+ except Exception as e:
591
+ print(f"⚠️ Inference failed for frame {offset + frame_number_in_batch}: {e}")
592
+ boxes = []
593
+ # for box in boxes:
594
+ # if box.cls_id == 2:
595
+ # box.cls_id = 3
596
+ # elif box.cls_id == 3:
597
+ # box.cls_id = 2
598
+
599
+
600
+
601
+ results.append(
602
+ TVFrameResult(
603
+ frame_id=offset + frame_number_in_batch,
604
+ boxes=boxes,
605
+ keypoints=[(0, 0) for _ in range(max(0, int(n_keypoints)))],
606
+ )
607
+ )
608
+
609
+ return results
610
+
611
+
612
+ if __name__ == "__main__":
613
+ # Simple manual test: load weights.onnx, run on 1.png, and draw bboxes
614
+ repo_dir = Path(__file__).parent
615
+ miner = Miner(repo_dir)
616
+
617
+ image_path = repo_dir / "car1.png"
618
+ if not image_path.exists():
619
+ raise FileNotFoundError(f"Test image not found: {image_path}")
620
+
621
+ image = cv2.imread(str(image_path), cv2.IMREAD_COLOR)
622
+ if image is None:
623
+ raise RuntimeError(f"Failed to read image: {image_path}")
624
+
625
+ results = miner.predict_batch([image], offset=0, n_keypoints=0)
626
+ # Draw bounding boxes on a copy of the image
627
+ vis = image.copy()
628
+ colors = [(0, 255, 0), (0, 0, 255), (255, 0, 0)]
629
+ for frame in results:
630
+ print(f"Frame {frame.frame_id}:")
631
+ for i, box in enumerate(frame.boxes):
632
+ color = colors[i % len(colors)]
633
+ cv2.rectangle(
634
+ vis,
635
+ (box.x1, box.y1),
636
+ (box.x2, box.y2),
637
+ color,
638
+ 2,
639
+ )
640
+ label = f"{box.cls_id }_{miner.class_names[box.cls_id] if box.cls_id < len(miner.class_names) else box.cls_id}:{box.conf:.2f}"
641
+ cv2.putText(
642
+ vis,
643
+ label,
644
+ (box.x1, max(0, box.y1 - 5)),
645
+ cv2.FONT_HERSHEY_SIMPLEX,
646
+ box.conf,
647
+ color,
648
+ 1,
649
+ cv2.LINE_AA,
650
+ )
651
+ print(
652
+ f" cls={box.cls_id} conf={box.conf:.3f} "
653
+ f"box=({box.x1},{box.y1},{box.x2},{box.y2})"
654
+ )
655
+ print(len(frame.boxes))
656
+
657
+ out_path = repo_dir / f"1_out_iou{miner.iou_thres:.2f}.png"
658
+ cv2.imwrite(str(out_path), vis)
659
+ print(f"Saved visualization to: {out_path}")
weights.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1b1af947bb9d53ee5d6d5699dcff651c06330c8536f5f86ad280c6f20e239d73
3
+ size 19408011