@sssxyd/face-liveness-detector 0.4.1-beta.6 → 0.4.1-beta.7

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/index.esm.js CHANGED
@@ -41,7 +41,6 @@ var DetectionCode;
41
41
  DetectionCode["FACE_TOO_SMALL"] = "FACE_TOO_SMALL";
42
42
  DetectionCode["FACE_TOO_LARGE"] = "FACE_TOO_LARGE";
43
43
  DetectionCode["FACE_NOT_FRONTAL"] = "FACE_NOT_FRONTAL";
44
- DetectionCode["FACE_NOT_REAL"] = "FACE_NOT_REAL";
45
44
  DetectionCode["FACE_NOT_LIVE"] = "FACE_NOT_LIVE";
46
45
  DetectionCode["FACE_LOW_QUALITY"] = "FACE_LOW_QUALITY";
47
46
  DetectionCode["FACE_CHECK_PASS"] = "FACE_CHECK_PASS";
@@ -109,7 +108,7 @@ const DEFAULT_OPTIONS$1 = {
109
108
  action_liveness_action_list: [LivenessAction.BLINK, LivenessAction.MOUTH_OPEN, LivenessAction.NOD_DOWN, LivenessAction.NOD_UP],
110
109
  action_liveness_action_count: 1,
111
110
  action_liveness_action_randomize: true,
112
- action_liveness_verify_timeout: 60000,
111
+ action_liveness_verify_timeout: 15000,
113
112
  action_liveness_min_mouth_open_percent: 0.2,
114
113
  };
115
114
  /**
@@ -1700,7 +1699,7 @@ class MotionLivenessDetector {
1700
1699
  // 综合判定(结合正向和逆向检测)
1701
1700
  const isLively = this.makeLivenessDecision(eyeActivity, mouthActivity, muscleActivity, photoGeometryResult);
1702
1701
  return new MotionDetectionResult(isLively, {
1703
- frameCount: Math.max(this.eyeAspectRatioHistory.length, this.mouthAspectRatioHistory.length),
1702
+ frameCount: Math.max(this.eyeAspectRatioHistory.length, this.mouthAspectRatioHistory.length, this.faceLandmarksHistory.length, this.normalizedLandmarksHistory.length),
1704
1703
  // 正向检测结果(生物特征)
1705
1704
  eyeAspectRatioStdDev: eyeActivity.stdDev,
1706
1705
  mouthAspectRatioStdDev: mouthActivity.stdDev,
@@ -1893,10 +1892,13 @@ class MotionLivenessDetector {
1893
1892
  * 检测面部肌肉的微动(关键点位置微妙变化)
1894
1893
  * 关键:允许刚性运动+生物特征(真人摇头),拒绝纯刚性运动(照片旋转)
1895
1894
  *
1896
- * 【重要修复】使用归一化坐标进行比较,消除人脸在画面中移动的影响
1895
+ * 【合理使用归一化坐标】这里使用归一化坐标是有意义的,因为:
1896
+ * - 目的是检测肌肉的【相对运动幅度】,与人脸尺寸无关
1897
+ * - 消除人脸在画面中位置变化的影响
1898
+ * - 用相对于人脸的比例运动来判断肌肉活动
1897
1899
  */
