MTerryJack commited on
Commit
82dd19f
·
verified ·
1 Parent(s): c2d2e10

subnet_bridge: copy winning miner repo into library

Browse files
Files changed (1) hide show
  1. main.py +90 -6
main.py CHANGED
@@ -51,6 +51,48 @@ def _extract_boxes(frame_result: Any) -> list[Any]:
51
  return []
52
 
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  def _resolve_runtime_class_names(miner: Any) -> list[str]:
55
  value = getattr(miner, "class_names", None)
56
  if isinstance(value, (list, tuple)):
@@ -63,13 +105,25 @@ def _resolve_runtime_class_names(miner: Any) -> list[str]:
63
  def _to_detection(box: Any, class_names: list[str]) -> dict[str, Any]:
64
  payload = _to_dict(box)
65
  cls_id = int(payload.get("cls_id", 0))
66
- x1 = float(payload.get("x1", 0.0))
67
- y1 = float(payload.get("y1", 0.0))
68
- x2 = float(payload.get("x2", 0.0))
69
- y2 = float(payload.get("y2", 0.0))
 
 
 
 
 
 
 
 
 
 
 
70
  width = max(0.0, x2 - x1)
71
  height = max(0.0, y2 - y1)
72
- return {
 
73
  "x": x1 + width / 2.0,
74
  "y": y1 + height / 2.0,
75
  "width": width,
@@ -78,6 +132,18 @@ def _to_detection(box: Any, class_names: list[str]) -> dict[str, Any]:
78
  "class_id": cls_id,
79
  "class": class_names[cls_id] if 0 <= cls_id < len(class_names) else str(cls_id),
80
  }
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
 
83
  def _normalize_image_for_miner(image: Any) -> Any:
@@ -164,7 +230,25 @@ def run_model(model: Any, image: Any = None, onnx_path: str | None = None, data_
164
  if not results:
165
  return [[]]
166
  frame_boxes = _extract_boxes(results[0])
167
- detections = [_to_detection(box, class_names) for box in frame_boxes]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
  return [detections]
169
 
170
 
 
51
  return []
52
 
53
 
54
+ def _extract_polygons(frame_result: Any) -> list[Any]:
55
+ frame = _to_dict(frame_result)
56
+ polygons = frame.get("polygons", [])
57
+ if isinstance(polygons, list):
58
+ return polygons
59
+ polygon = frame.get("polygon", [])
60
+ if isinstance(polygon, list):
61
+ return [polygon]
62
+ return []
63
+
64
+
65
+ def _normalize_polygon_item(polygon: Any) -> list[Any]:
66
+ payload = _to_dict(polygon)
67
+ points = payload.get("polygon")
68
+ if isinstance(points, list) and points:
69
+ return points
70
+ points = payload.get("points")
71
+ if isinstance(points, list) and points:
72
+ return points
73
+ if isinstance(polygon, list):
74
+ return polygon
75
+ return []
76
+
77
+
78
+ def _bbox_from_polygon(polygon: Any) -> tuple[float, float, float, float] | None:
79
+ if not isinstance(polygon, list) or not polygon:
80
+ return None
81
+ xs: list[float] = []
82
+ ys: list[float] = []
83
+ for point in polygon:
84
+ if not isinstance(point, (list, tuple)) or len(point) < 2:
85
+ continue
86
+ try:
87
+ xs.append(float(point[0]))
88
+ ys.append(float(point[1]))
89
+ except (TypeError, ValueError):
90
+ continue
91
+ if not xs or not ys:
92
+ return None
93
+ return min(xs), min(ys), max(xs), max(ys)
94
+
95
+
96
  def _resolve_runtime_class_names(miner: Any) -> list[str]:
97
  value = getattr(miner, "class_names", None)
98
  if isinstance(value, (list, tuple)):
 
105
  def _to_detection(box: Any, class_names: list[str]) -> dict[str, Any]:
106
  payload = _to_dict(box)
107
  cls_id = int(payload.get("cls_id", 0))
108
+ polygon = payload.get("polygon")
109
+ if not (isinstance(polygon, list) and polygon):
110
+ polygons = payload.get("polygons")
111
+ if isinstance(polygons, list) and polygons:
112
+ polygon = polygons[0]
113
+ if not (isinstance(polygon, list) and polygon):
114
+ points = payload.get("points")
115
+ if isinstance(points, list) and points:
116
+ polygon = points
117
+
118
+ bbox = _bbox_from_polygon(polygon) if polygon is not None else None
119
+ x1 = float(payload.get("x1", bbox[0] if bbox else 0.0))
120
+ y1 = float(payload.get("y1", bbox[1] if bbox else 0.0))
121
+ x2 = float(payload.get("x2", bbox[2] if bbox else 0.0))
122
+ y2 = float(payload.get("y2", bbox[3] if bbox else 0.0))
123
  width = max(0.0, x2 - x1)
124
  height = max(0.0, y2 - y1)
125
+ detection = {
126
+ "bbox": [x1, y1, x2, y2],
127
  "x": x1 + width / 2.0,
128
  "y": y1 + height / 2.0,
129
  "width": width,
 
132
  "class_id": cls_id,
133
  "class": class_names[cls_id] if 0 <= cls_id < len(class_names) else str(cls_id),
134
  }
135
+ if isinstance(polygon, list) and polygon:
136
+ detection["polygon"] = polygon
137
+ polygons = payload.get("polygons")
138
+ if isinstance(polygons, list) and polygons:
139
+ detection["polygons"] = polygons
140
+ if "polygon" not in detection:
141
+ detection["polygon"] = polygons[0]
142
+ if "polygon" not in detection:
143
+ points = payload.get("points")
144
+ if isinstance(points, list) and points:
145
+ detection["polygon"] = points
146
+ return detection
147
 
148
 
149
  def _normalize_image_for_miner(image: Any) -> Any:
 
230
  if not results:
231
  return [[]]
232
  frame_boxes = _extract_boxes(results[0])
233
+ frame_polygons = _extract_polygons(results[0])
234
+ detections = []
235
+ for idx, box in enumerate(frame_boxes):
236
+ detection = _to_detection(box, class_names)
237
+ box_polygon = _normalize_polygon_item(box)
238
+ if box_polygon:
239
+ detection["polygons"] = [box_polygon]
240
+ if idx < len(frame_polygons):
241
+ polygon = _normalize_polygon_item(frame_polygons[idx])
242
+ if polygon:
243
+ detection["polygon"] = polygon
244
+ detection["polygons"] = [polygon]
245
+ detections.append(detection)
246
+ if not detections and frame_polygons:
247
+ for idx, polygon in enumerate(frame_polygons):
248
+ normalized_polygon = _normalize_polygon_item(polygon)
249
+ detection = _to_detection({"polygon": normalized_polygon}, class_names)
250
+ detection["polygons"] = [normalized_polygon]
251
+ detections.append(detection)
252
  return [detections]
253
 
254