silvanus0930 MTerryJack commited on
Commit
0110d96
·
0 Parent(s):

Duplicate from manak0/Detect-road-signs

Browse files

Co-authored-by: Mohammed Terry-Jack <MTerryJack@users.noreply.huggingface.co>

.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - element_type:detect
4
+ - model:yolov11-nano
5
+ - object:road sign
6
+ manako:
7
+ description: Manako baseline - generated by element_trainer service to detect road
8
+ sign
9
+ source: element_trainer/road-signs
10
+ prompt_hints: null
11
+ input_payload:
12
+ - name: frame
13
+ type: image
14
+ description: RGB frame
15
+ output_payload:
16
+ - name: detections
17
+ type: detections
18
+ description: List of detections
19
+ evaluation_score: null
20
+ last_benchmark:
21
+ type: synthetic_fixed
22
+ ran_at: '2026-05-14T11:30:33.077393Z'
23
+ result_path: benchmark/synthetic/all_versions_2026-05-14T11-30-01.642192Z.json
24
+ score:
25
+ size_mb: 10.803857
26
+ performance_map50: 0.5919211196881521
27
+ E: 0.05478794468384319
28
+ ---
benchmark/synthetic/all_versions_2026-05-14T11-30-01.642192Z.json ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "meta": {
3
+ "element_id": "manak0/Detect-road-signs",
4
+ "run_id": "all_versions_2026-05-14T11-30-01.642192Z",
5
+ "benchmark_type": "synthetic_fixed",
6
+ "saved_at": "2026-05-14T11:30:33.077393Z",
7
+ "result_path": "benchmark/synthetic/all_versions_2026-05-14T11-30-01.642192Z.json"
8
+ },
9
+ "results": {
10
+ "overall_iou": 0.6995314762064266,
11
+ "per_class_iou": {
12
+ "road sign": 0.6995314762064266
13
+ },
14
+ "map_50": 0.5919211196881521,
15
+ "map_50_90": 0.46679792345917676,
16
+ "precision": 0.8596491228070176,
17
+ "recall": 0.6068111455108359,
18
+ "per_class_map_50": {
19
+ "road sign": 0.5919211196881521
20
+ },
21
+ "per_class_map_50_90": {
22
+ "road sign": 0.46679792345917676
23
+ },
24
+ "per_class_precision": {
25
+ "road sign": 0.8596491228070176
26
+ },
27
+ "per_class_recall": {
28
+ "road sign": 0.6068111455108359
29
+ },
30
+ "matched_images": 50,
31
+ "gt_count": 323,
32
+ "pred_count": 228,
33
+ "classes": [
34
+ "road sign"
35
+ ],
36
+ "dataset_id": "ea690ab5-a4de-45f0-8f0f-e3b07e3eefba",
37
+ "dataset_version_id": "all_versions",
38
+ "run_id": "all_versions_2026-05-14T11-30-01.642192Z",
39
+ "split_ref": null,
40
+ "selected_frame_count": 50,
41
+ "selected_frame_indices": [
42
+ 1,
43
+ 2,
44
+ 3,
45
+ 4,
46
+ 5,
47
+ 6,
48
+ 7,
49
+ 8,
50
+ 9,
51
+ 10,
52
+ 11,
53
+ 12,
54
+ 13,
55
+ 14,
56
+ 15,
57
+ 16,
58
+ 17,
59
+ 18,
60
+ 19,
61
+ 20,
62
+ 21,
63
+ 22,
64
+ 23,
65
+ 24,
66
+ 25,
67
+ 26,
68
+ 27,
69
+ 28,
70
+ 29,
71
+ 30,
72
+ 31,
73
+ 32,
74
+ 33,
75
+ 34,
76
+ 35,
77
+ 36,
78
+ 37,
79
+ 38,
80
+ 39,
81
+ 40,
82
+ 41,
83
+ 42,
84
+ 43,
85
+ 44,
86
+ 45,
87
+ 46,
88
+ 47,
89
+ 48,
90
+ 49,
91
+ 50
92
+ ],
93
+ "evaluated_version_ids": [
94
+ "bd9e3e46-9c3e-499f-9864-6b6890d53b21",
95
+ "34396eaa-58e0-47fa-89b1-bbebf46c4789",
96
+ "0d9ebe06-47c2-40d0-87ef-8cad3dc39c63"
97
+ ],
98
+ "skipped_missing_pairs": 0
99
+ }
100
+ }
benchmark/synthetic/bd9e3e46-9c3e-499f-9864-6b6890d53b21.json ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "meta": {
3
+ "element_id": "manak0/Detect-road-signs",
4
+ "run_id": "bd9e3e46-9c3e-499f-9864-6b6890d53b21",
5
+ "benchmark_type": "synthetic_fixed",
6
+ "saved_at": "2026-04-27T15:38:54.020844Z",
7
+ "result_path": "benchmark/synthetic/bd9e3e46-9c3e-499f-9864-6b6890d53b21.json"
8
+ },
9
+ "results": {
10
+ "overall_iou": 0.6995314762064266,
11
+ "per_class_iou": {
12
+ "road sign": 0.6995314762064266
13
+ },
14
+ "map_50": 0.5919211196881521,
15
+ "map_50_90": 0.46679792345917676,
16
+ "precision": 0.8596491228070176,
17
+ "recall": 0.6068111455108359,
18
+ "per_class_map_50": {
19
+ "road sign": 0.5919211196881521
20
+ },
21
+ "per_class_map_50_90": {
22
+ "road sign": 0.46679792345917676
23
+ },
24
+ "per_class_precision": {
25
+ "road sign": 0.8596491228070176
26
+ },
27
+ "per_class_recall": {
28
+ "road sign": 0.6068111455108359
29
+ },
30
+ "matched_images": 50,
31
+ "gt_count": 323,
32
+ "pred_count": 228,
33
+ "classes": [
34
+ "road sign"
35
+ ],
36
+ "dataset_id": "ea690ab5-a4de-45f0-8f0f-e3b07e3eefba",
37
+ "dataset_version_id": "bd9e3e46-9c3e-499f-9864-6b6890d53b21",
38
+ "run_id": "bd9e3e46-9c3e-499f-9864-6b6890d53b21",
39
+ "split_ref": null,
40
+ "selected_frame_count": 50,
41
+ "selected_frame_indices": [
42
+ 1,
43
+ 2,
44
+ 3,
45
+ 4,
46
+ 5,
47
+ 6,
48
+ 7,
49
+ 8,
50
+ 9,
51
+ 10,
52
+ 11,
53
+ 12,
54
+ 13,
55
+ 14,
56
+ 15,
57
+ 16,
58
+ 17,
59
+ 18,
60
+ 19,
61
+ 20,
62
+ 21,
63
+ 22,
64
+ 23,
65
+ 24,
66
+ 25,
67
+ 26,
68
+ 27,
69
+ 28,
70
+ 29,
71
+ 30,
72
+ 31,
73
+ 32,
74
+ 33,
75
+ 34,
76
+ 35,
77
+ 36,
78
+ 37,
79
+ 38,
80
+ 39,
81
+ 40,
82
+ 41,
83
+ 42,
84
+ 43,
85
+ 44,
86
+ 45,
87
+ 46,
88
+ 47,
89
+ 48,
90
+ 49,
91
+ 50
92
+ ],
93
+ "evaluated_version_ids": [
94
+ "bd9e3e46-9c3e-499f-9864-6b6890d53b21"
95
+ ],
96
+ "skipped_missing_pairs": 0
97
+ }
98
+ }
benchmark/synthetic/index.json ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "run_id": "all_versions_2026-05-14T11-30-01.642192Z",
4
+ "saved_at": "2026-05-14T11:30:33.077393Z",
5
+ "result_path": "benchmark/synthetic/all_versions_2026-05-14T11-30-01.642192Z.json",
6
+ "overall_iou": 0.6995314762064266,
7
+ "map_50": 0.5919211196881521,
8
+ "map_50_90": 0.46679792345917676,
9
+ "precision": 0.8596491228070176,
10
+ "recall": 0.6068111455108359,
11
+ "matched_images": 50,
12
+ "gt_count": 323,
13
+ "pred_count": 228
14
+ },
15
+ {
16
+ "run_id": "bd9e3e46-9c3e-499f-9864-6b6890d53b21",
17
+ "saved_at": "2026-04-27T15:38:54.020844Z",
18
+ "result_path": "benchmark/synthetic/bd9e3e46-9c3e-499f-9864-6b6890d53b21.json",
19
+ "overall_iou": 0.6995314762064266,
20
+ "map_50": 0.5919211196881521,
21
+ "map_50_90": 0.46679792345917676,
22
+ "precision": 0.8596491228070176,
23
+ "recall": 0.6068111455108359,
24
+ "matched_images": 50,
25
+ "gt_count": 323,
26
+ "pred_count": 228
27
+ }
28
+ ]
benchmark/synthetic/latest.json ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "meta": {
3
+ "element_id": "manak0/Detect-road-signs",
4
+ "run_id": "all_versions_2026-05-14T11-30-01.642192Z",
5
+ "benchmark_type": "synthetic_fixed",
6
+ "saved_at": "2026-05-14T11:30:33.077393Z",
7
+ "result_path": "benchmark/synthetic/all_versions_2026-05-14T11-30-01.642192Z.json"
8
+ },
9
+ "results": {
10
+ "overall_iou": 0.6995314762064266,
11
+ "per_class_iou": {
12
+ "road sign": 0.6995314762064266
13
+ },
14
+ "map_50": 0.5919211196881521,
15
+ "map_50_90": 0.46679792345917676,
16
+ "precision": 0.8596491228070176,
17
+ "recall": 0.6068111455108359,
18
+ "per_class_map_50": {
19
+ "road sign": 0.5919211196881521
20
+ },
21
+ "per_class_map_50_90": {
22
+ "road sign": 0.46679792345917676
23
+ },
24
+ "per_class_precision": {
25
+ "road sign": 0.8596491228070176
26
+ },
27
+ "per_class_recall": {
28
+ "road sign": 0.6068111455108359
29
+ },
30
+ "matched_images": 50,
31
+ "gt_count": 323,
32
+ "pred_count": 228,
33
+ "classes": [
34
+ "road sign"
35
+ ],
36
+ "dataset_id": "ea690ab5-a4de-45f0-8f0f-e3b07e3eefba",
37
+ "dataset_version_id": "all_versions",
38
+ "run_id": "all_versions_2026-05-14T11-30-01.642192Z",
39
+ "split_ref": null,
40
+ "selected_frame_count": 50,
41
+ "selected_frame_indices": [
42
+ 1,
43
+ 2,
44
+ 3,
45
+ 4,
46
+ 5,
47
+ 6,
48
+ 7,
49
+ 8,
50
+ 9,
51
+ 10,
52
+ 11,
53
+ 12,
54
+ 13,
55
+ 14,
56
+ 15,
57
+ 16,
58
+ 17,
59
+ 18,
60
+ 19,
61
+ 20,
62
+ 21,
63
+ 22,
64
+ 23,
65
+ 24,
66
+ 25,
67
+ 26,
68
+ 27,
69
+ 28,
70
+ 29,
71
+ 30,
72
+ 31,
73
+ 32,
74
+ 33,
75
+ 34,
76
+ 35,
77
+ 36,
78
+ 37,
79
+ 38,
80
+ 39,
81
+ 40,
82
+ 41,
83
+ 42,
84
+ 43,
85
+ 44,
86
+ 45,
87
+ 46,
88
+ 47,
89
+ 48,
90
+ 49,
91
+ 50
92
+ ],
93
+ "evaluated_version_ids": [
94
+ "bd9e3e46-9c3e-499f-9864-6b6890d53b21",
95
+ "34396eaa-58e0-47fa-89b1-bbebf46c4789",
96
+ "0d9ebe06-47c2-40d0-87ef-8cad3dc39c63"
97
+ ],
98
+ "skipped_missing_pairs": 0
99
+ }
100
+ }
chute_config.yml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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>=1.16' 'opencv-python>=4.7' 'pillow>=9.5' 'huggingface_hub>=0.19.4' 'pydantic>=2.0' 'pyyaml>=6.0' 'aiohttp>=3.9' 'torch>=2.8'
6
+
7
+ NodeSelector:
8
+ gpu_count: 1
9
+ min_vram_gb_per_gpu: 16
10
+ include:
11
+ - pro_6000
12
+
13
+ Chute:
14
+ timeout_seconds: 300
15
+ concurrency: 4
16
+ max_instances: 5
17
+ scaling_threshold: 0.5
18
+ tee: true
class_names.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ road sign
main.py ADDED
@@ -0,0 +1,679 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Auto-generated ONNX runner. This file is self-contained for a single model.
2
+ import json
3
+ import os
4
+ import sys
5
+ from typing import Any, Dict, List, Tuple
6
+
7
+ import cv2
8
+ import numpy as np
9
+ import onnxruntime as ort
10
+ from PIL import Image
11
+
12
+
13
+ def read_json(path: str) -> Dict[str, Any]:
14
+ with open(path, "r", encoding="utf-8") as f:
15
+ return json.load(f)
16
+
17
+
18
+ def read_text_lines(path: str) -> List[str]:
19
+ with open(path, "r", encoding="utf-8") as f:
20
+ return [line.strip() for line in f.readlines() if line.strip()]
21
+
22
+
23
+ def load_environment(data_dir: str) -> Dict[str, Any]:
24
+ env_path = os.path.join(data_dir, "environment.json")
25
+ if not os.path.exists(env_path):
26
+ return {}
27
+ env = read_json(env_path)
28
+ preproc = env.get("PREPROCESSING")
29
+ if isinstance(preproc, str):
30
+ try:
31
+ env["PREPROCESSING"] = json.loads(preproc)
32
+ except json.JSONDecodeError:
33
+ env["PREPROCESSING"] = {}
34
+ return env
35
+
36
+
37
+ def load_class_names(data_dir: str, environment: Dict[str, Any]) -> List[str]:
38
+ class_path = os.path.join(data_dir, "class_names.txt")
39
+ if os.path.exists(class_path):
40
+ return read_text_lines(class_path)
41
+ class_map = environment.get("CLASS_MAP")
42
+ if isinstance(class_map, dict):
43
+ class_names = []
44
+ for i in range(len(class_map.keys())):
45
+ class_names.append(class_map[str(i)])
46
+ return class_names
47
+ return []
48
+
49
+
50
+ def load_keypoints_metadata(data_dir: str) -> List[Dict[str, Any]]:
51
+ meta_path = os.path.join(data_dir, "keypoints_metadata.json")
52
+ if not os.path.exists(meta_path):
53
+ return []
54
+ return read_json(meta_path)
55
+
56
+
57
+ def load_image(value: Any) -> Tuple[np.ndarray, bool]:
58
+ if isinstance(value, np.ndarray):
59
+ return value, True
60
+ if isinstance(value, Image.Image):
61
+ return np.asarray(value.convert("RGB")), False
62
+ if isinstance(value, (bytes, bytearray)):
63
+ image = cv2.imdecode(np.frombuffer(value, np.uint8), cv2.IMREAD_COLOR)
64
+ return image, True
65
+ if isinstance(value, str):
66
+ image = cv2.imread(value, cv2.IMREAD_COLOR)
67
+ if image is None:
68
+ raise ValueError(f"Could not read image: {value}")
69
+ return image, True
70
+ raise ValueError(f"Unsupported image input type: {type(value)}")
71
+
72
+
73
+ def static_crop_should_be_applied(preprocessing_config: dict) -> bool:
74
+ cfg = preprocessing_config.get("static-crop")
75
+ return bool(cfg and cfg.get("enabled"))
76
+
77
+
78
+ def take_static_crop(image: np.ndarray, crop_parameters: Dict[str, int]) -> np.ndarray:
79
+ height, width = image.shape[:2]
80
+ x_min = int(crop_parameters["x_min"] / 100 * width)
81
+ y_min = int(crop_parameters["y_min"] / 100 * height)
82
+ x_max = int(crop_parameters["x_max"] / 100 * width)
83
+ y_max = int(crop_parameters["y_max"] / 100 * height)
84
+ return image[y_min:y_max, x_min:x_max, :]
85
+
86
+
87
+ def apply_grayscale_conversion(image: np.ndarray) -> np.ndarray:
88
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
89
+ return cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
90
+
91
+
92
+ def apply_contrast_stretching(image: np.ndarray) -> np.ndarray:
93
+ p2, p98 = np.percentile(image, (2, 98))
94
+ image = np.clip(image, p2, p98)
95
+ if p98 - p2 > 0:
96
+ image = (image - p2) * (255.0 / (p98 - p2))
97
+ return image.astype(np.uint8)
98
+
99
+
100
+ def apply_histogram_equalisation(image: np.ndarray) -> np.ndarray:
101
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
102
+ image = cv2.equalizeHist(image)
103
+ return cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
104
+
105
+
106
+ def apply_adaptive_equalisation(image: np.ndarray) -> np.ndarray:
107
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
108
+ clahe = cv2.createCLAHE(clipLimit=0.03, tileGridSize=(8, 8))
109
+ image = clahe.apply(image)
110
+ return cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
111
+
112
+
113
+ def apply_preproc(image: np.ndarray, preproc: Dict[str, Any]) -> Tuple[np.ndarray, Tuple[int, int]]:
114
+ h, w = image.shape[:2]
115
+ img_dims = (h, w)
116
+ if static_crop_should_be_applied(preproc):
117
+ image = take_static_crop(image, preproc["static-crop"])
118
+ if preproc.get("contrast", {}).get("enabled"):
119
+ ctype = preproc.get("contrast", {}).get("type")
120
+ if ctype == "Contrast Stretching":
121
+ image = apply_contrast_stretching(image)
122
+ elif ctype == "Histogram Equalization":
123
+ image = apply_histogram_equalisation(image)
124
+ elif ctype == "Adaptive Equalization":
125
+ image = apply_adaptive_equalisation(image)
126
+ if preproc.get("grayscale", {}).get("enabled"):
127
+ image = apply_grayscale_conversion(image)
128
+ return image, img_dims
129
+
130
+
131
+ def resize_image_keeping_aspect_ratio(image: np.ndarray, desired_size: Tuple[int, int]) -> np.ndarray:
132
+ height, width = image.shape[:2]
133
+ ratio = min(desired_size[1] / height, desired_size[0] / width)
134
+ new_width = int(width * ratio)
135
+ new_height = int(height * ratio)
136
+ return cv2.resize(image, (new_width, new_height))
137
+
138
+
139
+ def letterbox_image(image: np.ndarray, desired_size: Tuple[int, int], color: Tuple[int, int, int]) -> np.ndarray:
140
+ resized = resize_image_keeping_aspect_ratio(image, desired_size)
141
+ new_height, new_width = resized.shape[:2]
142
+ top = (desired_size[1] - new_height) // 2
143
+ bottom = desired_size[1] - new_height - top
144
+ left = (desired_size[0] - new_width) // 2
145
+ right = desired_size[0] - new_width - left
146
+ return cv2.copyMakeBorder(resized, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)
147
+
148
+
149
+ def get_resize_method(preproc: Dict[str, Any]) -> str:
150
+ resize = preproc.get("resize")
151
+ if not resize:
152
+ return "Stretch to"
153
+ method = resize.get("format", "Stretch to")
154
+ if method in {"Fit (reflect edges) in", "Fit within", "Fill (with center crop) in"}:
155
+ return "Fit (black edges) in"
156
+ if method not in {"Stretch to", "Fit (black edges) in", "Fit (white edges) in", "Fit (grey edges) in"}:
157
+ return "Stretch to"
158
+ return method
159
+
160
+
161
+ def preprocess_image(image: Any, preproc: Dict[str, Any], input_hw: Tuple[int, int]) -> Tuple[np.ndarray, Tuple[int, int]]:
162
+ np_image, is_bgr = load_image(image)
163
+ processed, img_dims = apply_preproc(np_image, preproc)
164
+ resize_method = get_resize_method(preproc)
165
+ h, w = input_hw
166
+ if resize_method == "Stretch to":
167
+ resized = cv2.resize(processed, (w, h))
168
+ elif resize_method == "Fit (white edges) in":
169
+ resized = letterbox_image(processed, (w, h), (255, 255, 255))
170
+ elif resize_method == "Fit (grey edges) in":
171
+ resized = letterbox_image(processed, (w, h), (114, 114, 114))
172
+ else:
173
+ resized = letterbox_image(processed, (w, h), (0, 0, 0))
174
+ if is_bgr:
175
+ resized = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB)
176
+ img_in = resized.astype(np.float32)
177
+ img_in = np.transpose(img_in, (2, 0, 1))
178
+ img_in = np.expand_dims(img_in, axis=0)
179
+ return img_in, img_dims
180
+
181
+
182
+ def sigmoid(x: np.ndarray) -> np.ndarray:
183
+ return 1.0 / (1.0 + np.exp(-x))
184
+
185
+
186
+ def non_max_suppression_fast(boxes: np.ndarray, overlap_thresh: float) -> List[np.ndarray]:
187
+ if len(boxes) == 0:
188
+ return []
189
+ if boxes.dtype.kind == "i":
190
+ boxes = boxes.astype("float")
191
+ pick = []
192
+ x1 = boxes[:, 0]
193
+ y1 = boxes[:, 1]
194
+ x2 = boxes[:, 2]
195
+ y2 = boxes[:, 3]
196
+ conf = boxes[:, 4]
197
+ area = (x2 - x1 + 1) * (y2 - y1 + 1)
198
+ idxs = np.argsort(conf)
199
+ while len(idxs) > 0:
200
+ last = len(idxs) - 1
201
+ i = idxs[last]
202
+ pick.append(i)
203
+ xx1 = np.maximum(x1[i], x1[idxs[:last]])
204
+ yy1 = np.maximum(y1[i], y1[idxs[:last]])
205
+ xx2 = np.minimum(x2[i], x2[idxs[:last]])
206
+ yy2 = np.minimum(y2[i], y2[idxs[:last]])
207
+ w = np.maximum(0, xx2 - xx1 + 1)
208
+ h = np.maximum(0, yy2 - yy1 + 1)
209
+ overlap = (w * h) / area[idxs[:last]]
210
+ idxs = np.delete(idxs, np.concatenate(([last], np.where(overlap > overlap_thresh)[0])))
211
+ return boxes[pick].astype("float")
212
+
213
+
214
+ def w_np_non_max_suppression(
215
+ prediction: np.ndarray,
216
+ conf_thresh: float = 0.25,
217
+ iou_thresh: float = 0.45,
218
+ class_agnostic: bool = False,
219
+ max_detections: int = 300,
220
+ max_candidate_detections: int = 3000,
221
+ num_masks: int = 0,
222
+ box_format: str = "xywh",
223
+ ):
224
+ num_classes = prediction.shape[2] - 5 - num_masks
225
+ if box_format == "xywh":
226
+ pred_view = prediction[:, :, :4]
227
+ x1 = pred_view[:, :, 0] - pred_view[:, :, 2] / 2
228
+ y1 = pred_view[:, :, 1] - pred_view[:, :, 3] / 2
229
+ x2 = pred_view[:, :, 0] + pred_view[:, :, 2] / 2
230
+ y2 = pred_view[:, :, 1] + pred_view[:, :, 3] / 2
231
+ pred_view[:, :, 0] = x1
232
+ pred_view[:, :, 1] = y1
233
+ pred_view[:, :, 2] = x2
234
+ pred_view[:, :, 3] = y2
235
+ elif box_format != "xyxy":
236
+ raise ValueError(f"box_format must be 'xywh' or 'xyxy', got {box_format}")
237
+
238
+ batch_predictions = []
239
+ for np_image_pred in prediction:
240
+ np_conf_mask = np_image_pred[:, 4] >= conf_thresh
241
+ if not np.any(np_conf_mask):
242
+ batch_predictions.append([])
243
+ continue
244
+ np_image_pred = np_image_pred[np_conf_mask]
245
+ if np_image_pred.shape[0] == 0:
246
+ batch_predictions.append([])
247
+ continue
248
+ cls_confs = np_image_pred[:, 5 : num_classes + 5]
249
+ if cls_confs.shape[1] == 0:
250
+ batch_predictions.append([])
251
+ continue
252
+ np_class_conf = np.max(cls_confs, axis=1, keepdims=True)
253
+ np_class_pred = np.argmax(cls_confs, axis=1, keepdims=True)
254
+ if num_masks > 0:
255
+ np_mask_pred = np_image_pred[:, 5 + num_classes :]
256
+ np_detections = np.concatenate(
257
+ [
258
+ np_image_pred[:, :5],
259
+ np_class_conf,
260
+ np_class_pred.astype(np.float32),
261
+ np_mask_pred,
262
+ ],
263
+ axis=1,
264
+ )
265
+ else:
266
+ np_detections = np.concatenate(
267
+ [np_image_pred[:, :5], np_class_conf, np_class_pred.astype(np.float32)],
268
+ axis=1,
269
+ )
270
+ filtered_predictions = []
271
+ if class_agnostic:
272
+ sorted_indices = np.argsort(-np_detections[:, 4])
273
+ np_detections_sorted = np_detections[sorted_indices]
274
+ filtered_predictions.extend(non_max_suppression_fast(np_detections_sorted, iou_thresh))
275
+ else:
276
+ np_unique_labels = np.unique(np_class_pred)
277
+ for c in np_unique_labels:
278
+ class_mask = np.atleast_1d(np_class_pred.squeeze() == c)
279
+ np_detections_class = np_detections[class_mask]
280
+ if np_detections_class.shape[0] == 0:
281
+ continue
282
+ sorted_indices = np.argsort(-np_detections_class[:, 4])
283
+ np_detections_sorted = np_detections_class[sorted_indices]
284
+ filtered_predictions.extend(non_max_suppression_fast(np_detections_sorted, iou_thresh))
285
+
286
+ if filtered_predictions:
287
+ filtered_np = np.array(filtered_predictions)
288
+ idx = np.argsort(-filtered_np[:, 4])
289
+ filtered_np = filtered_np[idx]
290
+ if len(filtered_np) > max_detections:
291
+ filtered_np = filtered_np[:max_detections]
292
+ batch_predictions.append(list(filtered_np))
293
+ else:
294
+ batch_predictions.append([])
295
+ return batch_predictions
296
+
297
+
298
+ def get_static_crop_dimensions(orig_shape: Tuple[int, int], preproc: dict) -> Tuple[Tuple[int, int], Tuple[int, int]]:
299
+ if not static_crop_should_be_applied(preproc):
300
+ return (0, 0), orig_shape
301
+ crop = preproc["static-crop"]
302
+ x_min, y_min, x_max, y_max = (crop[k] / 100.0 for k in ["x_min", "y_min", "x_max", "y_max"])
303
+ crop_shift_x, crop_shift_y = (round(x_min * orig_shape[1]), round(y_min * orig_shape[0]))
304
+ cropped_percent_x = x_max - x_min
305
+ cropped_percent_y = y_max - y_min
306
+ new_shape = (round(orig_shape[0] * cropped_percent_y), round(orig_shape[1] * cropped_percent_x))
307
+ return (crop_shift_x, crop_shift_y), new_shape
308
+
309
+
310
+ def post_process_bboxes(
311
+ predictions: List[List[List[float]]],
312
+ infer_shape: Tuple[int, int],
313
+ img_dims: List[Tuple[int, int]],
314
+ preproc: dict,
315
+ resize_method: str,
316
+ ) -> List[List[List[float]]]:
317
+ scaled_predictions = []
318
+ for i, batch_predictions in enumerate(predictions):
319
+ if len(batch_predictions) == 0:
320
+ scaled_predictions.append([])
321
+ continue
322
+ np_batch_predictions = np.array(batch_predictions)
323
+ predicted_bboxes = np_batch_predictions[:, :4]
324
+ (crop_shift_x, crop_shift_y), origin_shape = get_static_crop_dimensions(img_dims[i], preproc)
325
+ if resize_method == "Stretch to":
326
+ scale_height = origin_shape[0] / infer_shape[0]
327
+ scale_width = origin_shape[1] / infer_shape[1]
328
+ predicted_bboxes[:, 0] *= scale_width
329
+ predicted_bboxes[:, 2] *= scale_width
330
+ predicted_bboxes[:, 1] *= scale_height
331
+ predicted_bboxes[:, 3] *= scale_height
332
+ else:
333
+ scale = min(infer_shape[0] / origin_shape[0], infer_shape[1] / origin_shape[1])
334
+ inter_h = round(origin_shape[0] * scale)
335
+ inter_w = round(origin_shape[1] * scale)
336
+ pad_x = (infer_shape[1] - inter_w) / 2
337
+ pad_y = (infer_shape[0] - inter_h) / 2
338
+ predicted_bboxes[:, 0] -= pad_x
339
+ predicted_bboxes[:, 2] -= pad_x
340
+ predicted_bboxes[:, 1] -= pad_y
341
+ predicted_bboxes[:, 3] -= pad_y
342
+ predicted_bboxes /= scale
343
+ predicted_bboxes[:, 0] = np.round(np.clip(predicted_bboxes[:, 0], 0, origin_shape[1]))
344
+ predicted_bboxes[:, 2] = np.round(np.clip(predicted_bboxes[:, 2], 0, origin_shape[1]))
345
+ predicted_bboxes[:, 1] = np.round(np.clip(predicted_bboxes[:, 1], 0, origin_shape[0]))
346
+ predicted_bboxes[:, 3] = np.round(np.clip(predicted_bboxes[:, 3], 0, origin_shape[0]))
347
+ predicted_bboxes[:, 0] += crop_shift_x
348
+ predicted_bboxes[:, 2] += crop_shift_x
349
+ predicted_bboxes[:, 1] += crop_shift_y
350
+ predicted_bboxes[:, 3] += crop_shift_y
351
+ np_batch_predictions[:, :4] = predicted_bboxes
352
+ scaled_predictions.append(np_batch_predictions.tolist())
353
+ return scaled_predictions
354
+
355
+
356
+ def post_process_keypoints(
357
+ predictions: List[List[List[float]]],
358
+ keypoints_start_index: int,
359
+ infer_shape: Tuple[int, int],
360
+ img_dims: List[Tuple[int, int]],
361
+ preproc: dict,
362
+ resize_method: str,
363
+ ) -> List[List[List[float]]]:
364
+ scaled_predictions = []
365
+ for i, batch_predictions in enumerate(predictions):
366
+ if len(batch_predictions) == 0:
367
+ scaled_predictions.append([])
368
+ continue
369
+ np_batch_predictions = np.array(batch_predictions)
370
+ keypoints = np_batch_predictions[:, keypoints_start_index:]
371
+ (crop_shift_x, crop_shift_y), origin_shape = get_static_crop_dimensions(img_dims[i], preproc)
372
+ if resize_method == "Stretch to":
373
+ scale_width = origin_shape[1] / infer_shape[1]
374
+ scale_height = origin_shape[0] / infer_shape[0]
375
+ for k in range(keypoints.shape[1] // 3):
376
+ keypoints[:, k * 3] *= scale_width
377
+ keypoints[:, k * 3 + 1] *= scale_height
378
+ else:
379
+ scale = min(infer_shape[0] / origin_shape[0], infer_shape[1] / origin_shape[1])
380
+ inter_w = int(origin_shape[1] * scale)
381
+ inter_h = int(origin_shape[0] * scale)
382
+ pad_x = (infer_shape[1] - inter_w) / 2
383
+ pad_y = (infer_shape[0] - inter_h) / 2
384
+ for k in range(keypoints.shape[1] // 3):
385
+ keypoints[:, k * 3] -= pad_x
386
+ keypoints[:, k * 3] /= scale
387
+ keypoints[:, k * 3 + 1] -= pad_y
388
+ keypoints[:, k * 3 + 1] /= scale
389
+ for k in range(keypoints.shape[1] // 3):
390
+ keypoints[:, k * 3] = np.round(np.clip(keypoints[:, k * 3], 0, origin_shape[1]))
391
+ keypoints[:, k * 3 + 1] = np.round(np.clip(keypoints[:, k * 3 + 1], 0, origin_shape[0]))
392
+ keypoints[:, k * 3] += crop_shift_x
393
+ keypoints[:, k * 3 + 1] += crop_shift_y
394
+ np_batch_predictions[:, keypoints_start_index:] = keypoints
395
+ scaled_predictions.append(np_batch_predictions.tolist())
396
+ return scaled_predictions
397
+
398
+
399
+ def masks2poly(masks: np.ndarray) -> List[np.ndarray]:
400
+ segments = []
401
+ for mask in masks:
402
+ if mask.dtype == np.bool_:
403
+ m_uint8 = mask
404
+ if not m_uint8.flags.c_contiguous:
405
+ m_uint8 = np.ascontiguousarray(m_uint8)
406
+ m_uint8 = m_uint8.view(np.uint8)
407
+ elif mask.dtype == np.uint8:
408
+ m_uint8 = mask if mask.flags.c_contiguous else np.ascontiguousarray(mask)
409
+ else:
410
+ m_bool = mask > 0
411
+ if not m_bool.flags.c_contiguous:
412
+ m_bool = np.ascontiguousarray(m_bool)
413
+ m_uint8 = m_bool.view(np.uint8)
414
+ if not np.any(m_uint8):
415
+ segments.append(np.zeros((0, 2), dtype=np.float32))
416
+ continue
417
+ contours = cv2.findContours(m_uint8, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
418
+ if contours:
419
+ contours = np.array(contours[np.array([len(x) for x in contours]).argmax()]).reshape(-1, 2)
420
+ else:
421
+ contours = np.zeros((0, 2))
422
+ segments.append(contours.astype("float32"))
423
+ return segments
424
+
425
+
426
+ def post_process_polygons(
427
+ origin_shape: Tuple[int, int],
428
+ polys: List[List[Tuple[float, float]]],
429
+ infer_shape: Tuple[int, int],
430
+ preproc: dict,
431
+ resize_method: str,
432
+ ) -> List[List[Tuple[float, float]]]:
433
+ (crop_shift_x, crop_shift_y), origin_shape = get_static_crop_dimensions(origin_shape, preproc)
434
+ new_polys = []
435
+ if resize_method == "Stretch to":
436
+ width_ratio = origin_shape[1] / infer_shape[1]
437
+ height_ratio = origin_shape[0] / infer_shape[0]
438
+ for poly in polys:
439
+ new_polys.append([(p[0] * width_ratio, p[1] * height_ratio) for p in poly])
440
+ else:
441
+ scale = min(infer_shape[0] / origin_shape[0], infer_shape[1] / origin_shape[1])
442
+ inter_w = int(origin_shape[1] * scale)
443
+ inter_h = int(origin_shape[0] * scale)
444
+ pad_x = (infer_shape[1] - inter_w) / 2
445
+ pad_y = (infer_shape[0] - inter_h) / 2
446
+ for poly in polys:
447
+ new_polys.append([((p[0] - pad_x) / scale, (p[1] - pad_y) / scale) for p in poly])
448
+ shifted_polys = []
449
+ for poly in new_polys:
450
+ shifted_polys.append([(p[0] + crop_shift_x, p[1] + crop_shift_y) for p in poly])
451
+ return shifted_polys
452
+
453
+
454
+ def preprocess_segmentation_masks(protos: np.ndarray, masks_in: np.ndarray, shape: Tuple[int, int]) -> np.ndarray:
455
+ c, mh, mw = protos.shape
456
+ masks = protos.astype(np.float32)
457
+ masks = masks.reshape((c, -1))
458
+ masks = masks_in @ masks
459
+ masks = sigmoid(masks)
460
+ masks = masks.reshape((-1, mh, mw))
461
+ gain = min(mh / shape[0], mw / shape[1])
462
+ pad = (mw - shape[1] * gain) / 2, (mh - shape[0] * gain) / 2
463
+ top, left = int(pad[1]), int(pad[0])
464
+ bottom, right = int(mh - pad[1]), int(mw - pad[0])
465
+ return masks[:, top:bottom, left:right]
466
+
467
+
468
+ def crop_mask(masks: np.ndarray, boxes: np.ndarray) -> np.ndarray:
469
+ n, h, w = masks.shape
470
+ x1, y1, x2, y2 = np.split(boxes[:, :, None], 4, 1)
471
+ r = np.arange(w, dtype=x1.dtype)[None, None, :]
472
+ c = np.arange(h, dtype=x1.dtype)[None, :, None]
473
+ masks = masks * ((r >= x1) * (r < x2) * (c >= y1) * (c < y2))
474
+ return masks
475
+
476
+
477
+ def process_mask_accurate(protos: np.ndarray, masks_in: np.ndarray, bboxes: np.ndarray, shape: Tuple[int, int]) -> np.ndarray:
478
+ masks = preprocess_segmentation_masks(protos, masks_in, shape)
479
+ if len(masks.shape) == 2:
480
+ masks = np.expand_dims(masks, axis=0)
481
+ masks = masks.transpose((1, 2, 0))
482
+ masks = cv2.resize(masks, (shape[1], shape[0]), cv2.INTER_LINEAR)
483
+ if len(masks.shape) == 2:
484
+ masks = np.expand_dims(masks, axis=2)
485
+ masks = masks.transpose((2, 0, 1))
486
+ masks = crop_mask(masks, bboxes)
487
+ masks[masks < 0.5] = 0
488
+ return masks
489
+
490
+
491
+ def process_mask_tradeoff(protos: np.ndarray, masks_in: np.ndarray, bboxes: np.ndarray, shape: Tuple[int, int], tradeoff_factor: float) -> np.ndarray:
492
+ c, mh, mw = protos.shape
493
+ masks = preprocess_segmentation_masks(protos, masks_in, shape)
494
+ if len(masks.shape) == 2:
495
+ masks = np.expand_dims(masks, axis=0)
496
+ masks = masks.transpose((1, 2, 0))
497
+ ih, iw = shape
498
+ h = int(mh * (1 - tradeoff_factor) + ih * tradeoff_factor)
499
+ w = int(mw * (1 - tradeoff_factor) + iw * tradeoff_factor)
500
+ if tradeoff_factor != 0:
501
+ masks = cv2.resize(masks, (w, h), cv2.INTER_LINEAR)
502
+ if len(masks.shape) == 2:
503
+ masks = np.expand_dims(masks, axis=2)
504
+ masks = masks.transpose((2, 0, 1))
505
+ c, mh, mw = masks.shape
506
+ scale_x = mw / iw
507
+ scale_y = mh / ih
508
+ bboxes = bboxes.copy()
509
+ bboxes[:, 0] *= scale_x
510
+ bboxes[:, 2] *= scale_x
511
+ bboxes[:, 1] *= scale_y
512
+ bboxes[:, 3] *= scale_y
513
+ masks = crop_mask(masks, bboxes)
514
+ masks[masks < 0.5] = 0
515
+ return masks
516
+
517
+
518
+ def process_mask_fast(protos: np.ndarray, masks_in: np.ndarray, bboxes: np.ndarray, shape: Tuple[int, int]) -> np.ndarray:
519
+ ih, iw = shape
520
+ c, mh, mw = protos.shape
521
+ masks = preprocess_segmentation_masks(protos, masks_in, shape)
522
+ scale_x = mw / iw
523
+ scale_y = mh / ih
524
+ bboxes = bboxes.copy()
525
+ bboxes[:, 0] *= scale_x
526
+ bboxes[:, 2] *= scale_x
527
+ bboxes[:, 1] *= scale_y
528
+ bboxes[:, 3] *= scale_y
529
+ masks = crop_mask(masks, bboxes)
530
+ masks[masks < 0.5] = 0
531
+ return masks
532
+
533
+
534
+ def load_onnx_session(onnx_path: str, providers: List[str] = None) -> ort.InferenceSession:
535
+ if providers is None:
536
+ providers = ["CUDAExecutionProvider", "CPUExecutionProvider"]
537
+ return ort.InferenceSession(onnx_path, providers=providers)
538
+
539
+
540
+ def find_default_onnx(data_dir: str) -> str:
541
+ candidates = [f for f in os.listdir(data_dir) if f.lower().endswith(".onnx")]
542
+ candidates.sort()
543
+ if not candidates:
544
+ raise FileNotFoundError(f"No .onnx file found in {data_dir}")
545
+ if len(candidates) > 1:
546
+ # Prefer weights.onnx if present.
547
+ for name in candidates:
548
+ if name.lower() == "weights.onnx":
549
+ return os.path.join(data_dir, name)
550
+ return os.path.join(data_dir, candidates[0])
551
+
552
+
553
+ def get_input_hw(session: ort.InferenceSession, preproc: Dict[str, Any]) -> Tuple[int, int]:
554
+ inputs = session.get_inputs()[0]
555
+ shape = inputs.shape
556
+ h, w = shape[2], shape[3]
557
+ if isinstance(h, str) or isinstance(w, str) or h is None or w is None:
558
+ resize = preproc.get("resize") if preproc else None
559
+ if resize:
560
+ h = int(resize.get("height", 640))
561
+ w = int(resize.get("width", 640))
562
+ else:
563
+ h, w = 640, 640
564
+ return int(h), int(w)
565
+
566
+
567
+ def build_meta(data_dir: str, session: ort.InferenceSession) -> Dict[str, Any]:
568
+ environment = load_environment(data_dir)
569
+ preproc = environment.get("PREPROCESSING") or {}
570
+ class_names = load_class_names(data_dir, environment)
571
+ resize_method = get_resize_method(preproc)
572
+ input_hw = get_input_hw(session, preproc)
573
+ keypoints_metadata = load_keypoints_metadata(data_dir)
574
+ return {
575
+ "environment": environment,
576
+ "preproc": preproc,
577
+ "class_names": class_names,
578
+ "resize_method": resize_method,
579
+ "input_hw": input_hw,
580
+ "keypoints_metadata": keypoints_metadata,
581
+ }
582
+
583
+
584
+ def normalize_rgb(img_in: np.ndarray, means: List[float], stds: List[float]) -> np.ndarray:
585
+ img_in = img_in.astype(np.float32)
586
+ img_in /= 255.0
587
+ img_in[:, 0, :, :] = (img_in[:, 0, :, :] - means[0]) / stds[0]
588
+ img_in[:, 1, :, :] = (img_in[:, 1, :, :] - means[1]) / stds[1]
589
+ img_in[:, 2, :, :] = (img_in[:, 2, :, :] - means[2]) / stds[2]
590
+ return img_in
591
+
592
+
593
+ MODEL_TASK_TYPE = "object-detection"
594
+
595
+
596
+ def preprocess_for_model(image: Any, meta: Dict[str, Any]) -> Tuple[np.ndarray, Tuple[int, int]]:
597
+ img_in, img_dims = preprocess_image(image, meta["preproc"], meta["input_hw"])
598
+ img_in = img_in.astype(np.float32)
599
+ img_in /= 255.0
600
+ return img_in, img_dims
601
+
602
+
603
+ def pack_predictions(predictions: np.ndarray) -> np.ndarray:
604
+ predictions = predictions.transpose(0, 2, 1)
605
+ boxes = predictions[:, :, :4]
606
+ class_confs = predictions[:, :, 4:]
607
+ confs = np.expand_dims(np.max(class_confs, axis=2), axis=2)
608
+ return np.concatenate([boxes, confs, class_confs], axis=2)
609
+
610
+
611
+ def postprocess_predictions(predictions: np.ndarray, meta: Dict[str, Any], img_dims: List[Tuple[int, int]],
612
+ confidence: float = 0.4, iou_threshold: float = 0.3, max_detections: int = 300):
613
+ preds = w_np_non_max_suppression(
614
+ predictions,
615
+ conf_thresh=confidence,
616
+ iou_thresh=iou_threshold,
617
+ class_agnostic=False,
618
+ max_detections=max_detections,
619
+ box_format="xywh",
620
+ )
621
+ infer_shape = meta["input_hw"]
622
+ preds = post_process_bboxes(preds, infer_shape, img_dims, meta["preproc"], meta["resize_method"])
623
+ class_names = meta["class_names"]
624
+ results = []
625
+ for batch_preds in preds:
626
+ batch_out = []
627
+ for pred in batch_preds:
628
+ cls_id = int(pred[6])
629
+ batch_out.append({
630
+ "x": (pred[0] + pred[2]) / 2,
631
+ "y": (pred[1] + pred[3]) / 2,
632
+ "width": pred[2] - pred[0],
633
+ "height": pred[3] - pred[1],
634
+ "confidence": float(pred[4]),
635
+ "class_id": cls_id,
636
+ "class": class_names[cls_id] if cls_id < len(class_names) else str(cls_id),
637
+ })
638
+ results.append(batch_out)
639
+ return results
640
+
641
+
642
+ def load_model(onnx_path: str | None = None, data_dir: str | None = None):
643
+ data_dir = data_dir or os.path.dirname(os.path.abspath(__file__))
644
+ onnx_path = onnx_path or find_default_onnx(data_dir)
645
+ session = load_onnx_session(onnx_path)
646
+ meta = build_meta(data_dir, session)
647
+ model_type_fn = globals().get("load_model_type")
648
+ model_type = model_type_fn(data_dir) if callable(model_type_fn) else "unknown"
649
+ return {"session": session, "meta": meta, "model_type": model_type}
650
+
651
+
652
+ def run_model(model: Any, image: Any = None, onnx_path: str | None = None, data_dir: str | None = None):
653
+ if image is None:
654
+ image = model
655
+ model = load_model(onnx_path=onnx_path, data_dir=data_dir)
656
+ session = model["session"]
657
+ meta = model["meta"]
658
+ model_type = model["model_type"]
659
+
660
+ img_in, img_dims = preprocess_for_model(image, meta)
661
+ input_name = session.get_inputs()[0].name
662
+ outputs = session.run(None, {input_name: img_in})
663
+ predictions = pack_predictions(outputs[0])
664
+ return postprocess_predictions(predictions, meta, [img_dims])
665
+
666
+
667
+ def main():
668
+ if len(sys.argv) < 2:
669
+ print("Usage: main.py <image_path> [onnx_path]", file=sys.stderr)
670
+ sys.exit(1)
671
+ image_path = sys.argv[1]
672
+ data_dir = os.path.dirname(os.path.abspath(__file__))
673
+ onnx_path = sys.argv[2] if len(sys.argv) > 2 else find_default_onnx(data_dir)
674
+ results = run_model(image_path, onnx_path=onnx_path, data_dir=data_dir)
675
+ print(json.dumps(results, indent=2))
676
+
677
+
678
+ if __name__ == "__main__":
679
+ main()
miner.py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """
28
+ Auto-generated by subnet_bridge from a Manako element repo.
29
+ This miner is intentionally self-contained for chute import restrictions.
30
+ """
31
+
32
+ def __init__(self, path_hf_repo: Path) -> None:
33
+ self.path_hf_repo = path_hf_repo
34
+ self.class_names = ['road sign']
35
+ self.session = ort.InferenceSession(
36
+ str(path_hf_repo / "weights.onnx"),
37
+ providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
38
+ )
39
+ self.input_name = self.session.get_inputs()[0].name
40
+ input_shape = self.session.get_inputs()[0].shape
41
+ # expected [N, C, H, W]
42
+ self.input_h = int(input_shape[2])
43
+ self.input_w = int(input_shape[3])
44
+ self.conf_threshold = 0.25
45
+ self.iou_threshold = 0.45
46
+
47
+ def __repr__(self) -> str:
48
+ return f"ONNX Miner session={type(self.session).__name__} classes={len(self.class_names)}"
49
+
50
+ def _preprocess(self, image_bgr: ndarray) -> tuple[np.ndarray, tuple[int, int]]:
51
+ h, w = image_bgr.shape[:2]
52
+ rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
53
+ resized = cv2.resize(rgb, (self.input_w, self.input_h))
54
+ x = resized.astype(np.float32) / 255.0
55
+ x = np.transpose(x, (2, 0, 1))[None, ...]
56
+ return x, (h, w)
57
+
58
+ def _normalize_predictions(self, raw: np.ndarray) -> np.ndarray:
59
+ # Common ultralytics export shapes:
60
+ # - [1, C, N] where C=4+num_classes
61
+ # - [1, N, C]
62
+ pred = raw[0]
63
+ if pred.ndim != 2:
64
+ raise ValueError(f"Unexpected prediction shape: {raw.shape}")
65
+ if pred.shape[0] < pred.shape[1]:
66
+ pred = pred.transpose(1, 0)
67
+ return pred
68
+
69
+ def _nms(self, dets: list[tuple[float, float, float, float, float, int]]) -> list[tuple[float, float, float, float, float, int]]:
70
+ if not dets:
71
+ return []
72
+
73
+ boxes = np.array([[d[0], d[1], d[2], d[3]] for d in dets], dtype=np.float32)
74
+ scores = np.array([d[4] for d in dets], dtype=np.float32)
75
+ order = scores.argsort()[::-1]
76
+ keep = []
77
+
78
+ while order.size > 0:
79
+ i = order[0]
80
+ keep.append(i)
81
+
82
+ xx1 = np.maximum(boxes[i, 0], boxes[order[1:], 0])
83
+ yy1 = np.maximum(boxes[i, 1], boxes[order[1:], 1])
84
+ xx2 = np.minimum(boxes[i, 2], boxes[order[1:], 2])
85
+ yy2 = np.minimum(boxes[i, 3], boxes[order[1:], 3])
86
+
87
+ w = np.maximum(0.0, xx2 - xx1)
88
+ h = np.maximum(0.0, yy2 - yy1)
89
+ inter = w * h
90
+
91
+ area_i = (boxes[i, 2] - boxes[i, 0]) * (boxes[i, 3] - boxes[i, 1])
92
+ area_rest = (boxes[order[1:], 2] - boxes[order[1:], 0]) * (boxes[order[1:], 3] - boxes[order[1:], 1])
93
+ union = np.maximum(area_i + area_rest - inter, 1e-6)
94
+ iou = inter / union
95
+
96
+ remaining = np.where(iou <= self.iou_threshold)[0]
97
+ order = order[remaining + 1]
98
+
99
+ return [dets[idx] for idx in keep]
100
+
101
+ def _infer_single(self, image_bgr: ndarray) -> list[BoundingBox]:
102
+ inp, (orig_h, orig_w) = self._preprocess(image_bgr)
103
+ out = self.session.run(None, {self.input_name: inp})[0]
104
+ pred = self._normalize_predictions(out)
105
+
106
+ if pred.shape[1] < 5:
107
+ return []
108
+
109
+ boxes = pred[:, :4]
110
+ cls_scores = pred[:, 4:]
111
+
112
+ if cls_scores.shape[1] == 0:
113
+ return []
114
+
115
+ cls_ids = np.argmax(cls_scores, axis=1)
116
+ confs = np.max(cls_scores, axis=1)
117
+ keep = confs >= self.conf_threshold
118
+
119
+ boxes = boxes[keep]
120
+ confs = confs[keep]
121
+ cls_ids = cls_ids[keep]
122
+
123
+ if boxes.shape[0] == 0:
124
+ return []
125
+
126
+ sx = orig_w / float(self.input_w)
127
+ sy = orig_h / float(self.input_h)
128
+
129
+ dets: list[tuple[float, float, float, float, float, int]] = []
130
+ for i in range(boxes.shape[0]):
131
+ cx, cy, bw, bh = boxes[i].tolist()
132
+ x1 = (cx - bw / 2.0) * sx
133
+ y1 = (cy - bh / 2.0) * sy
134
+ x2 = (cx + bw / 2.0) * sx
135
+ y2 = (cy + bh / 2.0) * sy
136
+ dets.append((x1, y1, x2, y2, float(confs[i]), int(cls_ids[i])))
137
+
138
+ dets = self._nms(dets)
139
+
140
+ out_boxes: list[BoundingBox] = []
141
+ for x1, y1, x2, y2, conf, cls_id in dets:
142
+ ix1 = max(0, min(orig_w, math.floor(x1)))
143
+ iy1 = max(0, min(orig_h, math.floor(y1)))
144
+ ix2 = max(0, min(orig_w, math.ceil(x2)))
145
+ iy2 = max(0, min(orig_h, math.ceil(y2)))
146
+ out_boxes.append(
147
+ BoundingBox(
148
+ x1=ix1,
149
+ y1=iy1,
150
+ x2=ix2,
151
+ y2=iy2,
152
+ cls_id=cls_id,
153
+ conf=max(0.0, min(1.0, conf)),
154
+ )
155
+ )
156
+ return out_boxes
157
+
158
+ def predict_batch(
159
+ self,
160
+ batch_images: list[ndarray],
161
+ offset: int,
162
+ n_keypoints: int,
163
+ ) -> list[TVFrameResult]:
164
+ results: list[TVFrameResult] = []
165
+ for idx, image in enumerate(batch_images):
166
+ boxes = self._infer_single(image)
167
+ keypoints = [(0, 0) for _ in range(max(0, int(n_keypoints)))]
168
+ results.append(
169
+ TVFrameResult(
170
+ frame_id=offset + idx,
171
+ boxes=boxes,
172
+ keypoints=keypoints,
173
+ )
174
+ )
175
+ return results
model_type.json ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {
2
+ "task_type": "object-detection",
3
+ "model_type": "yolov11-nano"
4
+ }
pyproject.toml ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "onnx-runner-detection"
3
+ version = "0.1.0"
4
+ requires-python = ">=3.9"
5
+
6
+ dependencies = [
7
+ "numpy>=1.23",
8
+ "onnxruntime>=1.16",
9
+ "opencv-python>=4.7",
10
+ "pillow>=9.5",
11
+ ]
uv.lock ADDED
The diff for this file is too large to render. See raw diff
 
weights.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5448a66e81303a5ed80ae865d0281a7c23549b90fa7a510e3ac6c465258345e7
3
+ size 10604125