face-api实现浏览器调用摄像头人脸识别年龄性别表情脸部轮廓等数据代码
代码语言:html
所属分类:多媒体
代码描述:face-api实现浏览器调用摄像头人脸识别年龄性别表情脸部轮廓等数据代码,请在bfwstudio调试运行
代码标签: face-api 浏览器 调用 摄像头 人脸识别 年龄 性别 表情 脸部 轮廓 数据 代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en"> <head> <title>FaceAPI Live WebCam Demo</title> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <meta name="viewport" content="width=device-width, shrink-to-fit=yes"> </head> <body style="font-family: monospace; background: black; color: white; font-size: 16px; line-height: 22px; margin: 0; overflow: hidden"> <video id="video" playsinline class="video"></video> <canvas id="canvas" class="canvas" style="position: fixed; top: 0; left: 0; z-index: 10"></canvas> 请在bfwstudio调试运行。 <div id="log" style="overflow-y: scroll; height: 16.5rem"></div> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/face-api.1.7.13.js"></script> <script type="module"> /** * FaceAPI Demo for Browsers * Loaded via `webcam.html` */ // import * as faceapi from '//repo.bfw.wiki/bfwrepo/js/face-api.esm.1.7.13.js' // configuration options const modelPath = '../models/'; // path to model folder that will be loaded using http // const modelPath = 'https://cdn.jsdelivr.net/npm/@vladmandic/face-api/model/'; // path to model folder that will be loaded using http const minScore = 0.2; // minimum score const maxResults = 5; // maximum number of results to return let optionsSSDMobileNet; // helper function to pretty-print json object to string function str(json) { let text = '<font color="lightblue">'; text += json ? JSON.stringify(json).replace(/{|}|"|\[|\]/g, '').replace(/,/g, ', ') : ''; text += '</font>'; return text; } // helper function to print strings to html document as a log function log(...txt) { console.log(...txt); // eslint-disable-line no-console const div = document.getElementById('log'); if (div) div.innerHTML += `<br>${txt}`; } // helper function to draw detected faces function drawFaces(canvas, data, fps) { const ctx = canvas.getContext('2d', { willReadFrequently: true }); if (!ctx) return; ctx.clearRect(0, 0, canvas.width, canvas.height); // draw title ctx.font = 'small-caps 20px "Segoe UI"'; ctx.fillStyle = 'white'; ctx.fillText(`FPS: ${fps}`, 10, 25); for (const person of data) { // draw box around each face ctx.lineWidth = 3; ctx.strokeStyle = 'deepskyblue'; ctx.fillStyle = 'deepskyblue'; ctx.globalAlpha = 0.6; ctx.beginPath(); ctx.rect(person.detection.box.x, person.detection.box.y, person.detection.box.width, person.detection.box.height); ctx.stroke(); ctx.globalAlpha = 1; // draw text labels const expression = Object.entries(person.expressions).sort((a, b) => b[1] - a[1]); ctx.fillStyle = 'black'; ctx.fillText(`gender: ${Math.round(100 * person.genderProbability)}% ${person.gender}`, person.detection.box.x, person.detection.box.y - 59); ctx.fillText(`expression: ${Math.round(100 * expression[0][1])}% ${expression[0][0]}`, person.detection.box.x, person.detection.box.y - 41); ctx.fillText(`age: ${Math.round(person.age)} years`, person.detection.box.x, person.detection.box.y - 23); ctx.fillText(`roll:${person.angle.roll}° pitch:${person.angle.pitch}° yaw:${person.angle.yaw}°`, person.detection.box.x, person.detection.box.y - 5); ctx.fillStyle = 'lightblue'; ctx.fillText(`gender: ${Math.round(100 * person.genderProbability)}% ${person.gender}`, person.detection.box.x, person.detection.box.y - 60); ctx.fillText(`expression: ${Math.round(100 * expression[0][1])}% ${expression[0][0]}`, person.detection.box.x, person.detection.box.y - 42); ctx.fillText(`age: ${Math.round(person.age)} years`, person.detection.box.x, person.detection.box.y - 24); ctx.fillText(`roll:${person.angle.roll}° pitch:${person.angle.pitch}° yaw:${person.angle.yaw}°`, person.detection.box.x, person.detection.box.y - 6); // draw face points for each face ctx.globalAlpha = 0.8; ctx.fillStyle = 'lightblue'; const pointSize = 2; for (let i = 0; i < person.landmarks.positions.length; i++) { ctx.beginPath(); ctx.arc(person.landmarks.positions[i].x, person.landmarks.positions[i].y, pointSize, 0, 2 * Math.PI); ctx.fill(); } } } async function detectVideo(video, canvas) { if (!video || video.paused) return false; const t0 = performance.now(); faceapi .detectAllFaces(video, optionsSSDMobileNet) .withFaceLandmarks() .withFaceExpressions().........完整代码请登录后点击上方下载按钮下载查看
网友评论0