Add Manako element wrapper
Browse files- README.md +31 -0
- class_names.txt +12 -0
- main.py +679 -0
- model_type.json +4 -0
- pyproject.toml +11 -0
- uv.lock +0 -0
- weights.onnx +3 -0
README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
tags:
|
| 3 |
+
- element_type:detect
|
| 4 |
+
- model:yolov11-nano
|
| 5 |
+
- object:big bus
|
| 6 |
+
- object:big truck
|
| 7 |
+
- object:bus-l-
|
| 8 |
+
- object:bus-s-
|
| 9 |
+
- object:car
|
| 10 |
+
- object:mid truck
|
| 11 |
+
- object:small bus
|
| 12 |
+
- object:small truck
|
| 13 |
+
- object:truck-l-
|
| 14 |
+
- object:truck-m-
|
| 15 |
+
- object:truck-s-
|
| 16 |
+
- object:truck-xl-
|
| 17 |
+
|
| 18 |
+
manako:
|
| 19 |
+
description: Roboflow - generated by element_trainer service to detect big bus, big truck, bus-l-, bus-s-, car, mid truck, small bus, small truck, truck-l-, truck-m-, truck-s-, truck-xl-
|
| 20 |
+
source: element_trainer/vehicles-baseline
|
| 21 |
+
prompt_hints:
|
| 22 |
+
input_payload:
|
| 23 |
+
- name: frame
|
| 24 |
+
type: image
|
| 25 |
+
description: RGB frame
|
| 26 |
+
output_payload:
|
| 27 |
+
- name: detections
|
| 28 |
+
type: detections
|
| 29 |
+
description: List of detections
|
| 30 |
+
evaluation_score:
|
| 31 |
+
---
|
class_names.txt
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|
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:d8f5bb56522307a8d1e9d33e670953f9629c7c2ac1cd8c00f2c57e44355f0ead
|
| 3 |
+
size 10612870
|