Face_recognition: Multiple results for one person (face)

Created on 25 Jan 2019  路  1Comment  路  Source: ageitgey/face_recognition

  • face_recognition version: 1.2.2
  • Python version: 2.7
  • Operating System: Mac

First of all thanks for this amazing library.

What I need: I want to get multiple predictions for one face with respective distances and then allow the user/admin to take a call on which is the correct image.

What I did: By changing the n_neigbors to 3 I'm able to get the closest 3 matches for the face. The output (closest_distances) is distance and an index. But I'm unsure how to use that index to find out who is the person that index is referring to in the trained classifier.

closest_distances = knn_clf.kneighbors(faces_encodings, n_neighbors=3)

When I use knn_clf.classes_ , I do get a list of the class_labels but the index (aforementioned) is not the index of knn_clf.classes_

class_labels = knn_clf.classes_
user_id = class_labels[closest_distances[1][i][j]]

The user_id that I get in the above example is incorrect. I'm not sure how to fetch it from the classifier.

Is there something I'm missing? Is it possible to achieve this using this library?

TIA.

Most helpful comment

Figured it out.

knn_clf._y holds a mapping of which index (during training) is stored in knn_clf.classes_

So now the code becomes:

distance, index = knn_clf.kneighbors(face_encodings,n_neigbors=3)
training_labels_indices = knn_clf._y
class_labels = knn_clf.classes_
user_id = class_labels[training_labels_indices[index]]

_/_

>All comments

Figured it out.

knn_clf._y holds a mapping of which index (during training) is stored in knn_clf.classes_

So now the code becomes:

distance, index = knn_clf.kneighbors(face_encodings,n_neigbors=3)
training_labels_indices = knn_clf._y
class_labels = knn_clf.classes_
user_id = class_labels[training_labels_indices[index]]

_/_

Was this page helpful?
0 / 5 - 0 ratings