vladmandic / face-api

FaceAPI: AI-powered Face Detection & Rotation Tracking, Face Description & Recognition, Age & Gender & Emotion Prediction for Browser and NodeJS using TensorFlow/JS
https://vladmandic.github.io/face-api/demo/webcam.html
MIT License
830 stars 149 forks source link

findBestMatch - is not working correctly #69

Closed danies8 closed 2 years ago

danies8 commented 2 years ago

Hi , When I inserted this face into db : cGc and compared with this, he found the first one! why ? he should return unknown. cGc40 distance Threshold 0.5 and the actual threshold "bestMatchPerson": { "_label": "2", "_distance": 0.44472999761672266 } What do I miss ? the image inputs and compare are not good ? Thanks in advance.

vladmandic commented 2 years ago

there seems to be a typo in the method

fixed via https://github.com/vladmandic/face-api/commit/1b4580dd6ee175681655d9f4a9011310afc699a9#diff-e20cfb0253557048cbdfaa2de9a86d00cafa78932dd7ba9fb3ae6289b109473e

new version is published

danies8 commented 2 years ago

I used this code:

const distanceThreshold=0.5;
let matcher = new faceapi.FaceMatcher(labeledFaceDescriptors, distanceThreshold);
bestMatchPerson = matcher.findBestMatch(faces[0].descriptor);

I do npm install and check again and the problem still exist. image

danies8 commented 2 years ago

I check with another person that is not in DB, it happened with a lot of people that are not in DB. If I checked with people that are in DB is good matching. image And I got this people that in db 103 "bestMatchPerson": { "_label": "103", "_distance": 0.33258788746528983 }

vladmandic commented 2 years ago

i cannot reproduce the issue. post a link to a working gist (complete).

vladmandic commented 2 years ago

so what is the problem??
detected distance is 0.33 and distanceThreshold is 0.5
clearly distance is less than that so it returns a match!?!?!

and when someone asks for reproducible code, make an effort to write a reproduction that works
copy & paste from your app is not helpful

btw, this is the code i just wrote to test your two images:
https://github.com/vladmandic/face-api/blob/master/demo/node-match.js

and you have NOT addressed issues or suggestions i made earlier:

danies8 commented 2 years ago
  1. so what is the problem?? detected distance is 0.33 and distanceThreshold is 0.5 clearly distance is less than that so it returns a match!?!?!

But is not the right person, how can i give him to enter ??? he return label of other person ?**

2.youre still loading models again and again I used these modals in loading process when app is loading, i didn't provided to you this file

const tf = require('@tensorflow/tfjs-node');
const faceapi = require('@vladmandic/face-api');
const path = require('path');

async function loadModals() {
      const faceDetectionNet = new faceapi.FaceDetectionNet();
      const modelPath = '../../node_modules/@vladmandic/face-api/model';
      await faceDetectionNet.loadFromDisk(path.join(__dirname, modelPath));
      await faceapi.nets.ageGenderNet.loadFromDisk(path.join(__dirname, modelPath));
      await faceapi.nets.faceLandmark68Net.loadFromDisk(path.join(__dirname, modelPath));
      await faceapi.nets.faceRecognitionNet.loadFromDisk(path.join(__dirname, modelPath));
      await faceapi.nets.faceExpressionNet.loadFromDisk(path.join(__dirname, modelPath));
}
module.exports = { loadModals }
  1. youre still using canvas library instead of native decoding -fixed as follow // 2. Create tensor
    let arr = imageStrEncodedBase64.split(',');
      let bstr = atob(arr[1]); 
      let n = bstr.length; 
      let u8arr = new Uint8Array(n);
      while(n--){
          u8arr[n] = bstr.charCodeAt(n);
      }
      const tensor = tf.node.decodeImage(u8arr, 3);

    4.Check image is image is exactly 1 - fixed as follow

    if (!faces) {
        let detectAllFacesMessage = "compare all:faces is null or undefined";
        utils.logger.log({
          level: 'error',
          message: detectAllFacesMessage,
          label: 'faceRecognition.ts'
        });
        return { success: false, error: detectAllFacesMessage }
      }
vladmandic commented 2 years ago

If distance is above threshold, you don't get a person, you get 'unknown'.

This is working as expected. Issue is closed.

danies8 commented 2 years ago

So, how I can know if it right person or not ? I will ask differently, how can i know if to let him enter or not?

vladmandic commented 2 years ago

i think you got entire concept wrong - distance means how different two faces are. distance of 0 means identical. so if distanced is below a threshold, you get a match.

danies8 commented 2 years ago

I need to learn my pictures and define what is the distance that is right to my organization. I see good match is up to 0.35 and above is not good match. So 0.35 is the distance that above it will not enter, and below it it will enter. While threshold is 0.5. I understood you right ?