UNPKG

id-scanner-lib

Version:

Browser-based ID card, QR code, and face recognition scanner with liveness detection

178 lines (177 loc) 4.74 kB
/** * @file 活体检测模块 * @description 提供人脸活体检测功能 * @module modules/face/liveness-detector */ import { BaseScannerModule, ModuleCapabilities, ModuleInitOptions, ModuleType } from '../../interfaces/scanner-module'; import { LivenessAction, LivenessDetectionResult, LivenessDetectionType, LivenessSession } from '../../interfaces/face-detection'; import { Result } from '../../core/result'; import { FaceDetector } from './face-detector'; /** * 眨眼检测阈值配置 */ interface BlinkThresholds { /** 眼睛闭合阈值 */ eyeClosedThreshold: number; /** 眼睛张开阈值 */ eyeOpenThreshold: number; /** 检测阈值 */ detectionThreshold: number; } /** * 活体检测器配置 */ export interface LivenessDetectorConfig { /** 是否启用 */ enabled: boolean; /** 检测类型 */ detectionType: LivenessDetectionType; /** 检测置信度阈值 */ confidenceThreshold: number; /** 眨眼检测阈值 */ blinkThresholds: BlinkThresholds; /** 活体挑战超时(毫秒) */ challengeTimeout: number; /** 活体挑战动作 */ challengeActions: LivenessAction[]; } /** * 活体检测模块 * 提供人脸活体检测功能,支持被动式和主动式活体检测 */ export declare class LivenessDetector extends BaseScannerModule { /** 模块类型 */ readonly type: ModuleType; /** 模块配置 */ protected config: LivenessDetectorConfig; /** 默认配置 */ private static readonly DEFAULT_CONFIG; /** 配置管理器 */ private configManager; /** 日志记录器 */ private logger; /** 资源管理器 */ private resourceManager; /** 摄像头管理器 */ private cameraManager; /** 人脸检测器 */ private faceDetector; /** 当前活体会话 */ private currentSession; /** 检测历史记录,用于分析眨眼等动作 */ private detectionHistory; /** 最大历史记录长度 */ private readonly MAX_HISTORY_LENGTH; /** 防抖处理函数 */ private debouncedProcessFrame; /** * 构造函数 * @param config 初始配置 * @param faceDetector 可选的人脸检测器实例 */ constructor(config?: Partial<LivenessDetectorConfig>, faceDetector?: FaceDetector); /** * 获取模块能力 */ get capabilities(): ModuleCapabilities; /** * 初始化模块 * @param options 初始化选项 */ initialize(options?: ModuleInitOptions): Promise<void>; /** * 处理图片 * @param image 图片源 * @param options 处理选项 */ processImage(image: string | HTMLImageElement | HTMLCanvasElement | ImageData, options?: Record<string, any>): Promise<Result<LivenessDetectionResult>>; /** * 开始活体检测会话 * @param type 活体检测类型 */ startSession(type?: LivenessDetectionType): Result<LivenessSession>; /** * 停止当前会话 */ stopSession(): void; /** * 获取当前活体检测会话 */ getCurrentSession(): LivenessSession | null; /** * 开始实时处理 * @param videoElement 视频元素 * @param options 处理选项 */ startRealtime(videoElement?: HTMLVideoElement, options?: Record<string, any>): Promise<Result<boolean>>; /** * 停止实时处理 */ stopRealtime(): void; /** * 释放资源 */ dispose(): Promise<void>; /** * 处理摄像头帧 */ private handleCameraFrame; /** * 处理视频帧 */ private processFrame; /** * 处理人脸检测结果 */ private handleFaceDetectionResult; /** * 处理被动式活体检测 */ private handlePassiveLivenessDetection; /** * 处理主动式活体检测 */ private handleActiveLivenessDetection; /** * 处理混合式活体检测 */ private handleHybridLivenessDetection; /** * 执行被动式活体检测 */ private performPassiveLivenessDetection; /** * 添加检测结果到历史记录 */ private addToHistory; /** * 计算眼睛纵横比(EAR) * EAR是一种度量眼睛开合程度的指标 */ private calculateEyeAspectRatio; /** * 计算两点之间的距离 */ private distance; /** * 检测眨眼动作 */ private detectBlink; /** * 检测点头动作 */ private detectNod; /** * 检测摇头动作 */ private detectHeadShake; /** * 检测微笑动作 */ private detectSmile; /** * 检测张嘴动作 */ private detectMouthOpen; } export {};