Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

262
Views
How to detect and use face landmark detection from face-api.js on the image snapshotted from the webcam?

I want to use face landmark detection from face-api.js library on the image snapshotted from a webcam when the capture button is pressed. How do i do that?

I know it is not the perfect code, but my process of thinking was take the snapshot from the webcam, place it in the <img src""> then use the face landmark detection on the snapshotted image.

HTML

    <img src="" id="originalImg" />
    <canvas id="canvas" width="320" height="240"></canvas>
    <canvas id="reflay" width="320" height="240" class="overlay"></canvas>
    <video id="player" width="320" height="240" autoplay></video>
    <button id="capture">Capture</button>


    <script>
      const player = document.getElementById('player');
      const canvas = document.getElementById('canvas');
      const context = canvas.getContext('2d');
      const captureButton = document.getElementById('capture');

      const constraints = {
        video: true,
      };

      captureButton.addEventListener('click', () => {
        // Draw the video frame to the canvas.
        context.drawImage(player, 0, 0, canvas.width, canvas.height);
      });

      // Attach the video stream to the video element and autoplay.
      navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
        player.srcObject = stream;
      });
    </script>

JavaScript

$(document).ready(function(){
                
    async function face(){
        
        const MODEL_URL = '/models'

        await faceapi.loadSsdMobilenetv1Model(MODEL_URL)
        await faceapi.loadFaceLandmarkModel(MODEL_URL)


        const img = convertCanvasToImage();
        let faceDescriptions = await faceapi.detectAllFaces(img).withFaceLandmarks()
        const canvas = $('#reflay').get(0)
        faceapi.matchDimensions(canvas, img)

        faceDescriptions = faceapi.resizeResults(faceDescriptions, img)
        faceapi.draw.drawDetections(canvas, faceDescriptions)
        faceapi.draw.drawFaceLandmarks(canvas, faceDescriptions)

    }
    
    return face()
})

function convertCanvasToImage(){
    var image = new Image();
    image.src = canvas.toDataURL("image/jpeg");
    return image = document.getElementById('originalImg');
}

Here is the JSFiddle if that makes it easier. Thank you.

JSFiddle

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!