MTerryJack commited on
Commit
01451db
·
verified ·
1 Parent(s): 45c4687

subnet_bridge: regenerate main.py for explicit local miner loading

Browse files
Files changed (1) hide show
  1. main.py +46 -2
main.py CHANGED
@@ -1,5 +1,6 @@
1
  from __future__ import annotations
2
 
 
3
  import json
4
  import os
5
  import sys
@@ -8,7 +9,21 @@ from typing import Any
8
 
9
  import cv2
10
 
11
- from miner import Miner
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
 
14
  CLASS_NAMES = ['football', 'player', 'pitch']
@@ -66,13 +81,42 @@ def load_model(onnx_path: str | None = None, data_dir: str | None = None):
66
  }
67
 
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  def run_model(model: Any, image: Any = None, onnx_path: str | None = None, data_dir: str | None = None):
70
  del onnx_path
71
  if image is None:
72
  image = model
73
  model = load_model(data_dir=data_dir)
74
  miner = model["miner"]
75
- results = miner.predict_batch([image], offset=0, n_keypoints=0)
76
  if not results:
77
  return [[]]
78
  frame_boxes = _extract_boxes(results[0])
 
1
  from __future__ import annotations
2
 
3
+ import importlib.util
4
  import json
5
  import os
6
  import sys
 
9
 
10
  import cv2
11
 
12
+
13
+ def _load_local_miner_class():
14
+ miner_path = Path(__file__).resolve().parent / "miner.py"
15
+ spec = importlib.util.spec_from_file_location("manako_bridge_local_miner", str(miner_path))
16
+ if spec is None or spec.loader is None:
17
+ raise RuntimeError(f"Could not load miner module from {miner_path}")
18
+ module = importlib.util.module_from_spec(spec)
19
+ spec.loader.exec_module(module)
20
+ miner_class = getattr(module, "Miner", None)
21
+ if miner_class is None:
22
+ raise RuntimeError(f"miner.py does not export Miner in {miner_path}")
23
+ return miner_class
24
+
25
+
26
+ Miner = _load_local_miner_class()
27
 
28
 
29
  CLASS_NAMES = ['football', 'player', 'pitch']
 
81
  }
82
 
83
 
84
+ def _candidate_keypoint_counts(miner: Any) -> list[int]:
85
+ counts: list[int] = [0]
86
+ for attr in ("n_keypoints", "num_keypoints", "keypoint_count", "num_joints"):
87
+ value = getattr(miner, attr, None)
88
+ if isinstance(value, int) and value > 0:
89
+ counts.append(value)
90
+ counts.append(32)
91
+
92
+ seen: set[int] = set()
93
+ ordered: list[int] = []
94
+ for count in counts:
95
+ if count in seen:
96
+ continue
97
+ seen.add(count)
98
+ ordered.append(count)
99
+ return ordered
100
+
101
+
102
+ def _predict_batch_with_fallbacks(miner: Any, image: Any) -> list[Any]:
103
+ errors: list[str] = []
104
+ for n_keypoints in _candidate_keypoint_counts(miner):
105
+ try:
106
+ return miner.predict_batch([image], offset=0, n_keypoints=n_keypoints)
107
+ except Exception as exc:
108
+ errors.append(f"n_keypoints={n_keypoints} -> {exc}")
109
+ continue
110
+ raise RuntimeError("predict_batch failed for all keypoint candidates: " + " | ".join(errors))
111
+
112
+
113
  def run_model(model: Any, image: Any = None, onnx_path: str | None = None, data_dir: str | None = None):
114
  del onnx_path
115
  if image is None:
116
  image = model
117
  model = load_model(data_dir=data_dir)
118
  miner = model["miner"]
119
+ results = _predict_batch_with_fallbacks(miner, image)
120
  if not results:
121
  return [[]]
122
  frame_boxes = _extract_boxes(results[0])