Skip to content

人脸识别演示

实时检测人脸位置、面部特征点、人脸角度、属性分析,支持被动式活体检测。

交互演示

↗ 在新窗口打开演示

核心实现

typescript
import { IDScanner } from 'id-scanner-lib';

const scanner = new IDScanner();
await scanner.initialize();

const faceModule = scanner.getFaceModule({
  landmarks: true,
  landmarksType: 'full',
  livenessCheck: true,
  minFaceSize: 100
});

// 启动实时人脸检测
const video = document.getElementById('video');
await faceModule.startCapture(video, {
  facingMode: 'user',
  resolution: 'hd'
});

faceModule.on('detection', (result) => {
  result.faces.forEach(face => {
    // 绘制人脸框
    ctx.strokeRect(face.box.x, face.box.y, face.box.width, face.box.height);
    // 活体状态
    console.log('活体:', face.live);
    console.log('置信度:', face.score);
  });
});

人脸对比

typescript
// 提取人脸特征
const feat1 = await faceModule.extractFaceFeatures(image1);
const feat2 = await faceModule.extractFaceFeatures(image2);

// 计算相似度
const similarity = await faceModule.compareFaceFeatures(feat1, feat2);
console.log(`相似度: ${(similarity * 100).toFixed(2)}%`);

高级配置

typescript
const faceModule = scanner.getFaceModule({
  // 检测选项
  scoreThreshold: 0.7,
  minFaceSize: 100,

  // 特征点
  landmarks: true,
  landmarksType: 'full',

  // 活体检测
  livenessCheck: true,
  livenessThreshold: 0.85,

  // 属性
  age: true,
  gender: true,
  emotion: false,

  // 性能
  useWorker: true,
  modelType: 'standard'
});

基于 MIT 许可发布