# Is it possible to detect a person using facial recognition on the fly?

I am working on a small app for face detection in Python using face_recognition and opencv libraries.

I have trained the data on a number of people and used face recognition to login into the app.

If it detects the person from the picture, it allows him to log in, otherwise they are sent to the registration screen where the person has to upload his photos and other information.

So if a new registration comes in, I have to run the model again with additional data.

As the number of entries increases the model will take more time, affecting the time to identify a newly registered person.

How can I prepare for this slow-down, down the road?

Can I reduce the time taken to train the model again or train the model with incremental data?

If you need to re-train the model to classify new faces, this will not scale well to registering new people routinely. You may also suffer from glitches in accuracy during new registrations unless the training routines are carefully monitored.

Instead, recognition systems that need to register new items typically don't re-train. They are trained on the general task of separating objects - identifying whether they are different or what is different about them.

One common way to achieve this separation is to use the NN to map images of faces to a descriptive vector, and match each new image according to distance to stored vectors of profile pictures. The distance, even to the closest stored vector, should be small in order to consider it a successful match. Registering a new user is then a matter of saving a new vector embedding calculated from the neural network. Matches can be done with database lookups - they are still limited by how fast you can do the distance calculations looking for a match, but you only need run the NN forward once. The distance calculations can be fast and batched - it should be easy to use e.g. tensorflow or torch to calculate 1000 potential matches in a fraction of a second.

To ensure that faces are well categorised according to their differences, ignoring lighting, pose, hairstyle etc, the network needs to be trained with this objective in mind. One way to do this is to train with triplet loss using two pairs of images, one that should match, another that should not.

Andrew Ng's course on CNNs covers this approach very well, starting from this lecture.

I faced a similar problem. What I did was as follows.

1. Save the features extracted for each face in a file.

2. Load those features when running the model again.

Though it took some time to complete the successful working project, the result was satisfying.

I faced exactly same challenge and here is my solution, which worked for me.

Registration:

1. Extract the encodings and save it in PostgreSQL database

Verification

1. Extract the encodings of unknown face
2. Compute the distance with existing encodings stored in the PostgreSQL database
3. As per your threshold, you can take action - allow or reject

You can implement it in any database, the reason, I selected PostgreSQL was it in-built CUBE datatype support. With this approach, you don't have to retrain the entire images set for adding new registration, also you can implement 1-N comparison with good performance.

Check here for implementation details and check here for more details on improving overall system outcome.