仅捕获检测到的人脸广场 JS
Capture Detected Face Square Only JS
我是运行在网络摄像头中通过JS进行人脸检测模型,它可以识别人脸并正确绘制方框。那么我怎样才能将检测到的人脸仅作为本地图像保存到我的计算机上?
感谢您的帮助!我卡住了!
代码(来自face-api.js)如下:
JavaScript
const video = document.getElementById('video')
const snap = document.getElementById('snap')
const canvas = document.getElementById('canvas')
Promise.all([
faceapi.nets.tinyFaceDetector.loadFromUri('/static/models'),
faceapi.nets.faceExpressionNet.loadFromUri('/static/models')
]).then(startVideo)
function startVideo() {
navigator.getUserMedia(
{ video: {} },
stream => video.srcObject = stream,
err => console.error(err)
)
}
video.addEventListener('play', () => {
const canvas = faceapi.createCanvasFromMedia(video)
document.body.append(canvas)
const displaySize = { width: video.width, height: video.height }
faceapi.matchDimensions(canvas, displaySize)
setInterval(async () => {
const detections = await faceapi.detectAllFaces(video, new faceapi.TinyFaceDetectorOptions()).withFaceExpressions()
const resizedDetections = faceapi.resizeResults(detections, displaySize)
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height)
faceapi.draw.drawDetections(canvas, resizedDetections)
faceapi.draw.drawFaceExpressions(canvas, resizedDetections)
}, 100)
})
HTML
<div id="cam">
<video id="video" width="720" height="560" autoplay muted></video>
</div>
<div class="control">
<button id="snap" class="btn btn-primary">Capture</button>
</div>
<canvas id="canvas" width="197" height="197"></canvas>
你有一个canvas。你可以保存一个canvas:How To Save Canvas As An Image With canvas.toDataURL()?
假设detections
是一个数组:
// Taken from
function saveCanvas(canvas) {
const image = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
window.location.href=image; // it will save locally
}
// ... in your code ...
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height)
faceapi.draw.drawDetections(canvas, resizedDetections)
faceapi.draw.drawFaceExpressions(canvas, resizedDetections)
if (detections.length)
saveCanvas(canvas);
我是运行在网络摄像头中通过JS进行人脸检测模型,它可以识别人脸并正确绘制方框。那么我怎样才能将检测到的人脸仅作为本地图像保存到我的计算机上?
感谢您的帮助!我卡住了!
代码(来自face-api.js)如下:
JavaScript
const video = document.getElementById('video')
const snap = document.getElementById('snap')
const canvas = document.getElementById('canvas')
Promise.all([
faceapi.nets.tinyFaceDetector.loadFromUri('/static/models'),
faceapi.nets.faceExpressionNet.loadFromUri('/static/models')
]).then(startVideo)
function startVideo() {
navigator.getUserMedia(
{ video: {} },
stream => video.srcObject = stream,
err => console.error(err)
)
}
video.addEventListener('play', () => {
const canvas = faceapi.createCanvasFromMedia(video)
document.body.append(canvas)
const displaySize = { width: video.width, height: video.height }
faceapi.matchDimensions(canvas, displaySize)
setInterval(async () => {
const detections = await faceapi.detectAllFaces(video, new faceapi.TinyFaceDetectorOptions()).withFaceExpressions()
const resizedDetections = faceapi.resizeResults(detections, displaySize)
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height)
faceapi.draw.drawDetections(canvas, resizedDetections)
faceapi.draw.drawFaceExpressions(canvas, resizedDetections)
}, 100)
})
HTML
<div id="cam">
<video id="video" width="720" height="560" autoplay muted></video>
</div>
<div class="control">
<button id="snap" class="btn btn-primary">Capture</button>
</div>
<canvas id="canvas" width="197" height="197"></canvas>
你有一个canvas。你可以保存一个canvas:How To Save Canvas As An Image With canvas.toDataURL()?
假设detections
是一个数组:
// Taken from
function saveCanvas(canvas) {
const image = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
window.location.href=image; // it will save locally
}
// ... in your code ...
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height)
faceapi.draw.drawDetections(canvas, resizedDetections)
faceapi.draw.drawFaceExpressions(canvas, resizedDetections)
if (detections.length)
saveCanvas(canvas);