node.jsmatchface-detectionface-api

Face-api NodeJs same result returns , every two image matches


I use face-api in node js. When I match two images , the result always return the same value like "Similarity: person 1 (0) FaceMatch { _label: 'person 1', _distance: 0 }"

Node js FaceRecognition Class

const path = require('path');

class FaceRecognition {
  constructor() {
    this.faceMatcher = null;
  }
  async compareFaces(imgPath1, imgPath2) {
    try {
      // Load the models
      await this.loadModel();

      // Load the images
      const image1 = await canvas.loadImage(imgPath1);
      const image2 = await canvas.loadImage(imgPath2);

      // Detect faces in the images
      const face1 = await faceapi.detectSingleFace(image1).withFaceLandmarks().withFaceDescriptor();
      const face2 = await faceapi.detectSingleFace(image2).withFaceLandmarks().withFaceDescriptor();

      // Check if faces were detected in both images
      if (face1 && face2) {
        // Create an array of face descriptors
        const faceDescriptors = [face1.descriptor];

        // Create a FaceMatcher with the face descriptors
        const faceMatcher = new faceapi.FaceMatcher(faceDescriptors);

        // Compare the face descriptors of the second image
        const result = faceMatcher.findBestMatch(face2.descriptor);

        // Output the result
        console.log(`Similarity: ${result.toString()}`);
        return result;
      } else {
        throw new Error('Unable to detect faces in one or both images.');
      }
    } catch (error) {
      console.error(`Error occurred: ${error}`);
      throw error;
    }
  }
}

The result you provided indicates that the similarity comparison between the two faces resulted in a match labeled as "person 1" with a distance of 0. This suggests that the faces in the two images are considered to be highly similar or identical according to the comparison algorithm.

The label "person 1" and the distance of 0 indicate a perfect match between the two faces. The algorithm has determined that the face in the second image is the same as the face in the first image.

It's important to note that the exact label and distance values may vary depending on the specific implementation and the dataset used for training the face matching model. In your case, the result shows that the faces are considered a match with a distance of 0, indicating a very close resemblance.

If you have any further questions or if there's anything else I can assist you with, please let me know.

Chatpgt answers me like that but I don't think so.

What is the problem ? Can you help me ?


Solution

  • I fix the issue with generating two image's description, and matching the descriptions with faceapi.euclidean Distance.