Search code examples
javascriptcanvasface-detectionimage-capture

Capture Detected Face Square Only JS


I'm running a face detection model via JS in the webcam, it recognises the face and draws the box correctly. How can I then go about saving the detected face only as an image locally to my computer?

Im grateful for any help! Im stuck!

The code (from face-api.js) is as follows:

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>

Solution

  • You have a canvas. You can save a canvas: How To Save Canvas As An Image With canvas.toDataURL()?

    Assuming detections is an array:

    // Taken from https://stackoverflow.com/a/15685544/4088472
    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);