UNPKG

ppu-yolo-onnx-inference

Version:

Use your YOLO onnx object detection model in Typescript Bun environment easily.

77 lines (76 loc) 2.58 kB
import { Canvas } from "ppu-ocv"; import type { DetectedObject, YoloDetectionOptions } from "./interface"; /** * YOLOv11 Object Detection Inference Engine * * High-performance YOLO model inference with image preprocessing, * model execution, and NMS post-processing. * * @example * const detector = new YoloDetectionInference({ * model: { * path: './model.onnx', * classNames: ['person', 'car', 'bicycle'] * }, * thresholds: { * confidence: 0.5 * } * }); * * await detector.init(); * const detections = await detector.detect(imageBuffer); */ export declare class YoloDetectionInference { private readonly model; private readonly classNames; private readonly thresholds; private readonly debugging; private modelMetadata; private session; private static readonly CHANNELS; constructor(options: YoloDetectionOptions); /** * Initialize the YOLO model and prepare for inference */ init(): Promise<void>; /** * Convert an ArrayBuffer to a Canvas * @param buffer - The input image as ArrayBuffer * @returns A Canvas object containing the image * @throws Error if the conversion fails */ static convertBufferToCanvas(buffer: ArrayBuffer): Promise<Canvas>; /** * Detect objects in an image * @param image - The input image as ArrayBuffer or Canvas * @returns An array of detected objects with bounding boxes, class names, and confidence scores * @throws Error if the model is not initialized or detection fails * @example * const detections = await detector.detect(imageBuffer); * detections.forEach(detection => { * console.log(`Detected ${detection.className} at ${JSON.stringify(detection.box)} with confidence ${detection.confidence}`); * }); */ detect(image: ArrayBuffer | Canvas): Promise<DetectedObject[]>; private preprocessImage; private canvasToTensor; private runInference; private postprocessOutput; private extractCandidates; private extractWithLowerThreshold; private debugTensorData; private applyNMS; private scaleCandidates; private calculateIoU; private saveDebugImages; private savePreprocessedImage; private saveDetectionVisualization; private log; /** * Releases the onnx runtime session and cleans up resources. * This method should be called when the inference engine is no longer needed * to prevent memory leaks. * @throws Error if the session release fails */ destroy(): Promise<void>; }