@camstack/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.
Files changed (104) hide show
  1. package/dist/addons/animal-classifier/index.d.mts +25 -0
  2. package/dist/addons/animal-classifier/index.d.ts +25 -0
  3. package/dist/addons/animal-classifier/index.js +469 -0
  4. package/dist/addons/animal-classifier/index.js.map +1 -0
  5. package/dist/addons/animal-classifier/index.mjs +9 -0
  6. package/dist/addons/animal-classifier/index.mjs.map +1 -0
  7. package/dist/addons/audio-classification/index.d.mts +31 -0
  8. package/dist/addons/audio-classification/index.d.ts +31 -0
  9. package/dist/addons/audio-classification/index.js +411 -0
  10. package/dist/addons/audio-classification/index.js.map +1 -0
  11. package/dist/addons/audio-classification/index.mjs +8 -0
  12. package/dist/addons/audio-classification/index.mjs.map +1 -0
  13. package/dist/addons/bird-global-classifier/index.d.mts +26 -0
  14. package/dist/addons/bird-global-classifier/index.d.ts +26 -0
  15. package/dist/addons/bird-global-classifier/index.js +475 -0
  16. package/dist/addons/bird-global-classifier/index.js.map +1 -0
  17. package/dist/addons/bird-global-classifier/index.mjs +9 -0
  18. package/dist/addons/bird-global-classifier/index.mjs.map +1 -0
  19. package/dist/addons/bird-nabirds-classifier/index.d.mts +28 -0
  20. package/dist/addons/bird-nabirds-classifier/index.d.ts +28 -0
  21. package/dist/addons/bird-nabirds-classifier/index.js +517 -0
  22. package/dist/addons/bird-nabirds-classifier/index.js.map +1 -0
  23. package/dist/addons/bird-nabirds-classifier/index.mjs +9 -0
  24. package/dist/addons/bird-nabirds-classifier/index.mjs.map +1 -0
  25. package/dist/addons/camera-native-detection/index.d.mts +32 -0
  26. package/dist/addons/camera-native-detection/index.d.ts +32 -0
  27. package/dist/addons/camera-native-detection/index.js +99 -0
  28. package/dist/addons/camera-native-detection/index.js.map +1 -0
  29. package/dist/addons/camera-native-detection/index.mjs +7 -0
  30. package/dist/addons/camera-native-detection/index.mjs.map +1 -0
  31. package/dist/addons/face-detection/index.d.mts +24 -0
  32. package/dist/addons/face-detection/index.d.ts +24 -0
  33. package/dist/addons/face-detection/index.js +513 -0
  34. package/dist/addons/face-detection/index.js.map +1 -0
  35. package/dist/addons/face-detection/index.mjs +10 -0
  36. package/dist/addons/face-detection/index.mjs.map +1 -0
  37. package/dist/addons/face-recognition/index.d.mts +24 -0
  38. package/dist/addons/face-recognition/index.d.ts +24 -0
  39. package/dist/addons/face-recognition/index.js +437 -0
  40. package/dist/addons/face-recognition/index.js.map +1 -0
  41. package/dist/addons/face-recognition/index.mjs +9 -0
  42. package/dist/addons/face-recognition/index.mjs.map +1 -0
  43. package/dist/addons/motion-detection/index.d.mts +26 -0
  44. package/dist/addons/motion-detection/index.d.ts +26 -0
  45. package/dist/addons/motion-detection/index.js +273 -0
  46. package/dist/addons/motion-detection/index.js.map +1 -0
  47. package/dist/addons/motion-detection/index.mjs +8 -0
  48. package/dist/addons/motion-detection/index.mjs.map +1 -0
  49. package/dist/addons/object-detection/index.d.mts +25 -0
  50. package/dist/addons/object-detection/index.d.ts +25 -0
  51. package/dist/addons/object-detection/index.js +673 -0
  52. package/dist/addons/object-detection/index.js.map +1 -0
  53. package/dist/addons/object-detection/index.mjs +10 -0
  54. package/dist/addons/object-detection/index.mjs.map +1 -0
  55. package/dist/addons/plate-detection/index.d.mts +25 -0
  56. package/dist/addons/plate-detection/index.d.ts +25 -0
  57. package/dist/addons/plate-detection/index.js +477 -0
  58. package/dist/addons/plate-detection/index.js.map +1 -0
  59. package/dist/addons/plate-detection/index.mjs +10 -0
  60. package/dist/addons/plate-detection/index.mjs.map +1 -0
  61. package/dist/addons/plate-recognition/index.d.mts +25 -0
  62. package/dist/addons/plate-recognition/index.d.ts +25 -0
  63. package/dist/addons/plate-recognition/index.js +470 -0
  64. package/dist/addons/plate-recognition/index.js.map +1 -0
  65. package/dist/addons/plate-recognition/index.mjs +9 -0
  66. package/dist/addons/plate-recognition/index.mjs.map +1 -0
  67. package/dist/chunk-3BKYLBBH.mjs +229 -0
  68. package/dist/chunk-3BKYLBBH.mjs.map +1 -0
  69. package/dist/chunk-4PC262GU.mjs +203 -0
  70. package/dist/chunk-4PC262GU.mjs.map +1 -0
  71. package/dist/chunk-6OR5TE7A.mjs +101 -0
  72. package/dist/chunk-6OR5TE7A.mjs.map +1 -0
  73. package/dist/chunk-7SZAISGP.mjs +210 -0
  74. package/dist/chunk-7SZAISGP.mjs.map +1 -0
  75. package/dist/chunk-AD2TFYZA.mjs +235 -0
  76. package/dist/chunk-AD2TFYZA.mjs.map +1 -0
  77. package/dist/chunk-CGYSSHHM.mjs +363 -0
  78. package/dist/chunk-CGYSSHHM.mjs.map +1 -0
  79. package/dist/chunk-IYHMGYGP.mjs +79 -0
  80. package/dist/chunk-IYHMGYGP.mjs.map +1 -0
  81. package/dist/chunk-J3IUBPRE.mjs +187 -0
  82. package/dist/chunk-J3IUBPRE.mjs.map +1 -0
  83. package/dist/chunk-KFZDJPYL.mjs +190 -0
  84. package/dist/chunk-KFZDJPYL.mjs.map +1 -0
  85. package/dist/chunk-KUO2BVFY.mjs +90 -0
  86. package/dist/chunk-KUO2BVFY.mjs.map +1 -0
  87. package/dist/chunk-PXBY3QOA.mjs +152 -0
  88. package/dist/chunk-PXBY3QOA.mjs.map +1 -0
  89. package/dist/chunk-XUKDL23Y.mjs +216 -0
  90. package/dist/chunk-XUKDL23Y.mjs.map +1 -0
  91. package/dist/chunk-Z26BVC7S.mjs +214 -0
  92. package/dist/chunk-Z26BVC7S.mjs.map +1 -0
  93. package/dist/chunk-Z5AHZQEZ.mjs +258 -0
  94. package/dist/chunk-Z5AHZQEZ.mjs.map +1 -0
  95. package/dist/index.d.mts +152 -0
  96. package/dist/index.d.ts +152 -0
  97. package/dist/index.js +2775 -0
  98. package/dist/index.js.map +1 -0
  99. package/dist/index.mjs +205 -0
  100. package/dist/index.mjs.map +1 -0
  101. package/package.json +43 -0
  102. package/python/coreml_inference.py +67 -0
  103. package/python/openvino_inference.py +76 -0
  104. package/python/pytorch_inference.py +74 -0
