Increased speed and accuracy for the Face Recognition library
The experiment I reported about a few days ago was imagined while I was preparing a tool to speed up the process and further refine the accuracy of the face_recognition library (FR for short).
Luckily, an example of FR implementation in a small Python script came to my rescue, showing the possibility of saving the mapping of a face into a small text file.
FR is preset with a 6% tolerance: that is, face recognition is triggered if the library detects a similarity of at least 94% between the maps of two faces. In practice, this can translate into a series of false positives: for example, in the case of two people A and B who are quite similar to each other, FR can respond that the face to be analyzed belongs to both person A and person B. By telling the script to recursively repeat the comparison of the vector of the face to be identified with all the vectors of face A and all those of face B until it finds the vector that, through the calculation of the cosine similarity, is closest, the accuracy level gets drastically close to 100%. And this was the second task of my tool.
FR already has a very high level of accuracy: after using it for a while, it reaches about 98% accuracy. Which means that, considering false positives and missed detections, it is wrong about two times out of a hundred. This is already an excellent result, but on a large scale it could perhaps be further improved; for example, 2% out of a million images means twenty thousand errors. Not exactly a trifle.
Furthermore, the method of use suggested by the available documentation, that is, mapping and comparing all the photos in folder B with all those in folder A, is a bit slow, since each time the computer has to repeat the entire process from the beginning; in the case of a few hundred pictures, it is already in the order of hours.
FR translates each face into a vector of 128 numbers; by saving the vectors in a table and working on this last, the library is prevented from remapping what has already been mapped each time, saving a lot of time. This is therefore the first task of the tool mentioned above.
The extraction of vectors for FR is in itself quite fast and, once extracted, they can be compared with each other instead of the actual photographs; at this point the comparison is almost instantaneous.
The comparison between vectors consists in establishing the distance between one and the other by calculating the cosine similarity (which explanation I leave to those who know it much better than me). FR already performs this operation when it compares photographs; it is therefore a matter of having it executed exactly as it is by an external script that works directly on the vectors. Since the script is independent of FR, it can also be in a language other than Python; in my case I used PHP.
So I found myself with a sort of a tuner for face_recognition in my hands, an FR Tuner, which accelerates and improves FR performances, operating directly on the sequences of numbers it generates as face mapping vectors.