shape detection api 已废弃,chrome 85 起完全移除 facedetector;现仅能通过 tensorflow.js 或 blazeface 等 webassembly 模型实现客户端人脸检测,需预加载、注意尺寸与内存管理。

Shape Detection API 在现代浏览器中已废弃
Chrome 85 起,ShapeDetectionAPI(包括 FaceDetector)已被完全移除,调用 new FaceDetector() 会直接抛出 ReferenceError: FaceDetector is not defined 或 TypeError: Failed to construct 'FaceDetector': API not available。这不是权限或 HTTPS 问题,而是标准废弃——W3C 已终止该提案,Chrome、Edge 均不再支持。
替代方案:Client-side 人脸检测只能靠 WebAssembly 模型
没有原生 API 支持后,实际可用的方案只剩基于 TensorFlow.js 或 @tensorflow-models/blazeface 的纯前端推理。这些模型运行在 WebGL 或 WebAssembly 后端,不依赖服务端,但需注意:
-
blazeface模型约 1.2 MB,首次加载会阻塞检测初始化,建议预加载:await blazeface.load({ maxFaces: 1, backend: 'webgl' }) - 必须在
https://或localhost下运行,http://非本地域会触发getUserMedia拒绝 - 对低配 Android 设备(如骁龙 400 系列),
webgl后端可能崩溃,fallback 到wasm后性能下降明显(~300ms/frame) - 输入图像需转为
tf.tensor3d,尺寸建议 ≤ 640×480;过大尺寸会导致 OOM 或 WebGL context lost
一个最小可行检测流程(BlazeFace + video 元素)
以下代码跳过模型加载错误处理,仅展示核心链路:
使用 PptxGenJS 和 Python 解析,将结构化的 HTML/CSS 幻灯片内容转换为原生、可编辑的 PowerPoint(.pptx)演示文稿。
<pre class="brush:php;toolbar:false;">const model = await blazeface.load();
const video = document.getElementById('video');
const canvas = document.getElementById('overlay');
const ctx = canvas.getContext('2d');
<p>// 从 video 抓帧并送入模型
function detect() {
const tensor = tf.browser.fromPixels(video)
.resizeNearestNeighbor([128, 128]) // BlazeFace 输入固定为 128x128
.expandDims(0) // batch dim
.cast('int32');</p><p>return model.estimateFaces(tensor, { flipHorizontal: false })
.then(faces => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
faces.forEach(face => {
const { topLeft, bottomRight } = face.boundingBox;
ctx.strokeStyle = '#00ff00';
ctx.lineWidth = 2;
ctx.strokeRect(topLeft[0], topLeft[1],
bottomRight[0] - topLeft[0],
bottomRight[1] - topLeft[1]);
});
})
.finally(() => tensor.dispose());
}</p><p>// 每 100ms 检测一帧(避免过度消耗)
setInterval(detect, 100);
</p>
注意:resizeNearestNeighbor 必须显式调用,否则输入尺寸不匹配会静默失败;tensor.dispose() 不调用会导致内存持续增长。
为什么不用 face-api.js 或 MediaPipe?
face-api.js 底层仍依赖 tfjs,模型更大(SSD Mobilenet 约 5 MB),首屏延迟高;MediaPipe 的 @mediapipe/face_detection 虽精度略高,但需手动编排 WASM 加载与 GL 初始化,在 iOS Safari 上存在 WebGLRenderingContext 兼容性问题(尤其 iOS 16.4–16.6)。BlazeFace 是目前在多端兼容性与启动速度之间最平衡的选择。
真正难的不是调用 API,而是接受「浏览器里没有系统级人脸检测」这个事实——所有 JS 方案都绕不开模型体积、首帧延迟、低端机 fallback 这三道坎。
大量免费API接口:立即使用
涵盖生活服务API、金融科技API、企业工商API、等相关的API接口服务。免费API接口可安全、合规地连接上下游,为数据API应用能力赋能!










