使用faceapi.js进行人脸识别

时间:2020-05-13 07:51:12

标签: javascript face-recognition face-api

我已经从视频元素中捕获了图像。现在,我想从捕获的图像中识别出面部。但是,它始终无法识别面部。 当我计算图像中的面孔数量时,它可以正确显示。但是bestMatch.toString()返回“未知”。请帮忙。

async function takepicture() {
    var context = canvas.getContext("2d");
    if (width && height) {
      canvas.width = width;
      canvas.height = height;
      context.drawImage(video, 0, 0, width, height);
      const labeledFaceDescriptors = await loadLabeledImages()
      const faceMatcher = new faceapi.FaceMatcher(labeledFaceDescriptors,0.2)
      var data = canvas.toDataURL("image/jpeg");
      photo.setAttribute("src", data);

      const faceexist=await faceapi.detectAllFaces(photo)
      console.log("Number of faces= "+faceexist.length)

      const detections = await faceapi.detectAllFaces(photo).withFaceLandmarks().withFaceDescriptors();
      console.log("Detections= "+detections)

      detections.forEach((fd) => {
          const bestMatch = faceMatcher.findBestMatch(fd.descriptor)        
          console.log("Recognizing!")
          console.log( bestMatch.toString())  
      })
    } else {
      clearphoto();
    }
  }


  function loadLabeledImages() {
    const labels = ['Ruthvik']
    return Promise.all(
      labels.map(async label => {
        const descriptions = []
        for (let i = 1; i <= 4; i++) {
          const img = await faceapi.fetchImage(`https://raw.githubusercontent.com/RuthvikRaviKumar/Face-Recognition-JavaScript/master/labeled_images/${label}/${i}.jpg`)
          const detections = await faceapi.detectSingleFace(img).withFaceLandmarks().withFaceDescriptor()
          descriptions.push(detections.descriptor)
        }
        console.log("Labeled Images Loaded")
        return new faceapi.LabeledFaceDescriptors(label, descriptions)
      })
    )
  }

0 个答案:

没有答案