@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.
- package/dist/addons/animal-classifier/index.d.mts +25 -0
- package/dist/addons/animal-classifier/index.d.ts +25 -0
- package/dist/addons/animal-classifier/index.js +469 -0
- package/dist/addons/animal-classifier/index.js.map +1 -0
- package/dist/addons/animal-classifier/index.mjs +9 -0
- package/dist/addons/animal-classifier/index.mjs.map +1 -0
- package/dist/addons/audio-classification/index.d.mts +31 -0
- package/dist/addons/audio-classification/index.d.ts +31 -0
- package/dist/addons/audio-classification/index.js +411 -0
- package/dist/addons/audio-classification/index.js.map +1 -0
- package/dist/addons/audio-classification/index.mjs +8 -0
- package/dist/addons/audio-classification/index.mjs.map +1 -0
- package/dist/addons/bird-global-classifier/index.d.mts +26 -0
- package/dist/addons/bird-global-classifier/index.d.ts +26 -0
- package/dist/addons/bird-global-classifier/index.js +475 -0
- package/dist/addons/bird-global-classifier/index.js.map +1 -0
- package/dist/addons/bird-global-classifier/index.mjs +9 -0
- package/dist/addons/bird-global-classifier/index.mjs.map +1 -0
- package/dist/addons/bird-nabirds-classifier/index.d.mts +28 -0
- package/dist/addons/bird-nabirds-classifier/index.d.ts +28 -0
- package/dist/addons/bird-nabirds-classifier/index.js +517 -0
- package/dist/addons/bird-nabirds-classifier/index.js.map +1 -0
- package/dist/addons/bird-nabirds-classifier/index.mjs +9 -0
- package/dist/addons/bird-nabirds-classifier/index.mjs.map +1 -0
- package/dist/addons/camera-native-detection/index.d.mts +32 -0
- package/dist/addons/camera-native-detection/index.d.ts +32 -0
- package/dist/addons/camera-native-detection/index.js +99 -0
- package/dist/addons/camera-native-detection/index.js.map +1 -0
- package/dist/addons/camera-native-detection/index.mjs +7 -0
- package/dist/addons/camera-native-detection/index.mjs.map +1 -0
- package/dist/addons/face-detection/index.d.mts +24 -0
- package/dist/addons/face-detection/index.d.ts +24 -0
- package/dist/addons/face-detection/index.js +513 -0
- package/dist/addons/face-detection/index.js.map +1 -0
- package/dist/addons/face-detection/index.mjs +10 -0
- package/dist/addons/face-detection/index.mjs.map +1 -0
- package/dist/addons/face-recognition/index.d.mts +24 -0
- package/dist/addons/face-recognition/index.d.ts +24 -0
- package/dist/addons/face-recognition/index.js +437 -0
- package/dist/addons/face-recognition/index.js.map +1 -0
- package/dist/addons/face-recognition/index.mjs +9 -0
- package/dist/addons/face-recognition/index.mjs.map +1 -0
- package/dist/addons/motion-detection/index.d.mts +26 -0
- package/dist/addons/motion-detection/index.d.ts +26 -0
- package/dist/addons/motion-detection/index.js +273 -0
- package/dist/addons/motion-detection/index.js.map +1 -0
- package/dist/addons/motion-detection/index.mjs +8 -0
- package/dist/addons/motion-detection/index.mjs.map +1 -0
- package/dist/addons/object-detection/index.d.mts +25 -0
- package/dist/addons/object-detection/index.d.ts +25 -0
- package/dist/addons/object-detection/index.js +673 -0
- package/dist/addons/object-detection/index.js.map +1 -0
- package/dist/addons/object-detection/index.mjs +10 -0
- package/dist/addons/object-detection/index.mjs.map +1 -0
- package/dist/addons/plate-detection/index.d.mts +25 -0
- package/dist/addons/plate-detection/index.d.ts +25 -0
- package/dist/addons/plate-detection/index.js +477 -0
- package/dist/addons/plate-detection/index.js.map +1 -0
- package/dist/addons/plate-detection/index.mjs +10 -0
- package/dist/addons/plate-detection/index.mjs.map +1 -0
- package/dist/addons/plate-recognition/index.d.mts +25 -0
- package/dist/addons/plate-recognition/index.d.ts +25 -0
- package/dist/addons/plate-recognition/index.js +470 -0
- package/dist/addons/plate-recognition/index.js.map +1 -0
- package/dist/addons/plate-recognition/index.mjs +9 -0
- package/dist/addons/plate-recognition/index.mjs.map +1 -0
- package/dist/chunk-3BKYLBBH.mjs +229 -0
- package/dist/chunk-3BKYLBBH.mjs.map +1 -0
- package/dist/chunk-4PC262GU.mjs +203 -0
- package/dist/chunk-4PC262GU.mjs.map +1 -0
- package/dist/chunk-6OR5TE7A.mjs +101 -0
- package/dist/chunk-6OR5TE7A.mjs.map +1 -0
- package/dist/chunk-7SZAISGP.mjs +210 -0
- package/dist/chunk-7SZAISGP.mjs.map +1 -0
- package/dist/chunk-AD2TFYZA.mjs +235 -0
- package/dist/chunk-AD2TFYZA.mjs.map +1 -0
- package/dist/chunk-CGYSSHHM.mjs +363 -0
- package/dist/chunk-CGYSSHHM.mjs.map +1 -0
- package/dist/chunk-IYHMGYGP.mjs +79 -0
- package/dist/chunk-IYHMGYGP.mjs.map +1 -0
- package/dist/chunk-J3IUBPRE.mjs +187 -0
- package/dist/chunk-J3IUBPRE.mjs.map +1 -0
- package/dist/chunk-KFZDJPYL.mjs +190 -0
- package/dist/chunk-KFZDJPYL.mjs.map +1 -0
- package/dist/chunk-KUO2BVFY.mjs +90 -0
- package/dist/chunk-KUO2BVFY.mjs.map +1 -0
- package/dist/chunk-PXBY3QOA.mjs +152 -0
- package/dist/chunk-PXBY3QOA.mjs.map +1 -0
- package/dist/chunk-XUKDL23Y.mjs +216 -0
- package/dist/chunk-XUKDL23Y.mjs.map +1 -0
- package/dist/chunk-Z26BVC7S.mjs +214 -0
- package/dist/chunk-Z26BVC7S.mjs.map +1 -0
- package/dist/chunk-Z5AHZQEZ.mjs +258 -0
- package/dist/chunk-Z5AHZQEZ.mjs.map +1 -0
- package/dist/index.d.mts +152 -0
- package/dist/index.d.ts +152 -0
- package/dist/index.js +2775 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +205 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +43 -0
- package/python/coreml_inference.py +67 -0
- package/python/openvino_inference.py +76 -0
- package/python/pytorch_inference.py +74 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/addons/object-detection/index.ts","../../../src/shared/image-utils.ts","../../../src/shared/postprocess/yolo.ts","../../../src/shared/postprocess/yolo-seg.ts","../../../src/shared/engine-resolver.ts","../../../src/shared/node-engine.ts"],"sourcesContent":["import type {\n IDetectorProvider,\n IDetectionAddon,\n AddonManifest,\n AddonContext,\n FrameInput,\n DetectorOutput,\n SpatialDetection,\n ConfigUISchema,\n ClassMapDefinition,\n ProbeResult,\n ModelCatalogEntry,\n DetectionModel,\n LabelDefinition,\n IInferenceEngine,\n} from '@camstack/types'\nimport {\n OBJECT_DETECTION_MODELS,\n SEGMENTATION_MODELS,\n COCO_TO_MACRO,\n MACRO_LABELS,\n} from '@camstack/types'\nimport { letterbox } from '../../shared/image-utils.js'\nimport { yoloPostprocess } from '../../shared/postprocess/yolo.js'\nimport { yoloSegPostprocess } from '../../shared/postprocess/yolo-seg.js'\nimport { resolveEngine } from '../../shared/engine-resolver.js'\n\n/** Returns true when the model id identifies a YOLO segmentation model (e.g. yolov8n-seg, yolo11m-seg) */\nfunction isSegModel(modelId: string): boolean {\n return modelId.includes('-seg')\n}\n\n/** Combined catalog: regular detection models + segmentation models */\nconst ALL_DETECTION_MODELS: readonly ModelCatalogEntry[] = [\n ...OBJECT_DETECTION_MODELS,\n ...SEGMENTATION_MODELS,\n]\n\n/** Map COCO-class detections to macro categories (person/vehicle/animal) */\nfunction applyClassMap(\n detections: SpatialDetection[],\n classMap: ClassMapDefinition,\n): SpatialDetection[] {\n return detections\n .filter((d) => classMap.mapping[d.class] !== undefined)\n .map((d) => ({\n ...d,\n originalClass: d.class,\n class: classMap.mapping[d.class]!,\n }))\n}\n\nexport default class ObjectDetectionAddon implements IDetectorProvider, IDetectionAddon {\n readonly id = 'object-detection'\n readonly slot = 'detector' as const\n readonly inputClasses: readonly string[] | null = null\n readonly outputClasses = ['person', 'vehicle', 'animal'] as const\n readonly slotPriority = 0\n readonly manifest: AddonManifest = {\n id: 'object-detection',\n name: 'Object Detection',\n version: '0.1.0',\n description: 'YOLO-based object detection — detects persons, vehicles, and animals',\n packageName: '@camstack/vision',\n slot: 'detector',\n inputClasses: undefined,\n outputClasses: ['person', 'vehicle', 'animal'],\n supportsCustomModels: true,\n mayRequirePython: false,\n defaultConfig: {\n modelId: 'yolov8n',\n runtime: 'auto',\n backend: 'cpu',\n confidence: 0.5,\n iouThreshold: 0.45,\n },\n }\n\n private engine!: IInferenceEngine\n private modelEntry!: ModelCatalogEntry\n private confidence = 0.5\n private iouThreshold = 0.45\n\n async initialize(ctx: AddonContext): Promise<void> {\n const cfg = ctx.addonConfig\n const modelId = (cfg['modelId'] as string | undefined) ?? 'yolov8n'\n const runtime = (cfg['runtime'] as string | undefined) ?? 'auto'\n const backend = (cfg['backend'] as string | undefined) ?? 'cpu'\n this.confidence = (cfg['confidence'] as number | undefined) ?? 0.5\n this.iouThreshold = (cfg['iouThreshold'] as number | undefined) ?? 0.45\n\n const entry = ALL_DETECTION_MODELS.find((m) => m.id === modelId)\n if (!entry) {\n throw new Error(`ObjectDetectionAddon: unknown modelId \"${modelId}\"`)\n }\n this.modelEntry = entry\n\n const resolved = await resolveEngine({\n runtime: runtime as 'auto',\n backend,\n modelEntry: entry,\n modelsDir: ctx.locationPaths.models,\n })\n this.engine = resolved.engine\n }\n\n async detect(frame: FrameInput): Promise<DetectorOutput> {\n const start = Date.now()\n const { width: inputW, height: inputH } = this.modelEntry.inputSize\n const targetSize = Math.max(inputW, inputH)\n\n const lb = await letterbox(frame.data, targetSize)\n\n const numClasses = this.modelEntry.labels.length\n const labels = this.modelEntry.labels.map((l) => l.id)\n const postprocessOpts = {\n confidence: this.confidence,\n iouThreshold: this.iouThreshold,\n labels,\n scale: lb.scale,\n padX: lb.padX,\n padY: lb.padY,\n originalWidth: lb.originalWidth,\n originalHeight: lb.originalHeight,\n }\n\n let rawDetections: SpatialDetection[]\n\n if (isSegModel(this.modelEntry.id)) {\n // YOLO-seg models produce two outputs:\n // output0: [1, 4 + numClasses + 32, numBoxes] (detection + mask coefficients)\n // output1: [1, 32, 160, 160] (prototype masks)\n const outputs = await this.engine.runMultiOutput(lb.data, [1, 3, targetSize, targetSize])\n const outputNames = Object.keys(outputs)\n\n if (outputNames.length < 2) {\n throw new Error(\n `ObjectDetectionAddon: seg model \"${this.modelEntry.id}\" returned ${outputNames.length} output(s); expected 2`,\n )\n }\n\n // ONNX output order is deterministic; first output is the detection tensor, second is protos\n const detectionOutput = outputs[outputNames[0]!]!\n const protoOutput = outputs[outputNames[1]!]!\n\n // Infer dims from tensor sizes\n const numMaskCoeffs = 32\n const numBoxes = detectionOutput.length / (4 + numClasses + numMaskCoeffs)\n const maskHeight = 160\n const maskWidth = 160\n\n rawDetections = yoloSegPostprocess(\n {\n detectionOutput,\n protoOutput,\n numClasses,\n numBoxes,\n numMaskCoeffs,\n maskHeight,\n maskWidth,\n },\n postprocessOpts,\n )\n } else {\n // Standard YOLO output: [1, 4+numClasses, numBoxes]\n const output = await this.engine.run(lb.data, [1, 3, targetSize, targetSize])\n const numBoxes = output.length / (4 + numClasses)\n rawDetections = yoloPostprocess(output, numClasses, numBoxes, postprocessOpts)\n }\n\n const detections = applyClassMap(rawDetections, COCO_TO_MACRO)\n\n return {\n detections,\n inferenceMs: Date.now() - start,\n modelId: this.modelEntry.id,\n }\n }\n\n async shutdown(): Promise<void> {\n await this.engine?.dispose()\n }\n\n getConfigSchema(): ConfigUISchema {\n return {\n sections: [\n {\n id: 'model',\n title: 'Model',\n columns: 2,\n fields: [\n {\n key: 'modelId',\n label: 'Model',\n type: 'model-selector',\n catalog: [...ALL_DETECTION_MODELS],\n allowCustom: true,\n allowConversion: true,\n acceptFormats: ['onnx', 'coreml', 'openvino', 'tflite'],\n requiredMetadata: ['inputSize', 'labels', 'outputFormat'],\n outputFormatHint: 'yolo',\n },\n ],\n },\n {\n id: 'runtime',\n title: 'Runtime',\n columns: 2,\n fields: [\n {\n key: 'runtime',\n label: 'Runtime',\n type: 'select',\n options: [\n { value: 'auto', label: 'Auto (recommended)' },\n { value: 'onnx', label: 'ONNX Runtime' },\n { value: 'coreml', label: 'CoreML (Apple)' },\n { value: 'openvino', label: 'OpenVINO (Intel)' },\n ],\n },\n {\n key: 'backend',\n label: 'Backend',\n type: 'select',\n dependsOn: { runtime: 'onnx' },\n options: [\n { value: 'cpu', label: 'CPU' },\n { value: 'coreml', label: 'CoreML' },\n { value: 'cuda', label: 'CUDA (NVIDIA)' },\n { value: 'tensorrt', label: 'TensorRT (NVIDIA)' },\n ],\n },\n ],\n },\n {\n id: 'thresholds',\n title: 'Detection Thresholds',\n columns: 2,\n fields: [\n {\n key: 'confidence',\n label: 'Confidence Threshold',\n type: 'slider',\n min: 0.1,\n max: 1.0,\n step: 0.05,\n default: 0.5,\n },\n {\n key: 'iouThreshold',\n label: 'IoU Threshold (NMS)',\n type: 'slider',\n min: 0.1,\n max: 1.0,\n step: 0.05,\n default: 0.45,\n },\n ],\n },\n ],\n }\n }\n\n getClassMap(): ClassMapDefinition {\n return COCO_TO_MACRO\n }\n\n getModelCatalog(): ModelCatalogEntry[] {\n return [...ALL_DETECTION_MODELS]\n }\n\n getAvailableModels(): DetectionModel[] {\n // TODO: check downloaded models in modelsDir\n return []\n }\n\n getActiveLabels(): readonly LabelDefinition[] {\n return MACRO_LABELS\n }\n\n async probe(): Promise<ProbeResult> {\n return {\n available: true,\n runtime: this.engine?.runtime ?? 'onnx',\n device: this.engine?.device ?? 'cpu',\n capabilities: ['fp32'],\n }\n }\n}\n","import sharp from 'sharp'\nimport type { BoundingBox } from '@camstack/types'\n\n/** Decode JPEG to raw RGB pixels */\nexport async function jpegToRgb(\n jpeg: Buffer,\n): Promise<{ data: Buffer; width: number; height: number }> {\n const { data, info } = await sharp(jpeg)\n .removeAlpha()\n .raw()\n .toBuffer({ resolveWithObject: true })\n return { data, width: info.width, height: info.height }\n}\n\n/** Crop a region from a JPEG buffer */\nexport async function cropRegion(jpeg: Buffer, roi: BoundingBox): Promise<Buffer> {\n return sharp(jpeg)\n .extract({\n left: Math.round(roi.x),\n top: Math.round(roi.y),\n width: Math.round(roi.w),\n height: Math.round(roi.h),\n })\n .jpeg()\n .toBuffer()\n}\n\n/** Letterbox resize for YOLO: resize preserving aspect ratio, pad to square */\nexport async function letterbox(\n jpeg: Buffer,\n targetSize: number,\n): Promise<{\n data: Float32Array\n scale: number\n padX: number\n padY: number\n originalWidth: number\n originalHeight: number\n}> {\n const meta = await sharp(jpeg).metadata()\n const originalWidth = meta.width ?? 0\n const originalHeight = meta.height ?? 0\n\n const scale = Math.min(targetSize / originalWidth, targetSize / originalHeight)\n const scaledWidth = Math.round(originalWidth * scale)\n const scaledHeight = Math.round(originalHeight * scale)\n\n const padX = Math.floor((targetSize - scaledWidth) / 2)\n const padY = Math.floor((targetSize - scaledHeight) / 2)\n\n const { data } = await sharp(jpeg)\n .resize(scaledWidth, scaledHeight)\n .extend({\n top: padY,\n bottom: targetSize - scaledHeight - padY,\n left: padX,\n right: targetSize - scaledWidth - padX,\n background: { r: 114, g: 114, b: 114 },\n })\n .removeAlpha()\n .raw()\n .toBuffer({ resolveWithObject: true })\n\n // Convert HWC uint8 to CHW float [0,1]\n const numPixels = targetSize * targetSize\n const float32 = new Float32Array(3 * numPixels)\n for (let i = 0; i < numPixels; i++) {\n const srcBase = i * 3\n float32[0 * numPixels + i] = (data[srcBase]! / 255)\n float32[1 * numPixels + i] = (data[srcBase + 1]! / 255)\n float32[2 * numPixels + i] = (data[srcBase + 2]! / 255)\n }\n\n return { data: float32, scale, padX, padY, originalWidth, originalHeight }\n}\n\n/** Resize and normalize to Float32Array */\nexport async function resizeAndNormalize(\n jpeg: Buffer,\n targetWidth: number,\n targetHeight: number,\n normalization: 'zero-one' | 'imagenet' | 'none',\n layout: 'nchw' | 'nhwc',\n): Promise<Float32Array> {\n const { data } = await sharp(jpeg)\n .resize(targetWidth, targetHeight)\n .removeAlpha()\n .raw()\n .toBuffer({ resolveWithObject: true })\n\n const numPixels = targetWidth * targetHeight\n const float32 = new Float32Array(3 * numPixels)\n\n // ImageNet mean and std per channel\n const mean = [0.485, 0.456, 0.406]\n const std = [0.229, 0.224, 0.225]\n\n if (layout === 'nchw') {\n for (let i = 0; i < numPixels; i++) {\n const srcBase = i * 3\n for (let c = 0; c < 3; c++) {\n const raw = data[srcBase + c]! / 255\n let val: number\n if (normalization === 'zero-one') {\n val = raw\n } else if (normalization === 'imagenet') {\n val = (raw - mean[c]!) / std[c]!\n } else {\n val = data[srcBase + c]!\n }\n float32[c * numPixels + i] = val\n }\n }\n } else {\n // nhwc\n for (let i = 0; i < numPixels; i++) {\n const srcBase = i * 3\n for (let c = 0; c < 3; c++) {\n const raw = data[srcBase + c]! / 255\n let val: number\n if (normalization === 'zero-one') {\n val = raw\n } else if (normalization === 'imagenet') {\n val = (raw - mean[c]!) / std[c]!\n } else {\n val = data[srcBase + c]!\n }\n float32[i * 3 + c] = val\n }\n }\n }\n\n return float32\n}\n\n/** Convert raw RGB to grayscale Uint8Array */\nexport function rgbToGrayscale(rgb: Buffer, width: number, height: number): Uint8Array {\n const numPixels = width * height\n const gray = new Uint8Array(numPixels)\n for (let i = 0; i < numPixels; i++) {\n const r = rgb[i * 3]!\n const g = rgb[i * 3 + 1]!\n const b = rgb[i * 3 + 2]!\n // BT.601 luma\n gray[i] = Math.round(0.299 * r + 0.587 * g + 0.114 * b)\n }\n return gray\n}\n","import type { SpatialDetection, BoundingBox } from '@camstack/types'\n\nexport interface YoloPostprocessOptions {\n readonly confidence: number\n readonly iouThreshold: number\n readonly labels: readonly string[]\n readonly scale: number\n readonly padX: number\n readonly padY: number\n readonly originalWidth: number\n readonly originalHeight: number\n}\n\n/** Calculate IoU between two bounding boxes */\nexport function iou(a: BoundingBox, b: BoundingBox): number {\n const ax1 = a.x\n const ay1 = a.y\n const ax2 = a.x + a.w\n const ay2 = a.y + a.h\n\n const bx1 = b.x\n const by1 = b.y\n const bx2 = b.x + b.w\n const by2 = b.y + b.h\n\n const interX1 = Math.max(ax1, bx1)\n const interY1 = Math.max(ay1, by1)\n const interX2 = Math.min(ax2, bx2)\n const interY2 = Math.min(ay2, by2)\n\n const interW = Math.max(0, interX2 - interX1)\n const interH = Math.max(0, interY2 - interY1)\n const interArea = interW * interH\n\n if (interArea === 0) return 0\n\n const areaA = a.w * a.h\n const areaB = b.w * b.h\n const unionArea = areaA + areaB - interArea\n\n return unionArea === 0 ? 0 : interArea / unionArea\n}\n\n/** Non-maximum suppression — returns indices of kept boxes (sorted by score desc) */\nexport function nms(\n boxes: ReadonlyArray<{ readonly bbox: BoundingBox; readonly score: number }>,\n iouThreshold: number,\n): number[] {\n const indices = boxes\n .map((_, i) => i)\n .sort((a, b) => (boxes[b]!.score) - (boxes[a]!.score))\n\n const kept: number[] = []\n const suppressed = new Set<number>()\n\n for (const idx of indices) {\n if (suppressed.has(idx)) continue\n kept.push(idx)\n for (const other of indices) {\n if (other === idx || suppressed.has(other)) continue\n if (iou(boxes[idx]!.bbox, boxes[other]!.bbox) > iouThreshold) {\n suppressed.add(other)\n }\n }\n }\n\n return kept\n}\n\n/** Full YOLO v8/v9 postprocessing: filter → NMS → scale back to original coords */\nexport function yoloPostprocess(\n output: Float32Array,\n numClasses: number,\n numBoxes: number,\n options: YoloPostprocessOptions,\n): SpatialDetection[] {\n const { confidence, iouThreshold, labels, scale, padX, padY, originalWidth, originalHeight } = options\n\n interface Candidate {\n readonly bbox: BoundingBox\n readonly score: number\n readonly classIdx: number\n }\n\n const candidates: Candidate[] = []\n\n for (let i = 0; i < numBoxes; i++) {\n // YOLO v8/v9 output layout: [1, 4+numClasses, numBoxes] stored row-major\n const cx = output[0 * numBoxes + i]!\n const cy = output[1 * numBoxes + i]!\n const w = output[2 * numBoxes + i]!\n const h = output[3 * numBoxes + i]!\n\n let bestScore = -Infinity\n let bestClass = 0\n\n for (let j = 0; j < numClasses; j++) {\n const score = output[(4 + j) * numBoxes + i]!\n if (score > bestScore) {\n bestScore = score\n bestClass = j\n }\n }\n\n if (bestScore < confidence) continue\n\n // Convert cx,cy,w,h to x,y,w,h (top-left origin)\n const bbox: BoundingBox = {\n x: cx - w / 2,\n y: cy - h / 2,\n w,\n h,\n }\n\n candidates.push({ bbox, score: bestScore, classIdx: bestClass })\n }\n\n if (candidates.length === 0) return []\n\n const keptIndices = nms(candidates, iouThreshold)\n\n return keptIndices.map((idx) => {\n const { bbox, score, classIdx } = candidates[idx]!\n const label = labels[classIdx] ?? String(classIdx)\n\n // Transform from letterbox coords back to original image coords\n const x = Math.max(0, Math.min(originalWidth, (bbox.x - padX) / scale))\n const y = Math.max(0, Math.min(originalHeight, (bbox.y - padY) / scale))\n const x2 = Math.max(0, Math.min(originalWidth, (bbox.x + bbox.w - padX) / scale))\n const y2 = Math.max(0, Math.min(originalHeight, (bbox.y + bbox.h - padY) / scale))\n\n const finalBbox: BoundingBox = { x, y, w: x2 - x, h: y2 - y }\n\n return {\n class: label,\n originalClass: label,\n score,\n bbox: finalBbox,\n } satisfies SpatialDetection\n })\n}\n","import type { SpatialDetection, BoundingBox } from '@camstack/types'\nimport { nms, iou as _iou } from './yolo.js'\nimport type { YoloPostprocessOptions } from './yolo.js'\n\nexport interface YoloSegOutput {\n /** Detection output: [1, 4 + numClasses + numMaskCoeffs, numBoxes] (row-major, batch dim stripped) */\n readonly detectionOutput: Float32Array\n /** Prototype masks: [1, numMaskCoeffs, maskH, maskW] (batch dim stripped) */\n readonly protoOutput: Float32Array\n readonly numClasses: number\n readonly numBoxes: number\n /** Number of mask prototype coefficients per detection (typically 32) */\n readonly numMaskCoeffs: number\n /** Height of prototype mask grid (typically 160) */\n readonly maskHeight: number\n /** Width of prototype mask grid (typically 160) */\n readonly maskWidth: number\n}\n\nexport interface YoloSegPostprocessOptions extends YoloPostprocessOptions {\n /** Binary mask threshold — pixels above this value become 255, others 0 (default: 0.5) */\n readonly maskThreshold?: number\n}\n\n/** Sigmoid activation: σ(x) = 1 / (1 + e^-x) */\nexport function sigmoid(x: number): number {\n return 1 / (1 + Math.exp(-x))\n}\n\n/**\n * Compute instance segmentation mask for a single detection.\n *\n * @param coeffs [numMaskCoeffs] mask coefficients for this detection\n * @param protos [numMaskCoeffs × maskH × maskW] prototype masks (flat, row-major)\n * @param numMaskCoeffs number of prototype channels (e.g. 32)\n * @param maskH prototype grid height (e.g. 160)\n * @param maskW prototype grid width (e.g. 160)\n * @returns [maskH × maskW] sigmoid-activated raw mask values in [0, 1]\n */\nexport function computeRawMask(\n coeffs: Float32Array,\n protos: Float32Array,\n numMaskCoeffs: number,\n maskH: number,\n maskW: number,\n): Float32Array {\n const maskSize = maskH * maskW\n const rawMask = new Float32Array(maskSize)\n\n // mask_raw[y * maskW + x] = sum_k( coeffs[k] * protos[k * maskSize + y * maskW + x] )\n // Then apply sigmoid element-wise.\n for (let px = 0; px < maskSize; px++) {\n let val = 0\n for (let k = 0; k < numMaskCoeffs; k++) {\n val += (coeffs[k] ?? 0) * (protos[k * maskSize + px] ?? 0)\n }\n rawMask[px] = sigmoid(val)\n }\n\n return rawMask\n}\n\n/**\n * Crop a flat [maskH × maskW] mask to a bounding box region (in mask coordinate space),\n * threshold to binary, and return the cropped Uint8Array along with its dimensions.\n *\n * @param rawMask [maskH × maskW] sigmoid-activated values in [0, 1]\n * @param maskH full mask grid height\n * @param maskW full mask grid width\n * @param bbox detection bounding box in the inference input space (e.g. 640×640 letterboxed)\n * @param maskThreshold threshold above which a pixel is considered foreground (default 0.5)\n * @param maskScale ratio of mask resolution to inference resolution (e.g. 160/640 = 0.25)\n * @returns { data: Uint8Array, width, height } of the cropped binary mask\n */\nexport function cropAndThresholdMask(\n rawMask: Float32Array,\n maskH: number,\n maskW: number,\n bbox: BoundingBox,\n maskThreshold: number,\n maskScale: number,\n): { readonly data: Uint8Array; readonly width: number; readonly height: number } {\n const cropX1 = Math.max(0, Math.floor(bbox.x * maskScale))\n const cropY1 = Math.max(0, Math.floor(bbox.y * maskScale))\n const cropX2 = Math.min(maskW, Math.ceil((bbox.x + bbox.w) * maskScale))\n const cropY2 = Math.min(maskH, Math.ceil((bbox.y + bbox.h) * maskScale))\n\n const cropW = Math.max(1, cropX2 - cropX1)\n const cropH = Math.max(1, cropY2 - cropY1)\n\n const data = new Uint8Array(cropW * cropH)\n\n for (let row = 0; row < cropH; row++) {\n const srcRow = cropY1 + row\n for (let col = 0; col < cropW; col++) {\n const srcCol = cropX1 + col\n const srcIdx = srcRow * maskW + srcCol\n data[row * cropW + col] = (rawMask[srcIdx] ?? 0) > maskThreshold ? 255 : 0\n }\n }\n\n return { data, width: cropW, height: cropH }\n}\n\n/**\n * YOLO-seg postprocessing: run detection filtering + NMS, then decode instance masks.\n *\n * The seg detection tensor has layout [1, 4 + numClasses + numMaskCoeffs, numBoxes]:\n * rows 0–3 : cx, cy, w, h\n * rows 4 to 4+C-1 : class scores\n * rows 4+C to end : mask coefficients (32)\n *\n * Steps:\n * 1. Filter boxes by confidence threshold, collecting mask coefficients per candidate.\n * 2. Run NMS across all candidates.\n * 3. For each surviving detection, compute the instance mask and attach it.\n */\nexport function yoloSegPostprocess(\n segOutput: YoloSegOutput,\n options: YoloSegPostprocessOptions,\n): SpatialDetection[] {\n const {\n detectionOutput,\n protoOutput,\n numClasses,\n numBoxes,\n numMaskCoeffs,\n maskHeight,\n maskWidth,\n } = segOutput\n\n const {\n confidence,\n iouThreshold,\n labels,\n scale,\n padX,\n padY,\n originalWidth,\n originalHeight,\n maskThreshold = 0.5,\n } = options\n\n // The mask resolution to inference resolution scale factor (e.g. 160/640 = 0.25)\n // We derive the inference size from scale/pad context: the letterboxed input is typically 640×640\n // but we receive coordinates in that space already, so we use maskHeight / inferH.\n // Since all YOLO-seg models use 640×640 input and 160×160 proto, the ratio is always 160/640.\n // We compute it generically from the known prototype dims + fixed YOLO input of 640.\n const yoloInputSize = 640\n const maskScale = maskHeight / yoloInputSize\n\n interface Candidate {\n readonly bbox: BoundingBox\n readonly score: number\n readonly classIdx: number\n readonly coeffs: Float32Array\n }\n\n const candidates: Candidate[] = []\n\n for (let i = 0; i < numBoxes; i++) {\n const cx = detectionOutput[0 * numBoxes + i] ?? 0\n const cy = detectionOutput[1 * numBoxes + i] ?? 0\n const w = detectionOutput[2 * numBoxes + i] ?? 0\n const h = detectionOutput[3 * numBoxes + i] ?? 0\n\n let bestScore = -Infinity\n let bestClass = 0\n\n for (let j = 0; j < numClasses; j++) {\n const score = detectionOutput[(4 + j) * numBoxes + i] ?? 0\n if (score > bestScore) {\n bestScore = score\n bestClass = j\n }\n }\n\n if (bestScore < confidence) continue\n\n const bbox: BoundingBox = {\n x: cx - w / 2,\n y: cy - h / 2,\n w,\n h,\n }\n\n // Extract mask coefficients for this box\n const coeffs = new Float32Array(numMaskCoeffs)\n for (let k = 0; k < numMaskCoeffs; k++) {\n coeffs[k] = detectionOutput[(4 + numClasses + k) * numBoxes + i] ?? 0\n }\n\n candidates.push({ bbox, score: bestScore, classIdx: bestClass, coeffs })\n }\n\n if (candidates.length === 0) return []\n\n const keptIndices = nms(candidates, iouThreshold)\n\n return keptIndices.map((idx) => {\n const { bbox, score, classIdx, coeffs } = candidates[idx]!\n const label = labels[classIdx] ?? String(classIdx)\n\n // Scale bbox back from letterbox space to original image coords\n const x = Math.max(0, Math.min(originalWidth, (bbox.x - padX) / scale))\n const y = Math.max(0, Math.min(originalHeight, (bbox.y - padY) / scale))\n const x2 = Math.max(0, Math.min(originalWidth, (bbox.x + bbox.w - padX) / scale))\n const y2 = Math.max(0, Math.min(originalHeight, (bbox.y + bbox.h - padY) / scale))\n const finalBbox: BoundingBox = { x, y, w: x2 - x, h: y2 - y }\n\n // Decode instance mask using the letterbox-space bbox (before coord scaling)\n const rawMask = computeRawMask(coeffs, protoOutput, numMaskCoeffs, maskHeight, maskWidth)\n const { data: maskData, width: mW, height: mH } = cropAndThresholdMask(\n rawMask,\n maskHeight,\n maskWidth,\n bbox,\n maskThreshold,\n maskScale,\n )\n\n return {\n class: label,\n originalClass: label,\n score,\n bbox: finalBbox,\n mask: maskData,\n maskWidth: mW,\n maskHeight: mH,\n } satisfies SpatialDetection\n })\n}\n","import type {\n IInferenceEngine,\n DetectionRuntime,\n ModelCatalogEntry,\n ModelFormat,\n} from '@camstack/types'\nimport * as fs from 'node:fs'\nimport * as path from 'node:path'\nimport { NodeInferenceEngine } from './node-engine.js'\n\nexport interface EngineResolverOptions {\n readonly runtime: DetectionRuntime | 'auto'\n readonly backend: string\n readonly modelEntry: ModelCatalogEntry\n readonly modelsDir: string\n readonly pythonPath?: string\n readonly downloadModel?: (url: string, destDir: string) => Promise<string>\n}\n\nexport interface ResolvedEngine {\n readonly engine: IInferenceEngine\n readonly format: ModelFormat\n readonly modelPath: string\n}\n\n/** Priority order for auto-selection of ONNX backends */\nconst AUTO_BACKEND_PRIORITY = ['coreml', 'cuda', 'tensorrt', 'cpu'] as const\n\n/** Map backend names to the model format they require */\nconst BACKEND_TO_FORMAT: Readonly<Record<string, ModelFormat>> = {\n cpu: 'onnx',\n coreml: 'coreml',\n cuda: 'onnx',\n tensorrt: 'onnx',\n} as const\n\n/** Map DetectionRuntime to ModelFormat */\nconst RUNTIME_TO_FORMAT: Readonly<Partial<Record<DetectionRuntime, ModelFormat>>> = {\n onnx: 'onnx',\n coreml: 'coreml',\n openvino: 'openvino',\n tflite: 'tflite',\n pytorch: 'pt',\n} as const\n\nfunction modelFilePath(modelsDir: string, modelEntry: ModelCatalogEntry, format: ModelFormat): string {\n const formatEntry = modelEntry.formats[format]\n if (!formatEntry) {\n throw new Error(`Model ${modelEntry.id} has no ${format} format`)\n }\n // Derive filename from URL\n const urlParts = formatEntry.url.split('/')\n const filename = urlParts[urlParts.length - 1] ?? `${modelEntry.id}.${format}`\n return path.join(modelsDir, filename)\n}\n\nfunction modelExists(filePath: string): boolean {\n try {\n return fs.existsSync(filePath)\n } catch {\n return false\n }\n}\n\nexport async function resolveEngine(options: EngineResolverOptions): Promise<ResolvedEngine> {\n const { runtime, backend, modelEntry, modelsDir, downloadModel } = options\n\n let selectedFormat: ModelFormat\n let selectedBackend: string\n\n if (runtime === 'auto') {\n // Probe available ONNX backends and pick best\n const available = await probeOnnxBackends()\n\n // Pick first priority backend that has a corresponding model format available\n let chosen: { backend: string; format: ModelFormat } | null = null\n\n for (const b of AUTO_BACKEND_PRIORITY) {\n if (!available.includes(b)) continue\n const fmt = BACKEND_TO_FORMAT[b]\n if (!fmt) continue\n if (!modelEntry.formats[fmt]) continue\n chosen = { backend: b, format: fmt }\n break\n }\n\n if (!chosen) {\n throw new Error(\n `resolveEngine: no compatible backend found for model ${modelEntry.id}. Available backends: ${available.join(', ')}`,\n )\n }\n\n selectedFormat = chosen.format\n selectedBackend = chosen.backend\n } else {\n // Explicit runtime requested\n const fmt = RUNTIME_TO_FORMAT[runtime]\n if (!fmt) {\n throw new Error(`resolveEngine: unsupported runtime \"${runtime}\"`)\n }\n if (!modelEntry.formats[fmt]) {\n throw new Error(\n `resolveEngine: model ${modelEntry.id} has no ${fmt} format for runtime ${runtime}`,\n )\n }\n selectedFormat = fmt\n // For onnx runtime, use the provided backend; otherwise use the runtime name\n selectedBackend = runtime === 'onnx' ? (backend || 'cpu') : runtime\n }\n\n // Resolve model path\n let modelPath = modelFilePath(modelsDir, modelEntry, selectedFormat)\n\n if (!modelExists(modelPath)) {\n if (downloadModel) {\n const formatEntry = modelEntry.formats[selectedFormat]!\n modelPath = await downloadModel(formatEntry.url, modelsDir)\n } else {\n throw new Error(\n `resolveEngine: model file not found at ${modelPath} and no downloadModel function provided`,\n )\n }\n }\n\n // Only ONNX runtime is handled by NodeInferenceEngine currently\n if (selectedFormat === 'onnx' || selectedFormat === 'coreml') {\n const engine = new NodeInferenceEngine(modelPath, selectedBackend)\n await engine.initialize()\n return { engine, format: selectedFormat, modelPath }\n }\n\n // For other formats (openvino, tflite, pt), fall back to ONNX cpu with a warning\n // until those engines are implemented\n const fallbackPath = modelFilePath(modelsDir, modelEntry, 'onnx')\n if (modelEntry.formats['onnx'] && modelExists(fallbackPath)) {\n const engine = new NodeInferenceEngine(fallbackPath, 'cpu')\n await engine.initialize()\n return { engine, format: 'onnx', modelPath: fallbackPath }\n }\n\n throw new Error(\n `resolveEngine: format ${selectedFormat} is not yet supported by NodeInferenceEngine and no ONNX fallback is available`,\n )\n}\n\n/** Probe which ONNX execution providers are available on this system */\nexport async function probeOnnxBackends(): Promise<string[]> {\n const available: string[] = ['cpu'] // CPU is always available\n\n try {\n const ort = await import('onnxruntime-node')\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const providers: string[] = (ort as any).env?.webgl?.disabled !== undefined\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ? ((ort as any).InferenceSession?.getAvailableProviders?.() ?? [])\n : []\n\n for (const p of providers) {\n const normalized = p.toLowerCase().replace('executionprovider', '')\n if (normalized === 'coreml') available.push('coreml')\n else if (normalized === 'cuda') available.push('cuda')\n else if (normalized === 'tensorrt') available.push('tensorrt')\n }\n } catch {\n // onnxruntime-node may not be installed; CPU only\n }\n\n // Platform-specific hints when getAvailableProviders isn't exposed\n if (process.platform === 'darwin' && !available.includes('coreml')) {\n available.push('coreml')\n }\n\n return [...new Set(available)]\n}\n","import type { IInferenceEngine, DetectionRuntime, DetectionDevice } from '@camstack/types'\nimport * as path from 'node:path'\n\nconst BACKEND_TO_PROVIDER: Readonly<Record<string, string>> = {\n cpu: 'cpu',\n coreml: 'coreml',\n cuda: 'cuda',\n tensorrt: 'tensorrt',\n dml: 'dml',\n} as const\n\nconst BACKEND_TO_DEVICE: Readonly<Record<string, DetectionDevice>> = {\n cpu: 'cpu',\n coreml: 'gpu-mps',\n cuda: 'gpu-cuda',\n tensorrt: 'tensorrt',\n} as const\n\nexport class NodeInferenceEngine implements IInferenceEngine {\n readonly runtime: DetectionRuntime = 'onnx'\n readonly device: DetectionDevice\n private session: unknown = null\n\n constructor(\n private readonly modelPath: string,\n private readonly backend: string,\n ) {\n this.device = (BACKEND_TO_DEVICE[backend] ?? 'cpu') as DetectionDevice\n }\n\n async initialize(): Promise<void> {\n const ort = await import('onnxruntime-node')\n const provider = BACKEND_TO_PROVIDER[this.backend] ?? 'cpu'\n\n // Resolve absolute path\n const absModelPath = path.isAbsolute(this.modelPath)\n ? this.modelPath\n : path.resolve(process.cwd(), this.modelPath)\n\n const sessionOptions: Record<string, unknown> = {\n executionProviders: [provider],\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n this.session = await (ort as any).InferenceSession.create(absModelPath, sessionOptions)\n }\n\n async run(input: Float32Array, inputShape: readonly number[]): Promise<Float32Array> {\n if (!this.session) {\n throw new Error('NodeInferenceEngine: not initialized — call initialize() first')\n }\n\n const ort = await import('onnxruntime-node')\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const sess = this.session as any\n\n // Get the first input name\n const inputName: string = sess.inputNames[0]\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const tensor = new (ort as any).Tensor('float32', input, [...inputShape])\n const feeds: Record<string, unknown> = { [inputName]: tensor }\n\n const results = await sess.run(feeds)\n const outputName: string = sess.outputNames[0]\n const outputTensor = results[outputName]\n\n return outputTensor.data as Float32Array\n }\n\n async runMultiOutput(\n input: Float32Array,\n inputShape: readonly number[],\n ): Promise<Record<string, Float32Array>> {\n if (!this.session) {\n throw new Error('NodeInferenceEngine: not initialized — call initialize() first')\n }\n\n const ort = await import('onnxruntime-node')\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const sess = this.session as any\n\n const inputName: string = sess.inputNames[0]\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const tensor = new (ort as any).Tensor('float32', input, [...inputShape])\n const feeds: Record<string, unknown> = { [inputName]: tensor }\n\n const results = await sess.run(feeds)\n\n const out: Record<string, Float32Array> = {}\n for (const name of sess.outputNames as string[]) {\n out[name] = results[name].data as Float32Array\n }\n return out\n }\n\n async dispose(): Promise<void> {\n // onnxruntime-node sessions don't have explicit close in all versions\n // but we clear the reference\n this.session = null\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAgBA,mBAKO;;;ACrBP,mBAAkB;AA4BlB,eAAsB,UACpB,MACA,YAQC;AACD,QAAM,OAAO,UAAM,aAAAA,SAAM,IAAI,EAAE,SAAS;AACxC,QAAM,gBAAgB,KAAK,SAAS;AACpC,QAAM,iBAAiB,KAAK,UAAU;AAEtC,QAAM,QAAQ,KAAK,IAAI,aAAa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AS,OAAO;AAAA,cAC7B,SAAS;AAAA,gBACP,EAAE,OAAO,OAAO,OAAO,MAAM;AAAA,gBAC7B,EAAE,OAAO,UAAU,OAAO,SAAS;AAAA,gBACnC,EAAE,OAAO,QAAQ,OAAO,gBAAgB;AAAA,gBACxC,EAAE,OAAO,YAAY,OAAO,oBAAoB;AAAA,cAClD;AAAA,YACF;AAAA,UACF;AAAA,QACF;AAAA,QACA;AAAA,UACE,IAAI;AAAA,UACJ,OAAO;AAAA,UACP,SAAS;AAAA,UACT,QAAQ;AAAA,YACN;AAAA,cACE,KAAK;AAAA,cACL,OAAO;AAAA,cACP,MAAM;AAAA,cACN,KAAK;AAAA,cACL,KAAK;AAAA,cACL,MAAM;AAAA,cACN,SAAS;AAAA,YACX;AAAA,YACA;AAAA,cACE,KAAK;AAAA,cACL,OAAO;AAAA,cACP,MAAM;AAAA,cACN,KAAK;AAAA,cACL,KAAK;AAAA,cACL,MAAM;AAAA,cACN,SAAS;AAAA,YACX;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA,EAEA,cAAkC;AAChC,WAAO;AAAA,EACT;AAAA,EAEA,kBAAuC;AACrC,WAAO,CAAC,GAAG,oBAAoB;AAAA,EACjC;AAAA,EAEA,qBAAuC;AAErC,WAAO,CAAC;AAAA,EACV;AAAA,EAEA,kBAA8C;AAC5C,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,QAA8B;AAClC,WAAO;AAAA,MACL,WAAW;AAAA,MACX,SAAS,KAAK,QAAQ,WAAW;AAAA,MACjC,QAAQ,KAAK,QAAQ,UAAU;AAAA,MAC/B,cAAc,CAAC,MAAM;AAAA,IACvB;AAAA,EACF;AACF;","names":["sharp","path"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ObjectDetectionAddon
|
|
3
|
+
} from "../../chunk-CGYSSHHM.mjs";
|
|
4
|
+
import "../../chunk-KUO2BVFY.mjs";
|
|
5
|
+
import "../../chunk-6OR5TE7A.mjs";
|
|
6
|
+
import "../../chunk-J3IUBPRE.mjs";
|
|
7
|
+
export {
|
|
8
|
+
ObjectDetectionAddon as default
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ICropperProvider, IDetectionAddon, AddonManifest, AddonContext, CropInput, CropperOutput, ConfigUISchema, ClassMapDefinition, ModelCatalogEntry, DetectionModel, LabelDefinition, ProbeResult } from '@camstack/types';
|
|
2
|
+
|
|
3
|
+
declare class PlateDetectionAddon implements ICropperProvider, IDetectionAddon {
|
|
4
|
+
readonly id = "plate-detection";
|
|
5
|
+
readonly slot: "cropper";
|
|
6
|
+
readonly inputClasses: readonly ["vehicle"];
|
|
7
|
+
readonly outputClasses: readonly ["plate"];
|
|
8
|
+
readonly slotPriority = 0;
|
|
9
|
+
readonly manifest: AddonManifest;
|
|
10
|
+
private engine;
|
|
11
|
+
private modelEntry;
|
|
12
|
+
private confidence;
|
|
13
|
+
private iouThreshold;
|
|
14
|
+
initialize(ctx: AddonContext): Promise<void>;
|
|
15
|
+
crop(input: CropInput): Promise<CropperOutput>;
|
|
16
|
+
shutdown(): Promise<void>;
|
|
17
|
+
getConfigSchema(): ConfigUISchema;
|
|
18
|
+
getClassMap(): ClassMapDefinition;
|
|
19
|
+
getModelCatalog(): ModelCatalogEntry[];
|
|
20
|
+
getAvailableModels(): DetectionModel[];
|
|
21
|
+
getActiveLabels(): readonly LabelDefinition[];
|
|
22
|
+
probe(): Promise<ProbeResult>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { PlateDetectionAddon as default };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ICropperProvider, IDetectionAddon, AddonManifest, AddonContext, CropInput, CropperOutput, ConfigUISchema, ClassMapDefinition, ModelCatalogEntry, DetectionModel, LabelDefinition, ProbeResult } from '@camstack/types';
|
|
2
|
+
|
|
3
|
+
declare class PlateDetectionAddon implements ICropperProvider, IDetectionAddon {
|
|
4
|
+
readonly id = "plate-detection";
|
|
5
|
+
readonly slot: "cropper";
|
|
6
|
+
readonly inputClasses: readonly ["vehicle"];
|
|
7
|
+
readonly outputClasses: readonly ["plate"];
|
|
8
|
+
readonly slotPriority = 0;
|
|
9
|
+
readonly manifest: AddonManifest;
|
|
10
|
+
private engine;
|
|
11
|
+
private modelEntry;
|
|
12
|
+
private confidence;
|
|
13
|
+
private iouThreshold;
|
|
14
|
+
initialize(ctx: AddonContext): Promise<void>;
|
|
15
|
+
crop(input: CropInput): Promise<CropperOutput>;
|
|
16
|
+
shutdown(): Promise<void>;
|
|
17
|
+
getConfigSchema(): ConfigUISchema;
|
|
18
|
+
getClassMap(): ClassMapDefinition;
|
|
19
|
+
getModelCatalog(): ModelCatalogEntry[];
|
|
20
|
+
getAvailableModels(): DetectionModel[];
|
|
21
|
+
getActiveLabels(): readonly LabelDefinition[];
|
|
22
|
+
probe(): Promise<ProbeResult>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { PlateDetectionAddon as default };
|
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/addons/plate-detection/index.ts
|
|
31
|
+
var plate_detection_exports = {};
|
|
32
|
+
__export(plate_detection_exports, {
|
|
33
|
+
default: () => PlateDetectionAddon
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(plate_detection_exports);
|
|
36
|
+
var import_types = require("@camstack/types");
|
|
37
|
+
|
|
38
|
+
// src/shared/image-utils.ts
|
|
39
|
+
var import_sharp = __toESM(require("sharp"));
|
|
40
|
+
async function cropRegion(jpeg, roi) {
|
|
41
|
+
return (0, import_sharp.default)(jpeg).extract({
|
|
42
|
+
left: Math.round(roi.x),
|
|
43
|
+
top: Math.round(roi.y),
|
|
44
|
+
width: Math.round(roi.w),
|
|
45
|
+
height: Math.round(roi.h)
|
|
46
|
+
}).jpeg().toBuffer();
|
|
47
|
+
}
|
|
48
|
+
async function letterbox(jpeg, targetSize) {
|
|
49
|
+
const meta = await (0, import_sharp.default)(jpeg).metadata();
|
|
50
|
+
const originalWidth = meta.width ?? 0;
|
|
51
|
+
const originalHeight = meta.height ?? 0;
|
|
52
|
+
const scale = Math.min(targetSize / originalWidth, targetSize / originalHeight);
|
|
53
|
+
const scaledWidth = Math.round(originalWidth * scale);
|
|
54
|
+
const scaledHeight = Math.round(originalHeight * scale);
|
|
55
|
+
const padX = Math.floor((targetSize - scaledWidth) / 2);
|
|
56
|
+
const padY = Math.floor((targetSize - scaledHeight) / 2);
|
|
57
|
+
const { data } = await (0, import_sharp.default)(jpeg).resize(scaledWidth, scaledHeight).extend({
|
|
58
|
+
top: padY,
|
|
59
|
+
bottom: targetSize - scaledHeight - padY,
|
|
60
|
+
left: padX,
|
|
61
|
+
right: targetSize - scaledWidth - padX,
|
|
62
|
+
background: { r: 114, g: 114, b: 114 }
|
|
63
|
+
}).removeAlpha().raw().toBuffer({ resolveWithObject: true });
|
|
64
|
+
const numPixels = targetSize * targetSize;
|
|
65
|
+
const float32 = new Float32Array(3 * numPixels);
|
|
66
|
+
for (let i = 0; i < numPixels; i++) {
|
|
67
|
+
const srcBase = i * 3;
|
|
68
|
+
float32[0 * numPixels + i] = data[srcBase] / 255;
|
|
69
|
+
float32[1 * numPixels + i] = data[srcBase + 1] / 255;
|
|
70
|
+
float32[2 * numPixels + i] = data[srcBase + 2] / 255;
|
|
71
|
+
}
|
|
72
|
+
return { data: float32, scale, padX, padY, originalWidth, originalHeight };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/shared/postprocess/yolo.ts
|
|
76
|
+
function iou(a, b) {
|
|
77
|
+
const ax1 = a.x;
|
|
78
|
+
const ay1 = a.y;
|
|
79
|
+
const ax2 = a.x + a.w;
|
|
80
|
+
const ay2 = a.y + a.h;
|
|
81
|
+
const bx1 = b.x;
|
|
82
|
+
const by1 = b.y;
|
|
83
|
+
const bx2 = b.x + b.w;
|
|
84
|
+
const by2 = b.y + b.h;
|
|
85
|
+
const interX1 = Math.max(ax1, bx1);
|
|
86
|
+
const interY1 = Math.max(ay1, by1);
|
|
87
|
+
const interX2 = Math.min(ax2, bx2);
|
|
88
|
+
const interY2 = Math.min(ay2, by2);
|
|
89
|
+
const interW = Math.max(0, interX2 - interX1);
|
|
90
|
+
const interH = Math.max(0, interY2 - interY1);
|
|
91
|
+
const interArea = interW * interH;
|
|
92
|
+
if (interArea === 0) return 0;
|
|
93
|
+
const areaA = a.w * a.h;
|
|
94
|
+
const areaB = b.w * b.h;
|
|
95
|
+
const unionArea = areaA + areaB - interArea;
|
|
96
|
+
return unionArea === 0 ? 0 : interArea / unionArea;
|
|
97
|
+
}
|
|
98
|
+
function nms(boxes, iouThreshold) {
|
|
99
|
+
const indices = boxes.map((_, i) => i).sort((a, b) => boxes[b].score - boxes[a].score);
|
|
100
|
+
const kept = [];
|
|
101
|
+
const suppressed = /* @__PURE__ */ new Set();
|
|
102
|
+
for (const idx of indices) {
|
|
103
|
+
if (suppressed.has(idx)) continue;
|
|
104
|
+
kept.push(idx);
|
|
105
|
+
for (const other of indices) {
|
|
106
|
+
if (other === idx || suppressed.has(other)) continue;
|
|
107
|
+
if (iou(boxes[idx].bbox, boxes[other].bbox) > iouThreshold) {
|
|
108
|
+
suppressed.add(other);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return kept;
|
|
113
|
+
}
|
|
114
|
+
function yoloPostprocess(output, numClasses, numBoxes, options) {
|
|
115
|
+
const { confidence, iouThreshold, labels, scale, padX, padY, originalWidth, originalHeight } = options;
|
|
116
|
+
const candidates = [];
|
|
117
|
+
for (let i = 0; i < numBoxes; i++) {
|
|
118
|
+
const cx = output[0 * numBoxes + i];
|
|
119
|
+
const cy = output[1 * numBoxes + i];
|
|
120
|
+
const w = output[2 * numBoxes + i];
|
|
121
|
+
const h = output[3 * numBoxes + i];
|
|
122
|
+
let bestScore = -Infinity;
|
|
123
|
+
let bestClass = 0;
|
|
124
|
+
for (let j = 0; j < numClasses; j++) {
|
|
125
|
+
const score = output[(4 + j) * numBoxes + i];
|
|
126
|
+
if (score > bestScore) {
|
|
127
|
+
bestScore = score;
|
|
128
|
+
bestClass = j;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (bestScore < confidence) continue;
|
|
132
|
+
const bbox = {
|
|
133
|
+
x: cx - w / 2,
|
|
134
|
+
y: cy - h / 2,
|
|
135
|
+
w,
|
|
136
|
+
h
|
|
137
|
+
};
|
|
138
|
+
candidates.push({ bbox, score: bestScore, classIdx: bestClass });
|
|
139
|
+
}
|
|
140
|
+
if (candidates.length === 0) return [];
|
|
141
|
+
const keptIndices = nms(candidates, iouThreshold);
|
|
142
|
+
return keptIndices.map((idx) => {
|
|
143
|
+
const { bbox, score, classIdx } = candidates[idx];
|
|
144
|
+
const label = labels[classIdx] ?? String(classIdx);
|
|
145
|
+
const x = Math.max(0, Math.min(originalWidth, (bbox.x - padX) / scale));
|
|
146
|
+
const y = Math.max(0, Math.min(originalHeight, (bbox.y - padY) / scale));
|
|
147
|
+
const x2 = Math.max(0, Math.min(originalWidth, (bbox.x + bbox.w - padX) / scale));
|
|
148
|
+
const y2 = Math.max(0, Math.min(originalHeight, (bbox.y + bbox.h - padY) / scale));
|
|
149
|
+
const finalBbox = { x, y, w: x2 - x, h: y2 - y };
|
|
150
|
+
return {
|
|
151
|
+
class: label,
|
|
152
|
+
originalClass: label,
|
|
153
|
+
score,
|
|
154
|
+
bbox: finalBbox
|
|
155
|
+
};
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// src/shared/engine-resolver.ts
|
|
160
|
+
var fs = __toESM(require("fs"));
|
|
161
|
+
var path2 = __toESM(require("path"));
|
|
162
|
+
|
|
163
|
+
// src/shared/node-engine.ts
|
|
164
|
+
var path = __toESM(require("path"));
|
|
165
|
+
var BACKEND_TO_PROVIDER = {
|
|
166
|
+
cpu: "cpu",
|
|
167
|
+
coreml: "coreml",
|
|
168
|
+
cuda: "cuda",
|
|
169
|
+
tensorrt: "tensorrt",
|
|
170
|
+
dml: "dml"
|
|
171
|
+
};
|
|
172
|
+
var BACKEND_TO_DEVICE = {
|
|
173
|
+
cpu: "cpu",
|
|
174
|
+
coreml: "gpu-mps",
|
|
175
|
+
cuda: "gpu-cuda",
|
|
176
|
+
tensorrt: "tensorrt"
|
|
177
|
+
};
|
|
178
|
+
var NodeInferenceEngine = class {
|
|
179
|
+
constructor(modelPath, backend) {
|
|
180
|
+
this.modelPath = modelPath;
|
|
181
|
+
this.backend = backend;
|
|
182
|
+
this.device = BACKEND_TO_DEVICE[backend] ?? "cpu";
|
|
183
|
+
}
|
|
184
|
+
runtime = "onnx";
|
|
185
|
+
device;
|
|
186
|
+
session = null;
|
|
187
|
+
async initialize() {
|
|
188
|
+
const ort = await import("onnxruntime-node");
|
|
189
|
+
const provider = BACKEND_TO_PROVIDER[this.backend] ?? "cpu";
|
|
190
|
+
const absModelPath = path.isAbsolute(this.modelPath) ? this.modelPath : path.resolve(process.cwd(), this.modelPath);
|
|
191
|
+
const sessionOptions = {
|
|
192
|
+
executionProviders: [provider]
|
|
193
|
+
};
|
|
194
|
+
this.session = await ort.InferenceSession.create(absModelPath, sessionOptions);
|
|
195
|
+
}
|
|
196
|
+
async run(input, inputShape) {
|
|
197
|
+
if (!this.session) {
|
|
198
|
+
throw new Error("NodeInferenceEngine: not initialized \u2014 call initialize() first");
|
|
199
|
+
}
|
|
200
|
+
const ort = await import("onnxruntime-node");
|
|
201
|
+
const sess = this.session;
|
|
202
|
+
const inputName = sess.inputNames[0];
|
|
203
|
+
const tensor = new ort.Tensor("float32", input, [...inputShape]);
|
|
204
|
+
const feeds = { [inputName]: tensor };
|
|
205
|
+
const results = await sess.run(feeds);
|
|
206
|
+
const outputName = sess.outputNames[0];
|
|
207
|
+
const outputTensor = results[outputName];
|
|
208
|
+
return outputTensor.data;
|
|
209
|
+
}
|
|
210
|
+
async runMultiOutput(input, inputShape) {
|
|
211
|
+
if (!this.session) {
|
|
212
|
+
throw new Error("NodeInferenceEngine: not initialized \u2014 call initialize() first");
|
|
213
|
+
}
|
|
214
|
+
const ort = await import("onnxruntime-node");
|
|
215
|
+
const sess = this.session;
|
|
216
|
+
const inputName = sess.inputNames[0];
|
|
217
|
+
const tensor = new ort.Tensor("float32", input, [...inputShape]);
|
|
218
|
+
const feeds = { [inputName]: tensor };
|
|
219
|
+
const results = await sess.run(feeds);
|
|
220
|
+
const out = {};
|
|
221
|
+
for (const name of sess.outputNames) {
|
|
222
|
+
out[name] = results[name].data;
|
|
223
|
+
}
|
|
224
|
+
return out;
|
|
225
|
+
}
|
|
226
|
+
async dispose() {
|
|
227
|
+
this.session = null;
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
// src/shared/engine-resolver.ts
|
|
232
|
+
var AUTO_BACKEND_PRIORITY = ["coreml", "cuda", "tensorrt", "cpu"];
|
|
233
|
+
var BACKEND_TO_FORMAT = {
|
|
234
|
+
cpu: "onnx",
|
|
235
|
+
coreml: "coreml",
|
|
236
|
+
cuda: "onnx",
|
|
237
|
+
tensorrt: "onnx"
|
|
238
|
+
};
|
|
239
|
+
var RUNTIME_TO_FORMAT = {
|
|
240
|
+
onnx: "onnx",
|
|
241
|
+
coreml: "coreml",
|
|
242
|
+
openvino: "openvino",
|
|
243
|
+
tflite: "tflite",
|
|
244
|
+
pytorch: "pt"
|
|
245
|
+
};
|
|
246
|
+
function modelFilePath(modelsDir, modelEntry, format) {
|
|
247
|
+
const formatEntry = modelEntry.formats[format];
|
|
248
|
+
if (!formatEntry) {
|
|
249
|
+
throw new Error(`Model ${modelEntry.id} has no ${format} format`);
|
|
250
|
+
}
|
|
251
|
+
const urlParts = formatEntry.url.split("/");
|
|
252
|
+
const filename = urlParts[urlParts.length - 1] ?? `${modelEntry.id}.${format}`;
|
|
253
|
+
return path2.join(modelsDir, filename);
|
|
254
|
+
}
|
|
255
|
+
function modelExists(filePath) {
|
|
256
|
+
try {
|
|
257
|
+
return fs.existsSync(filePath);
|
|
258
|
+
} catch {
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
async function resolveEngine(options) {
|
|
263
|
+
const { runtime, backend, modelEntry, modelsDir, downloadModel } = options;
|
|
264
|
+
let selectedFormat;
|
|
265
|
+
let selectedBackend;
|
|
266
|
+
if (runtime === "auto") {
|
|
267
|
+
const available = await probeOnnxBackends();
|
|
268
|
+
let chosen = null;
|
|
269
|
+
for (const b of AUTO_BACKEND_PRIORITY) {
|
|
270
|
+
if (!available.includes(b)) continue;
|
|
271
|
+
const fmt = BACKEND_TO_FORMAT[b];
|
|
272
|
+
if (!fmt) continue;
|
|
273
|
+
if (!modelEntry.formats[fmt]) continue;
|
|
274
|
+
chosen = { backend: b, format: fmt };
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
if (!chosen) {
|
|
278
|
+
throw new Error(
|
|
279
|
+
`resolveEngine: no compatible backend found for model ${modelEntry.id}. Available backends: ${available.join(", ")}`
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
selectedFormat = chosen.format;
|
|
283
|
+
selectedBackend = chosen.backend;
|
|
284
|
+
} else {
|
|
285
|
+
const fmt = RUNTIME_TO_FORMAT[runtime];
|
|
286
|
+
if (!fmt) {
|
|
287
|
+
throw new Error(`resolveEngine: unsupported runtime "${runtime}"`);
|
|
288
|
+
}
|
|
289
|
+
if (!modelEntry.formats[fmt]) {
|
|
290
|
+
throw new Error(
|
|
291
|
+
`resolveEngine: model ${modelEntry.id} has no ${fmt} format for runtime ${runtime}`
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
selectedFormat = fmt;
|
|
295
|
+
selectedBackend = runtime === "onnx" ? backend || "cpu" : runtime;
|
|
296
|
+
}
|
|
297
|
+
let modelPath = modelFilePath(modelsDir, modelEntry, selectedFormat);
|
|
298
|
+
if (!modelExists(modelPath)) {
|
|
299
|
+
if (downloadModel) {
|
|
300
|
+
const formatEntry = modelEntry.formats[selectedFormat];
|
|
301
|
+
modelPath = await downloadModel(formatEntry.url, modelsDir);
|
|
302
|
+
} else {
|
|
303
|
+
throw new Error(
|
|
304
|
+
`resolveEngine: model file not found at ${modelPath} and no downloadModel function provided`
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
if (selectedFormat === "onnx" || selectedFormat === "coreml") {
|
|
309
|
+
const engine = new NodeInferenceEngine(modelPath, selectedBackend);
|
|
310
|
+
await engine.initialize();
|
|
311
|
+
return { engine, format: selectedFormat, modelPath };
|
|
312
|
+
}
|
|
313
|
+
const fallbackPath = modelFilePath(modelsDir, modelEntry, "onnx");
|
|
314
|
+
if (modelEntry.formats["onnx"] && modelExists(fallbackPath)) {
|
|
315
|
+
const engine = new NodeInferenceEngine(fallbackPath, "cpu");
|
|
316
|
+
await engine.initialize();
|
|
317
|
+
return { engine, format: "onnx", modelPath: fallbackPath };
|
|
318
|
+
}
|
|
319
|
+
throw new Error(
|
|
320
|
+
`resolveEngine: format ${selectedFormat} is not yet supported by NodeInferenceEngine and no ONNX fallback is available`
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
async function probeOnnxBackends() {
|
|
324
|
+
const available = ["cpu"];
|
|
325
|
+
try {
|
|
326
|
+
const ort = await import("onnxruntime-node");
|
|
327
|
+
const providers = ort.env?.webgl?.disabled !== void 0 ? ort.InferenceSession?.getAvailableProviders?.() ?? [] : [];
|
|
328
|
+
for (const p of providers) {
|
|
329
|
+
const normalized = p.toLowerCase().replace("executionprovider", "");
|
|
330
|
+
if (normalized === "coreml") available.push("coreml");
|
|
331
|
+
else if (normalized === "cuda") available.push("cuda");
|
|
332
|
+
else if (normalized === "tensorrt") available.push("tensorrt");
|
|
333
|
+
}
|
|
334
|
+
} catch {
|
|
335
|
+
}
|
|
336
|
+
if (process.platform === "darwin" && !available.includes("coreml")) {
|
|
337
|
+
available.push("coreml");
|
|
338
|
+
}
|
|
339
|
+
return [...new Set(available)];
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// src/addons/plate-detection/index.ts
|
|
343
|
+
var PLATE_LABEL = { id: "plate", name: "License Plate" };
|
|
344
|
+
var PLATE_LABELS = [PLATE_LABEL];
|
|
345
|
+
var PLATE_CLASS_MAP = { mapping: {}, preserveOriginal: true };
|
|
346
|
+
var PlateDetectionAddon = class {
|
|
347
|
+
id = "plate-detection";
|
|
348
|
+
slot = "cropper";
|
|
349
|
+
inputClasses = ["vehicle"];
|
|
350
|
+
outputClasses = ["plate"];
|
|
351
|
+
slotPriority = 0;
|
|
352
|
+
manifest = {
|
|
353
|
+
id: "plate-detection",
|
|
354
|
+
name: "License Plate Detection",
|
|
355
|
+
version: "0.1.0",
|
|
356
|
+
description: "YOLO-based license plate detector \u2014 crops plate regions from vehicle detections",
|
|
357
|
+
packageName: "@camstack/vision",
|
|
358
|
+
slot: "cropper",
|
|
359
|
+
inputClasses: ["vehicle"],
|
|
360
|
+
outputClasses: ["plate"],
|
|
361
|
+
supportsCustomModels: false,
|
|
362
|
+
mayRequirePython: false,
|
|
363
|
+
defaultConfig: {
|
|
364
|
+
modelId: "yolov8n-plate",
|
|
365
|
+
runtime: "auto",
|
|
366
|
+
backend: "cpu",
|
|
367
|
+
confidence: 0.5,
|
|
368
|
+
iouThreshold: 0.45
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
engine;
|
|
372
|
+
modelEntry;
|
|
373
|
+
confidence = 0.5;
|
|
374
|
+
iouThreshold = 0.45;
|
|
375
|
+
async initialize(ctx) {
|
|
376
|
+
const cfg = ctx.addonConfig;
|
|
377
|
+
const modelId = cfg["modelId"] ?? "yolov8n-plate";
|
|
378
|
+
const runtime = cfg["runtime"] ?? "auto";
|
|
379
|
+
const backend = cfg["backend"] ?? "cpu";
|
|
380
|
+
this.confidence = cfg["confidence"] ?? 0.5;
|
|
381
|
+
this.iouThreshold = cfg["iouThreshold"] ?? 0.45;
|
|
382
|
+
const entry = import_types.PLATE_DETECTION_MODELS.find((m) => m.id === modelId);
|
|
383
|
+
if (!entry) {
|
|
384
|
+
throw new Error(`PlateDetectionAddon: unknown modelId "${modelId}"`);
|
|
385
|
+
}
|
|
386
|
+
this.modelEntry = entry;
|
|
387
|
+
const resolved = await resolveEngine({
|
|
388
|
+
runtime,
|
|
389
|
+
backend,
|
|
390
|
+
modelEntry: entry,
|
|
391
|
+
modelsDir: ctx.locationPaths.models
|
|
392
|
+
});
|
|
393
|
+
this.engine = resolved.engine;
|
|
394
|
+
}
|
|
395
|
+
async crop(input) {
|
|
396
|
+
const start = Date.now();
|
|
397
|
+
const { width: inputW, height: inputH } = this.modelEntry.inputSize;
|
|
398
|
+
const targetSize = Math.max(inputW, inputH);
|
|
399
|
+
const vehicleCrop = await cropRegion(input.frame.data, input.roi);
|
|
400
|
+
const lb = await letterbox(vehicleCrop, targetSize);
|
|
401
|
+
const output = await this.engine.run(lb.data, [1, 3, targetSize, targetSize]);
|
|
402
|
+
const numClasses = this.modelEntry.labels.length;
|
|
403
|
+
const numBoxes = output.length / (4 + numClasses);
|
|
404
|
+
const labels = this.modelEntry.labels.map((l) => l.id);
|
|
405
|
+
const plates = yoloPostprocess(output, numClasses, numBoxes, {
|
|
406
|
+
confidence: this.confidence,
|
|
407
|
+
iouThreshold: this.iouThreshold,
|
|
408
|
+
labels,
|
|
409
|
+
scale: lb.scale,
|
|
410
|
+
padX: lb.padX,
|
|
411
|
+
padY: lb.padY,
|
|
412
|
+
originalWidth: lb.originalWidth,
|
|
413
|
+
originalHeight: lb.originalHeight
|
|
414
|
+
});
|
|
415
|
+
const crops = plates.map((p) => ({ ...p, class: "plate", originalClass: p.originalClass }));
|
|
416
|
+
return {
|
|
417
|
+
crops,
|
|
418
|
+
inferenceMs: Date.now() - start,
|
|
419
|
+
modelId: this.modelEntry.id
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
async shutdown() {
|
|
423
|
+
await this.engine?.dispose();
|
|
424
|
+
}
|
|
425
|
+
getConfigSchema() {
|
|
426
|
+
return {
|
|
427
|
+
sections: [
|
|
428
|
+
{
|
|
429
|
+
id: "thresholds",
|
|
430
|
+
title: "Detection Thresholds",
|
|
431
|
+
columns: 2,
|
|
432
|
+
fields: [
|
|
433
|
+
{
|
|
434
|
+
key: "confidence",
|
|
435
|
+
label: "Confidence Threshold",
|
|
436
|
+
type: "slider",
|
|
437
|
+
min: 0.1,
|
|
438
|
+
max: 1,
|
|
439
|
+
step: 0.05,
|
|
440
|
+
default: 0.5
|
|
441
|
+
},
|
|
442
|
+
{
|
|
443
|
+
key: "iouThreshold",
|
|
444
|
+
label: "IoU Threshold (NMS)",
|
|
445
|
+
type: "slider",
|
|
446
|
+
min: 0.1,
|
|
447
|
+
max: 1,
|
|
448
|
+
step: 0.05,
|
|
449
|
+
default: 0.45
|
|
450
|
+
}
|
|
451
|
+
]
|
|
452
|
+
}
|
|
453
|
+
]
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
getClassMap() {
|
|
457
|
+
return PLATE_CLASS_MAP;
|
|
458
|
+
}
|
|
459
|
+
getModelCatalog() {
|
|
460
|
+
return [...import_types.PLATE_DETECTION_MODELS];
|
|
461
|
+
}
|
|
462
|
+
getAvailableModels() {
|
|
463
|
+
return [];
|
|
464
|
+
}
|
|
465
|
+
getActiveLabels() {
|
|
466
|
+
return PLATE_LABELS;
|
|
467
|
+
}
|
|
468
|
+
async probe() {
|
|
469
|
+
return {
|
|
470
|
+
available: true,
|
|
471
|
+
runtime: this.engine?.runtime ?? "onnx",
|
|
472
|
+
device: this.engine?.device ?? "cpu",
|
|
473
|
+
capabilities: ["fp32"]
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
//# sourceMappingURL=index.js.map
|