justadudewhohacks / face-api.js

JavaScript API for face detection and face recognition in the browser and nodejs with tensorflow.js
MIT License
16.45k stars 3.67k forks source link

Speed up the face matching process #319

Open Panepo opened 5 years ago

Panepo commented 5 years ago

Hi @justadudewhohacks

As you wrote before. The face match process is computing the every euclidean distance between the descriptor of your photo and the descriptors from your database.

The computation time will increase with the database growing. Is there any methods to speed up the face matching process ?

Thanks

Panepo commented 5 years ago

Just tried tensorflow KNN classifier for face matching process, but do not know which is better. Anyone have ideas? Thanks

A part of the training code is as follows

const classifier = knnClassifier.create()

await Promise.all(
      this.props.train.face.map(async face => {
        const image = await faceapi.fetchImage(face)
        const descriptors = await faceapi.computeFaceDescriptor(image)
        const tensor = tf.tensor(descriptors)
        classifier.addExample(tensor, 'Name')
      })
)

A part of the recognizing code is as follows

const results = await faceapi
      .detectAllFaces(
        image,
        new faceapi.TinyFaceDetectorOptions({
          inputSize: environment.tinyInputSize,
          scoreThreshold: environment.tinyThreshold
        })
      )
      .withFaceLandmarks(true)
      .withFaceDescriptors()

 if (results) {
      faceapi.matchDimensions(canvas, image)
      const resizedResults = faceapi.resizeResults(results, image)
      resizedResults.forEach(async ({ detection, descriptor }) => {
        const tensor = tf.tensor(descriptor)
        const predict = await this.classifier.predictClass(tensor, 1)
        const label = predict.label.toString()
        const options = { label }
        const drawBox = new faceapi.draw.DrawBox(detection.box, options)
        drawBox.draw(canvas)
     })
}

Full code can be found here and here.