package/dist/index.js ADDED
@@ -0,0 +1,2775 @@
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/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ AnimalClassifierAddon: () => AnimalClassifierAddon,
34
+ AudioClassificationAddon: () => AudioClassificationAddon,
35
+ BirdGlobalClassifierAddon: () => BirdGlobalClassifierAddon,
36
+ BirdNABirdsClassifierAddon: () => BirdNABirdsClassifierAddon,
37
+ CameraNativeDetectionAddon: () => CameraNativeDetectionAddon,
38
+ FaceDetectionAddon: () => FaceDetectionAddon,
39
+ FaceRecognitionAddon: () => FaceRecognitionAddon,
40
+ MotionDetectionAddon: () => MotionDetectionAddon,
41
+ NodeInferenceEngine: () => NodeInferenceEngine,
42
+ ObjectDetectionAddon: () => ObjectDetectionAddon,
43
+ PlateDetectionAddon: () => PlateDetectionAddon,
44
+ PlateRecognitionAddon: () => PlateRecognitionAddon,
45
+ PythonInferenceEngine: () => PythonInferenceEngine,
46
+ cosineSimilarity: () => cosineSimilarity,
47
+ cropRegion: () => cropRegion,
48
+ ctcDecode: () => ctcDecode,
49
+ detectMotion: () => detectMotion,
50
+ iou: () => iou,
51
+ jpegToRgb: () => jpegToRgb,
52
+ l2Normalize: () => l2Normalize,
53
+ letterbox: () => letterbox,
54
+ nms: () => nms,
55
+ probeOnnxBackends: () => probeOnnxBackends,
56
+ resizeAndNormalize: () => resizeAndNormalize,
57
+ resolveEngine: () => resolveEngine,
58
+ rgbToGrayscale: () => rgbToGrayscale,
59
+ scrfdPostprocess: () => scrfdPostprocess,
60
+ yamnetPostprocess: () => yamnetPostprocess,
61
+ yoloPostprocess: () => yoloPostprocess
62
+ });
63
+ module.exports = __toCommonJS(src_exports);
64
+
65
+ // src/shared/image-utils.ts
66
+ var import_sharp = __toESM(require("sharp"));
67
+ async function jpegToRgb(jpeg) {
68
+ const { data, info } = await (0, import_sharp.default)(jpeg).removeAlpha().raw().toBuffer({ resolveWithObject: true });
69
+ return { data, width: info.width, height: info.height };
70
+ }
71
+ async function cropRegion(jpeg, roi) {
72
+ return (0, import_sharp.default)(jpeg).extract({
73
+ left: Math.round(roi.x),
74
+ top: Math.round(roi.y),
75
+ width: Math.round(roi.w),
76
+ height: Math.round(roi.h)
77
+ }).jpeg().toBuffer();
78
+ }
79
+ async function letterbox(jpeg, targetSize) {
80
+ const meta = await (0, import_sharp.default)(jpeg).metadata();
81
+ const originalWidth = meta.width ?? 0;
82
+ const originalHeight = meta.height ?? 0;
83
+ const scale = Math.min(targetSize / originalWidth, targetSize / originalHeight);
84
+ const scaledWidth = Math.round(originalWidth * scale);
85
+ const scaledHeight = Math.round(originalHeight * scale);
86
+ const padX = Math.floor((targetSize - scaledWidth) / 2);
87
+ const padY = Math.floor((targetSize - scaledHeight) / 2);
88
+ const { data } = await (0, import_sharp.default)(jpeg).resize(scaledWidth, scaledHeight).extend({
89
+ top: padY,
90
+ bottom: targetSize - scaledHeight - padY,
91
+ left: padX,
92
+ right: targetSize - scaledWidth - padX,
93
+ background: { r: 114, g: 114, b: 114 }
94
+ }).removeAlpha().raw().toBuffer({ resolveWithObject: true });
95
+ const numPixels = targetSize * targetSize;
96
+ const float32 = new Float32Array(3 * numPixels);
97
+ for (let i = 0; i < numPixels; i++) {
98
+ const srcBase = i * 3;
99
+ float32[0 * numPixels + i] = data[srcBase] / 255;
100
+ float32[1 * numPixels + i] = data[srcBase + 1] / 255;
101
+ float32[2 * numPixels + i] = data[srcBase + 2] / 255;
102
+ }
103
+ return { data: float32, scale, padX, padY, originalWidth, originalHeight };
104
+ }
105
+ async function resizeAndNormalize(jpeg, targetWidth, targetHeight, normalization, layout) {
106
+ const { data } = await (0, import_sharp.default)(jpeg).resize(targetWidth, targetHeight).removeAlpha().raw().toBuffer({ resolveWithObject: true });
107
+ const numPixels = targetWidth * targetHeight;
108
+ const float32 = new Float32Array(3 * numPixels);
109
+ const mean = [0.485, 0.456, 0.406];
110
+ const std = [0.229, 0.224, 0.225];
111
+ if (layout === "nchw") {
112
+ for (let i = 0; i < numPixels; i++) {
113
+ const srcBase = i * 3;
114
+ for (let c = 0; c < 3; c++) {
115
+ const raw = data[srcBase + c] / 255;
116
+ let val;
117
+ if (normalization === "zero-one") {
118
+ val = raw;
119
+ } else if (normalization === "imagenet") {
120
+ val = (raw - mean[c]) / std[c];
121
+ } else {
122
+ val = data[srcBase + c];
123
+ }
124
+ float32[c * numPixels + i] = val;
125
+ }
126
+ }
127
+ } else {
128
+ for (let i = 0; i < numPixels; i++) {
129
+ const srcBase = i * 3;
130
+ for (let c = 0; c < 3; c++) {
131
+ const raw = data[srcBase + c] / 255;
132
+ let val;
133
+ if (normalization === "zero-one") {
134
+ val = raw;
135
+ } else if (normalization === "imagenet") {
136
+ val = (raw - mean[c]) / std[c];
137
+ } else {
138
+ val = data[srcBase + c];
139
+ }
140
+ float32[i * 3 + c] = val;
141
+ }
142
+ }
143
+ }
144
+ return float32;
145
+ }
146
+ function rgbToGrayscale(rgb, width, height) {
147
+ const numPixels = width * height;
148
+ const gray = new Uint8Array(numPixels);
149
+ for (let i = 0; i < numPixels; i++) {
150
+ const r = rgb[i * 3];
151
+ const g = rgb[i * 3 + 1];
152
+ const b = rgb[i * 3 + 2];
153
+ gray[i] = Math.round(0.299 * r + 0.587 * g + 0.114 * b);
154
+ }
155
+ return gray;
156
+ }
157
+
158
+ // src/shared/postprocess/yolo.ts
159
+ function iou(a, b) {
160
+ const ax1 = a.x;
161
+ const ay1 = a.y;
162
+ const ax2 = a.x + a.w;
163
+ const ay2 = a.y + a.h;
164
+ const bx1 = b.x;
165
+ const by1 = b.y;
166
+ const bx2 = b.x + b.w;
167
+ const by2 = b.y + b.h;
168
+ const interX1 = Math.max(ax1, bx1);
169
+ const interY1 = Math.max(ay1, by1);
170
+ const interX2 = Math.min(ax2, bx2);
171
+ const interY2 = Math.min(ay2, by2);
172
+ const interW = Math.max(0, interX2 - interX1);
173
+ const interH = Math.max(0, interY2 - interY1);
174
+ const interArea = interW * interH;
175
+ if (interArea === 0) return 0;
176
+ const areaA = a.w * a.h;
177
+ const areaB = b.w * b.h;
178
+ const unionArea = areaA + areaB - interArea;
179
+ return unionArea === 0 ? 0 : interArea / unionArea;
180
+ }
181
+ function nms(boxes, iouThreshold) {
182
+ const indices = boxes.map((_, i) => i).sort((a, b) => boxes[b].score - boxes[a].score);
183
+ const kept = [];
184
+ const suppressed = /* @__PURE__ */ new Set();
185
+ for (const idx of indices) {
186
+ if (suppressed.has(idx)) continue;
187
+ kept.push(idx);
188
+ for (const other of indices) {
189
+ if (other === idx || suppressed.has(other)) continue;
190
+ if (iou(boxes[idx].bbox, boxes[other].bbox) > iouThreshold) {
191
+ suppressed.add(other);
192
+ }
193
+ }
194
+ }
195
+ return kept;
196
+ }
197
+ function yoloPostprocess(output, numClasses, numBoxes, options) {
198
+ const { confidence, iouThreshold, labels, scale, padX, padY, originalWidth, originalHeight } = options;
199
+ const candidates = [];
200
+ for (let i = 0; i < numBoxes; i++) {
201
+ const cx = output[0 * numBoxes + i];
202
+ const cy = output[1 * numBoxes + i];
203
+ const w = output[2 * numBoxes + i];
204
+ const h = output[3 * numBoxes + i];
205
+ let bestScore = -Infinity;
206
+ let bestClass = 0;
207
+ for (let j = 0; j < numClasses; j++) {
208
+ const score = output[(4 + j) * numBoxes + i];
209
+ if (score > bestScore) {
210
+ bestScore = score;
211
+ bestClass = j;
212
+ }
213
+ }
214
+ if (bestScore < confidence) continue;
215
+ const bbox = {
216
+ x: cx - w / 2,
217
+ y: cy - h / 2,
218
+ w,
219
+ h
220
+ };
221
+ candidates.push({ bbox, score: bestScore, classIdx: bestClass });
222
+ }
223
+ if (candidates.length === 0) return [];
224
+ const keptIndices = nms(candidates, iouThreshold);
225
+ return keptIndices.map((idx) => {
226
+ const { bbox, score, classIdx } = candidates[idx];
227
+ const label = labels[classIdx] ?? String(classIdx);
228
+ const x = Math.max(0, Math.min(originalWidth, (bbox.x - padX) / scale));
229
+ const y = Math.max(0, Math.min(originalHeight, (bbox.y - padY) / scale));
230
+ const x2 = Math.max(0, Math.min(originalWidth, (bbox.x + bbox.w - padX) / scale));
231
+ const y2 = Math.max(0, Math.min(originalHeight, (bbox.y + bbox.h - padY) / scale));
232
+ const finalBbox = { x, y, w: x2 - x, h: y2 - y };
233
+ return {
234
+ class: label,
235
+ originalClass: label,
236
+ score,
237
+ bbox: finalBbox
238
+ };
239
+ });
240
+ }
241
+
242
+ // src/shared/postprocess/scrfd.ts
243
+ var STRIDES = [8, 16, 32];
244
+ var NUM_ANCHORS_PER_STRIDE = 2;
245
+ function generateAnchors(stride, inputSize) {
246
+ const featureSize = Math.ceil(inputSize / stride);
247
+ const anchors = [];
248
+ for (let y = 0; y < featureSize; y++) {
249
+ for (let x = 0; x < featureSize; x++) {
250
+ for (let k = 0; k < NUM_ANCHORS_PER_STRIDE; k++) {
251
+ anchors.push({
252
+ cx: (x + 0.5) * stride,
253
+ cy: (y + 0.5) * stride
254
+ });
255
+ }
256
+ }
257
+ }
258
+ return anchors;
259
+ }
260
+ function scrfdPostprocess(outputs, confidence, inputSize, originalWidth, originalHeight) {
261
+ const scaleX = originalWidth / inputSize;
262
+ const scaleY = originalHeight / inputSize;
263
+ const candidates = [];
264
+ for (const stride of STRIDES) {
265
+ const scoreKey = Object.keys(outputs).find((k) => k.includes(`score_${stride}`) || k.includes(`_${stride}_score`));
266
+ const bboxKey = Object.keys(outputs).find((k) => k.includes(`bbox_${stride}`) || k.includes(`_${stride}_bbox`));
267
+ const kpsKey = Object.keys(outputs).find((k) => k.includes(`kps_${stride}`) || k.includes(`_${stride}_kps`));
268
+ if (!scoreKey || !bboxKey) continue;
269
+ const scores = outputs[scoreKey];
270
+ const bboxes = outputs[bboxKey];
271
+ const kps = kpsKey ? outputs[kpsKey] : void 0;
272
+ const anchors = generateAnchors(stride, inputSize);
273
+ const n = anchors.length;
274
+ for (let i = 0; i < n; i++) {
275
+ const score = scores[i];
276
+ if (score < confidence) continue;
277
+ const anchor = anchors[i];
278
+ const x1 = anchor.cx - bboxes[i * 4] * stride;
279
+ const y1 = anchor.cy - bboxes[i * 4 + 1] * stride;
280
+ const x2 = anchor.cx + bboxes[i * 4 + 2] * stride;
281
+ const y2 = anchor.cy + bboxes[i * 4 + 3] * stride;
282
+ const bbox = {
283
+ x: x1 * scaleX,
284
+ y: y1 * scaleY,
285
+ w: (x2 - x1) * scaleX,
286
+ h: (y2 - y1) * scaleY
287
+ };
288
+ let landmarks;
289
+ if (kps) {
290
+ const pts = [];
291
+ for (let p = 0; p < 5; p++) {
292
+ pts.push({
293
+ x: (anchor.cx + kps[i * 10 + p * 2] * stride) * scaleX,
294
+ y: (anchor.cy + kps[i * 10 + p * 2 + 1] * stride) * scaleY
295
+ });
296
+ }
297
+ landmarks = pts;
298
+ }
299
+ candidates.push({ bbox, score, landmarks });
300
+ }
301
+ }
302
+ if (candidates.length === 0) return [];
303
+ const keptIndices = nms(candidates, 0.45);
304
+ return keptIndices.map((idx) => {
305
+ const { bbox, score, landmarks } = candidates[idx];
306
+ return {
307
+ class: "face",
308
+ originalClass: "face",
309
+ score,
310
+ bbox,
311
+ ...landmarks ? { landmarks } : {}
312
+ };
313
+ });
314
+ }
315
+
316
+ // src/shared/postprocess/arcface.ts
317
+ function l2Normalize(vec) {
318
+ let sumSq = 0;
319
+ for (let i = 0; i < vec.length; i++) {
320
+ sumSq += vec[i] * vec[i];
321
+ }
322
+ const norm = Math.sqrt(sumSq);
323
+ if (norm === 0) return new Float32Array(vec.length);
324
+ const out = new Float32Array(vec.length);
325
+ for (let i = 0; i < vec.length; i++) {
326
+ out[i] = vec[i] / norm;
327
+ }
328
+ return out;
329
+ }
330
+ function cosineSimilarity(a, b) {
331
+ if (a.length !== b.length) throw new Error("Embedding length mismatch");
332
+ let dot = 0;
333
+ for (let i = 0; i < a.length; i++) {
334
+ dot += a[i] * b[i];
335
+ }
336
+ return dot;
337
+ }
338
+
339
+ // src/shared/postprocess/paddleocr.ts
340
+ function ctcDecode(output, seqLen, numChars, charset) {
341
+ let totalLogScore = 0;
342
+ const rawIndices = [];
343
+ for (let t = 0; t < seqLen; t++) {
344
+ const offset = t * numChars;
345
+ let bestIdx = 0;
346
+ let bestVal = output[offset];
347
+ for (let c = 1; c < numChars; c++) {
348
+ const val = output[offset + c];
349
+ if (val > bestVal) {
350
+ bestVal = val;
351
+ bestIdx = c;
352
+ }
353
+ }
354
+ rawIndices.push(bestIdx);
355
+ totalLogScore += bestVal;
356
+ }
357
+ const collapsed = [];
358
+ for (let i = 0; i < rawIndices.length; i++) {
359
+ const cur = rawIndices[i];
360
+ if (i === 0 || cur !== rawIndices[i - 1]) {
361
+ collapsed.push(cur);
362
+ }
363
+ }
364
+ const filtered = collapsed.filter((idx) => idx !== 0);
365
+ const text = filtered.map((idx) => charset[idx] ?? "").join("");
366
+ const confidence = seqLen > 0 ? totalLogScore / seqLen : 0;
367
+ return { text, confidence };
368
+ }
369
+
370
+ // src/shared/postprocess/yamnet.ts
371
+ function yamnetPostprocess(output, numFrames, numClasses, classNames, minScore) {
372
+ const avgScores = new Float32Array(numClasses);
373
+ for (let f = 0; f < numFrames; f++) {
374
+ for (let c = 0; c < numClasses; c++) {
375
+ const prev = avgScores[c] ?? 0;
376
+ avgScores[c] = prev + (output[f * numClasses + c] ?? 0);
377
+ }
378
+ }
379
+ if (numFrames > 0) {
380
+ for (let c = 0; c < numClasses; c++) {
381
+ const val = avgScores[c] ?? 0;
382
+ avgScores[c] = val / numFrames;
383
+ }
384
+ }
385
+ const results = [];
386
+ for (let c = 0; c < numClasses; c++) {
387
+ const score = avgScores[c];
388
+ if (score >= minScore) {
389
+ results.push({
390
+ className: classNames[c] ?? String(c),
391
+ score
392
+ });
393
+ }
394
+ }
395
+ return results.sort((a, b) => b.score - a.score);
396
+ }
397
+
398
+ // src/shared/node-engine.ts
399
+ var path = __toESM(require("path"));
400
+ var BACKEND_TO_PROVIDER = {
401
+ cpu: "cpu",
402
+ coreml: "coreml",
403
+ cuda: "cuda",
404
+ tensorrt: "tensorrt",
405
+ dml: "dml"
406
+ };
407
+ var BACKEND_TO_DEVICE = {
408
+ cpu: "cpu",
409
+ coreml: "gpu-mps",
410
+ cuda: "gpu-cuda",
411
+ tensorrt: "tensorrt"
412
+ };
413
+ var NodeInferenceEngine = class {
414
+ constructor(modelPath, backend) {
415
+ this.modelPath = modelPath;
416
+ this.backend = backend;
417
+ this.device = BACKEND_TO_DEVICE[backend] ?? "cpu";
418
+ }
419
+ runtime = "onnx";
420
+ device;
421
+ session = null;
422
+ async initialize() {
423
+ const ort = await import("onnxruntime-node");
424
+ const provider = BACKEND_TO_PROVIDER[this.backend] ?? "cpu";
425
+ const absModelPath = path.isAbsolute(this.modelPath) ? this.modelPath : path.resolve(process.cwd(), this.modelPath);
426
+ const sessionOptions = {
427
+ executionProviders: [provider]
428
+ };
429
+ this.session = await ort.InferenceSession.create(absModelPath, sessionOptions);
430
+ }
431
+ async run(input, inputShape) {
432
+ if (!this.session) {
433
+ throw new Error("NodeInferenceEngine: not initialized \u2014 call initialize() first");
434
+ }
435
+ const ort = await import("onnxruntime-node");
436
+ const sess = this.session;
437
+ const inputName = sess.inputNames[0];
438
+ const tensor = new ort.Tensor("float32", input, [...inputShape]);
439
+ const feeds = { [inputName]: tensor };
440
+ const results = await sess.run(feeds);
441
+ const outputName = sess.outputNames[0];
442
+ const outputTensor = results[outputName];
443
+ return outputTensor.data;
444
+ }
445
+ async runMultiOutput(input, inputShape) {
446
+ if (!this.session) {
447
+ throw new Error("NodeInferenceEngine: not initialized \u2014 call initialize() first");
448
+ }
449
+ const ort = await import("onnxruntime-node");
450
+ const sess = this.session;
451
+ const inputName = sess.inputNames[0];
452
+ const tensor = new ort.Tensor("float32", input, [...inputShape]);
453
+ const feeds = { [inputName]: tensor };
454
+ const results = await sess.run(feeds);
455
+ const out = {};
456
+ for (const name of sess.outputNames) {
457
+ out[name] = results[name].data;
458
+ }
459
+ return out;
460
+ }
461
+ async dispose() {
462
+ this.session = null;
463
+ }
464
+ };
465
+
466
+ // src/shared/python-engine.ts
467
+ var import_node_child_process = require("child_process");
468
+ var PythonInferenceEngine = class {
469
+ constructor(pythonPath, runtime, modelPath, extraArgs = []) {
470
+ this.pythonPath = pythonPath;
471
+ this.modelPath = modelPath;
472
+ this.extraArgs = extraArgs;
473
+ this.runtime = runtime;
474
+ const runtimeDeviceMap = {
475
+ onnx: "cpu",
476
+ coreml: "gpu-mps",
477
+ pytorch: "cpu",
478
+ openvino: "cpu",
479
+ tflite: "cpu"
480
+ };
481
+ this.device = runtimeDeviceMap[runtime];
482
+ }
483
+ runtime;
484
+ device;
485
+ process = null;
486
+ receiveBuffer = Buffer.alloc(0);
487
+ pendingResolve = null;
488
+ pendingReject = null;
489
+ async initialize() {
490
+ const args = [this.modelPath, ...this.extraArgs];
491
+ this.process = (0, import_node_child_process.spawn)(this.pythonPath, args, {
492
+ stdio: ["pipe", "pipe", "pipe"]
493
+ });
494
+ if (!this.process.stdout || !this.process.stdin) {
495
+ throw new Error("PythonInferenceEngine: failed to create process pipes");
496
+ }
497
+ this.process.stderr?.on("data", (chunk) => {
498
+ process.stderr.write(`[python-engine] ${chunk.toString()}`);
499
+ });
500
+ this.process.on("error", (err) => {
501
+ this.pendingReject?.(err);
502
+ this.pendingReject = null;
503
+ this.pendingResolve = null;
504
+ });
505
+ this.process.on("exit", (code) => {
506
+ if (code !== 0) {
507
+ const err = new Error(`PythonInferenceEngine: process exited with code ${code}`);
508
+ this.pendingReject?.(err);
509
+ this.pendingReject = null;
510
+ this.pendingResolve = null;
511
+ }
512
+ });
513
+ this.process.stdout.on("data", (chunk) => {
514
+ this.receiveBuffer = Buffer.concat([this.receiveBuffer, chunk]);
515
+ this._tryReceive();
516
+ });
517
+ await new Promise((resolve2, reject) => {
518
+ const timeout = setTimeout(() => resolve2(), 2e3);
519
+ this.process?.on("error", (err) => {
520
+ clearTimeout(timeout);
521
+ reject(err);
522
+ });
523
+ this.process?.on("exit", (code) => {
524
+ clearTimeout(timeout);
525
+ if (code !== 0) {
526
+ reject(new Error(`PythonInferenceEngine: process exited early with code ${code}`));
527
+ }
528
+ });
529
+ });
530
+ }
531
+ _tryReceive() {
532
+ if (this.receiveBuffer.length < 4) return;
533
+ const length = this.receiveBuffer.readUInt32LE(0);
534
+ if (this.receiveBuffer.length < 4 + length) return;
535
+ const jsonBytes = this.receiveBuffer.subarray(4, 4 + length);
536
+ this.receiveBuffer = this.receiveBuffer.subarray(4 + length);
537
+ const resolve2 = this.pendingResolve;
538
+ const reject = this.pendingReject;
539
+ this.pendingResolve = null;
540
+ this.pendingReject = null;
541
+ if (!resolve2) return;
542
+ try {
543
+ const parsed = JSON.parse(jsonBytes.toString("utf8"));
544
+ resolve2(parsed);
545
+ } catch (err) {
546
+ reject?.(err instanceof Error ? err : new Error(String(err)));
547
+ }
548
+ }
549
+ /** Send JPEG buffer, receive JSON detection results */
550
+ async runJpeg(jpeg) {
551
+ if (!this.process?.stdin) {
552
+ throw new Error("PythonInferenceEngine: process not initialized");
553
+ }
554
+ return new Promise((resolve2, reject) => {
555
+ this.pendingResolve = resolve2;
556
+ this.pendingReject = reject;
557
+ const lengthBuf = Buffer.allocUnsafe(4);
558
+ lengthBuf.writeUInt32LE(jpeg.length, 0);
559
+ this.process.stdin.write(Buffer.concat([lengthBuf, jpeg]));
560
+ });
561
+ }
562
+ /** IInferenceEngine.run — wraps runJpeg for compatibility */
563
+ async run(_input, _inputShape) {
564
+ throw new Error(
565
+ "PythonInferenceEngine: use runJpeg() directly \u2014 this engine operates on JPEG input"
566
+ );
567
+ }
568
+ /** IInferenceEngine.runMultiOutput — not supported by Python engine (operates on JPEG input) */
569
+ async runMultiOutput(_input, _inputShape) {
570
+ throw new Error(
571
+ "PythonInferenceEngine: runMultiOutput() is not supported \u2014 this engine operates on JPEG input"
572
+ );
573
+ }
574
+ async dispose() {
575
+ if (this.process) {
576
+ this.process.stdin?.end();
577
+ this.process.kill("SIGTERM");
578
+ this.process = null;
579
+ }
580
+ }
581
+ };
582
+
583
+ // src/shared/engine-resolver.ts
584
+ var fs = __toESM(require("fs"));
585
+ var path2 = __toESM(require("path"));
586
+ var AUTO_BACKEND_PRIORITY = ["coreml", "cuda", "tensorrt", "cpu"];
587
+ var BACKEND_TO_FORMAT = {
588
+ cpu: "onnx",
589
+ coreml: "coreml",
590
+ cuda: "onnx",
591
+ tensorrt: "onnx"
592
+ };
593
+ var RUNTIME_TO_FORMAT = {
594
+ onnx: "onnx",
595
+ coreml: "coreml",
596
+ openvino: "openvino",
597
+ tflite: "tflite",
598
+ pytorch: "pt"
599
+ };
600
+ function modelFilePath(modelsDir, modelEntry, format) {
601
+ const formatEntry = modelEntry.formats[format];
602
+ if (!formatEntry) {
603
+ throw new Error(`Model ${modelEntry.id} has no ${format} format`);
604
+ }
605
+ const urlParts = formatEntry.url.split("/");
606
+ const filename = urlParts[urlParts.length - 1] ?? `${modelEntry.id}.${format}`;
607
+ return path2.join(modelsDir, filename);
608
+ }
609
+ function modelExists(filePath) {
610
+ try {
611
+ return fs.existsSync(filePath);
612
+ } catch {
613
+ return false;
614
+ }
615
+ }
616
+ async function resolveEngine(options) {
617
+ const { runtime, backend, modelEntry, modelsDir, downloadModel } = options;
618
+ let selectedFormat;
619
+ let selectedBackend;
620
+ if (runtime === "auto") {
621
+ const available = await probeOnnxBackends();
622
+ let chosen = null;
623
+ for (const b of AUTO_BACKEND_PRIORITY) {
624
+ if (!available.includes(b)) continue;
625
+ const fmt = BACKEND_TO_FORMAT[b];
626
+ if (!fmt) continue;
627
+ if (!modelEntry.formats[fmt]) continue;
628
+ chosen = { backend: b, format: fmt };
629
+ break;
630
+ }
631
+ if (!chosen) {
632
+ throw new Error(
633
+ `resolveEngine: no compatible backend found for model ${modelEntry.id}. Available backends: ${available.join(", ")}`
634
+ );
635
+ }
636
+ selectedFormat = chosen.format;
637
+ selectedBackend = chosen.backend;
638
+ } else {
639
+ const fmt = RUNTIME_TO_FORMAT[runtime];
640
+ if (!fmt) {
641
+ throw new Error(`resolveEngine: unsupported runtime "${runtime}"`);
642
+ }
643
+ if (!modelEntry.formats[fmt]) {
644
+ throw new Error(
645
+ `resolveEngine: model ${modelEntry.id} has no ${fmt} format for runtime ${runtime}`
646
+ );
647
+ }
648
+ selectedFormat = fmt;
649
+ selectedBackend = runtime === "onnx" ? backend || "cpu" : runtime;
650
+ }
651
+ let modelPath = modelFilePath(modelsDir, modelEntry, selectedFormat);
652
+ if (!modelExists(modelPath)) {
653
+ if (downloadModel) {
654
+ const formatEntry = modelEntry.formats[selectedFormat];
655
+ modelPath = await downloadModel(formatEntry.url, modelsDir);
656
+ } else {
657
+ throw new Error(
658
+ `resolveEngine: model file not found at ${modelPath} and no downloadModel function provided`
659
+ );
660
+ }
661
+ }
662
+ if (selectedFormat === "onnx" || selectedFormat === "coreml") {
663
+ const engine = new NodeInferenceEngine(modelPath, selectedBackend);
664
+ await engine.initialize();
665
+ return { engine, format: selectedFormat, modelPath };
666
+ }
667
+ const fallbackPath = modelFilePath(modelsDir, modelEntry, "onnx");
668
+ if (modelEntry.formats["onnx"] && modelExists(fallbackPath)) {
669
+ const engine = new NodeInferenceEngine(fallbackPath, "cpu");
670
+ await engine.initialize();
671
+ return { engine, format: "onnx", modelPath: fallbackPath };
672
+ }
673
+ throw new Error(
674
+ `resolveEngine: format ${selectedFormat} is not yet supported by NodeInferenceEngine and no ONNX fallback is available`
675
+ );
676
+ }
677
+ async function probeOnnxBackends() {
678
+ const available = ["cpu"];
679
+ try {
680
+ const ort = await import("onnxruntime-node");
681
+ const providers = ort.env?.webgl?.disabled !== void 0 ? ort.InferenceSession?.getAvailableProviders?.() ?? [] : [];
682
+ for (const p of providers) {
683
+ const normalized = p.toLowerCase().replace("executionprovider", "");
684
+ if (normalized === "coreml") available.push("coreml");
685
+ else if (normalized === "cuda") available.push("cuda");
686
+ else if (normalized === "tensorrt") available.push("tensorrt");
687
+ }
688
+ } catch {
689
+ }
690
+ if (process.platform === "darwin" && !available.includes("coreml")) {
691
+ available.push("coreml");
692
+ }
693
+ return [...new Set(available)];
694
+ }
695
+
696
+ // src/addons/motion-detection/frame-diff.ts
697
+ function detectMotion(current, previous, width, height, threshold, minArea) {
698
+ const numPixels = width * height;
699
+ const mask = new Uint8Array(numPixels);
700
+ for (let i = 0; i < numPixels; i++) {
701
+ mask[i] = Math.abs((current[i] ?? 0) - (previous[i] ?? 0)) > threshold ? 1 : 0;
702
+ }
703
+ const labels = new Int32Array(numPixels).fill(0);
704
+ const parent = new Int32Array(numPixels + 1).fill(0);
705
+ let nextLabel = 1;
706
+ function findRoot(x) {
707
+ while (parent[x] !== x) {
708
+ parent[x] = parent[parent[x]];
709
+ x = parent[x];
710
+ }
711
+ return x;
712
+ }
713
+ function union(a, b) {
714
+ const ra = findRoot(a);
715
+ const rb = findRoot(b);
716
+ if (ra !== rb) parent[rb] = ra;
717
+ return ra;
718
+ }
719
+ for (let i = 0; i <= numPixels; i++) {
720
+ parent[i] = i;
721
+ }
722
+ for (let y = 0; y < height; y++) {
723
+ for (let x = 0; x < width; x++) {
724
+ const idx = y * width + x;
725
+ if (!mask[idx]) continue;
726
+ const above = y > 0 ? labels[(y - 1) * width + x] ?? 0 : 0;
727
+ const left = x > 0 ? labels[y * width + (x - 1)] ?? 0 : 0;
728
+ if (above === 0 && left === 0) {
729
+ labels[idx] = nextLabel;
730
+ parent[nextLabel] = nextLabel;
731
+ nextLabel++;
732
+ } else if (above !== 0 && left === 0) {
733
+ labels[idx] = above;
734
+ } else if (above === 0 && left !== 0) {
735
+ labels[idx] = left;
736
+ } else {
737
+ labels[idx] = union(above, left);
738
+ }
739
+ }
740
+ }
741
+ for (let i = 0; i < numPixels; i++) {
742
+ if (labels[i]) {
743
+ labels[i] = findRoot(labels[i]);
744
+ }
745
+ }
746
+ const bboxMap = /* @__PURE__ */ new Map();
747
+ for (let y = 0; y < height; y++) {
748
+ for (let x = 0; x < width; x++) {
749
+ const idx = y * width + x;
750
+ const label = labels[idx];
751
+ if (!label) continue;
752
+ const diff = Math.abs((current[idx] ?? 0) - (previous[idx] ?? 0));
753
+ const existing = bboxMap.get(label);
754
+ if (existing) {
755
+ existing.minX = Math.min(existing.minX, x);
756
+ existing.minY = Math.min(existing.minY, y);
757
+ existing.maxX = Math.max(existing.maxX, x);
758
+ existing.maxY = Math.max(existing.maxY, y);
759
+ existing.count++;
760
+ existing.intensitySum += diff;
761
+ } else {
762
+ bboxMap.set(label, {
763
+ minX: x,
764
+ minY: y,
765
+ maxX: x,
766
+ maxY: y,
767
+ count: 1,
768
+ intensitySum: diff
769
+ });
770
+ }
771
+ }
772
+ }
773
+ const regions = [];
774
+ for (const [, info] of bboxMap) {
775
+ if (info.count < minArea) continue;
776
+ regions.push({
777
+ bbox: {
778
+ x: info.minX,
779
+ y: info.minY,
780
+ w: info.maxX - info.minX + 1,
781
+ h: info.maxY - info.minY + 1
782
+ },
783
+ pixelCount: info.count,
784
+ intensity: info.intensitySum / info.count
785
+ });
786
+ }
787
+ return regions;
788
+ }
789
+
790
+ // src/addons/object-detection/index.ts
791
+ var import_types = require("@camstack/types");
792
+
793
+ // src/shared/postprocess/yolo-seg.ts
794
+ function sigmoid(x) {
795
+ return 1 / (1 + Math.exp(-x));
796
+ }
797
+ function computeRawMask(coeffs, protos, numMaskCoeffs, maskH, maskW) {
798
+ const maskSize = maskH * maskW;
799
+ const rawMask = new Float32Array(maskSize);
800
+ for (let px = 0; px < maskSize; px++) {
801
+ let val = 0;
802
+ for (let k = 0; k < numMaskCoeffs; k++) {
803
+ val += (coeffs[k] ?? 0) * (protos[k * maskSize + px] ?? 0);
804
+ }
805
+ rawMask[px] = sigmoid(val);
806
+ }
807
+ return rawMask;
808
+ }
809
+ function cropAndThresholdMask(rawMask, maskH, maskW, bbox, maskThreshold, maskScale) {
810
+ const cropX1 = Math.max(0, Math.floor(bbox.x * maskScale));
811
+ const cropY1 = Math.max(0, Math.floor(bbox.y * maskScale));
812
+ const cropX2 = Math.min(maskW, Math.ceil((bbox.x + bbox.w) * maskScale));
813
+ const cropY2 = Math.min(maskH, Math.ceil((bbox.y + bbox.h) * maskScale));
814
+ const cropW = Math.max(1, cropX2 - cropX1);
815
+ const cropH = Math.max(1, cropY2 - cropY1);
816
+ const data = new Uint8Array(cropW * cropH);
817
+ for (let row = 0; row < cropH; row++) {
818
+ const srcRow = cropY1 + row;
819
+ for (let col = 0; col < cropW; col++) {
820
+ const srcCol = cropX1 + col;
821
+ const srcIdx = srcRow * maskW + srcCol;
822
+ data[row * cropW + col] = (rawMask[srcIdx] ?? 0) > maskThreshold ? 255 : 0;
823
+ }
824
+ }
825
+ return { data, width: cropW, height: cropH };
826
+ }
827
+ function yoloSegPostprocess(segOutput, options) {
828
+ const {
829
+ detectionOutput,
830
+ protoOutput,
831
+ numClasses,
832
+ numBoxes,
833
+ numMaskCoeffs,
834
+ maskHeight,
835
+ maskWidth
836
+ } = segOutput;
837
+ const {
838
+ confidence,
839
+ iouThreshold,
840
+ labels,
841
+ scale,
842
+ padX,
843
+ padY,
844
+ originalWidth,
845
+ originalHeight,
846
+ maskThreshold = 0.5
847
+ } = options;
848
+ const yoloInputSize = 640;
849
+ const maskScale = maskHeight / yoloInputSize;
850
+ const candidates = [];
851
+ for (let i = 0; i < numBoxes; i++) {
852
+ const cx = detectionOutput[0 * numBoxes + i] ?? 0;
853
+ const cy = detectionOutput[1 * numBoxes + i] ?? 0;
854
+ const w = detectionOutput[2 * numBoxes + i] ?? 0;
855
+ const h = detectionOutput[3 * numBoxes + i] ?? 0;
856
+ let bestScore = -Infinity;
857
+ let bestClass = 0;
858
+ for (let j = 0; j < numClasses; j++) {
859
+ const score = detectionOutput[(4 + j) * numBoxes + i] ?? 0;
860
+ if (score > bestScore) {
861
+ bestScore = score;
862
+ bestClass = j;
863
+ }
864
+ }
865
+ if (bestScore < confidence) continue;
866
+ const bbox = {
867
+ x: cx - w / 2,
868
+ y: cy - h / 2,
869
+ w,
870
+ h
871
+ };
872
+ const coeffs = new Float32Array(numMaskCoeffs);
873
+ for (let k = 0; k < numMaskCoeffs; k++) {
874
+ coeffs[k] = detectionOutput[(4 + numClasses + k) * numBoxes + i] ?? 0;
875
+ }
876
+ candidates.push({ bbox, score: bestScore, classIdx: bestClass, coeffs });
877
+ }
878
+ if (candidates.length === 0) return [];
879
+ const keptIndices = nms(candidates, iouThreshold);
880
+ return keptIndices.map((idx) => {
881
+ const { bbox, score, classIdx, coeffs } = candidates[idx];
882
+ const label = labels[classIdx] ?? String(classIdx);
883
+ const x = Math.max(0, Math.min(originalWidth, (bbox.x - padX) / scale));
884
+ const y = Math.max(0, Math.min(originalHeight, (bbox.y - padY) / scale));
885
+ const x2 = Math.max(0, Math.min(originalWidth, (bbox.x + bbox.w - padX) / scale));
886
+ const y2 = Math.max(0, Math.min(originalHeight, (bbox.y + bbox.h - padY) / scale));
887
+ const finalBbox = { x, y, w: x2 - x, h: y2 - y };
888
+ const rawMask = computeRawMask(coeffs, protoOutput, numMaskCoeffs, maskHeight, maskWidth);
889
+ const { data: maskData, width: mW, height: mH } = cropAndThresholdMask(
890
+ rawMask,
891
+ maskHeight,
892
+ maskWidth,
893
+ bbox,
894
+ maskThreshold,
895
+ maskScale
896
+ );
897
+ return {
898
+ class: label,
899
+ originalClass: label,
900
+ score,
901
+ bbox: finalBbox,
902
+ mask: maskData,
903
+ maskWidth: mW,
904
+ maskHeight: mH
905
+ };
906
+ });
907
+ }
908
+
909
+ // src/addons/object-detection/index.ts
910
+ function isSegModel(modelId) {
911
+ return modelId.includes("-seg");
912
+ }
913
+ var ALL_DETECTION_MODELS = [
914
+ ...import_types.OBJECT_DETECTION_MODELS,
915
+ ...import_types.SEGMENTATION_MODELS
916
+ ];
917
+ function applyClassMap(detections, classMap) {
918
+ return detections.filter((d) => classMap.mapping[d.class] !== void 0).map((d) => ({
919
+ ...d,
920
+ originalClass: d.class,
921
+ class: classMap.mapping[d.class]
922
+ }));
923
+ }
924
+ var ObjectDetectionAddon = class {
925
+ id = "object-detection";
926
+ slot = "detector";
927
+ inputClasses = null;
928
+ outputClasses = ["person", "vehicle", "animal"];
929
+ slotPriority = 0;
930
+ manifest = {
931
+ id: "object-detection",
932
+ name: "Object Detection",
933
+ version: "0.1.0",
934
+ description: "YOLO-based object detection \u2014 detects persons, vehicles, and animals",
935
+ packageName: "@camstack/vision",
936
+ slot: "detector",
937
+ inputClasses: void 0,
938
+ outputClasses: ["person", "vehicle", "animal"],
939
+ supportsCustomModels: true,
940
+ mayRequirePython: false,
941
+ defaultConfig: {
942
+ modelId: "yolov8n",
943
+ runtime: "auto",
944
+ backend: "cpu",
945
+ confidence: 0.5,
946
+ iouThreshold: 0.45
947
+ }
948
+ };
949
+ engine;
950
+ modelEntry;
951
+ confidence = 0.5;
952
+ iouThreshold = 0.45;
953
+ async initialize(ctx) {
954
+ const cfg = ctx.addonConfig;
955
+ const modelId = cfg["modelId"] ?? "yolov8n";
956
+ const runtime = cfg["runtime"] ?? "auto";
957
+ const backend = cfg["backend"] ?? "cpu";
958
+ this.confidence = cfg["confidence"] ?? 0.5;
959
+ this.iouThreshold = cfg["iouThreshold"] ?? 0.45;
960
+ const entry = ALL_DETECTION_MODELS.find((m) => m.id === modelId);
961
+ if (!entry) {
962
+ throw new Error(`ObjectDetectionAddon: unknown modelId "${modelId}"`);
963
+ }
964
+ this.modelEntry = entry;
965
+ const resolved = await resolveEngine({
966
+ runtime,
967
+ backend,
968
+ modelEntry: entry,
969
+ modelsDir: ctx.locationPaths.models
970
+ });
971
+ this.engine = resolved.engine;
972
+ }
973
+ async detect(frame) {
974
+ const start = Date.now();
975
+ const { width: inputW, height: inputH } = this.modelEntry.inputSize;
976
+ const targetSize = Math.max(inputW, inputH);
977
+ const lb = await letterbox(frame.data, targetSize);
978
+ const numClasses = this.modelEntry.labels.length;
979
+ const labels = this.modelEntry.labels.map((l) => l.id);
980
+ const postprocessOpts = {
981
+ confidence: this.confidence,
982
+ iouThreshold: this.iouThreshold,
983
+ labels,
984
+ scale: lb.scale,
985
+ padX: lb.padX,
986
+ padY: lb.padY,
987
+ originalWidth: lb.originalWidth,
988
+ originalHeight: lb.originalHeight
989
+ };
990
+ let rawDetections;
991
+ if (isSegModel(this.modelEntry.id)) {
992
+ const outputs = await this.engine.runMultiOutput(lb.data, [1, 3, targetSize, targetSize]);
993
+ const outputNames = Object.keys(outputs);
994
+ if (outputNames.length < 2) {
995
+ throw new Error(
996
+ `ObjectDetectionAddon: seg model "${this.modelEntry.id}" returned ${outputNames.length} output(s); expected 2`
997
+ );
998
+ }
999
+ const detectionOutput = outputs[outputNames[0]];
1000
+ const protoOutput = outputs[outputNames[1]];
1001
+ const numMaskCoeffs = 32;
1002
+ const numBoxes = detectionOutput.length / (4 + numClasses + numMaskCoeffs);
1003
+ const maskHeight = 160;
1004
+ const maskWidth = 160;
1005
+ rawDetections = yoloSegPostprocess(
1006
+ {
1007
+ detectionOutput,
1008
+ protoOutput,
1009
+ numClasses,
1010
+ numBoxes,
1011
+ numMaskCoeffs,
1012
+ maskHeight,
1013
+ maskWidth
1014
+ },
1015
+ postprocessOpts
1016
+ );
1017
+ } else {
1018
+ const output = await this.engine.run(lb.data, [1, 3, targetSize, targetSize]);
1019
+ const numBoxes = output.length / (4 + numClasses);
1020
+ rawDetections = yoloPostprocess(output, numClasses, numBoxes, postprocessOpts);
1021
+ }
1022
+ const detections = applyClassMap(rawDetections, import_types.COCO_TO_MACRO);
1023
+ return {
1024
+ detections,
1025
+ inferenceMs: Date.now() - start,
1026
+ modelId: this.modelEntry.id
1027
+ };
1028
+ }
1029
+ async shutdown() {
1030
+ await this.engine?.dispose();
1031
+ }
1032
+ getConfigSchema() {
1033
+ return {
1034
+ sections: [
1035
+ {
1036
+ id: "model",
1037
+ title: "Model",
1038
+ columns: 2,
1039
+ fields: [
1040
+ {
1041
+ key: "modelId",
1042
+ label: "Model",
1043
+ type: "model-selector",
1044
+ catalog: [...ALL_DETECTION_MODELS],
1045
+ allowCustom: true,
1046
+ allowConversion: true,
1047
+ acceptFormats: ["onnx", "coreml", "openvino", "tflite"],
1048
+ requiredMetadata: ["inputSize", "labels", "outputFormat"],
1049
+ outputFormatHint: "yolo"
1050
+ }
1051
+ ]
1052
+ },
1053
+ {
1054
+ id: "runtime",
1055
+ title: "Runtime",
1056
+ columns: 2,
1057
+ fields: [
1058
+ {
1059
+ key: "runtime",
1060
+ label: "Runtime",
1061
+ type: "select",
1062
+ options: [
1063
+ { value: "auto", label: "Auto (recommended)" },
1064
+ { value: "onnx", label: "ONNX Runtime" },
1065
+ { value: "coreml", label: "CoreML (Apple)" },
1066
+ { value: "openvino", label: "OpenVINO (Intel)" }
1067
+ ]
1068
+ },
1069
+ {
1070
+ key: "backend",
1071
+ label: "Backend",
1072
+ type: "select",
1073
+ dependsOn: { runtime: "onnx" },
1074
+ options: [
1075
+ { value: "cpu", label: "CPU" },
1076
+ { value: "coreml", label: "CoreML" },
1077
+ { value: "cuda", label: "CUDA (NVIDIA)" },
1078
+ { value: "tensorrt", label: "TensorRT (NVIDIA)" }
1079
+ ]
1080
+ }
1081
+ ]
1082
+ },
1083
+ {
1084
+ id: "thresholds",
1085
+ title: "Detection Thresholds",
1086
+ columns: 2,
1087
+ fields: [
1088
+ {
1089
+ key: "confidence",
1090
+ label: "Confidence Threshold",
1091
+ type: "slider",
1092
+ min: 0.1,
1093
+ max: 1,
1094
+ step: 0.05,
1095
+ default: 0.5
1096
+ },
1097
+ {
1098
+ key: "iouThreshold",
1099
+ label: "IoU Threshold (NMS)",
1100
+ type: "slider",
1101
+ min: 0.1,
1102
+ max: 1,
1103
+ step: 0.05,
1104
+ default: 0.45
1105
+ }
1106
+ ]
1107
+ }
1108
+ ]
1109
+ };
1110
+ }
1111
+ getClassMap() {
1112
+ return import_types.COCO_TO_MACRO;
1113
+ }
1114
+ getModelCatalog() {
1115
+ return [...ALL_DETECTION_MODELS];
1116
+ }
1117
+ getAvailableModels() {
1118
+ return [];
1119
+ }
1120
+ getActiveLabels() {
1121
+ return import_types.MACRO_LABELS;
1122
+ }
1123
+ async probe() {
1124
+ return {
1125
+ available: true,
1126
+ runtime: this.engine?.runtime ?? "onnx",
1127
+ device: this.engine?.device ?? "cpu",
1128
+ capabilities: ["fp32"]
1129
+ };
1130
+ }
1131
+ };
1132
+
1133
+ // src/addons/motion-detection/index.ts
1134
+ var MOTION_LABEL = { id: "motion", name: "Motion" };
1135
+ var MOTION_LABELS = [MOTION_LABEL];
1136
+ var EMPTY_CLASS_MAP = { mapping: {}, preserveOriginal: true };
1137
+ var MotionDetectionAddon = class {
1138
+ id = "motion-detection";
1139
+ slot = "detector";
1140
+ inputClasses = null;
1141
+ outputClasses = ["motion"];
1142
+ slotPriority = 10;
1143
+ // runs first — feeds other detectors
1144
+ manifest = {
1145
+ id: "motion-detection",
1146
+ name: "Motion Detection",
1147
+ version: "0.1.0",
1148
+ description: "Frame-differencing motion detector \u2014 no inference engine required",
1149
+ packageName: "@camstack/vision",
1150
+ slot: "detector",
1151
+ inputClasses: void 0,
1152
+ outputClasses: ["motion"],
1153
+ supportsCustomModels: false,
1154
+ mayRequirePython: false,
1155
+ defaultConfig: {
1156
+ threshold: 25,
1157
+ minArea: 500
1158
+ }
1159
+ };
1160
+ previousGray = null;
1161
+ previousWidth = 0;
1162
+ previousHeight = 0;
1163
+ threshold = 25;
1164
+ minArea = 500;
1165
+ async initialize(ctx) {
1166
+ const cfg = ctx.addonConfig;
1167
+ this.threshold = cfg["threshold"] ?? 25;
1168
+ this.minArea = cfg["minArea"] ?? 500;
1169
+ }
1170
+ async detect(frame) {
1171
+ const start = Date.now();
1172
+ const { data, width, height } = await jpegToRgb(frame.data);
1173
+ const currentGray = rgbToGrayscale(data, width, height);
1174
+ if (!this.previousGray || this.previousWidth !== width || this.previousHeight !== height) {
1175
+ this.previousGray = currentGray;
1176
+ this.previousWidth = width;
1177
+ this.previousHeight = height;
1178
+ return { detections: [], inferenceMs: Date.now() - start, modelId: "frame-diff" };
1179
+ }
1180
+ const regions = detectMotion(
1181
+ currentGray,
1182
+ this.previousGray,
1183
+ width,
1184
+ height,
1185
+ this.threshold,
1186
+ this.minArea
1187
+ );
1188
+ this.previousGray = currentGray;
1189
+ const detections = regions.map((r) => ({
1190
+ class: "motion",
1191
+ originalClass: "motion",
1192
+ score: Math.min(1, r.intensity / 255),
1193
+ bbox: r.bbox
1194
+ }));
1195
+ return {
1196
+ detections,
1197
+ inferenceMs: Date.now() - start,
1198
+ modelId: "frame-diff"
1199
+ };
1200
+ }
1201
+ async shutdown() {
1202
+ this.previousGray = null;
1203
+ }
1204
+ getConfigSchema() {
1205
+ return {
1206
+ sections: [
1207
+ {
1208
+ id: "motion",
1209
+ title: "Motion Detection",
1210
+ columns: 2,
1211
+ fields: [
1212
+ {
1213
+ key: "threshold",
1214
+ label: "Pixel Difference Threshold",
1215
+ description: "Minimum per-pixel intensity change to count as motion (0-255)",
1216
+ type: "slider",
1217
+ min: 5,
1218
+ max: 100,
1219
+ step: 5,
1220
+ default: 25
1221
+ },
1222
+ {
1223
+ key: "minArea",
1224
+ label: "Minimum Region Area (px)",
1225
+ description: "Minimum number of changed pixels to report a motion region",
1226
+ type: "number",
1227
+ min: 50,
1228
+ max: 1e4
1229
+ }
1230
+ ]
1231
+ }
1232
+ ]
1233
+ };
1234
+ }
1235
+ getClassMap() {
1236
+ return EMPTY_CLASS_MAP;
1237
+ }
1238
+ getModelCatalog() {
1239
+ return [];
1240
+ }
1241
+ getAvailableModels() {
1242
+ return [];
1243
+ }
1244
+ getActiveLabels() {
1245
+ return MOTION_LABELS;
1246
+ }
1247
+ async probe() {
1248
+ return {
1249
+ available: true,
1250
+ runtime: "onnx",
1251
+ // no inference; satisfies the type (any runtime works)
1252
+ device: "cpu",
1253
+ capabilities: ["fp32"]
1254
+ };
1255
+ }
1256
+ };
1257
+
1258
+ // src/addons/face-detection/index.ts
1259
+ var import_types2 = require("@camstack/types");
1260
+ var FACE_LABEL = { id: "face", name: "Face" };
1261
+ var FACE_LABELS = [FACE_LABEL];
1262
+ var FACE_CLASS_MAP = { mapping: {}, preserveOriginal: true };
1263
+ var FaceDetectionAddon = class {
1264
+ id = "face-detection";
1265
+ slot = "cropper";
1266
+ inputClasses = ["person"];
1267
+ outputClasses = ["face"];
1268
+ slotPriority = 0;
1269
+ manifest = {
1270
+ id: "face-detection",
1271
+ name: "Face Detection",
1272
+ version: "0.1.0",
1273
+ description: "SCRFD-based face detector \u2014 crops face regions from person detections",
1274
+ packageName: "@camstack/vision",
1275
+ slot: "cropper",
1276
+ inputClasses: ["person"],
1277
+ outputClasses: ["face"],
1278
+ supportsCustomModels: false,
1279
+ mayRequirePython: false,
1280
+ defaultConfig: {
1281
+ modelId: "scrfd-500m",
1282
+ runtime: "auto",
1283
+ backend: "cpu",
1284
+ confidence: 0.5
1285
+ }
1286
+ };
1287
+ engine;
1288
+ modelEntry;
1289
+ confidence = 0.5;
1290
+ async initialize(ctx) {
1291
+ const cfg = ctx.addonConfig;
1292
+ const modelId = cfg["modelId"] ?? "scrfd-500m";
1293
+ const runtime = cfg["runtime"] ?? "auto";
1294
+ const backend = cfg["backend"] ?? "cpu";
1295
+ this.confidence = cfg["confidence"] ?? 0.5;
1296
+ const entry = import_types2.FACE_DETECTION_MODELS.find((m) => m.id === modelId);
1297
+ if (!entry) {
1298
+ throw new Error(`FaceDetectionAddon: unknown modelId "${modelId}"`);
1299
+ }
1300
+ this.modelEntry = entry;
1301
+ const resolved = await resolveEngine({
1302
+ runtime,
1303
+ backend,
1304
+ modelEntry: entry,
1305
+ modelsDir: ctx.locationPaths.models
1306
+ });
1307
+ this.engine = resolved.engine;
1308
+ }
1309
+ async crop(input) {
1310
+ const start = Date.now();
1311
+ const { width: inputW, height: inputH } = this.modelEntry.inputSize;
1312
+ const targetSize = Math.max(inputW, inputH);
1313
+ const personCrop = await cropRegion(input.frame.data, input.roi);
1314
+ const lb = await letterbox(personCrop, targetSize);
1315
+ const engineWithMulti = this.engine;
1316
+ let outputs;
1317
+ if (typeof engineWithMulti.runMultiOutput === "function") {
1318
+ outputs = await engineWithMulti.runMultiOutput(lb.data, [1, 3, targetSize, targetSize]);
1319
+ } else {
1320
+ const single = await this.engine.run(lb.data, [1, 3, targetSize, targetSize]);
1321
+ outputs = { output0: single };
1322
+ }
1323
+ const crops = scrfdPostprocess(
1324
+ outputs,
1325
+ this.confidence,
1326
+ targetSize,
1327
+ lb.originalWidth,
1328
+ lb.originalHeight
1329
+ );
1330
+ return {
1331
+ crops,
1332
+ inferenceMs: Date.now() - start,
1333
+ modelId: this.modelEntry.id
1334
+ };
1335
+ }
1336
+ async shutdown() {
1337
+ await this.engine?.dispose();
1338
+ }
1339
+ getConfigSchema() {
1340
+ return {
1341
+ sections: [
1342
+ {
1343
+ id: "model",
1344
+ title: "Model",
1345
+ columns: 1,
1346
+ fields: [
1347
+ {
1348
+ key: "modelId",
1349
+ label: "Model",
1350
+ type: "model-selector",
1351
+ catalog: [...import_types2.FACE_DETECTION_MODELS],
1352
+ allowCustom: false,
1353
+ allowConversion: false,
1354
+ acceptFormats: ["onnx", "coreml", "openvino"],
1355
+ requiredMetadata: ["inputSize", "labels", "outputFormat"],
1356
+ outputFormatHint: "ssd"
1357
+ }
1358
+ ]
1359
+ },
1360
+ {
1361
+ id: "thresholds",
1362
+ title: "Detection Thresholds",
1363
+ columns: 1,
1364
+ fields: [
1365
+ {
1366
+ key: "confidence",
1367
+ label: "Confidence Threshold",
1368
+ type: "slider",
1369
+ min: 0.1,
1370
+ max: 1,
1371
+ step: 0.05,
1372
+ default: 0.5
1373
+ }
1374
+ ]
1375
+ }
1376
+ ]
1377
+ };
1378
+ }
1379
+ getClassMap() {
1380
+ return FACE_CLASS_MAP;
1381
+ }
1382
+ getModelCatalog() {
1383
+ return [...import_types2.FACE_DETECTION_MODELS];
1384
+ }
1385
+ getAvailableModels() {
1386
+ return [];
1387
+ }
1388
+ getActiveLabels() {
1389
+ return FACE_LABELS;
1390
+ }
1391
+ async probe() {
1392
+ return {
1393
+ available: true,
1394
+ runtime: this.engine?.runtime ?? "onnx",
1395
+ device: this.engine?.device ?? "cpu",
1396
+ capabilities: ["fp32"]
1397
+ };
1398
+ }
1399
+ };
1400
+
1401
+ // src/addons/face-recognition/index.ts
1402
+ var import_types3 = require("@camstack/types");
1403
+ var IDENTITY_LABEL = { id: "identity", name: "Identity" };
1404
+ var IDENTITY_LABELS = [IDENTITY_LABEL];
1405
+ var FACE_REC_CLASS_MAP = { mapping: {}, preserveOriginal: true };
1406
+ var REQUIRED_STEPS = [
1407
+ { slot: "cropper", outputClasses: ["face"], description: "Requires a face detector" }
1408
+ ];
1409
+ var FaceRecognitionAddon = class {
1410
+ id = "face-recognition";
1411
+ slot = "classifier";
1412
+ inputClasses = ["face"];
1413
+ outputClasses = ["identity:*"];
1414
+ slotPriority = 0;
1415
+ requiredSteps = REQUIRED_STEPS;
1416
+ manifest = {
1417
+ id: "face-recognition",
1418
+ name: "Face Recognition",
1419
+ version: "0.1.0",
1420
+ description: "ArcFace-based face recognition \u2014 produces 512-d identity embeddings",
1421
+ packageName: "@camstack/vision",
1422
+ slot: "classifier",
1423
+ inputClasses: ["face"],
1424
+ outputClasses: ["identity:*"],
1425
+ requiredSteps: REQUIRED_STEPS,
1426
+ supportsCustomModels: false,
1427
+ mayRequirePython: false,
1428
+ defaultConfig: {
1429
+ modelId: "arcface-r100",
1430
+ runtime: "auto",
1431
+ backend: "cpu"
1432
+ }
1433
+ };
1434
+ engine;
1435
+ modelEntry;
1436
+ async initialize(ctx) {
1437
+ const cfg = ctx.addonConfig;
1438
+ const modelId = cfg["modelId"] ?? "arcface-r100";
1439
+ const runtime = cfg["runtime"] ?? "auto";
1440
+ const backend = cfg["backend"] ?? "cpu";
1441
+ const entry = import_types3.FACE_RECOGNITION_MODELS.find((m) => m.id === modelId);
1442
+ if (!entry) {
1443
+ throw new Error(`FaceRecognitionAddon: unknown modelId "${modelId}"`);
1444
+ }
1445
+ this.modelEntry = entry;
1446
+ const resolved = await resolveEngine({
1447
+ runtime,
1448
+ backend,
1449
+ modelEntry: entry,
1450
+ modelsDir: ctx.locationPaths.models
1451
+ });
1452
+ this.engine = resolved.engine;
1453
+ }
1454
+ async classify(input) {
1455
+ const start = Date.now();
1456
+ const { width: inputW, height: inputH } = this.modelEntry.inputSize;
1457
+ const faceCrop = await cropRegion(input.frame.data, input.roi);
1458
+ const layout = this.modelEntry.inputLayout ?? "nhwc";
1459
+ const normalization = this.modelEntry.inputNormalization ?? "zero-one";
1460
+ const normalized = await resizeAndNormalize(faceCrop, inputW, inputH, normalization, layout);
1461
+ const rawEmbedding = await this.engine.run(normalized, [1, inputH, inputW, 3]);
1462
+ const embedding = l2Normalize(rawEmbedding);
1463
+ return {
1464
+ classifications: [
1465
+ {
1466
+ class: "identity",
1467
+ score: 1,
1468
+ embedding
1469
+ }
1470
+ ],
1471
+ inferenceMs: Date.now() - start,
1472
+ modelId: this.modelEntry.id
1473
+ };
1474
+ }
1475
+ async shutdown() {
1476
+ await this.engine?.dispose();
1477
+ }
1478
+ getConfigSchema() {
1479
+ return {
1480
+ sections: [
1481
+ {
1482
+ id: "model",
1483
+ title: "Model",
1484
+ columns: 1,
1485
+ fields: [
1486
+ {
1487
+ key: "modelId",
1488
+ label: "Model",
1489
+ type: "model-selector",
1490
+ catalog: [...import_types3.FACE_RECOGNITION_MODELS],
1491
+ allowCustom: false,
1492
+ allowConversion: false,
1493
+ acceptFormats: ["onnx", "coreml", "openvino"],
1494
+ requiredMetadata: ["inputSize", "labels", "outputFormat"],
1495
+ outputFormatHint: "embedding"
1496
+ }
1497
+ ]
1498
+ },
1499
+ {
1500
+ id: "runtime",
1501
+ title: "Runtime",
1502
+ columns: 2,
1503
+ fields: [
1504
+ {
1505
+ key: "runtime",
1506
+ label: "Runtime",
1507
+ type: "select",
1508
+ options: [
1509
+ { value: "auto", label: "Auto (recommended)" },
1510
+ { value: "onnx", label: "ONNX Runtime" },
1511
+ { value: "coreml", label: "CoreML (Apple)" }
1512
+ ]
1513
+ },
1514
+ {
1515
+ key: "backend",
1516
+ label: "Backend",
1517
+ type: "select",
1518
+ dependsOn: { runtime: "onnx" },
1519
+ options: [
1520
+ { value: "cpu", label: "CPU" },
1521
+ { value: "coreml", label: "CoreML" },
1522
+ { value: "cuda", label: "CUDA (NVIDIA)" }
1523
+ ]
1524
+ }
1525
+ ]
1526
+ }
1527
+ ]
1528
+ };
1529
+ }
1530
+ getClassMap() {
1531
+ return FACE_REC_CLASS_MAP;
1532
+ }
1533
+ getModelCatalog() {
1534
+ return [...import_types3.FACE_RECOGNITION_MODELS];
1535
+ }
1536
+ getAvailableModels() {
1537
+ return [];
1538
+ }
1539
+ getActiveLabels() {
1540
+ return IDENTITY_LABELS;
1541
+ }
1542
+ async probe() {
1543
+ return {
1544
+ available: true,
1545
+ runtime: this.engine?.runtime ?? "onnx",
1546
+ device: this.engine?.device ?? "cpu",
1547
+ capabilities: ["fp32"]
1548
+ };
1549
+ }
1550
+ };
1551
+
1552
+ // src/addons/plate-detection/index.ts
1553
+ var import_types4 = require("@camstack/types");
1554
+ var PLATE_LABEL = { id: "plate", name: "License Plate" };
1555
+ var PLATE_LABELS = [PLATE_LABEL];
1556
+ var PLATE_CLASS_MAP = { mapping: {}, preserveOriginal: true };
1557
+ var PlateDetectionAddon = class {
1558
+ id = "plate-detection";
1559
+ slot = "cropper";
1560
+ inputClasses = ["vehicle"];
1561
+ outputClasses = ["plate"];
1562
+ slotPriority = 0;
1563
+ manifest = {
1564
+ id: "plate-detection",
1565
+ name: "License Plate Detection",
1566
+ version: "0.1.0",
1567
+ description: "YOLO-based license plate detector \u2014 crops plate regions from vehicle detections",
1568
+ packageName: "@camstack/vision",
1569
+ slot: "cropper",
1570
+ inputClasses: ["vehicle"],
1571
+ outputClasses: ["plate"],
1572
+ supportsCustomModels: false,
1573
+ mayRequirePython: false,
1574
+ defaultConfig: {
1575
+ modelId: "yolov8n-plate",
1576
+ runtime: "auto",
1577
+ backend: "cpu",
1578
+ confidence: 0.5,
1579
+ iouThreshold: 0.45
1580
+ }
1581
+ };
1582
+ engine;
1583
+ modelEntry;
1584
+ confidence = 0.5;
1585
+ iouThreshold = 0.45;
1586
+ async initialize(ctx) {
1587
+ const cfg = ctx.addonConfig;
1588
+ const modelId = cfg["modelId"] ?? "yolov8n-plate";
1589
+ const runtime = cfg["runtime"] ?? "auto";
1590
+ const backend = cfg["backend"] ?? "cpu";
1591
+ this.confidence = cfg["confidence"] ?? 0.5;
1592
+ this.iouThreshold = cfg["iouThreshold"] ?? 0.45;
1593
+ const entry = import_types4.PLATE_DETECTION_MODELS.find((m) => m.id === modelId);
1594
+ if (!entry) {
1595
+ throw new Error(`PlateDetectionAddon: unknown modelId "${modelId}"`);
1596
+ }
1597
+ this.modelEntry = entry;
1598
+ const resolved = await resolveEngine({
1599
+ runtime,
1600
+ backend,
1601
+ modelEntry: entry,
1602
+ modelsDir: ctx.locationPaths.models
1603
+ });
1604
+ this.engine = resolved.engine;
1605
+ }
1606
+ async crop(input) {
1607
+ const start = Date.now();
1608
+ const { width: inputW, height: inputH } = this.modelEntry.inputSize;
1609
+ const targetSize = Math.max(inputW, inputH);
1610
+ const vehicleCrop = await cropRegion(input.frame.data, input.roi);
1611
+ const lb = await letterbox(vehicleCrop, targetSize);
1612
+ const output = await this.engine.run(lb.data, [1, 3, targetSize, targetSize]);
1613
+ const numClasses = this.modelEntry.labels.length;
1614
+ const numBoxes = output.length / (4 + numClasses);
1615
+ const labels = this.modelEntry.labels.map((l) => l.id);
1616
+ const plates = yoloPostprocess(output, numClasses, numBoxes, {
1617
+ confidence: this.confidence,
1618
+ iouThreshold: this.iouThreshold,
1619
+ labels,
1620
+ scale: lb.scale,
1621
+ padX: lb.padX,
1622
+ padY: lb.padY,
1623
+ originalWidth: lb.originalWidth,
1624
+ originalHeight: lb.originalHeight
1625
+ });
1626
+ const crops = plates.map((p) => ({ ...p, class: "plate", originalClass: p.originalClass }));
1627
+ return {
1628
+ crops,
1629
+ inferenceMs: Date.now() - start,
1630
+ modelId: this.modelEntry.id
1631
+ };
1632
+ }
1633
+ async shutdown() {
1634
+ await this.engine?.dispose();
1635
+ }
1636
+ getConfigSchema() {
1637
+ return {
1638
+ sections: [
1639
+ {
1640
+ id: "thresholds",
1641
+ title: "Detection Thresholds",
1642
+ columns: 2,
1643
+ fields: [
1644
+ {
1645
+ key: "confidence",
1646
+ label: "Confidence Threshold",
1647
+ type: "slider",
1648
+ min: 0.1,
1649
+ max: 1,
1650
+ step: 0.05,
1651
+ default: 0.5
1652
+ },
1653
+ {
1654
+ key: "iouThreshold",
1655
+ label: "IoU Threshold (NMS)",
1656
+ type: "slider",
1657
+ min: 0.1,
1658
+ max: 1,
1659
+ step: 0.05,
1660
+ default: 0.45
1661
+ }
1662
+ ]
1663
+ }
1664
+ ]
1665
+ };
1666
+ }
1667
+ getClassMap() {
1668
+ return PLATE_CLASS_MAP;
1669
+ }
1670
+ getModelCatalog() {
1671
+ return [...import_types4.PLATE_DETECTION_MODELS];
1672
+ }
1673
+ getAvailableModels() {
1674
+ return [];
1675
+ }
1676
+ getActiveLabels() {
1677
+ return PLATE_LABELS;
1678
+ }
1679
+ async probe() {
1680
+ return {
1681
+ available: true,
1682
+ runtime: this.engine?.runtime ?? "onnx",
1683
+ device: this.engine?.device ?? "cpu",
1684
+ capabilities: ["fp32"]
1685
+ };
1686
+ }
1687
+ };
1688
+
1689
+ // src/addons/plate-recognition/index.ts
1690
+ var import_types5 = require("@camstack/types");
1691
+ var fs2 = __toESM(require("fs"));
1692
+ var path3 = __toESM(require("path"));
1693
+ var PLATE_TEXT_LABEL = { id: "plate-text", name: "Plate Text" };
1694
+ var PLATE_TEXT_LABELS = [PLATE_TEXT_LABEL];
1695
+ var PLATE_REC_CLASS_MAP = { mapping: {}, preserveOriginal: true };
1696
+ function loadCharset(modelsDir, modelId) {
1697
+ const dictNames = [
1698
+ `camstack-${modelId}-dict.txt`,
1699
+ `camstack-paddleocr-latin-dict.txt`,
1700
+ `camstack-paddleocr-en-dict.txt`
1701
+ ];
1702
+ for (const name of dictNames) {
1703
+ const dictPath = path3.join(modelsDir, name);
1704
+ if (fs2.existsSync(dictPath)) {
1705
+ const lines = fs2.readFileSync(dictPath, "utf-8").split("\n").filter((l) => l.length > 0);
1706
+ return ["", ...lines, " "];
1707
+ }
1708
+ }
1709
+ throw new Error(`PlateRecognitionAddon: dict.txt not found in ${modelsDir}`);
1710
+ }
1711
+ var CHARSET = [];
1712
+ var REQUIRED_STEPS2 = [
1713
+ { slot: "cropper", outputClasses: ["plate"], description: "Requires a plate detector" }
1714
+ ];
1715
+ var PlateRecognitionAddon = class {
1716
+ id = "plate-recognition";
1717
+ slot = "classifier";
1718
+ inputClasses = ["plate"];
1719
+ outputClasses = ["plate-text:*"];
1720
+ slotPriority = 0;
1721
+ requiredSteps = REQUIRED_STEPS2;
1722
+ manifest = {
1723
+ id: "plate-recognition",
1724
+ name: "License Plate Recognition (OCR)",
1725
+ version: "0.1.0",
1726
+ description: "PaddleOCR-based license plate text recognition",
1727
+ packageName: "@camstack/vision",
1728
+ slot: "classifier",
1729
+ inputClasses: ["plate"],
1730
+ outputClasses: ["plate-text:*"],
1731
+ requiredSteps: REQUIRED_STEPS2,
1732
+ supportsCustomModels: false,
1733
+ mayRequirePython: false,
1734
+ defaultConfig: {
1735
+ modelId: "paddleocr-latin",
1736
+ runtime: "auto",
1737
+ backend: "cpu",
1738
+ minConfidence: 0.5
1739
+ }
1740
+ };
1741
+ engine;
1742
+ modelEntry;
1743
+ minConfidence = 0.5;
1744
+ async initialize(ctx) {
1745
+ const cfg = ctx.addonConfig;
1746
+ const modelId = cfg["modelId"] ?? "paddleocr-latin";
1747
+ const runtime = cfg["runtime"] ?? "auto";
1748
+ const backend = cfg["backend"] ?? "cpu";
1749
+ this.minConfidence = cfg["minConfidence"] ?? 0.5;
1750
+ const entry = import_types5.PLATE_RECOGNITION_MODELS.find((m) => m.id === modelId);
1751
+ if (!entry) {
1752
+ throw new Error(`PlateRecognitionAddon: unknown modelId "${modelId}"`);
1753
+ }
1754
+ this.modelEntry = entry;
1755
+ CHARSET = loadCharset(ctx.locationPaths.models, modelId);
1756
+ const resolved = await resolveEngine({
1757
+ runtime,
1758
+ backend,
1759
+ modelEntry: entry,
1760
+ modelsDir: ctx.locationPaths.models
1761
+ });
1762
+ this.engine = resolved.engine;
1763
+ }
1764
+ async classify(input) {
1765
+ const start = Date.now();
1766
+ const { width: inputW, height: inputH } = this.modelEntry.inputSize;
1767
+ const plateCrop = await cropRegion(input.frame.data, input.roi);
1768
+ const normalized = await resizeAndNormalize(plateCrop, inputW, inputH, "zero-one", "nchw");
1769
+ const output = await this.engine.run(normalized, [1, 3, inputH, inputW]);
1770
+ const numChars = CHARSET.length;
1771
+ const seqLen = output.length / numChars;
1772
+ const { text, confidence } = ctcDecode(output, seqLen, numChars, CHARSET);
1773
+ if (confidence < this.minConfidence || text.trim().length === 0) {
1774
+ return {
1775
+ classifications: [],
1776
+ inferenceMs: Date.now() - start,
1777
+ modelId: this.modelEntry.id
1778
+ };
1779
+ }
1780
+ return {
1781
+ classifications: [
1782
+ {
1783
+ class: "plate-text",
1784
+ score: confidence,
1785
+ text
1786
+ }
1787
+ ],
1788
+ inferenceMs: Date.now() - start,
1789
+ modelId: this.modelEntry.id
1790
+ };
1791
+ }
1792
+ async shutdown() {
1793
+ await this.engine?.dispose();
1794
+ }
1795
+ getConfigSchema() {
1796
+ return {
1797
+ sections: [
1798
+ {
1799
+ id: "model",
1800
+ title: "Model",
1801
+ columns: 1,
1802
+ fields: [
1803
+ {
1804
+ key: "modelId",
1805
+ label: "Model",
1806
+ type: "model-selector",
1807
+ catalog: [...import_types5.PLATE_RECOGNITION_MODELS],
1808
+ allowCustom: false,
1809
+ allowConversion: false,
1810
+ acceptFormats: ["onnx", "openvino"],
1811
+ requiredMetadata: ["inputSize", "labels", "outputFormat"],
1812
+ outputFormatHint: "ocr"
1813
+ }
1814
+ ]
1815
+ },
1816
+ {
1817
+ id: "thresholds",
1818
+ title: "Recognition Settings",
1819
+ columns: 1,
1820
+ fields: [
1821
+ {
1822
+ key: "minConfidence",
1823
+ label: "Minimum Confidence",
1824
+ type: "slider",
1825
+ min: 0.1,
1826
+ max: 1,
1827
+ step: 0.05,
1828
+ default: 0.5
1829
+ }
1830
+ ]
1831
+ }
1832
+ ]
1833
+ };
1834
+ }
1835
+ getClassMap() {
1836
+ return PLATE_REC_CLASS_MAP;
1837
+ }
1838
+ getModelCatalog() {
1839
+ return [...import_types5.PLATE_RECOGNITION_MODELS];
1840
+ }
1841
+ getAvailableModels() {
1842
+ return [];
1843
+ }
1844
+ getActiveLabels() {
1845
+ return PLATE_TEXT_LABELS;
1846
+ }
1847
+ async probe() {
1848
+ return {
1849
+ available: true,
1850
+ runtime: this.engine?.runtime ?? "onnx",
1851
+ device: this.engine?.device ?? "cpu",
1852
+ capabilities: ["fp32"]
1853
+ };
1854
+ }
1855
+ };
1856
+
1857
+ // src/addons/audio-classification/index.ts
1858
+ var import_types6 = require("@camstack/types");
1859
+ var YAMNET_NUM_CLASSES = 521;
1860
+ var AUDIO_EVENT_LABEL = { id: "audio-event", name: "Audio Event" };
1861
+ var AUDIO_LABELS = [AUDIO_EVENT_LABEL];
1862
+ var AUDIO_CLASS_MAP = { mapping: {}, preserveOriginal: true };
1863
+ var AudioClassificationAddon = class {
1864
+ id = "audio-classification";
1865
+ slot = "classifier";
1866
+ inputClasses = null;
1867
+ outputClasses = ["audio-event:*"];
1868
+ slotPriority = 0;
1869
+ manifest = {
1870
+ id: "audio-classification",
1871
+ name: "Audio Classification",
1872
+ version: "0.1.0",
1873
+ description: "YAMNet-based audio event classification from audio waveform",
1874
+ packageName: "@camstack/vision",
1875
+ slot: "classifier",
1876
+ inputClasses: void 0,
1877
+ outputClasses: ["audio-event:*"],
1878
+ supportsCustomModels: false,
1879
+ mayRequirePython: false,
1880
+ defaultConfig: {
1881
+ modelId: "yamnet",
1882
+ runtime: "auto",
1883
+ backend: "cpu",
1884
+ minScore: 0.3
1885
+ }
1886
+ };
1887
+ engine;
1888
+ modelEntry;
1889
+ minScore = 0.3;
1890
+ async initialize(ctx) {
1891
+ const cfg = ctx.addonConfig;
1892
+ const modelId = cfg["modelId"] ?? "yamnet";
1893
+ const runtime = cfg["runtime"] ?? "auto";
1894
+ const backend = cfg["backend"] ?? "cpu";
1895
+ this.minScore = cfg["minScore"] ?? 0.3;
1896
+ const entry = import_types6.AUDIO_CLASSIFICATION_MODELS.find((m) => m.id === modelId);
1897
+ if (!entry) {
1898
+ throw new Error(`AudioClassificationAddon: unknown modelId "${modelId}"`);
1899
+ }
1900
+ this.modelEntry = entry;
1901
+ const resolved = await resolveEngine({
1902
+ runtime,
1903
+ backend,
1904
+ modelEntry: entry,
1905
+ modelsDir: ctx.locationPaths.models
1906
+ });
1907
+ this.engine = resolved.engine;
1908
+ }
1909
+ /**
1910
+ * classify() receives a CropInput but internally treats input.frame.data as raw audio context.
1911
+ * For audio, the actual audio chunk data should be stored in frame.data as a Float32Array
1912
+ * serialized into a Buffer (little-endian float32 samples at 16 kHz).
1913
+ *
1914
+ * The CropInput.roi is not used for audio — it is ignored.
1915
+ */
1916
+ async classify(input) {
1917
+ const start = Date.now();
1918
+ const buf = input.frame.data;
1919
+ const numSamples = Math.floor(buf.length / 4);
1920
+ const audioData = new Float32Array(numSamples);
1921
+ for (let i = 0; i < numSamples; i++) {
1922
+ audioData[i] = buf.readFloatLE(i * 4);
1923
+ }
1924
+ const output = await this.engine.run(audioData, [numSamples]);
1925
+ const numFrames = output.length / YAMNET_NUM_CLASSES;
1926
+ const classNames = this.modelEntry.labels.map((l) => l.id);
1927
+ while (classNames.length < YAMNET_NUM_CLASSES) {
1928
+ classNames.push(`class_${classNames.length}`);
1929
+ }
1930
+ const results = yamnetPostprocess(
1931
+ output,
1932
+ Math.round(numFrames),
1933
+ YAMNET_NUM_CLASSES,
1934
+ classNames,
1935
+ this.minScore
1936
+ );
1937
+ const classifications = results.map((r) => ({
1938
+ class: `audio-event:${r.className}`,
1939
+ score: r.score
1940
+ }));
1941
+ return {
1942
+ classifications,
1943
+ inferenceMs: Date.now() - start,
1944
+ modelId: this.modelEntry.id
1945
+ };
1946
+ }
1947
+ async shutdown() {
1948
+ await this.engine?.dispose();
1949
+ }
1950
+ getConfigSchema() {
1951
+ return {
1952
+ sections: [
1953
+ {
1954
+ id: "runtime",
1955
+ title: "Runtime",
1956
+ columns: 2,
1957
+ fields: [
1958
+ {
1959
+ key: "runtime",
1960
+ label: "Runtime",
1961
+ type: "select",
1962
+ options: [
1963
+ { value: "auto", label: "Auto (recommended)" },
1964
+ { value: "onnx", label: "ONNX Runtime" },
1965
+ { value: "openvino", label: "OpenVINO (Intel)" }
1966
+ ]
1967
+ },
1968
+ {
1969
+ key: "backend",
1970
+ label: "Backend",
1971
+ type: "select",
1972
+ dependsOn: { runtime: "onnx" },
1973
+ options: [
1974
+ { value: "cpu", label: "CPU" },
1975
+ { value: "cuda", label: "CUDA (NVIDIA)" }
1976
+ ]
1977
+ }
1978
+ ]
1979
+ },
1980
+ {
1981
+ id: "thresholds",
1982
+ title: "Classification Settings",
1983
+ columns: 1,
1984
+ fields: [
1985
+ {
1986
+ key: "minScore",
1987
+ label: "Minimum Score",
1988
+ type: "slider",
1989
+ min: 0.05,
1990
+ max: 1,
1991
+ step: 0.05,
1992
+ default: 0.3
1993
+ }
1994
+ ]
1995
+ }
1996
+ ]
1997
+ };
1998
+ }
1999
+ getClassMap() {
2000
+ return AUDIO_CLASS_MAP;
2001
+ }
2002
+ getModelCatalog() {
2003
+ return [...import_types6.AUDIO_CLASSIFICATION_MODELS];
2004
+ }
2005
+ getAvailableModels() {
2006
+ return [];
2007
+ }
2008
+ getActiveLabels() {
2009
+ return AUDIO_LABELS;
2010
+ }
2011
+ async probe() {
2012
+ return {
2013
+ available: true,
2014
+ runtime: this.engine?.runtime ?? "onnx",
2015
+ device: this.engine?.device ?? "cpu",
2016
+ capabilities: ["fp32"]
2017
+ };
2018
+ }
2019
+ };
2020
+
2021
+ // src/addons/camera-native-detection/index.ts
2022
+ var NATIVE_LABELS = [
2023
+ { id: "person", name: "Person" },
2024
+ { id: "vehicle", name: "Vehicle" },
2025
+ { id: "motion", name: "Motion" },
2026
+ { id: "face", name: "Face" }
2027
+ ];
2028
+ var NATIVE_CLASS_MAP = { mapping: {}, preserveOriginal: true };
2029
+ var CameraNativeDetectionAddon = class {
2030
+ id = "camera-native-detection";
2031
+ slot = "detector";
2032
+ inputClasses = null;
2033
+ outputClasses = ["person", "vehicle", "motion", "face"];
2034
+ slotPriority = 5;
2035
+ manifest = {
2036
+ id: "camera-native-detection",
2037
+ name: "Camera Native Detection",
2038
+ version: "0.1.0",
2039
+ description: "Passthrough adapter for camera-native events (Frigate, Scrypted, ONVIF) \u2014 no inference engine",
2040
+ packageName: "@camstack/vision",
2041
+ slot: "detector",
2042
+ inputClasses: void 0,
2043
+ outputClasses: ["person", "vehicle", "motion", "face"],
2044
+ supportsCustomModels: false,
2045
+ mayRequirePython: false,
2046
+ defaultConfig: {}
2047
+ };
2048
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
2049
+ async initialize(_ctx) {
2050
+ }
2051
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
2052
+ async detect(_frame) {
2053
+ return {
2054
+ detections: [],
2055
+ inferenceMs: 0,
2056
+ modelId: "camera-native"
2057
+ };
2058
+ }
2059
+ async shutdown() {
2060
+ }
2061
+ getConfigSchema() {
2062
+ return {
2063
+ sections: [
2064
+ {
2065
+ id: "info",
2066
+ title: "Camera Native Detection",
2067
+ description: "This addon forwards detections from native camera events (Frigate webhooks, Scrypted push notifications, ONVIF events). No configuration required.",
2068
+ fields: []
2069
+ }
2070
+ ]
2071
+ };
2072
+ }
2073
+ getClassMap() {
2074
+ return NATIVE_CLASS_MAP;
2075
+ }
2076
+ getModelCatalog() {
2077
+ return [];
2078
+ }
2079
+ getAvailableModels() {
2080
+ return [];
2081
+ }
2082
+ getActiveLabels() {
2083
+ return NATIVE_LABELS;
2084
+ }
2085
+ async probe() {
2086
+ return {
2087
+ available: true,
2088
+ runtime: "onnx",
2089
+ // no runtime used; satisfies the type
2090
+ device: "cpu",
2091
+ capabilities: ["fp32"]
2092
+ };
2093
+ }
2094
+ };
2095
+
2096
+ // src/addons/bird-global-classifier/index.ts
2097
+ var import_types7 = require("@camstack/types");
2098
+ var fs3 = __toESM(require("fs"));
2099
+ var path4 = __toESM(require("path"));
2100
+ var SPECIES_LABEL = { id: "species", name: "Bird Species" };
2101
+ var SPECIES_LABELS = [SPECIES_LABEL];
2102
+ var BIRD_CLASS_MAP = { mapping: {}, preserveOriginal: true };
2103
+ function loadLabels(modelsDir, modelId) {
2104
+ const labelNames = [
2105
+ `camstack-${modelId}-labels.json`,
2106
+ `camstack-bird-species-525-labels.json`
2107
+ ];
2108
+ for (const name of labelNames) {
2109
+ const labelPath = path4.join(modelsDir, name);
2110
+ if (fs3.existsSync(labelPath)) {
2111
+ const raw = fs3.readFileSync(labelPath, "utf-8");
2112
+ return JSON.parse(raw);
2113
+ }
2114
+ }
2115
+ throw new Error(`BirdGlobalClassifierAddon: labels JSON not found in ${modelsDir}`);
2116
+ }
2117
+ function softmax(logits) {
2118
+ const max = logits.reduce((a, b) => Math.max(a, b), -Infinity);
2119
+ const exps = logits.map((v) => Math.exp(v - max));
2120
+ const sum = exps.reduce((a, b) => a + b, 0);
2121
+ return exps.map((v) => v / sum);
2122
+ }
2123
+ var BirdGlobalClassifierAddon = class {
2124
+ id = "bird-global-classifier";
2125
+ slot = "classifier";
2126
+ inputClasses = ["animal"];
2127
+ outputClasses = ["species:*"];
2128
+ slotPriority = 0;
2129
+ requiredSteps = [];
2130
+ manifest = {
2131
+ id: "bird-global-classifier",
2132
+ name: "Bird Classifier (Global, 525 species)",
2133
+ version: "0.1.0",
2134
+ description: "EfficientNet \u2014 525 worldwide bird species (MIT license, ONNX only)",
2135
+ packageName: "@camstack/vision",
2136
+ slot: "classifier",
2137
+ inputClasses: ["animal"],
2138
+ outputClasses: ["species:*"],
2139
+ supportsCustomModels: false,
2140
+ mayRequirePython: false,
2141
+ defaultConfig: {
2142
+ modelId: "bird-species-525",
2143
+ runtime: "auto",
2144
+ backend: "cpu",
2145
+ minConfidence: 0.3
2146
+ }
2147
+ };
2148
+ engine;
2149
+ modelEntry;
2150
+ labels = [];
2151
+ minConfidence = 0.3;
2152
+ async initialize(ctx) {
2153
+ const cfg = ctx.addonConfig;
2154
+ const modelId = cfg["modelId"] ?? "bird-species-525";
2155
+ const runtime = cfg["runtime"] ?? "auto";
2156
+ const backend = cfg["backend"] ?? "cpu";
2157
+ this.minConfidence = cfg["minConfidence"] ?? 0.3;
2158
+ const entry = import_types7.BIRD_SPECIES_MODELS.find((m) => m.id === modelId);
2159
+ if (!entry) {
2160
+ throw new Error(`BirdGlobalClassifierAddon: unknown modelId "${modelId}"`);
2161
+ }
2162
+ this.modelEntry = entry;
2163
+ this.labels = loadLabels(ctx.locationPaths.models, modelId);
2164
+ const resolved = await resolveEngine({
2165
+ runtime,
2166
+ backend,
2167
+ modelEntry: entry,
2168
+ modelsDir: ctx.locationPaths.models
2169
+ });
2170
+ this.engine = resolved.engine;
2171
+ }
2172
+ async classify(input) {
2173
+ const start = Date.now();
2174
+ const { width: inputW, height: inputH } = this.modelEntry.inputSize;
2175
+ const animalCrop = await cropRegion(input.frame.data, input.roi);
2176
+ const normalized = await resizeAndNormalize(animalCrop, inputW, inputH, "imagenet", "nchw");
2177
+ const rawOutput = await this.engine.run(normalized, [1, 3, inputH, inputW]);
2178
+ const probs = softmax(rawOutput);
2179
+ let maxIdx = 0;
2180
+ let maxScore = probs[0] ?? 0;
2181
+ for (let i = 1; i < probs.length; i++) {
2182
+ const score = probs[i] ?? 0;
2183
+ if (score > maxScore) {
2184
+ maxScore = score;
2185
+ maxIdx = i;
2186
+ }
2187
+ }
2188
+ if (maxScore < this.minConfidence) {
2189
+ return {
2190
+ classifications: [],
2191
+ inferenceMs: Date.now() - start,
2192
+ modelId: this.modelEntry.id
2193
+ };
2194
+ }
2195
+ const label = this.labels[maxIdx] ?? `species_${maxIdx}`;
2196
+ return {
2197
+ classifications: [
2198
+ {
2199
+ class: label,
2200
+ score: maxScore
2201
+ }
2202
+ ],
2203
+ inferenceMs: Date.now() - start,
2204
+ modelId: this.modelEntry.id
2205
+ };
2206
+ }
2207
+ async shutdown() {
2208
+ await this.engine?.dispose();
2209
+ }
2210
+ getConfigSchema() {
2211
+ return {
2212
+ sections: [
2213
+ {
2214
+ id: "model",
2215
+ title: "Model",
2216
+ columns: 1,
2217
+ fields: [
2218
+ {
2219
+ key: "modelId",
2220
+ label: "Model",
2221
+ type: "model-selector",
2222
+ catalog: [...import_types7.BIRD_SPECIES_MODELS],
2223
+ allowCustom: false,
2224
+ allowConversion: false,
2225
+ acceptFormats: ["onnx", "coreml", "openvino"],
2226
+ requiredMetadata: ["inputSize", "labels"],
2227
+ outputFormatHint: "classification"
2228
+ }
2229
+ ]
2230
+ },
2231
+ {
2232
+ id: "thresholds",
2233
+ title: "Classification Settings",
2234
+ columns: 1,
2235
+ fields: [
2236
+ {
2237
+ key: "minConfidence",
2238
+ label: "Minimum Confidence",
2239
+ type: "slider",
2240
+ min: 0.05,
2241
+ max: 1,
2242
+ step: 0.05,
2243
+ default: 0.3
2244
+ }
2245
+ ]
2246
+ },
2247
+ {
2248
+ id: "runtime",
2249
+ title: "Runtime",
2250
+ columns: 2,
2251
+ fields: [
2252
+ {
2253
+ key: "runtime",
2254
+ label: "Runtime",
2255
+ type: "select",
2256
+ options: [
2257
+ { value: "auto", label: "Auto (recommended)" },
2258
+ { value: "onnx", label: "ONNX Runtime" },
2259
+ { value: "coreml", label: "CoreML (Apple)" }
2260
+ ]
2261
+ },
2262
+ {
2263
+ key: "backend",
2264
+ label: "Backend",
2265
+ type: "select",
2266
+ dependsOn: { runtime: "onnx" },
2267
+ options: [
2268
+ { value: "cpu", label: "CPU" },
2269
+ { value: "coreml", label: "CoreML" },
2270
+ { value: "cuda", label: "CUDA (NVIDIA)" }
2271
+ ]
2272
+ }
2273
+ ]
2274
+ }
2275
+ ]
2276
+ };
2277
+ }
2278
+ getClassMap() {
2279
+ return BIRD_CLASS_MAP;
2280
+ }
2281
+ getModelCatalog() {
2282
+ return [...import_types7.BIRD_SPECIES_MODELS];
2283
+ }
2284
+ getAvailableModels() {
2285
+ return [];
2286
+ }
2287
+ getActiveLabels() {
2288
+ return SPECIES_LABELS;
2289
+ }
2290
+ async probe() {
2291
+ return {
2292
+ available: true,
2293
+ runtime: this.engine?.runtime ?? "onnx",
2294
+ device: this.engine?.device ?? "cpu",
2295
+ capabilities: ["fp32"]
2296
+ };
2297
+ }
2298
+ };
2299
+
2300
+ // src/addons/bird-nabirds-classifier/index.ts
2301
+ var import_types8 = require("@camstack/types");
2302
+ var fs4 = __toESM(require("fs"));
2303
+ var path5 = __toESM(require("path"));
2304
+ var SPECIES_LABEL2 = { id: "species", name: "Bird Species" };
2305
+ var SPECIES_LABELS2 = [SPECIES_LABEL2];
2306
+ var BIRD_CLASS_MAP2 = { mapping: {}, preserveOriginal: true };
2307
+ function loadLabels2(modelsDir, modelId) {
2308
+ const labelNames = [
2309
+ `camstack-${modelId}-labels.json`,
2310
+ `camstack-bird-nabirds-404-labels.json`
2311
+ ];
2312
+ for (const name of labelNames) {
2313
+ const labelPath = path5.join(modelsDir, name);
2314
+ if (fs4.existsSync(labelPath)) {
2315
+ const raw = fs4.readFileSync(labelPath, "utf-8");
2316
+ return JSON.parse(raw);
2317
+ }
2318
+ }
2319
+ throw new Error(`BirdNABirdsClassifierAddon: labels JSON not found in ${modelsDir}`);
2320
+ }
2321
+ function softmax2(logits) {
2322
+ const max = logits.reduce((a, b) => Math.max(a, b), -Infinity);
2323
+ const exps = logits.map((v) => Math.exp(v - max));
2324
+ const sum = exps.reduce((a, b) => a + b, 0);
2325
+ return exps.map((v) => v / sum);
2326
+ }
2327
+ var BirdNABirdsClassifierAddon = class {
2328
+ id = "bird-nabirds-classifier";
2329
+ slot = "classifier";
2330
+ inputClasses = ["animal"];
2331
+ outputClasses = ["species:*"];
2332
+ slotPriority = 0;
2333
+ requiredSteps = [];
2334
+ manifest = {
2335
+ id: "bird-nabirds-classifier",
2336
+ name: "Bird Classifier (NABirds, 404 species)",
2337
+ version: "0.1.0",
2338
+ description: "ResNet50 \u2014 404 North American bird species (NABirds dataset, ONNX + CoreML + OpenVINO)",
2339
+ packageName: "@camstack/vision",
2340
+ slot: "classifier",
2341
+ inputClasses: ["animal"],
2342
+ outputClasses: ["species:*"],
2343
+ supportsCustomModels: false,
2344
+ mayRequirePython: false,
2345
+ defaultConfig: {
2346
+ modelId: "bird-nabirds-404",
2347
+ runtime: "auto",
2348
+ backend: "cpu",
2349
+ minConfidence: 0.3
2350
+ }
2351
+ };
2352
+ engine;
2353
+ modelEntry;
2354
+ labels = [];
2355
+ minConfidence = 0.3;
2356
+ allowedSpecies;
2357
+ async initialize(ctx) {
2358
+ const cfg = ctx.addonConfig;
2359
+ const modelId = cfg["modelId"] ?? "bird-nabirds-404";
2360
+ const runtime = cfg["runtime"] ?? "auto";
2361
+ const backend = cfg["backend"] ?? "cpu";
2362
+ this.minConfidence = cfg["minConfidence"] ?? 0.3;
2363
+ this.allowedSpecies = cfg["allowedSpecies"];
2364
+ const entry = import_types8.BIRD_NABIRDS_MODELS.find((m) => m.id === modelId);
2365
+ if (!entry) {
2366
+ throw new Error(`BirdNABirdsClassifierAddon: unknown modelId "${modelId}"`);
2367
+ }
2368
+ this.modelEntry = entry;
2369
+ this.labels = loadLabels2(ctx.locationPaths.models, modelId);
2370
+ const resolved = await resolveEngine({
2371
+ runtime,
2372
+ backend,
2373
+ modelEntry: entry,
2374
+ modelsDir: ctx.locationPaths.models
2375
+ });
2376
+ this.engine = resolved.engine;
2377
+ }
2378
+ applyRegionFilter(scores, labels) {
2379
+ if (!this.allowedSpecies || this.allowedSpecies.length === 0) return;
2380
+ const allowedSet = new Set(this.allowedSpecies.map((s) => s.toLowerCase()));
2381
+ for (let i = 0; i < scores.length; i++) {
2382
+ if (!allowedSet.has(labels[i].toLowerCase())) {
2383
+ scores[i] = 0;
2384
+ }
2385
+ }
2386
+ }
2387
+ async classify(input) {
2388
+ const start = Date.now();
2389
+ const { width: inputW, height: inputH } = this.modelEntry.inputSize;
2390
+ const animalCrop = await cropRegion(input.frame.data, input.roi);
2391
+ const normalized = await resizeAndNormalize(animalCrop, inputW, inputH, "imagenet", "nchw");
2392
+ const rawOutput = await this.engine.run(normalized, [1, 3, inputH, inputW]);
2393
+ const probs = softmax2(rawOutput);
2394
+ this.applyRegionFilter(probs, this.labels);
2395
+ const filteredSum = probs.reduce((a, b) => a + b, 0);
2396
+ if (filteredSum > 0) {
2397
+ for (let i = 0; i < probs.length; i++) {
2398
+ probs[i] = (probs[i] ?? 0) / filteredSum;
2399
+ }
2400
+ }
2401
+ let maxIdx = 0;
2402
+ let maxScore = probs[0] ?? 0;
2403
+ for (let i = 1; i < probs.length; i++) {
2404
+ const score = probs[i] ?? 0;
2405
+ if (score > maxScore) {
2406
+ maxScore = score;
2407
+ maxIdx = i;
2408
+ }
2409
+ }
2410
+ if (maxScore < this.minConfidence) {
2411
+ return {
2412
+ classifications: [],
2413
+ inferenceMs: Date.now() - start,
2414
+ modelId: this.modelEntry.id
2415
+ };
2416
+ }
2417
+ const label = this.labels[maxIdx] ?? `species_${maxIdx}`;
2418
+ return {
2419
+ classifications: [
2420
+ {
2421
+ class: label,
2422
+ score: maxScore
2423
+ }
2424
+ ],
2425
+ inferenceMs: Date.now() - start,
2426
+ modelId: this.modelEntry.id
2427
+ };
2428
+ }
2429
+ async shutdown() {
2430
+ await this.engine?.dispose();
2431
+ }
2432
+ getConfigSchema() {
2433
+ return {
2434
+ sections: [
2435
+ {
2436
+ id: "model",
2437
+ title: "Model",
2438
+ columns: 1,
2439
+ fields: [
2440
+ {
2441
+ key: "modelId",
2442
+ label: "Model",
2443
+ type: "model-selector",
2444
+ catalog: [...import_types8.BIRD_NABIRDS_MODELS],
2445
+ allowCustom: false,
2446
+ allowConversion: false,
2447
+ acceptFormats: ["onnx", "coreml", "openvino"],
2448
+ requiredMetadata: ["inputSize", "labels"],
2449
+ outputFormatHint: "classification"
2450
+ }
2451
+ ]
2452
+ },
2453
+ {
2454
+ id: "thresholds",
2455
+ title: "Classification Settings",
2456
+ columns: 1,
2457
+ fields: [
2458
+ {
2459
+ key: "minConfidence",
2460
+ label: "Minimum Confidence",
2461
+ type: "slider",
2462
+ min: 0.05,
2463
+ max: 1,
2464
+ step: 0.05,
2465
+ default: 0.3
2466
+ }
2467
+ ]
2468
+ },
2469
+ {
2470
+ id: "region",
2471
+ title: "Regional Filter",
2472
+ columns: 1,
2473
+ fields: [
2474
+ {
2475
+ key: "regionFilter",
2476
+ label: "Region Preset",
2477
+ type: "select",
2478
+ options: [
2479
+ { value: "", label: "None (all 404 species)" },
2480
+ { value: "north-america-east", label: "North America \u2014 Eastern" },
2481
+ { value: "north-america-west", label: "North America \u2014 Western" },
2482
+ { value: "north-america-south", label: "North America \u2014 Southern" }
2483
+ ]
2484
+ },
2485
+ {
2486
+ key: "allowedSpecies",
2487
+ label: "Custom Allowed Species (comma-separated)",
2488
+ type: "text"
2489
+ }
2490
+ ]
2491
+ },
2492
+ {
2493
+ id: "runtime",
2494
+ title: "Runtime",
2495
+ columns: 2,
2496
+ fields: [
2497
+ {
2498
+ key: "runtime",
2499
+ label: "Runtime",
2500
+ type: "select",
2501
+ options: [
2502
+ { value: "auto", label: "Auto (recommended)" },
2503
+ { value: "onnx", label: "ONNX Runtime" },
2504
+ { value: "coreml", label: "CoreML (Apple)" },
2505
+ { value: "openvino", label: "OpenVINO (Intel)" }
2506
+ ]
2507
+ },
2508
+ {
2509
+ key: "backend",
2510
+ label: "Backend",
2511
+ type: "select",
2512
+ dependsOn: { runtime: "onnx" },
2513
+ options: [
2514
+ { value: "cpu", label: "CPU" },
2515
+ { value: "coreml", label: "CoreML" },
2516
+ { value: "cuda", label: "CUDA (NVIDIA)" }
2517
+ ]
2518
+ }
2519
+ ]
2520
+ }
2521
+ ]
2522
+ };
2523
+ }
2524
+ getClassMap() {
2525
+ return BIRD_CLASS_MAP2;
2526
+ }
2527
+ getModelCatalog() {
2528
+ return [...import_types8.BIRD_NABIRDS_MODELS];
2529
+ }
2530
+ getAvailableModels() {
2531
+ return [];
2532
+ }
2533
+ getActiveLabels() {
2534
+ return SPECIES_LABELS2;
2535
+ }
2536
+ async probe() {
2537
+ return {
2538
+ available: true,
2539
+ runtime: this.engine?.runtime ?? "onnx",
2540
+ device: this.engine?.device ?? "cpu",
2541
+ capabilities: ["fp32"]
2542
+ };
2543
+ }
2544
+ };
2545
+
2546
+ // src/addons/animal-classifier/index.ts
2547
+ var import_types9 = require("@camstack/types");
2548
+ var ANIMAL_TYPE_LABEL = { id: "animal-type", name: "Animal Type" };
2549
+ var ANIMAL_TYPE_LABELS = [ANIMAL_TYPE_LABEL];
2550
+ var ANIMAL_CLASS_MAP = { mapping: {}, preserveOriginal: true };
2551
+ var ANIMAL_10_CLASSES = [
2552
+ "cat",
2553
+ "cow",
2554
+ "dog",
2555
+ "dolphin",
2556
+ "eagle",
2557
+ "giant panda",
2558
+ "horse",
2559
+ "monkey",
2560
+ "sheep",
2561
+ "spider"
2562
+ ];
2563
+ function softmax3(logits) {
2564
+ const max = logits.reduce((a, b) => Math.max(a, b), -Infinity);
2565
+ const exps = logits.map((v) => Math.exp(v - max));
2566
+ const sum = exps.reduce((a, b) => a + b, 0);
2567
+ return exps.map((v) => v / sum);
2568
+ }
2569
+ var AnimalClassifierAddon = class {
2570
+ id = "animal-classifier";
2571
+ slot = "classifier";
2572
+ inputClasses = ["animal"];
2573
+ outputClasses = ["animal-type:*"];
2574
+ slotPriority = 0;
2575
+ requiredSteps = [];
2576
+ manifest = {
2577
+ id: "animal-classifier",
2578
+ name: "Animal Classifier",
2579
+ version: "0.1.0",
2580
+ description: "ViT-based animal type classifier \u2014 10 common species",
2581
+ packageName: "@camstack/vision",
2582
+ slot: "classifier",
2583
+ inputClasses: ["animal"],
2584
+ outputClasses: ["animal-type:*"],
2585
+ supportsCustomModels: false,
2586
+ mayRequirePython: false,
2587
+ defaultConfig: {
2588
+ modelId: "animals-10",
2589
+ runtime: "auto",
2590
+ backend: "cpu",
2591
+ minConfidence: 0.3
2592
+ }
2593
+ };
2594
+ engine;
2595
+ modelEntry;
2596
+ minConfidence = 0.3;
2597
+ async initialize(ctx) {
2598
+ const cfg = ctx.addonConfig;
2599
+ const modelId = cfg["modelId"] ?? "animals-10";
2600
+ const runtime = cfg["runtime"] ?? "auto";
2601
+ const backend = cfg["backend"] ?? "cpu";
2602
+ this.minConfidence = cfg["minConfidence"] ?? 0.3;
2603
+ const entry = import_types9.ANIMAL_TYPE_MODELS.find((m) => m.id === modelId);
2604
+ if (!entry) {
2605
+ throw new Error(`AnimalClassifierAddon: unknown modelId "${modelId}"`);
2606
+ }
2607
+ this.modelEntry = entry;
2608
+ const resolved = await resolveEngine({
2609
+ runtime,
2610
+ backend,
2611
+ modelEntry: entry,
2612
+ modelsDir: ctx.locationPaths.models
2613
+ });
2614
+ this.engine = resolved.engine;
2615
+ }
2616
+ async classify(input) {
2617
+ const start = Date.now();
2618
+ const { width: inputW, height: inputH } = this.modelEntry.inputSize;
2619
+ const animalCrop = await cropRegion(input.frame.data, input.roi);
2620
+ const normalized = await resizeAndNormalize(animalCrop, inputW, inputH, "imagenet", "nchw");
2621
+ const rawOutput = await this.engine.run(normalized, [1, 3, inputH, inputW]);
2622
+ const probs = softmax3(rawOutput);
2623
+ let maxIdx = 0;
2624
+ let maxScore = probs[0] ?? 0;
2625
+ for (let i = 1; i < probs.length; i++) {
2626
+ const score = probs[i] ?? 0;
2627
+ if (score > maxScore) {
2628
+ maxScore = score;
2629
+ maxIdx = i;
2630
+ }
2631
+ }
2632
+ if (maxScore < this.minConfidence) {
2633
+ return {
2634
+ classifications: [],
2635
+ inferenceMs: Date.now() - start,
2636
+ modelId: this.modelEntry.id
2637
+ };
2638
+ }
2639
+ const label = ANIMAL_10_CLASSES[maxIdx] ?? `animal_${maxIdx}`;
2640
+ return {
2641
+ classifications: [
2642
+ {
2643
+ class: label,
2644
+ score: maxScore
2645
+ }
2646
+ ],
2647
+ inferenceMs: Date.now() - start,
2648
+ modelId: this.modelEntry.id
2649
+ };
2650
+ }
2651
+ async shutdown() {
2652
+ await this.engine?.dispose();
2653
+ }
2654
+ getConfigSchema() {
2655
+ return {
2656
+ sections: [
2657
+ {
2658
+ id: "model",
2659
+ title: "Model",
2660
+ columns: 1,
2661
+ fields: [
2662
+ {
2663
+ key: "modelId",
2664
+ label: "Model",
2665
+ type: "model-selector",
2666
+ catalog: [...import_types9.ANIMAL_TYPE_MODELS],
2667
+ allowCustom: false,
2668
+ allowConversion: false,
2669
+ acceptFormats: ["onnx", "coreml", "openvino"],
2670
+ requiredMetadata: ["inputSize", "labels"],
2671
+ outputFormatHint: "classification"
2672
+ }
2673
+ ]
2674
+ },
2675
+ {
2676
+ id: "thresholds",
2677
+ title: "Classification Settings",
2678
+ columns: 1,
2679
+ fields: [
2680
+ {
2681
+ key: "minConfidence",
2682
+ label: "Minimum Confidence",
2683
+ type: "slider",
2684
+ min: 0.05,
2685
+ max: 1,
2686
+ step: 0.05,
2687
+ default: 0.3
2688
+ }
2689
+ ]
2690
+ },
2691
+ {
2692
+ id: "runtime",
2693
+ title: "Runtime",
2694
+ columns: 2,
2695
+ fields: [
2696
+ {
2697
+ key: "runtime",
2698
+ label: "Runtime",
2699
+ type: "select",
2700
+ options: [
2701
+ { value: "auto", label: "Auto (recommended)" },
2702
+ { value: "onnx", label: "ONNX Runtime" },
2703
+ { value: "coreml", label: "CoreML (Apple)" }
2704
+ ]
2705
+ },
2706
+ {
2707
+ key: "backend",
2708
+ label: "Backend",
2709
+ type: "select",
2710
+ dependsOn: { runtime: "onnx" },
2711
+ options: [
2712
+ { value: "cpu", label: "CPU" },
2713
+ { value: "coreml", label: "CoreML" },
2714
+ { value: "cuda", label: "CUDA (NVIDIA)" }
2715
+ ]
2716
+ }
2717
+ ]
2718
+ }
2719
+ ]
2720
+ };
2721
+ }
2722
+ getClassMap() {
2723
+ return ANIMAL_CLASS_MAP;
2724
+ }
2725
+ getModelCatalog() {
2726
+ return [...import_types9.ANIMAL_TYPE_MODELS];
2727
+ }
2728
+ getAvailableModels() {
2729
+ return [];
2730
+ }
2731
+ getActiveLabels() {
2732
+ return ANIMAL_TYPE_LABELS;
2733
+ }
2734
+ async probe() {
2735
+ return {
2736
+ available: true,
2737
+ runtime: this.engine?.runtime ?? "onnx",
2738
+ device: this.engine?.device ?? "cpu",
2739
+ capabilities: ["fp32"]
2740
+ };
2741
+ }
2742
+ };
2743
+ // Annotate the CommonJS export names for ESM import in node:
2744
+ 0 && (module.exports = {
2745
+ AnimalClassifierAddon,
2746
+ AudioClassificationAddon,
2747
+ BirdGlobalClassifierAddon,
2748
+ BirdNABirdsClassifierAddon,
2749
+ CameraNativeDetectionAddon,
2750
+ FaceDetectionAddon,
2751
+ FaceRecognitionAddon,
2752
+ MotionDetectionAddon,
2753
+ NodeInferenceEngine,
2754
+ ObjectDetectionAddon,
2755
+ PlateDetectionAddon,
2756
+ PlateRecognitionAddon,
2757
+ PythonInferenceEngine,
2758
+ cosineSimilarity,
2759
+ cropRegion,
2760
+ ctcDecode,
2761
+ detectMotion,
2762
+ iou,
2763
+ jpegToRgb,
2764
+ l2Normalize,
2765
+ letterbox,
2766
+ nms,
2767
+ probeOnnxBackends,
2768
+ resizeAndNormalize,
2769
+ resolveEngine,
2770
+ rgbToGrayscale,
2771
+ scrfdPostprocess,
2772
+ yamnetPostprocess,
2773
+ yoloPostprocess
2774
+ });
2775
+ //# sourceMappingURL=index.js.map