Search code examples
javascriptartificial-intelligencetypeerrorface-recognition

Face recognition in javascript using face api getting "Uncaught (in promise) TypeError: Cannot read property 'descriptor' of undefined"


Facing this issue in the implementation of face recognition on the webcam camera for the browser using node js for the web application. This error only comes when I add more than 1 name in my labels in loadLabeledImages() function. If I have one name, it works perfectly fine. Beginner here and I have been getting the same error for days now

If I log Resized detections and detections, I get this: Error after logging detections and resized detections

Any help is appreciated, thank you so much!

setInterval(async () => {
const detections = await faceapi.detectSingleFace(video).withFaceLandmarks().withFaceDescriptor()
const resizedDetections = faceapi.resizeResults(detections, displaySize)
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height)
faceapi.draw.drawFaceLandmarks(canvas, resizedDetections)
this.labeledFaceDescriptors = await this.loadLabeledImages()
const faceMatcher = new faceapi.FaceMatcher(labeledFaceDescriptors, 0.6)
const results = resizedDetections.map(d => faceMatcher.findBestMatch(d.descriptor))
results.forEach((result, i) => {
  const box = resizedDetections[i].detection.box
  const drawBox = new faceapi.draw.DrawBox(box, {label: result.toString()})
  drawBox.draw(canvas) 
})

}, 100) 
})

function loadLabeledImages() {

try{
const labels = ['Jane', 'Alex']

return Promise.all(
  labels.map(async label => {
    const descriptions = []
    for (let i = 1; i <= 3; i++) {
      const img = await faceapi.fetchImage(`public/img/${label}/${i}.jpg`)
      const detections = await faceapi.detectSingleFace(img).withFaceLandmarks().withFaceDescriptor()
      descriptions.push(detections.descriptor)
    }

    return new faceapi.LabeledFaceDescriptors(label, this.descriptions)
  })
)

    }
   catch(err){
     console.log(err)
     }

       }

Solution

  • Apologies for the delay, my problem was solved by changing the images. The error was because it could not identify a face (descriptor) in the images provided by me. I checked the quality of the images, which was very bad so I changed them to a good quality image and the error went away.