html人脸识别代码
导读:最近在学习人脸识别,发现了一种 HTML 实现人脸识别的方法。以下是代码实现:<!DOCTYPE html><html><head> <title>人脸识别</title>...
最近在学习人脸识别,发现了一种 HTML 实现人脸识别的方法。以下是代码实现:
!DOCTYPE html> html> head> title> 人脸识别/title> script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> /script> script src="https://github.com/justadudewhohacks/face-api.js/blob/master/dist/face-api.min.js"> /script> script> Promise.all([ faceapi.nets.faceRecognitionNet.loadFromUri('/models'), faceapi.nets.faceLandmark68Net.loadFromUri('/models'), faceapi.nets.ssdMobilenetv1.loadFromUri('/models') ]).then(start); async function start() { const container = document.createElement('div'); container.style.position = 'relative'; document.body.append(container); const image = await faceapi.fetchImage('img.jpg'); const detections = await faceapi.detectAllFaces(image).withFaceLandmarks().withFaceDescriptors(); const dims = faceapi.matchDimensions(container, image); const resizedDetections = faceapi.resizeResults(detections, dims); resizedDetections.forEach(detection => { const box = detection.detection.box; const drawBox = new faceapi.draw.DrawBox(box, { label: 'Face' } ); drawBox.draw(container); } ); } /script> /head> body> /body> /html>
解释一下代码:首先载入 jQuery 和 face-api.js 库,接着在 Promise 中载入人脸识别所需的三个模型(faceRecognitionNet、faceLandmark68Net、ssdMobilenetv1),然后使用 start 函数开始识别人脸。
在 start 函数中,我们先创建一个 div 容器,然后读取图片并进行人脸识别。使用 matchDimensions 函数将识别结果缩放到容器大小,再使用 draw 函数将人脸部位标注到图片上,最后将标注后的图片放入容器。
总体上,这段代码实现了简单的人脸识别,基于 HTML 实现,使用较为简单,适合初学者。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: html人脸识别代码
本文地址: https://pptw.com/jishu/534616.html