ageitgey / face_recognition

The world's simplest facial recognition api for Python and the command line
MIT License
53.42k stars 13.49k forks source link

Multiple results for one person (face) #723

Closed 316karan closed 5 years ago

316karan commented 5 years ago

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 knnclf.classes , I do get a list of the class_labels but the index (aforementioned) is not the index of knnclf.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.

316karan commented 5 years ago

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]]

/\

ferasawadi commented 3 years ago

Hi there and thank for open this ticket , can you please share the full code of getting the names plus the distances for the people ? am stuck with this since a while warm regards

ferasawadi commented 3 years ago

so here is my Code after alot of work but it gives me the same results for every one tryingto use the face recognition module

# Find encodings for faces in the in_stream image
    face_encodings = face_recognition.face_encodings(X_img, known_face_locations=X_face_locations, num_jitters=5)
    distance, index = knn_clf.kneighbors(face_encodings, n_neighbors=4)
    training_labels_indices = knn_clf._y
    class_labels = knn_clf.classes_
    user_id = class_labels[training_labels_indices[index]]
    for us, i in zip(user_id[0], range(len(index[0]))):
        # print(user)
        # user = class_labels[training_labels_indices[i]]
        percentage = "{:.0%}".format(face_distance_to_conf(face_distance=distance[0][i],
                                                           face_match_threshold=distance_threshold))
        # print(user)
        # print(percentage)
        if not any(u.user_id == us for u in results):
            results.append(DetectionResultsModule(us, percentage))

i would appreicate any help with this