Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

263
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda