@camstack/addon-vision 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/addons/animal-classifier/index.d.mts +25 -0
- package/dist/addons/animal-classifier/index.d.ts +25 -0
- package/dist/addons/animal-classifier/index.js +652 -0
- package/dist/addons/animal-classifier/index.js.map +1 -0
- package/dist/addons/animal-classifier/index.mjs +10 -0
- package/dist/addons/animal-classifier/index.mjs.map +1 -0
- package/dist/addons/audio-classification/index.d.mts +31 -0
- package/dist/addons/audio-classification/index.d.ts +31 -0
- package/dist/addons/audio-classification/index.js +572 -0
- package/dist/addons/audio-classification/index.js.map +1 -0
- package/dist/addons/audio-classification/index.mjs +8 -0
- package/dist/addons/audio-classification/index.mjs.map +1 -0
- package/dist/addons/bird-global-classifier/index.d.mts +26 -0
- package/dist/addons/bird-global-classifier/index.d.ts +26 -0
- package/dist/addons/bird-global-classifier/index.js +658 -0
- package/dist/addons/bird-global-classifier/index.js.map +1 -0
- package/dist/addons/bird-global-classifier/index.mjs +10 -0
- package/dist/addons/bird-global-classifier/index.mjs.map +1 -0
- package/dist/addons/bird-nabirds-classifier/index.d.mts +28 -0
- package/dist/addons/bird-nabirds-classifier/index.d.ts +28 -0
- package/dist/addons/bird-nabirds-classifier/index.js +700 -0
- package/dist/addons/bird-nabirds-classifier/index.js.map +1 -0
- package/dist/addons/bird-nabirds-classifier/index.mjs +10 -0
- package/dist/addons/bird-nabirds-classifier/index.mjs.map +1 -0
- package/dist/addons/camera-native-detection/index.d.mts +32 -0
- package/dist/addons/camera-native-detection/index.d.ts +32 -0
- package/dist/addons/camera-native-detection/index.js +99 -0
- package/dist/addons/camera-native-detection/index.js.map +1 -0
- package/dist/addons/camera-native-detection/index.mjs +7 -0
- package/dist/addons/camera-native-detection/index.mjs.map +1 -0
- package/dist/addons/face-detection/index.d.mts +24 -0
- package/dist/addons/face-detection/index.d.ts +24 -0
- package/dist/addons/face-detection/index.js +720 -0
- package/dist/addons/face-detection/index.js.map +1 -0
- package/dist/addons/face-detection/index.mjs +10 -0
- package/dist/addons/face-detection/index.mjs.map +1 -0
- package/dist/addons/face-recognition/index.d.mts +24 -0
- package/dist/addons/face-recognition/index.d.ts +24 -0
- package/dist/addons/face-recognition/index.js +603 -0
- package/dist/addons/face-recognition/index.js.map +1 -0
- package/dist/addons/face-recognition/index.mjs +9 -0
- package/dist/addons/face-recognition/index.mjs.map +1 -0
- package/dist/addons/motion-detection/index.d.mts +26 -0
- package/dist/addons/motion-detection/index.d.ts +26 -0
- package/dist/addons/motion-detection/index.js +273 -0
- package/dist/addons/motion-detection/index.js.map +1 -0
- package/dist/addons/motion-detection/index.mjs +8 -0
- package/dist/addons/motion-detection/index.mjs.map +1 -0
- package/dist/addons/object-detection/index.d.mts +26 -0
- package/dist/addons/object-detection/index.d.ts +26 -0
- package/dist/addons/object-detection/index.js +1214 -0
- package/dist/addons/object-detection/index.js.map +1 -0
- package/dist/addons/object-detection/index.mjs +10 -0
- package/dist/addons/object-detection/index.mjs.map +1 -0
- package/dist/addons/plate-detection/index.d.mts +25 -0
- package/dist/addons/plate-detection/index.d.ts +25 -0
- package/dist/addons/plate-detection/index.js +646 -0
- package/dist/addons/plate-detection/index.js.map +1 -0
- package/dist/addons/plate-detection/index.mjs +10 -0
- package/dist/addons/plate-detection/index.mjs.map +1 -0
- package/dist/addons/plate-recognition/index.d.mts +25 -0
- package/dist/addons/plate-recognition/index.d.ts +25 -0
- package/dist/addons/plate-recognition/index.js +648 -0
- package/dist/addons/plate-recognition/index.js.map +1 -0
- package/dist/addons/plate-recognition/index.mjs +9 -0
- package/dist/addons/plate-recognition/index.mjs.map +1 -0
- package/dist/chunk-3MQFUDRU.mjs +260 -0
- package/dist/chunk-3MQFUDRU.mjs.map +1 -0
- package/dist/chunk-5AIQSN32.mjs +227 -0
- package/dist/chunk-5AIQSN32.mjs.map +1 -0
- package/dist/chunk-5JJZGKL7.mjs +186 -0
- package/dist/chunk-5JJZGKL7.mjs.map +1 -0
- package/dist/chunk-6OR5TE7A.mjs +101 -0
- package/dist/chunk-6OR5TE7A.mjs.map +1 -0
- package/dist/chunk-AYBFB7ID.mjs +763 -0
- package/dist/chunk-AYBFB7ID.mjs.map +1 -0
- package/dist/chunk-B3R66MPF.mjs +219 -0
- package/dist/chunk-B3R66MPF.mjs.map +1 -0
- package/dist/chunk-DTOAB2CE.mjs +79 -0
- package/dist/chunk-DTOAB2CE.mjs.map +1 -0
- package/dist/chunk-ISOIDU4U.mjs +54 -0
- package/dist/chunk-ISOIDU4U.mjs.map +1 -0
- package/dist/chunk-J4WRYHHY.mjs +212 -0
- package/dist/chunk-J4WRYHHY.mjs.map +1 -0
- package/dist/chunk-KUO2BVFY.mjs +90 -0
- package/dist/chunk-KUO2BVFY.mjs.map +1 -0
- package/dist/chunk-LPI42WL6.mjs +324 -0
- package/dist/chunk-LPI42WL6.mjs.map +1 -0
- package/dist/chunk-MEVASN3P.mjs +305 -0
- package/dist/chunk-MEVASN3P.mjs.map +1 -0
- package/dist/chunk-PDSHDDPV.mjs +255 -0
- package/dist/chunk-PDSHDDPV.mjs.map +1 -0
- package/dist/chunk-Q3SQOYG6.mjs +218 -0
- package/dist/chunk-Q3SQOYG6.mjs.map +1 -0
- package/dist/chunk-QIMDG34B.mjs +229 -0
- package/dist/chunk-QIMDG34B.mjs.map +1 -0
- package/dist/index.d.mts +171 -0
- package/dist/index.d.ts +171 -0
- package/dist/index.js +3463 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +111 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +49 -0
- package/python/__pycache__/coreml_inference.cpython-313.pyc +0 -0
- package/python/__pycache__/openvino_inference.cpython-313.pyc +0 -0
- package/python/__pycache__/pytorch_inference.cpython-313.pyc +0 -0
- package/python/coreml_inference.py +319 -0
- package/python/openvino_inference.py +247 -0
- package/python/pytorch_inference.py +255 -0
|
@@ -0,0 +1,646 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/addons/plate-detection/index.ts
|
|
31
|
+
var plate_detection_exports = {};
|
|
32
|
+
__export(plate_detection_exports, {
|
|
33
|
+
default: () => PlateDetectionAddon
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(plate_detection_exports);
|
|
36
|
+
|
|
37
|
+
// src/catalogs/plate-detection-models.ts
|
|
38
|
+
var import_types = require("@camstack/types");
|
|
39
|
+
var HF_REPO = "camstack/camstack-models";
|
|
40
|
+
var PLATE_LABELS = [
|
|
41
|
+
{ id: "plate", name: "License Plate" }
|
|
42
|
+
];
|
|
43
|
+
var PLATE_DETECTION_MODELS = [
|
|
44
|
+
{
|
|
45
|
+
id: "yolov8n-plate",
|
|
46
|
+
name: "YOLOv8 Nano \u2014 License Plate",
|
|
47
|
+
description: "YOLOv8 Nano fine-tuned for license plate detection",
|
|
48
|
+
inputSize: { width: 640, height: 640 },
|
|
49
|
+
labels: PLATE_LABELS,
|
|
50
|
+
formats: {
|
|
51
|
+
onnx: {
|
|
52
|
+
url: (0, import_types.hfModelUrl)(HF_REPO, "plateDetection/yolov8-plate/onnx/camstack-yolov8n-plate.onnx"),
|
|
53
|
+
sizeMB: 12
|
|
54
|
+
},
|
|
55
|
+
coreml: {
|
|
56
|
+
url: (0, import_types.hfModelUrl)(HF_REPO, "plateDetection/yolov8-plate/coreml/camstack-yolov8n-plate.mlpackage"),
|
|
57
|
+
sizeMB: 5.9
|
|
58
|
+
},
|
|
59
|
+
openvino: {
|
|
60
|
+
url: (0, import_types.hfModelUrl)(HF_REPO, "plateDetection/yolov8-plate/openvino/camstack-yolov8n-plate.xml"),
|
|
61
|
+
sizeMB: 6.1
|
|
62
|
+
},
|
|
63
|
+
tflite: {
|
|
64
|
+
url: (0, import_types.hfModelUrl)(HF_REPO, "plateDetection/yolov8-plate/tflite/camstack-yolov8n-plate_float32.tflite"),
|
|
65
|
+
sizeMB: 12
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
// src/shared/image-utils.ts
|
|
72
|
+
var import_sharp = __toESM(require("sharp"));
|
|
73
|
+
async function cropRegion(jpeg, roi) {
|
|
74
|
+
return (0, import_sharp.default)(jpeg).extract({
|
|
75
|
+
left: Math.round(roi.x),
|
|
76
|
+
top: Math.round(roi.y),
|
|
77
|
+
width: Math.round(roi.w),
|
|
78
|
+
height: Math.round(roi.h)
|
|
79
|
+
}).jpeg().toBuffer();
|
|
80
|
+
}
|
|
81
|
+
async function letterbox(jpeg, targetSize) {
|
|
82
|
+
const meta = await (0, import_sharp.default)(jpeg).metadata();
|
|
83
|
+
const originalWidth = meta.width ?? 0;
|
|
84
|
+
const originalHeight = meta.height ?? 0;
|
|
85
|
+
const scale = Math.min(targetSize / originalWidth, targetSize / originalHeight);
|
|
86
|
+
const scaledWidth = Math.round(originalWidth * scale);
|
|
87
|
+
const scaledHeight = Math.round(originalHeight * scale);
|
|
88
|
+
const padX = Math.floor((targetSize - scaledWidth) / 2);
|
|
89
|
+
const padY = Math.floor((targetSize - scaledHeight) / 2);
|
|
90
|
+
const { data } = await (0, import_sharp.default)(jpeg).resize(scaledWidth, scaledHeight).extend({
|
|
91
|
+
top: padY,
|
|
92
|
+
bottom: targetSize - scaledHeight - padY,
|
|
93
|
+
left: padX,
|
|
94
|
+
right: targetSize - scaledWidth - padX,
|
|
95
|
+
background: { r: 114, g: 114, b: 114 }
|
|
96
|
+
}).removeAlpha().raw().toBuffer({ resolveWithObject: true });
|
|
97
|
+
const numPixels = targetSize * targetSize;
|
|
98
|
+
const float32 = new Float32Array(3 * numPixels);
|
|
99
|
+
for (let i = 0; i < numPixels; i++) {
|
|
100
|
+
const srcBase = i * 3;
|
|
101
|
+
float32[0 * numPixels + i] = data[srcBase] / 255;
|
|
102
|
+
float32[1 * numPixels + i] = data[srcBase + 1] / 255;
|
|
103
|
+
float32[2 * numPixels + i] = data[srcBase + 2] / 255;
|
|
104
|
+
}
|
|
105
|
+
return { data: float32, scale, padX, padY, originalWidth, originalHeight };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/shared/postprocess/yolo.ts
|
|
109
|
+
function iou(a, b) {
|
|
110
|
+
const ax1 = a.x;
|
|
111
|
+
const ay1 = a.y;
|
|
112
|
+
const ax2 = a.x + a.w;
|
|
113
|
+
const ay2 = a.y + a.h;
|
|
114
|
+
const bx1 = b.x;
|
|
115
|
+
const by1 = b.y;
|
|
116
|
+
const bx2 = b.x + b.w;
|
|
117
|
+
const by2 = b.y + b.h;
|
|
118
|
+
const interX1 = Math.max(ax1, bx1);
|
|
119
|
+
const interY1 = Math.max(ay1, by1);
|
|
120
|
+
const interX2 = Math.min(ax2, bx2);
|
|
121
|
+
const interY2 = Math.min(ay2, by2);
|
|
122
|
+
const interW = Math.max(0, interX2 - interX1);
|
|
123
|
+
const interH = Math.max(0, interY2 - interY1);
|
|
124
|
+
const interArea = interW * interH;
|
|
125
|
+
if (interArea === 0) return 0;
|
|
126
|
+
const areaA = a.w * a.h;
|
|
127
|
+
const areaB = b.w * b.h;
|
|
128
|
+
const unionArea = areaA + areaB - interArea;
|
|
129
|
+
return unionArea === 0 ? 0 : interArea / unionArea;
|
|
130
|
+
}
|
|
131
|
+
function nms(boxes, iouThreshold) {
|
|
132
|
+
const indices = boxes.map((_, i) => i).sort((a, b) => boxes[b].score - boxes[a].score);
|
|
133
|
+
const kept = [];
|
|
134
|
+
const suppressed = /* @__PURE__ */ new Set();
|
|
135
|
+
for (const idx of indices) {
|
|
136
|
+
if (suppressed.has(idx)) continue;
|
|
137
|
+
kept.push(idx);
|
|
138
|
+
for (const other of indices) {
|
|
139
|
+
if (other === idx || suppressed.has(other)) continue;
|
|
140
|
+
if (iou(boxes[idx].bbox, boxes[other].bbox) > iouThreshold) {
|
|
141
|
+
suppressed.add(other);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return kept;
|
|
146
|
+
}
|
|
147
|
+
function yoloPostprocess(output, numClasses, numBoxes, options) {
|
|
148
|
+
const { confidence, iouThreshold, labels, scale, padX, padY, originalWidth, originalHeight } = options;
|
|
149
|
+
const candidates = [];
|
|
150
|
+
for (let i = 0; i < numBoxes; i++) {
|
|
151
|
+
const cx = output[0 * numBoxes + i];
|
|
152
|
+
const cy = output[1 * numBoxes + i];
|
|
153
|
+
const w = output[2 * numBoxes + i];
|
|
154
|
+
const h = output[3 * numBoxes + i];
|
|
155
|
+
let bestScore = -Infinity;
|
|
156
|
+
let bestClass = 0;
|
|
157
|
+
for (let j = 0; j < numClasses; j++) {
|
|
158
|
+
const score = output[(4 + j) * numBoxes + i];
|
|
159
|
+
if (score > bestScore) {
|
|
160
|
+
bestScore = score;
|
|
161
|
+
bestClass = j;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (bestScore < confidence) continue;
|
|
165
|
+
const bbox = {
|
|
166
|
+
x: cx - w / 2,
|
|
167
|
+
y: cy - h / 2,
|
|
168
|
+
w,
|
|
169
|
+
h
|
|
170
|
+
};
|
|
171
|
+
candidates.push({ bbox, score: bestScore, classIdx: bestClass });
|
|
172
|
+
}
|
|
173
|
+
if (candidates.length === 0) return [];
|
|
174
|
+
const keptIndices = nms(candidates, iouThreshold);
|
|
175
|
+
return keptIndices.map((idx) => {
|
|
176
|
+
const { bbox, score, classIdx } = candidates[idx];
|
|
177
|
+
const label = labels[classIdx] ?? String(classIdx);
|
|
178
|
+
const x = Math.max(0, Math.min(originalWidth, (bbox.x - padX) / scale));
|
|
179
|
+
const y = Math.max(0, Math.min(originalHeight, (bbox.y - padY) / scale));
|
|
180
|
+
const x2 = Math.max(0, Math.min(originalWidth, (bbox.x + bbox.w - padX) / scale));
|
|
181
|
+
const y2 = Math.max(0, Math.min(originalHeight, (bbox.y + bbox.h - padY) / scale));
|
|
182
|
+
const finalBbox = { x, y, w: x2 - x, h: y2 - y };
|
|
183
|
+
return {
|
|
184
|
+
class: label,
|
|
185
|
+
originalClass: label,
|
|
186
|
+
score,
|
|
187
|
+
bbox: finalBbox
|
|
188
|
+
};
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// src/shared/engine-resolver.ts
|
|
193
|
+
var fs = __toESM(require("fs"));
|
|
194
|
+
var path2 = __toESM(require("path"));
|
|
195
|
+
|
|
196
|
+
// src/shared/node-engine.ts
|
|
197
|
+
var path = __toESM(require("path"));
|
|
198
|
+
var BACKEND_TO_PROVIDER = {
|
|
199
|
+
cpu: "cpu",
|
|
200
|
+
coreml: "coreml",
|
|
201
|
+
cuda: "cuda",
|
|
202
|
+
tensorrt: "tensorrt",
|
|
203
|
+
dml: "dml"
|
|
204
|
+
};
|
|
205
|
+
var BACKEND_TO_DEVICE = {
|
|
206
|
+
cpu: "cpu",
|
|
207
|
+
coreml: "gpu-mps",
|
|
208
|
+
cuda: "gpu-cuda",
|
|
209
|
+
tensorrt: "tensorrt"
|
|
210
|
+
};
|
|
211
|
+
var NodeInferenceEngine = class {
|
|
212
|
+
constructor(modelPath, backend) {
|
|
213
|
+
this.modelPath = modelPath;
|
|
214
|
+
this.backend = backend;
|
|
215
|
+
this.device = BACKEND_TO_DEVICE[backend] ?? "cpu";
|
|
216
|
+
}
|
|
217
|
+
runtime = "onnx";
|
|
218
|
+
device;
|
|
219
|
+
session = null;
|
|
220
|
+
async initialize() {
|
|
221
|
+
const ort = await import("onnxruntime-node");
|
|
222
|
+
const provider = BACKEND_TO_PROVIDER[this.backend] ?? "cpu";
|
|
223
|
+
const absModelPath = path.isAbsolute(this.modelPath) ? this.modelPath : path.resolve(process.cwd(), this.modelPath);
|
|
224
|
+
const sessionOptions = {
|
|
225
|
+
executionProviders: [provider]
|
|
226
|
+
};
|
|
227
|
+
this.session = await ort.InferenceSession.create(absModelPath, sessionOptions);
|
|
228
|
+
}
|
|
229
|
+
async run(input, inputShape) {
|
|
230
|
+
if (!this.session) {
|
|
231
|
+
throw new Error("NodeInferenceEngine: not initialized \u2014 call initialize() first");
|
|
232
|
+
}
|
|
233
|
+
const ort = await import("onnxruntime-node");
|
|
234
|
+
const sess = this.session;
|
|
235
|
+
const inputName = sess.inputNames[0];
|
|
236
|
+
const tensor = new ort.Tensor("float32", input, [...inputShape]);
|
|
237
|
+
const feeds = { [inputName]: tensor };
|
|
238
|
+
const results = await sess.run(feeds);
|
|
239
|
+
const outputName = sess.outputNames[0];
|
|
240
|
+
const outputTensor = results[outputName];
|
|
241
|
+
return outputTensor.data;
|
|
242
|
+
}
|
|
243
|
+
async runMultiOutput(input, inputShape) {
|
|
244
|
+
if (!this.session) {
|
|
245
|
+
throw new Error("NodeInferenceEngine: not initialized \u2014 call initialize() first");
|
|
246
|
+
}
|
|
247
|
+
const ort = await import("onnxruntime-node");
|
|
248
|
+
const sess = this.session;
|
|
249
|
+
const inputName = sess.inputNames[0];
|
|
250
|
+
const tensor = new ort.Tensor("float32", input, [...inputShape]);
|
|
251
|
+
const feeds = { [inputName]: tensor };
|
|
252
|
+
const results = await sess.run(feeds);
|
|
253
|
+
const out = {};
|
|
254
|
+
for (const name of sess.outputNames) {
|
|
255
|
+
out[name] = results[name].data;
|
|
256
|
+
}
|
|
257
|
+
return out;
|
|
258
|
+
}
|
|
259
|
+
async dispose() {
|
|
260
|
+
this.session = null;
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
// src/shared/python-engine.ts
|
|
265
|
+
var import_node_child_process = require("child_process");
|
|
266
|
+
var PythonInferenceEngine = class {
|
|
267
|
+
constructor(pythonPath, scriptPath, runtime, modelPath, extraArgs = []) {
|
|
268
|
+
this.pythonPath = pythonPath;
|
|
269
|
+
this.scriptPath = scriptPath;
|
|
270
|
+
this.modelPath = modelPath;
|
|
271
|
+
this.extraArgs = extraArgs;
|
|
272
|
+
this.runtime = runtime;
|
|
273
|
+
const runtimeDeviceMap = {
|
|
274
|
+
onnx: "cpu",
|
|
275
|
+
coreml: "gpu-mps",
|
|
276
|
+
pytorch: "cpu",
|
|
277
|
+
openvino: "cpu",
|
|
278
|
+
tflite: "cpu"
|
|
279
|
+
};
|
|
280
|
+
this.device = runtimeDeviceMap[runtime];
|
|
281
|
+
}
|
|
282
|
+
runtime;
|
|
283
|
+
device;
|
|
284
|
+
process = null;
|
|
285
|
+
receiveBuffer = Buffer.alloc(0);
|
|
286
|
+
pendingResolve = null;
|
|
287
|
+
pendingReject = null;
|
|
288
|
+
async initialize() {
|
|
289
|
+
const args = [this.scriptPath, this.modelPath, ...this.extraArgs];
|
|
290
|
+
this.process = (0, import_node_child_process.spawn)(this.pythonPath, args, {
|
|
291
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
292
|
+
});
|
|
293
|
+
if (!this.process.stdout || !this.process.stdin) {
|
|
294
|
+
throw new Error("PythonInferenceEngine: failed to create process pipes");
|
|
295
|
+
}
|
|
296
|
+
this.process.stderr?.on("data", (chunk) => {
|
|
297
|
+
process.stderr.write(`[python-engine] ${chunk.toString()}`);
|
|
298
|
+
});
|
|
299
|
+
this.process.on("error", (err) => {
|
|
300
|
+
this.pendingReject?.(err);
|
|
301
|
+
this.pendingReject = null;
|
|
302
|
+
this.pendingResolve = null;
|
|
303
|
+
});
|
|
304
|
+
this.process.on("exit", (code) => {
|
|
305
|
+
if (code !== 0) {
|
|
306
|
+
const err = new Error(`PythonInferenceEngine: process exited with code ${code}`);
|
|
307
|
+
this.pendingReject?.(err);
|
|
308
|
+
this.pendingReject = null;
|
|
309
|
+
this.pendingResolve = null;
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
this.process.stdout.on("data", (chunk) => {
|
|
313
|
+
this.receiveBuffer = Buffer.concat([this.receiveBuffer, chunk]);
|
|
314
|
+
this._tryReceive();
|
|
315
|
+
});
|
|
316
|
+
await new Promise((resolve2, reject) => {
|
|
317
|
+
const timeout = setTimeout(() => resolve2(), 2e3);
|
|
318
|
+
this.process?.on("error", (err) => {
|
|
319
|
+
clearTimeout(timeout);
|
|
320
|
+
reject(err);
|
|
321
|
+
});
|
|
322
|
+
this.process?.on("exit", (code) => {
|
|
323
|
+
clearTimeout(timeout);
|
|
324
|
+
if (code !== 0) {
|
|
325
|
+
reject(new Error(`PythonInferenceEngine: process exited early with code ${code}`));
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
_tryReceive() {
|
|
331
|
+
if (this.receiveBuffer.length < 4) return;
|
|
332
|
+
const length = this.receiveBuffer.readUInt32LE(0);
|
|
333
|
+
if (this.receiveBuffer.length < 4 + length) return;
|
|
334
|
+
const jsonBytes = this.receiveBuffer.subarray(4, 4 + length);
|
|
335
|
+
this.receiveBuffer = this.receiveBuffer.subarray(4 + length);
|
|
336
|
+
const resolve2 = this.pendingResolve;
|
|
337
|
+
const reject = this.pendingReject;
|
|
338
|
+
this.pendingResolve = null;
|
|
339
|
+
this.pendingReject = null;
|
|
340
|
+
if (!resolve2) return;
|
|
341
|
+
try {
|
|
342
|
+
const parsed = JSON.parse(jsonBytes.toString("utf8"));
|
|
343
|
+
resolve2(parsed);
|
|
344
|
+
} catch (err) {
|
|
345
|
+
reject?.(err instanceof Error ? err : new Error(String(err)));
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
/** Send JPEG buffer, receive JSON detection results */
|
|
349
|
+
async runJpeg(jpeg) {
|
|
350
|
+
if (!this.process?.stdin) {
|
|
351
|
+
throw new Error("PythonInferenceEngine: process not initialized");
|
|
352
|
+
}
|
|
353
|
+
return new Promise((resolve2, reject) => {
|
|
354
|
+
this.pendingResolve = resolve2;
|
|
355
|
+
this.pendingReject = reject;
|
|
356
|
+
const lengthBuf = Buffer.allocUnsafe(4);
|
|
357
|
+
lengthBuf.writeUInt32LE(jpeg.length, 0);
|
|
358
|
+
this.process.stdin.write(Buffer.concat([lengthBuf, jpeg]));
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
/** IInferenceEngine.run — wraps runJpeg for compatibility */
|
|
362
|
+
async run(_input, _inputShape) {
|
|
363
|
+
throw new Error(
|
|
364
|
+
"PythonInferenceEngine: use runJpeg() directly \u2014 this engine operates on JPEG input"
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
/** IInferenceEngine.runMultiOutput — not supported by Python engine (operates on JPEG input) */
|
|
368
|
+
async runMultiOutput(_input, _inputShape) {
|
|
369
|
+
throw new Error(
|
|
370
|
+
"PythonInferenceEngine: runMultiOutput() is not supported \u2014 this engine operates on JPEG input"
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
async dispose() {
|
|
374
|
+
if (this.process) {
|
|
375
|
+
this.process.stdin?.end();
|
|
376
|
+
this.process.kill("SIGTERM");
|
|
377
|
+
this.process = null;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
// src/shared/engine-resolver.ts
|
|
383
|
+
var AUTO_BACKEND_PRIORITY = ["coreml", "cuda", "tensorrt", "cpu"];
|
|
384
|
+
var BACKEND_TO_FORMAT = {
|
|
385
|
+
cpu: "onnx",
|
|
386
|
+
coreml: "coreml",
|
|
387
|
+
cuda: "onnx",
|
|
388
|
+
tensorrt: "onnx"
|
|
389
|
+
};
|
|
390
|
+
var RUNTIME_TO_FORMAT = {
|
|
391
|
+
onnx: "onnx",
|
|
392
|
+
coreml: "coreml",
|
|
393
|
+
openvino: "openvino",
|
|
394
|
+
tflite: "tflite",
|
|
395
|
+
pytorch: "pt"
|
|
396
|
+
};
|
|
397
|
+
function modelFilePath(modelsDir, modelEntry, format) {
|
|
398
|
+
const formatEntry = modelEntry.formats[format];
|
|
399
|
+
if (!formatEntry) {
|
|
400
|
+
throw new Error(`Model ${modelEntry.id} has no ${format} format`);
|
|
401
|
+
}
|
|
402
|
+
const urlParts = formatEntry.url.split("/");
|
|
403
|
+
const filename = urlParts[urlParts.length - 1] ?? `${modelEntry.id}.${format}`;
|
|
404
|
+
return path2.join(modelsDir, filename);
|
|
405
|
+
}
|
|
406
|
+
function modelExists(filePath) {
|
|
407
|
+
try {
|
|
408
|
+
return fs.existsSync(filePath);
|
|
409
|
+
} catch {
|
|
410
|
+
return false;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
async function resolveEngine(options) {
|
|
414
|
+
const { runtime, backend, modelEntry, modelsDir, downloadModel } = options;
|
|
415
|
+
let selectedFormat;
|
|
416
|
+
let selectedBackend;
|
|
417
|
+
if (runtime === "auto") {
|
|
418
|
+
const available = await probeOnnxBackends();
|
|
419
|
+
let chosen = null;
|
|
420
|
+
for (const b of AUTO_BACKEND_PRIORITY) {
|
|
421
|
+
if (!available.includes(b)) continue;
|
|
422
|
+
const fmt = BACKEND_TO_FORMAT[b];
|
|
423
|
+
if (!fmt) continue;
|
|
424
|
+
if (!modelEntry.formats[fmt]) continue;
|
|
425
|
+
chosen = { backend: b, format: fmt };
|
|
426
|
+
break;
|
|
427
|
+
}
|
|
428
|
+
if (!chosen) {
|
|
429
|
+
throw new Error(
|
|
430
|
+
`resolveEngine: no compatible backend found for model ${modelEntry.id}. Available backends: ${available.join(", ")}`
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
selectedFormat = chosen.format;
|
|
434
|
+
selectedBackend = chosen.backend;
|
|
435
|
+
} else {
|
|
436
|
+
const fmt = RUNTIME_TO_FORMAT[runtime];
|
|
437
|
+
if (!fmt) {
|
|
438
|
+
throw new Error(`resolveEngine: unsupported runtime "${runtime}"`);
|
|
439
|
+
}
|
|
440
|
+
if (!modelEntry.formats[fmt]) {
|
|
441
|
+
throw new Error(
|
|
442
|
+
`resolveEngine: model ${modelEntry.id} has no ${fmt} format for runtime ${runtime}`
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
selectedFormat = fmt;
|
|
446
|
+
selectedBackend = runtime === "onnx" ? backend || "cpu" : runtime;
|
|
447
|
+
}
|
|
448
|
+
let modelPath = modelFilePath(modelsDir, modelEntry, selectedFormat);
|
|
449
|
+
if (!modelExists(modelPath)) {
|
|
450
|
+
if (downloadModel) {
|
|
451
|
+
const formatEntry = modelEntry.formats[selectedFormat];
|
|
452
|
+
modelPath = await downloadModel(formatEntry.url, modelsDir);
|
|
453
|
+
} else {
|
|
454
|
+
throw new Error(
|
|
455
|
+
`resolveEngine: model file not found at ${modelPath} and no downloadModel function provided`
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
if (selectedFormat === "onnx" || selectedFormat === "coreml") {
|
|
460
|
+
const engine = new NodeInferenceEngine(modelPath, selectedBackend);
|
|
461
|
+
await engine.initialize();
|
|
462
|
+
return { engine, format: selectedFormat, modelPath };
|
|
463
|
+
}
|
|
464
|
+
const { pythonPath } = options;
|
|
465
|
+
const PYTHON_SCRIPT_MAP = {
|
|
466
|
+
coreml: "coreml_inference.py",
|
|
467
|
+
pytorch: "pytorch_inference.py",
|
|
468
|
+
openvino: "openvino_inference.py"
|
|
469
|
+
};
|
|
470
|
+
const effectiveRuntime = runtime === "auto" ? selectedBackend : runtime;
|
|
471
|
+
const scriptName = PYTHON_SCRIPT_MAP[effectiveRuntime];
|
|
472
|
+
if (scriptName && pythonPath) {
|
|
473
|
+
const scriptPath = path2.join(__dirname, "../../python", scriptName);
|
|
474
|
+
const inputSize = Math.max(modelEntry.inputSize.width, modelEntry.inputSize.height);
|
|
475
|
+
const engine = new PythonInferenceEngine(pythonPath, scriptPath, effectiveRuntime, modelPath, [
|
|
476
|
+
`--input-size=${inputSize}`,
|
|
477
|
+
`--confidence=0.25`
|
|
478
|
+
]);
|
|
479
|
+
await engine.initialize();
|
|
480
|
+
return { engine, format: selectedFormat, modelPath };
|
|
481
|
+
}
|
|
482
|
+
const fallbackPath = modelFilePath(modelsDir, modelEntry, "onnx");
|
|
483
|
+
if (modelEntry.formats["onnx"] && modelExists(fallbackPath)) {
|
|
484
|
+
const engine = new NodeInferenceEngine(fallbackPath, "cpu");
|
|
485
|
+
await engine.initialize();
|
|
486
|
+
return { engine, format: "onnx", modelPath: fallbackPath };
|
|
487
|
+
}
|
|
488
|
+
throw new Error(
|
|
489
|
+
`resolveEngine: format ${selectedFormat} is not yet supported by NodeInferenceEngine, no Python runtime is available, and no ONNX fallback exists`
|
|
490
|
+
);
|
|
491
|
+
}
|
|
492
|
+
async function probeOnnxBackends() {
|
|
493
|
+
const available = ["cpu"];
|
|
494
|
+
try {
|
|
495
|
+
const ort = await import("onnxruntime-node");
|
|
496
|
+
const providers = ort.env?.webgl?.disabled !== void 0 ? ort.InferenceSession?.getAvailableProviders?.() ?? [] : [];
|
|
497
|
+
for (const p of providers) {
|
|
498
|
+
const normalized = p.toLowerCase().replace("executionprovider", "");
|
|
499
|
+
if (normalized === "coreml") available.push("coreml");
|
|
500
|
+
else if (normalized === "cuda") available.push("cuda");
|
|
501
|
+
else if (normalized === "tensorrt") available.push("tensorrt");
|
|
502
|
+
}
|
|
503
|
+
} catch {
|
|
504
|
+
}
|
|
505
|
+
if (process.platform === "darwin" && !available.includes("coreml")) {
|
|
506
|
+
available.push("coreml");
|
|
507
|
+
}
|
|
508
|
+
return [...new Set(available)];
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
// src/addons/plate-detection/index.ts
|
|
512
|
+
var PLATE_LABEL = { id: "plate", name: "License Plate" };
|
|
513
|
+
var PLATE_LABELS2 = [PLATE_LABEL];
|
|
514
|
+
var PLATE_CLASS_MAP = { mapping: {}, preserveOriginal: true };
|
|
515
|
+
var PlateDetectionAddon = class {
|
|
516
|
+
id = "plate-detection";
|
|
517
|
+
slot = "cropper";
|
|
518
|
+
inputClasses = ["vehicle"];
|
|
519
|
+
outputClasses = ["plate"];
|
|
520
|
+
slotPriority = 0;
|
|
521
|
+
manifest = {
|
|
522
|
+
id: "plate-detection",
|
|
523
|
+
name: "License Plate Detection",
|
|
524
|
+
version: "0.1.0",
|
|
525
|
+
description: "YOLO-based license plate detector \u2014 crops plate regions from vehicle detections",
|
|
526
|
+
packageName: "@camstack/addon-vision",
|
|
527
|
+
slot: "cropper",
|
|
528
|
+
inputClasses: ["vehicle"],
|
|
529
|
+
outputClasses: ["plate"],
|
|
530
|
+
supportsCustomModels: false,
|
|
531
|
+
mayRequirePython: false,
|
|
532
|
+
defaultConfig: {
|
|
533
|
+
modelId: "yolov8n-plate",
|
|
534
|
+
runtime: "auto",
|
|
535
|
+
backend: "cpu",
|
|
536
|
+
confidence: 0.5,
|
|
537
|
+
iouThreshold: 0.45
|
|
538
|
+
}
|
|
539
|
+
};
|
|
540
|
+
engine;
|
|
541
|
+
modelEntry;
|
|
542
|
+
confidence = 0.5;
|
|
543
|
+
iouThreshold = 0.45;
|
|
544
|
+
async initialize(ctx) {
|
|
545
|
+
const cfg = ctx.addonConfig;
|
|
546
|
+
const modelId = cfg["modelId"] ?? "yolov8n-plate";
|
|
547
|
+
const runtime = cfg["runtime"] ?? "auto";
|
|
548
|
+
const backend = cfg["backend"] ?? "cpu";
|
|
549
|
+
this.confidence = cfg["confidence"] ?? 0.5;
|
|
550
|
+
this.iouThreshold = cfg["iouThreshold"] ?? 0.45;
|
|
551
|
+
const entry = PLATE_DETECTION_MODELS.find((m) => m.id === modelId);
|
|
552
|
+
if (!entry) {
|
|
553
|
+
throw new Error(`PlateDetectionAddon: unknown modelId "${modelId}"`);
|
|
554
|
+
}
|
|
555
|
+
this.modelEntry = entry;
|
|
556
|
+
const resolved = await resolveEngine({
|
|
557
|
+
runtime,
|
|
558
|
+
backend,
|
|
559
|
+
modelEntry: entry,
|
|
560
|
+
modelsDir: ctx.locationPaths.models
|
|
561
|
+
});
|
|
562
|
+
this.engine = resolved.engine;
|
|
563
|
+
}
|
|
564
|
+
async crop(input) {
|
|
565
|
+
const start = Date.now();
|
|
566
|
+
const { width: inputW, height: inputH } = this.modelEntry.inputSize;
|
|
567
|
+
const targetSize = Math.max(inputW, inputH);
|
|
568
|
+
const vehicleCrop = await cropRegion(input.frame.data, input.roi);
|
|
569
|
+
const lb = await letterbox(vehicleCrop, targetSize);
|
|
570
|
+
const output = await this.engine.run(lb.data, [1, 3, targetSize, targetSize]);
|
|
571
|
+
const numClasses = this.modelEntry.labels.length;
|
|
572
|
+
const numBoxes = output.length / (4 + numClasses);
|
|
573
|
+
const labels = this.modelEntry.labels.map((l) => l.id);
|
|
574
|
+
const plates = yoloPostprocess(output, numClasses, numBoxes, {
|
|
575
|
+
confidence: this.confidence,
|
|
576
|
+
iouThreshold: this.iouThreshold,
|
|
577
|
+
labels,
|
|
578
|
+
scale: lb.scale,
|
|
579
|
+
padX: lb.padX,
|
|
580
|
+
padY: lb.padY,
|
|
581
|
+
originalWidth: lb.originalWidth,
|
|
582
|
+
originalHeight: lb.originalHeight
|
|
583
|
+
});
|
|
584
|
+
const crops = plates.map((p) => ({ ...p, class: "plate", originalClass: p.originalClass }));
|
|
585
|
+
return {
|
|
586
|
+
crops,
|
|
587
|
+
inferenceMs: Date.now() - start,
|
|
588
|
+
modelId: this.modelEntry.id
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
async shutdown() {
|
|
592
|
+
await this.engine?.dispose();
|
|
593
|
+
}
|
|
594
|
+
getConfigSchema() {
|
|
595
|
+
return {
|
|
596
|
+
sections: [
|
|
597
|
+
{
|
|
598
|
+
id: "thresholds",
|
|
599
|
+
title: "Detection Thresholds",
|
|
600
|
+
columns: 2,
|
|
601
|
+
fields: [
|
|
602
|
+
{
|
|
603
|
+
key: "confidence",
|
|
604
|
+
label: "Confidence Threshold",
|
|
605
|
+
type: "slider",
|
|
606
|
+
min: 0.1,
|
|
607
|
+
max: 1,
|
|
608
|
+
step: 0.05,
|
|
609
|
+
default: 0.5
|
|
610
|
+
},
|
|
611
|
+
{
|
|
612
|
+
key: "iouThreshold",
|
|
613
|
+
label: "IoU Threshold (NMS)",
|
|
614
|
+
type: "slider",
|
|
615
|
+
min: 0.1,
|
|
616
|
+
max: 1,
|
|
617
|
+
step: 0.05,
|
|
618
|
+
default: 0.45
|
|
619
|
+
}
|
|
620
|
+
]
|
|
621
|
+
}
|
|
622
|
+
]
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
getClassMap() {
|
|
626
|
+
return PLATE_CLASS_MAP;
|
|
627
|
+
}
|
|
628
|
+
getModelCatalog() {
|
|
629
|
+
return [...PLATE_DETECTION_MODELS];
|
|
630
|
+
}
|
|
631
|
+
getAvailableModels() {
|
|
632
|
+
return [];
|
|
633
|
+
}
|
|
634
|
+
getActiveLabels() {
|
|
635
|
+
return PLATE_LABELS2;
|
|
636
|
+
}
|
|
637
|
+
async probe() {
|
|
638
|
+
return {
|
|
639
|
+
available: true,
|
|
640
|
+
runtime: this.engine?.runtime ?? "onnx",
|
|
641
|
+
device: this.engine?.device ?? "cpu",
|
|
642
|
+
capabilities: ["fp32"]
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
};
|
|
646
|
+
//# sourceMappingURL=index.js.map
|