Finding co-ordinates of all points in JavaScript Based facial landmark detection

361 views Asked by At

I have to detect the facial landmarks using the JavaScript programming language. For this, I have seen this video tutorial. Also, I am successfully able to execute the code of the video provided at. Now I have display the co-ordinates values of each facial landmark points. I have the following code for facial landmark detection:

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()).withFaceLandmarks().withFaceExpressions()
    const resizedDetections = faceapi.resizeResults(detections, displaySize)
    canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height)
    faceapi.draw.drawDetections(canvas, resizedDetections)
    faceapi.draw.drawFaceLandmarks(canvas, resizedDetections)
    faceapi.draw.drawFaceExpressions(canvas, resizedDetections)
  }, 100)
})

Please suggest me what should I do to access the coordinates of facial landmark points.

1

There are 1 answers

0
Vaibhav singh On

You can try this code to get landmarks of resized dimentions :

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()).withFaceLandmarks()
    const resizedDetections = faceapi.resizeResults(detections, displaySize)
    canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height)
  
   //parsing the landmarks correctly
    var landmarks = resizedDetections[0]['landmarks']._positions;


    //adding canvas to draw over the face
    var ctx = canvas.getContext("2d");


    //looping over landmarks to draw the canvas
    for (var i = 0; i < landmarks.length; i++) {
      var x_val = landmarks[i]._x;
      var y_val = landmarks[i]._y;
      ctx.beginPath();
      console.log(x_val);
      console.log(y_val);
      ctx.arc(x_val, y_val, 2, 0, Math.PI*2, false);
      ctx.fillStyle = "green";
      ctx.fill();
      ctx.closePath();
    }
    
  
  }, 100)
})