1898
1900
  detectMuscleMovement() {
1899
- // 【关键】使用归一化坐标历史,而非绝对坐标
1901
+ // 使用归一化坐标历史,消除人脸位置和尺寸影响
1900
1902
  if (this.normalizedLandmarksHistory.length < 2) {
1901
1903
  return { score: 0, variation: 0, hasMovement: false };
1902
1904
  }
@@ -1916,7 +1918,7 @@ class MotionLivenessDetector {
1916
1918
  127, 356 // 脸颊
1917
1919
  ];
1918
1920
  const distances = [];
1919
- // 【关键】使用归一化坐标计算位移
1921
+ // 使用归一化坐标计算相对位移
1920
1922
  for (let i = 1; i < this.normalizedLandmarksHistory.length; i++) {
1921
1923
  const prevFrame = this.normalizedLandmarksHistory[i - 1];
1922
1924
  const currFrame = this.normalizedLandmarksHistory[i];
@@ -2017,12 +2019,14 @@ class MotionLivenessDetector {
2017
2019
  * - 照片所有关键点运动是【刚性的】→ 所有点以相同方向、相似幅度移动
2018
2020
  * - 活体肌肉运动是【非刚性的】→ 不同部位独立运动(眼睛、嘴、脸颊等)
2019
2021
  *
2020
- * 【重要修复】使用归一化坐标进行比较
2022
+ * 【合理使用归一化坐标】这里使用归一化坐标是有意义的,因为:
2023
+ * - 消除人脸在画面中的平移,只关注人脸内部的相对运动模式
2024
+ * - 检测的是运动向量的方向一致性,不依赖绝对坐标
2021
2025
  *
2022
2026
  * 返回值 0-1:值越接近1说明是刚性运动(照片运动)
2023
2027
  */
2024
2028
  detectRigidMotion() {
2025
- // 【关键】使用归一化坐标历史
2029
+ // 使用归一化坐标历史,消除平移影响
2026
2030
  if (this.normalizedLandmarksHistory.length < 2) {
2027
2031
  return 0; // 数据不足,不判定为刚性运动
2028
2032
  }
@@ -2035,7 +2039,7 @@ class MotionLivenessDetector {
2035
2039
  61, 291 // 嘴角
2036
2040
  ];
2037
2041
  const motionVectors = [];
2038
- // 【关键】使用归一化坐标计算运动向量
2042
+ // 使用最近两帧计算运动向量
2039
2043
  const frame1 = this.normalizedLandmarksHistory[this.normalizedLandmarksHistory.length - 2];
2040
2044
  const frame2 = this.normalizedLandmarksHistory[this.normalizedLandmarksHistory.length - 1];
2041
2045
  for (const ptIdx of samplePoints) {
@@ -2361,7 +2365,7 @@ class MotionLivenessDetector {
2361
2365
  * 2. 跨帧深度模式 - 辅助参考
2362
2366
  */
2363
2367
  detectPhotoGeometry() {
2364
- if (this.faceLandmarksHistory.length < 3) {
2368
+ if (this.normalizedLandmarksHistory.length < 3) {
2365
2369
  return { isPhoto: false, confidence: 0, details: {} };
2366
2370
  }
2367
2371
  // 【核心检测1】平面单应性约束检测(最可靠,纯2D几何)
@@ -2374,12 +2378,13 @@ class MotionLivenessDetector {
2374
2378
  const depthResult = this.detectDepthConsistency();
2375
2379
  const crossFrameDepth = this.detectCrossFrameDepthPattern();
2376
2380
  // 综合判定:2D几何约束权重高,Z坐标权重低
2377
- const photoScore = homographyResult.planarScore * 0.35 + // 单应性约束(最可靠)
2378
- perspectivePattern.perspectiveScore * 0.30 + // 透视变换模式(可靠)
2379
- crossRatioResult.invarianceScore * 0.20 + // 交叉比率不变性(可靠)
2380
- (1 - depthResult.depthVariation) * 0.10 + // 深度(辅助,低权重)
2381
+ // 【改进】提高perspectiveScore的权重,因为完美的平滑变换是照片的强特征
2382
+ const photoScore = homographyResult.planarScore * 0.30 + // 单应性约束(最可靠)
2383
+ perspectivePattern.perspectiveScore * 0.40 + // 透视变换模式(可靠且权重提高)
2384
+ crossRatioResult.invarianceScore * 0.15 + // 交叉比率不变性(可靠)
2385
+ (1 - Math.min(depthResult.depthVariation, 1)) * 0.10 + // 深度(辅助,低权重)
2381
2386
  crossFrameDepth.planarPattern * 0.05; // 跨帧深度(辅助,低权重)
2382
- const isPhoto = photoScore > 0.60; // 阈值
2387
+ const isPhoto = photoScore > 0.50; // 【改进】降低阈值到0.50(从0.60)
2383
2388
  const confidence = Math.min(photoScore, 1);
2384
2389
  // 记录历史
2385
2390
  this.planarityScores.push(photoScore);
@@ -2425,18 +2430,19 @@ class MotionLivenessDetector {
2425
2430
  * - 真实3D人脸旋转时,面部各点不共面,交叉比率会变化
2426
2431
  * - 照片无论怎么偏转,共线点的交叉比率保持不变
2427
2432
  *
2428
- * 【注意】交叉比率本身是比率,不依赖绝对坐标
2429
- * 使用归一化坐标只是为了一致性
2433
+ * 虽然交叉比率是射影不变量(用任何坐标系都可以),
2434
+ * 但使用原始坐标以保持一致性和物理意义的清晰性
2430
2435
  */
2431
2436
  detectCrossRatioInvariance() {
2432
- // 【使用归一化坐标历史,保持一致性】
2433
- if (this.normalizedLandmarksHistory.length < 3) {
2437
+ // 【使用原始坐标历史】虽然交叉比率是射影不变量,
2438
+ // 但原始坐标保持物理清晰性
2439
+ if (this.faceLandmarksHistory.length < 3) {
2434
2440
  return { invarianceScore: 0 };
2435
2441
  }
2436
2442
  // 选择面部中线上近似共线的点(额头-鼻梁-鼻尖-嘴-下巴)
2437
2443
  const midlinePoints = [10, 168, 1, 0, 152]; // 从上到下
2438
2444
  const crossRatios = [];
2439
- for (const frame of this.normalizedLandmarksHistory) {
2445
+ for (const frame of this.faceLandmarksHistory) {
2440
2446
  if (frame.length < 468)
2441
2447
  continue;
2442
2448
  // 提取中线点的Y坐标(它们大致在一条垂直线上)
@@ -2492,118 +2498,366 @@ class MotionLivenessDetector {
2492
2498
  /**
2493
2499
  * 【单应性约束检测】判断多帧特征点是否满足平面约束
2494
2500
  *
2495
- * 【重要修复】使用归一化坐标进行比较
2501
+ * 【关键改进】:
2502
+ * 1. 使用DLT算法计算完整的3x3单应性矩阵(8参数)
2503
+ * 2. 使用相邻帧而不是首尾帧(减少变化幅度)
2504
+ * 3. 检查单应性矩阵的性质(秩、行列式等)
2505
+ * 4. 计算多帧的平均误差(更稳定)
2506
+ * 5. 对所有帧对的H矩阵一致性进行验证
2507
+ *
2496
2508
  * 这是纯 2D 几何检测,最可靠!
2497
2509
  */
2498
2510
  detectHomographyConstraint() {
2499
- // 【关键】使用归一化坐标历史
2500
- if (this.normalizedLandmarksHistory.length < 2) {
2511
+ // 【关键】使用原始坐标历史,而不是归一化坐标
2512
+ // 原因:单应性矩阵在原始图像坐标中定义
2513
+ // 归一化坐标虽然消除平移影响,但破坏了H矩阵的定义
2514
+ if (this.faceLandmarksHistory.length < 2) {
2501
2515
  return { planarScore: 0 };
2502
2516
  }
2503
- const frame1 = this.normalizedLandmarksHistory[0];
2504
- const frame2 = this.normalizedLandmarksHistory[this.normalizedLandmarksHistory.length - 1];
2505
- if (frame1.length < 468 || frame2.length < 468) {
2506
- return { planarScore: 0, error: 0 };
2507
- }
2508
- // 选择用于计算单应性的4个基准点(面部四角)
2509
- const basePoints = [10, 152, 234, 454]; // 额头、下巴、左脸颊、右脸颊
2510
- // 选择用于验证的检验点
2511
- const testPoints = [33, 263, 61, 291, 1, 168]; // 眼角、嘴角、鼻尖、鼻梁
2512
- // 提取基准点坐标(归一化后的坐标)
2513
- const srcBase = [];
2514
- const dstBase = [];
2515
- for (const idx of basePoints) {
2516
- if (frame1[idx] && frame2[idx]) {
2517
- srcBase.push([frame1[idx][0], frame1[idx][1]]);
2518
- dstBase.push([frame2[idx][0], frame2[idx][1]]);
2517
+ // 【改进】使用所有面部关键点(468个点)而不是采样点
2518
+ // 更多的点对会给出更准确的单应性矩阵估计
2519
+ const errors = [];
2520
+ const homographyMatrices = [];
2521
+ // 计算相邻帧的变换误差
2522
+ for (let i = 1; i < this.faceLandmarksHistory.length; i++) {
2523
+ const frame1 = this.faceLandmarksHistory[i - 1];
2524
+ const frame2 = this.faceLandmarksHistory[i];
2525
+ if (frame1.length < 468 || frame2.length < 468)
2526
+ continue;
2527
+ // 【改进】收集所有有效的点对(而不是只采样10个点)
2528
+ // 这给出更好的H矩阵估计
2529
+ const srcPoints = [];
2530
+ const dstPoints = [];
2531
+ for (let ptIdx = 0; ptIdx < frame1.length; ptIdx++) {
2532
+ if (frame1[ptIdx] && frame2[ptIdx] &&
2533
+ frame1[ptIdx].length >= 2 && frame2[ptIdx].length >= 2) {
2534
+ // 使用x, y原始坐标
2535
+ srcPoints.push([frame1[ptIdx][0], frame1[ptIdx][1]]);
2536
+ dstPoints.push([frame2[ptIdx][0], frame2[ptIdx][1]]);
2537
+ }
2519
2538
  }
2520
- }
2521
- if (srcBase.length < 4) {
2522
- return { planarScore: 0, error: 0 };
2523
- }
2524
- // 计算简化的仿射变换(近似单应性)
2525
- // 使用最小二乘法拟合仿射变换 [a, b, c; d, e, f]
2526
- const transform = this.estimateAffineTransform(srcBase, dstBase);
2527
- if (!transform) {
2528
- return { planarScore: 0, error: 0 };
2529
- }
2530
- // 用仿射变换预测检验点位置,计算误差
2531
- let totalError = 0;
2532
- let validPoints = 0;
2533
- for (const idx of testPoints) {
2534
- if (frame1[idx] && frame2[idx]) {
2535
- const predicted = this.applyAffineTransform(transform, frame1[idx][0], frame1[idx][1]);
2536
- const actual = [frame2[idx][0], frame2[idx][1]];
2537
- // 归一化坐标下的误差(相对于人脸尺寸的比例)
2538
- const error = Math.sqrt((predicted[0] - actual[0]) ** 2 + (predicted[1] - actual[1]) ** 2);
2539
- totalError += error;
2540
- validPoints++;
2539
+ if (srcPoints.length < 4)
2540
+ continue;
2541
+ // 【新增】使用DLT算法计算完整的3x3单应性矩阵
2542
+ const H = this.estimateHomographyDLT(srcPoints, dstPoints);
2543
+ if (!H)
2544
+ continue;
2545
+ homographyMatrices.push(H);
2546
+ // 【改进】使用单应性矩阵计算误差(而不是仿射变换)
2547
+ let frameError = 0;
2548
+ let validCount = 0;
2549
+ for (let j = 0; j < srcPoints.length; j++) {
2550
+ const transformed = this.applyHomography(H, srcPoints[j][0], srcPoints[j][1]);
2551
+ const actual = dstPoints[j];
2552
+ const error = Math.sqrt((transformed[0] - actual[0]) ** 2 + (transformed[1] - actual[1]) ** 2);
2553
+ frameError += error;
2554
+ validCount++;
2555
+ }
2556
+ if (validCount > 0) {
2557
+ errors.push(frameError / validCount);
2541
2558
  }
2542
2559
  }
2543
- if (validPoints === 0) {
2560
+ if (errors.length === 0) {
2544
2561
  return { planarScore: 0, error: 0 };
2545
2562
  }
2546
- const avgError = totalError / validPoints;
2547
- // 归一化坐标下,误差已经是相对于人脸尺寸的比例
2548
- // 不需要再除以脸宽
2549
- const relativeError = avgError;
2550
- // 平面得分:误差越小,越可能是平面(照片)
2551
- // relativeError < 0.02 → 非常可能是平面
2552
- // relativeError > 0.08 → 不太可能是平面
2553
- const planarScore = Math.max(0, 1 - relativeError / 0.05);
2554
- // 记录误差历史
2555
- this.homographyErrors.push(relativeError);
2556
- if (this.homographyErrors.length > this.config.frameBufferSize) {
2557
- this.homographyErrors.shift();
2558
- }
2559
- return { planarScore: Math.min(planarScore, 1), error: relativeError };
2563
+ // 计算所有帧的平均误差
2564
+ const avgError = errors.reduce((a, b) => a + b, 0) / errors.length;
2565
+ // 【新增】检查H矩阵的一致性
2566
+ // 照片的H矩阵在不同帧对中应该保持相对稳定
2567
+ let matrixConsistency = 1.0;
2568
+ if (homographyMatrices.length > 1) {
2569
+ matrixConsistency = this.checkHomographyConsistency(homographyMatrices);
2570
+ }
2571
+ // 平面得分 = 误差低 且 H矩阵一致
2572
+ // avgError < 0.01 → 非常可能是平面
2573
+ // avgError > 0.05 → 可能是立体(活体)
2574
+ const errorScore = Math.max(0, 1 - avgError / 0.03);
2575
+ const planarScore = errorScore * matrixConsistency;
2576
+ console.debug('[HomographyConstraint]', {
2577
+ frameCount: errors.length,
2578
+ avgError: avgError.toFixed(4),
2579
+ errorScore: errorScore.toFixed(3),
2580
+ matrixConsistency: matrixConsistency.toFixed(3),
2581
+ planarScore: planarScore.toFixed(3)
2582
+ });
2583
+ return { planarScore: Math.min(planarScore, 1), error: avgError };
2560
2584
  }
2561
2585
  /**
2562
- * 估计仿射变换矩阵 (简化的单应性)
2563
- * 输入:源点和目标点对
2564
- * 输出:[a, b, c, d, e, f] 表示变换 x' = ax + by + c, y' = dx + ey + f
2586
+ * 估计仿射变换矩阵
2587
+ * 【改进】使用完整的6参数仿射变换(包含剪切分量)
2588
+ *
2589
+ * 变换形式: x' = ax + by + c, y' = dx + ey + f
2590
+ * 其中 [a, b] 和 [d, e] 可以包含旋转、缩放、剪切分量
2591
+ /**
2592
+ * 【新增】使用DLT算法估计单应性矩阵
2593
+ *
2594
+ * DLT (Direct Linear Transform) 是估计射影变换的标准算法
2595
+ * 输入:源点和目标点对 (至少4对)
2596
+ * 输出:3x3单应性矩阵H,使得 p' = H * p (齐次坐标)
2597
+ *
2598
+ * 相比仿射变换:
2599
+ * - 仿射:6参数,处理旋转+缩放+平移+剪切
2600
+ * - 单应性:8参数,处理完整的射影变换(包括透视)
2601
+ *
2602
+ * 照片倾斜拍摄时,需要完整的单应性矩阵!
2565
2603
  */
2566
- estimateAffineTransform(src, dst) {
2567
- if (src.length < 3 || dst.length < 3)
2604
+ estimateHomographyDLT(src, dst) {
2605
+ if (src.length < 4 || dst.length < 4 || src.length !== dst.length)
2606
+ return null;
2607
+ const n = src.length;
2608
+ // 【关键】对点进行归一化,提高数值稳定性
2609
+ const srcNorm = this.normalizePoints(src);
2610
+ const dstNorm = this.normalizePoints(dst);
2611
+ if (!srcNorm || !dstNorm)
2568
2612
  return null;
2569
- const n = Math.min(src.length, dst.length);
2570
- // 构建方程组 Ax = b (最小二乘)
2571
- // 对于 x': [x1, y1, 1, 0, 0, 0] * [a,b,c,d,e,f]^T = x1'
2572
- // 对于 y': [0, 0, 0, x1, y1, 1] * [a,b,c,d,e,f]^T = y1'
2573
- let sumX = 0, sumY = 0, sumX2 = 0, sumY2 = 0;
2574
- let sumXpX = 0, sumYpY = 0, sumXp = 0, sumYp = 0;
2613
+ // 构建DLT方程矩阵 A (2n x 9)
2614
+ // 对每对点,构建2行方程:
2615
+ // [-x, -y, -1, 0, 0, 0, x*x', y*x', x']
2616
+ // [0, 0, 0, -x, -y, -1, x*y', y*y', y']
2617
+ const A = [];
2575
2618
  for (let i = 0; i < n; i++) {
2576
- const x = src[i][0], y = src[i][1];
2577
- const xp = dst[i][0], yp = dst[i][1];
2578
- sumX += x;
2579
- sumY += y;
2580
- sumX2 += x * x;
2581
- sumY2 += y * y;
2582
- sumXpX += xp * x;
2583
- sumXp += xp;
2584
- sumYpY += yp * y;
2585
- sumYp += yp;
2586
- }
2587
- // 计算缩放和旋转(简化版本)
2588
- const det = sumX2 * n - sumX * sumX;
2619
+ const x = srcNorm.points[i][0];
2620
+ const y = srcNorm.points[i][1];
2621
+ const xp = dstNorm.points[i][0];
2622
+ const yp = dstNorm.points[i][1];
2623
+ // 第一行
2624
+ A.push([-x, -y, -1, 0, 0, 0, x * xp, y * xp, xp]);
2625
+ // 第二行
2626
+ A.push([0, 0, 0, -x, -y, -1, x * yp, y * yp, yp]);
2627
+ }
2628
+ // 使用SVD求解 Ah = 0
2629
+ // h 是最小奇异值对应的右奇异向量
2630
+ const h = this.solveHomographyLSQ(A);
2631
+ if (!h)
2632
+ return null;
2633
+ // 反演应化矩阵(从归一化坐标回到原始坐标)
2634
+ const H = this.denormalizeHomography(h, srcNorm, dstNorm);
2635
+ return H;
2636
+ }
2637
+ /**
2638
+ * 点集归一化(提高数值稳定性)
2639
+ * 变换点使得重心在原点,平均距离为sqrt(2)
2640
+ */
2641
+ normalizePoints(points) {
2642
+ if (points.length === 0)
2643
+ return null;
2644
+ // 计算重心
2645
+ let cx = 0, cy = 0;
2646
+ for (const p of points) {
2647
+ cx += p[0];
2648
+ cy += p[1];
2649
+ }
2650
+ cx /= points.length;
2651
+ cy /= points.length;
2652
+ // 计算平均距离
2653
+ let avgDist = 0;
2654
+ for (const p of points) {
2655
+ const dx = p[0] - cx;
2656
+ const dy = p[1] - cy;
2657
+ avgDist += Math.sqrt(dx * dx + dy * dy);
2658
+ }
2659
+ avgDist /= points.length;
2660
+ // 缩放因子
2661
+ const scale = avgDist > 0.001 ? Math.sqrt(2) / avgDist : 1;
2662
+ // 应用归一化变换
2663
+ const normalized = [];
2664
+ for (const p of points) {
2665
+ normalized.push([
2666
+ (p[0] - cx) * scale,
2667
+ (p[1] - cy) * scale
2668
+ ]);
2669
+ }
2670
+ // 归一化矩阵 T
2671
+ const T = [
2672
+ [scale, 0, -cx * scale],
2673
+ [0, scale, -cy * scale],
2674
+ [0, 0, 1]
2675
+ ];
2676
+ return { points: normalized, T };
2677
+ }
2678
+ /**
2679
+ * 最小二乘法求解 Ah = 0
2680
+ * 其中 h 是3x3矩阵的向量化形式
2681
+ */
2682
+ solveHomographyLSQ(A) {
2683
+ // 简化的SVD求解:找使 ||Ah|| 最小的 h
2684
+ // 为了简化,使用迭代最小二乘法或对称矩阵的特征向量
2685
+ if (A.length < 8)
2686
+ return null;
2687
+ // A^T * A 矩阵 (9x9)
2688
+ const ATA = Array(9).fill(0).map(() => Array(9).fill(0));
2689
+ for (let i = 0; i < 9; i++) {
2690
+ for (let j = 0; j < 9; j++) {
2691
+ for (let k = 0; k < A.length; k++) {
2692
+ ATA[i][j] += A[k][i] * A[k][j];
2693
+ }
2694
+ }
2695
+ }
2696
+ // 求ATA的最小特征向量(对应最小特征值)
2697
+ const eigenVec = this.getSmallestEigenvector(ATA);
2698
+ return eigenVec;
2699
+ }
2700
+ /**
2701
+ * 求3x3对称矩阵的最小特征向量(简化版本)
2702
+ * 使用幂迭代法或直接求解
2703
+ */
2704
+ getSmallestEigenvector(mat) {
2705
+ if (mat.length !== 9)
2706
+ return null;
2707
+ // 为了简化,使用初步估计:取行和最小的方向
2708
+ // 或者使用固定迭代次数的幂法
2709
+ // 简化:返回一个初步猜测的向量
2710
+ // 实际应用应该使用完整的SVD或特征值分解库
2711
+ // 这里使用Power Iteration的反向版本(找最小特征值)
2712
+ let v = [1, 1, 1, 1, 1, 1, 1, 1, 1];
2713
+ for (let iter = 0; iter < 10; iter++) {
2714
+ // v_new = A * v
2715
+ const v_new = [0, 0, 0, 0, 0, 0, 0, 0, 0];
2716
+ for (let i = 0; i < 9; i++) {
2717
+ for (let j = 0; j < 9; j++) {
2718
+ v_new[i] += mat[i][j] * v[j];
2719
+ }
2720
+ }
2721
+ // 归一化
2722
+ const norm = Math.sqrt(v_new.reduce((a, b) => a + b * b, 0));
2723
+ if (norm < 0.0001)
2724
+ break;
2725
+ for (let i = 0; i < 9; i++) {
2726
+ v[i] = v_new[i] / norm;
2727
+ }
2728
+ }
2729
+ return v;
2730
+ }
2731
+ /**
2732
+ * 反演应化矩阵
2733
+ * H_orig = T_dst^-1 * H_norm * T_src
2734
+ */
2735
+ denormalizeHomography(h, srcNorm, dstNorm) {
2736
+ // 将向量h转换为3x3矩阵
2737
+ const H_norm = [
2738
+ [h[0], h[1], h[2]],
2739
+ [h[3], h[4], h[5]],
2740
+ [h[6], h[7], h[8]]
2741
+ ];
2742
+ // H = T_dst^-1 * H_norm * T_src
2743
+ const T_src = srcNorm.T;
2744
+ const T_dst = dstNorm.T;
2745
+ const T_dst_inv = this.invertMatrix3x3(T_dst);
2746
+ if (!T_dst_inv)
2747
+ return H_norm;
2748
+ // 矩阵乘法:(3x3) * (3x3) * (3x3)
2749
+ const temp = this.multiplyMatrix3x3(T_dst_inv, H_norm);
2750
+ const H = this.multiplyMatrix3x3(temp, T_src);
2751
+ return H;
2752
+ }
2753
+ /**
2754
+ * 3x3矩阵求逆
2755
+ */
2756
+ invertMatrix3x3(m) {
2757
+ const [m00, m01, m02] = m[0];
2758
+ const [m10, m11, m12] = m[1];
2759
+ const [m20, m21, m22] = m[2];
2760
+ const det = m00 * (m11 * m22 - m12 * m21) -
2761
+ m01 * (m10 * m22 - m12 * m20) +
2762
+ m02 * (m10 * m21 - m11 * m20);
2589
2763
  if (Math.abs(det) < 0.0001)
2590
2764
  return null;
2591
- const a = (sumXpX * n - sumXp * sumX) / (sumX2 * n - sumX * sumX + 0.0001);
2592
- const b = 0; // 简化,忽略剪切
2593
- const d = 0;
2594
- const e = (sumYpY * n - sumYp * sumY) / (sumY2 * n - sumY * sumY + 0.0001);
2595
- const c = sumXp / n - a * sumX / n;
2596
- const f = sumYp / n - e * sumY / n;
2597
- return [a || 1, b, c || 0, d, e || 1, f || 0];
2765
+ const inv = [
2766
+ [
2767
+ (m11 * m22 - m12 * m21) / det,
2768
+ (m02 * m21 - m01 * m22) / det,
2769
+ (m01 * m12 - m02 * m11) / det
2770
+ ],
2771
+ [
2772
+ (m12 * m20 - m10 * m22) / det,
2773
+ (m00 * m22 - m02 * m20) / det,
2774
+ (m02 * m10 - m00 * m12) / det
2775
+ ],
2776
+ [
2777
+ (m10 * m21 - m11 * m20) / det,
2778
+ (m01 * m20 - m00 * m21) / det,
2779
+ (m00 * m11 - m01 * m10) / det
2780
+ ]
2781
+ ];
2782
+ return inv;
2783
+ }
2784
+ /**
2785
+ * 3x3矩阵乘法
2786
+ */
2787
+ multiplyMatrix3x3(A, B) {
2788
+ const result = Array(3).fill(0).map(() => Array(3).fill(0));
2789
+ for (let i = 0; i < 3; i++) {
2790
+ for (let j = 0; j < 3; j++) {
2791
+ for (let k = 0; k < 3; k++) {
2792
+ result[i][j] += A[i][k] * B[k][j];
2793
+ }
2794
+ }
2795
+ }
2796
+ return result;
2598
2797
  }
2599
2798
  /**
2600
- * 应用仿射变换
2799
+ * 应用单应性变换(齐次坐标)
2800
+ * p' = H * p / (H * p 的 Z 分量)
2601
2801
  */
2602
- applyAffineTransform(t, x, y) {
2603
- return [
2604
- t[0] * x + t[1] * y + t[2],
2605
- t[3] * x + t[4] * y + t[5]
2802
+ applyHomography(H, x, y) {
2803
+ // 齐次坐标
2804
+ const p = [x, y, 1];
2805
+ // H * p
2806
+ const Hp = [
2807
+ H[0][0] * p[0] + H[0][1] * p[1] + H[0][2] * p[2],
2808
+ H[1][0] * p[0] + H[1][1] * p[1] + H[1][2] * p[2],
2809
+ H[2][0] * p[0] + H[2][1] * p[1] + H[2][2] * p[2]
2606
2810
  ];
2811
+ // 反齐次化
2812
+ if (Math.abs(Hp[2]) < 0.0001) {
2813
+ return [Hp[0], Hp[1]];
2814
+ }
2815
+ return [Hp[0] / Hp[2], Hp[1] / Hp[2]];
2816
+ }
2817
+ /**
2818
+ * 【新增】检查单应性矩阵的一致性
2819
+ *
2820
+ * 原理:
2821
+ * - 照片旋转时,每对相邻帧的H矩阵应该相近(因为是持续旋转)
2822
+ * - 真实人脸做随机动作时,H矩阵会变化很大
2823
+ */
2824
+ checkHomographyConsistency(matrices) {
2825
+ if (matrices.length < 2)
2826
+ return 1;
2827
+ // 计算矩阵间的相似度
2828
+ let totalSimilarity = 0;
2829
+ let pairCount = 0;
2830
+ for (let i = 1; i < matrices.length; i++) {
2831
+ const M1 = matrices[i - 1];
2832
+ const M2 = matrices[i];
2833
+ // Frobenius范数相似度
2834
+ let sumDiff = 0;
2835
+ for (let r = 0; r < 3; r++) {
2836
+ for (let c = 0; c < 3; c++) {
2837
+ const diff = M1[r][c] - M2[r][c];
2838
+ sumDiff += diff * diff;
2839
+ }
2840
+ }
2841
+ const frobeniusDist = Math.sqrt(sumDiff);
2842
+ // 标准化距离(除以矩阵范数)
2843
+ let normM1 = 0, normM2 = 0;
2844
+ for (let r = 0; r < 3; r++) {
2845
+ for (let c = 0; c < 3; c++) {
2846
+ normM1 += M1[r][c] * M1[r][c];
2847
+ normM2 += M2[r][c] * M2[r][c];
2848
+ }
2849
+ }
2850
+ normM1 = Math.sqrt(normM1);
2851
+ normM2 = Math.sqrt(normM2);
2852
+ const avgNorm = (normM1 + normM2) / 2;
2853
+ const normalizedDist = avgNorm > 0.1 ? Math.min(frobeniusDist / avgNorm, 2) : 2;
2854
+ // 将距离转换为相似度 (0-1)
2855
+ // 距离越小,相似度越高
2856
+ const similarity = Math.max(0, 1 - normalizedDist / 2);
2857
+ totalSimilarity += similarity;
2858
+ pairCount++;
2859
+ }
2860
+ return pairCount > 0 ? totalSimilarity / pairCount : 1;
2607
2861
  }
2608
2862
  /**
2609
2863
  * 【关键】检测深度一致性
@@ -2705,32 +2959,28 @@ class MotionLivenessDetector {
2705
2959
  const planarPattern = consistentFrames / depthChanges.length;
2706
2960
  return { planarPattern };
2707
2961
  }
2708
- /**
2709
- * 【关键】检测透视变换模式
2710
- *
2711
- * 原理:
2712
- * - 照片偏转时,特征点位置变化遵循严格的透视变换规律
2713
- * - 检测左右脸的相对变化是否符合透视投影
2714
- */
2715
2962
  /**
2716
2963
  * 【透视变换模式检测】
2717
2964
  *
2718
- * 【重要修复】使用归一化坐标进行比较
2965
+ * 【改进】使用原始坐标而不是归一化坐标
2719
2966
  *
2720
2967
  * 原理:照片左右偏转时,左右脸宽度比例会平滑变化
2968
+ * 这种比例变化遵循严格的透视投影规律
2721
2969
  */
2722
2970
  detectPerspectiveTransformPattern() {
2723
- // 【关键】使用归一化坐标历史
2724
- if (this.normalizedLandmarksHistory.length < 3) {
2971
+ // 【关键】使用原始坐标历史,而不是归一化坐标
2972
+ // 透视变换的宽度比例变化在原始图像坐标中更准确
2973
+ if (this.faceLandmarksHistory.length < 3) {
2725
2974
  return { perspectiveScore: 0 };
2726
2975
  }
2727
2976
  // 比较左右脸的宽度比例变化
2728
2977
  // 照片左偏时:右脸变窄,左脸变宽(透视效果)
2729
2978
  // 这种变化应该是平滑且可预测的
2730
2979
  const widthRatios = [];
2731
- for (const frame of this.normalizedLandmarksHistory) {
2980
+ for (const frame of this.faceLandmarksHistory) {
2732
2981
  if (frame.length >= 468) {
2733
- // 使用归一化坐标计算距离比例
2982
+ // 使用原始坐标计算距离比例
2983
+ // 234: 左脸颊边缘,1: 鼻尖,454: 右脸颊边缘
2734
2984
  const leftWidth = this.pointDist(frame[234], frame[1]); // 左脸到鼻子
2735
2985
  const rightWidth = this.pointDist(frame[1], frame[454]); // 鼻子到右脸
2736
2986
  if (leftWidth > 0 && rightWidth > 0) {
@@ -2774,10 +3024,28 @@ class MotionLivenessDetector {
2774
3024
  // 这是最可靠的检测方式,优先级最高
2775
3025
  const isPhotoByGeometry = photoGeometry.isPhoto;
2776
3026
  const photoConfidence = photoGeometry.confidence || 0;
3027
+ const frameCount = Math.max(this.eyeAspectRatioHistory.length, this.mouthAspectRatioHistory.length, this.faceLandmarksHistory.length, this.normalizedLandmarksHistory.length);
3028
+ // 【改进】根据帧数调整照片检测的敏感度
3029
+ // 少帧情况下,照片特征更容易误判,但如果几何约束强,仍应拒绝
3030
+ let photoConfidenceThreshold = 0.55;
3031
+ if (frameCount < 8) {
3032
+ // 少于8帧时,提高拒绝阈值,但只对超强照片特征有效
3033
+ // perspectiveScore=1.0 是超强信号,不应该放过
3034
+ if ((photoGeometry.details?.perspectiveScore || 0) > 0.95) {
3035
+ photoConfidenceThreshold = 0.45; // 降低阈值
3036
+ }
3037
+ else {
3038
+ photoConfidenceThreshold = 0.65; // 提高阈值
3039
+ }
3040
+ }
2777
3041
  // 如果照片几何检测高置信度判定为照片,直接拒绝
2778
- if (isPhotoByGeometry && photoConfidence > 0.75) {
3042
+ // 【改进】根据帧数和具体特征调整阈值
3043
+ if (isPhotoByGeometry && photoConfidence > photoConfidenceThreshold) {
2779
3044
  console.debug('[Decision] REJECTED by photo geometry detection', {
2780
3045
  photoConfidence: photoConfidence.toFixed(3),
3046
+ photoConfidenceThreshold: photoConfidenceThreshold.toFixed(3),
3047
+ perspectiveScore: (photoGeometry.details?.perspectiveScore || 0).toFixed(3),
3048
+ frameCount,
2781
3049
  details: photoGeometry.details
2782
3050
  });
2783
3051
  return false;
@@ -2847,11 +3115,13 @@ class MotionLivenessDetector {
2847
3115
  /**
2848
3116
  * 检查脸部形状稳定性
2849
3117
  *
2850
- * 【重要修复】使用归一化坐标进行比较
2851
- * 这样即使人脸在画面中移动或缩放,比较仍然有效
3118
+ * 【合理使用归一化坐标】这里使用归一化坐标是有意义的,因为:
3119
+ * - 目的是检测【脸部形状的变化】(眼睛距离、嘴巴高度等)
3120
+ * - 与人脸在画面中的位置和尺寸无关
3121
+ * - 消除平移和缩放影响,专注于形状的变化
2852
3122
  */
2853
3123
  checkFaceShapeStability() {
2854
- // 【关键】使用归一化坐标历史
3124
+ // 使用归一化坐标历史,消除平移和缩放影响
2855
3125
  if (this.normalizedLandmarksHistory.length < 5) {
2856
3126
  return 0.5; // 数据不足
2857
3127
  }
@@ -2864,7 +3134,7 @@ class MotionLivenessDetector {
2864
3134
  return 0.95; // 非常可能是照片
2865
3135
  }
2866
3136
  // 【第二层防护】检测脸部形状稳定性
2867
- // 使用归一化坐标计算距离
3137
+ // 使用归一化坐标计算相对距离,检测形状变化
2868
3138
  const faceDistances = [];
2869
3139
  // 计算以下距离:
2870
3140
  // 1. 左眼-右眼(眼距)