import type { InferenceSession } from "onnxruntime-common"; import type { CoreCanvas, PlatformProvider } from "../core/platform.js"; import { Base } from "../global.interface.js"; /** * Configuration options for face detection models */ export interface DetectionModelOptions { /** Threshold values for detection */ threshold: { /** Minimum confidence score for face detection */ confidence: number; /** IoU threshold for non-maximum suppression */ nonMaximumSuppression: number; }; /** Top-K filtering options */ topK: { /** Maximum detections before NMS */ preNonMaximumSuppression: number; /** Maximum detections after NMS */ postNonMaxiumSuppression: number; }; /** Model input size */ size: { /** Input dimensions [height, width] */ input: [number, number]; }; } /** * Options for detect method */ export interface DetectOptions { /** Threshold values for detection */ threshold?: { /** Minimum confidence score for face detection (default: 0.7) */ confidence?: number; /** IoU threshold for non-maximum suppression (default: 0.4) */ nonMaximumSuppression?: number; }; } /** * Face detection result */ export interface DetectionResult { /** Bounding box coordinates and dimensions */ box: { x: number; y: number; width: number; height: number; }; /** Confidence score of the detection */ confidence: number; /** Facial landmarks (5 points: left eye, right eye, nose, left mouth, right mouth) */ landmarks: number[][]; /** Whether multiple faces were detected in the image */ multipleFaces: boolean; } /** * Base class for face detection models */ export declare abstract class BaseDetection extends Base { /** Class name for logging */ protected abstract className: string; /** Path to the model file */ protected abstract modelPath: string; /** Detection configuration options */ protected abstract detectionOptions: DetectionModelOptions; /** ONNX inference session */ protected abstract session: InferenceSession | null; constructor(platform?: PlatformProvider); /** Initializes the detection model */ abstract initialize(): Promise; /** * Detects faces in an image * @param image - Input image as ArrayBuffer or Canvas * @param options - Optional detection options * @returns Detection result or null if no face detected */ abstract detect(image: ArrayBuffer | CoreCanvas, options?: DetectOptions): Promise; /** * Preprocesses image for model inference * @param image - Input canvas * @param height - Image height * @param width - Image width * @returns Preprocessed tensor */ abstract preprocess(image: CoreCanvas, height: number, width: number): Float32Array; /** * Runs model inference * @param tensor - Preprocessed input tensor * @param shape - Input shape [height, width] * @returns Model outputs */ abstract inference(tensor: Float32Array, shape: [number, number]): Promise; /** * Postprocesses model outputs * @param outputs - Raw model outputs * @returns Processed boxes, scores, and landmarks */ abstract postprocess(outputs: InferenceSession.OnnxValueMapType): { boxes: Float32Array; scores: Float32Array; landmarks: Float32Array; }; /** Releases model resources */ abstract destroy(): Promise; /** * Checks if the model has been initialized * @returns True if initialized */ protected isInitialized(): boolean; /** * Generates anchor boxes for face detection * @param imageSize - Image dimensions [height, width] * @returns Flat array of anchor boxes [cx, cy, w, h, ...] */ protected generateAnchors(imageSize?: [number, number]): Float32Array; /** * Decode locations from predictions using priors to undo * the encoding done for offset regression at train time. */ protected decodeBoxes(loc: Float32Array, priors: Float32Array, variances?: [number, number]): Float32Array; /** * Decode landmark predictions using prior boxes. */ protected decodeLandmarks(predictions: Float32Array, priors: Float32Array, variances?: [number, number]): Float32Array; /** * Apply Non-Maximum Suppression (NMS) to reduce overlapping bounding boxes. */ protected nonMaximumSuppression(dets: Float32Array, numDets: number, threshold: number): number[]; }