Face_recognition: Return confidence level on KNN example

Created on 18 Nov 2018  路  6Comments  路  Source: ageitgey/face_recognition

  • face_recognition version: 1.2.3
  • Python version: python:3.4-slim
  • Operating System: Docker Linux on a Windows Machine (Development) and RancherOS K8s (Production)

Description

Basically I want to achieve what you suggested on #541 on the following knn example
https://github.com/ageitgey/face_recognition/blob/master/examples/face_recognition_knn.py

What I Did

I used docker to get the example working fine

Have tried to print the result from this code, which are

closest_distances, are_matches but don't think those are relevant to what I need

    # Find encodings for faces in the test iamge
    faces_encodings = face_recognition.face_encodings(X_img, known_face_locations=X_face_locations)

    # Use the KNN model to find the best matches for the test face
    closest_distances = knn_clf.kneighbors(faces_encodings, n_neighbors=1)
    are_matches = [closest_distances[0][i][0] <= distance_threshold for i in range(len(X_face_locations))]

    # Predict classes and remove classifications that aren't within the threshold
    return [(pred, loc) if rec else ("unknown", loc) for pred, loc, rec in zip(knn_clf.predict(faces_encodings), X_face_locations, are_matches)]

Most helpful comment

closest_distances is the distance between the unknown face and the known face. The lower the value, the stronger the match. The distance_threshold is how small that distance has to be to be considered a match. The default distance threshold is 0.6. So a closest_distance greater than 0.6 is not considered a match and a value less than 0.6 is considered a match - with smaller numbers being stronger matches.

That's how this model works. It doesn't use percentage match scores. But if you want to convert that distance value into an estimate of a percentage match so the number is more clear to you, you can use this function: https://github.com/ageitgey/face_recognition/wiki/Calculating-Accuracy-as-a-Percentage

All 6 comments

Did you find any solution for this??

@ageitgey could you please on this?

closest_distances is the distance between the unknown face and the known face. The lower the value, the stronger the match. The distance_threshold is how small that distance has to be to be considered a match. The default distance threshold is 0.6. So a closest_distance greater than 0.6 is not considered a match and a value less than 0.6 is considered a match - with smaller numbers being stronger matches.

That's how this model works. It doesn't use percentage match scores. But if you want to convert that distance value into an estimate of a percentage match so the number is more clear to you, you can use this function: https://github.com/ageitgey/face_recognition/wiki/Calculating-Accuracy-as-a-Percentage

I tried using that function.
this is my syntax :
percentage = face_distance_to_conf(closest_distances)

but i got the below error for the same:
TypeError: '>' not supported between instances of 'tuple' and 'float'

I got it working. However when I am trying the same for more than one person, it gives an error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

@ageitgey How can I use the same function for multiple faces in the frame?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

paulaceccon picture paulaceccon  路  5Comments

Allenstin picture Allenstin  路  5Comments

ramineniraviteja picture ramineniraviteja  路  5Comments

InnovativeInventor picture InnovativeInventor  路  6Comments

carlhung picture carlhung  路  6Comments