face-api实现浏览器调用摄像头人脸识别添加面具跟随效果代码
代码语言:html
所属分类:多媒体
代码描述:face-api实现浏览器调用摄像头人脸识别添加面具跟随效果代码,类似抖音中的摄像头特效。
代码标签: face-api 浏览器 调用 摄像头 人脸识别 添加 面具 跟随
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Face Mask with Camera</title> <style> body { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; margin: 0; background-color: #f0f0f0; } .video-container { position: relative; width: 720px; height: 560px; } video, canvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%; border: 1px solid #ccc; border-radius: 4px; } </style> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/face-api.1.7.13.js"></script> </head> <body> <video id="video" width="720" height="560" autoplay muted></video> <canvas id="overlay" width="720" height="560"></canvas> 请在bfwstudio中调试运行 <script> async function getThirdCameraDeviceId() { const devices = await navigator.mediaDevices.enumerateDevices(); const videoDevices = devices.filter(device => device.kind === 'videoinput'); // if (videoDevices.length >= 3) { return videoDevices[0].deviceId; // } else { // throw new Error('Less than 3 video input devices found'); // } } async function setupCamera() { const video = document.getElementById('video'); try { const thirdCameraId = await getThirdCameraDeviceId(); const stream = await navigator.mediaDevices.getUserMedia({ video: { deviceId: thirdCameraId } }); video.srcObject = stream; } catch (error) { console.error('Error accessing camera:', error); .........完整代码请登录后点击上方下载按钮下载查看
网友评论0