Simple python code for face matching for beginners : Face Detection and Face Recognition

Gaurav Bansal
3 min readJan 6, 2021

Since a long time I was planning to build a simple utility which can detect the faces and mark the person’s name if it matches from any of the images present in our preloaded DB. So finally I put some time into it and created a small python program for the same, so sharing my learnings here.

I tried to cover all the steps, so that any beginner can easily understand the code.

It basically contains 3 steps as below :

  1. Create a image db to keep preloaded image set and also the information about the image data into SQLite database
  2. Trained a model from the image data and create a .yml file
  3. Finally use those models, to detect input image and mark them.

In this article, I will explain all the steps in details with their code (verified on the mac system, but should work on windows too), but before that below are basic pre-requisites, that needs to be fulfilled.

A) Below modules should be downloaded.

i) Python

ii) Open CV

B) Create a folder named as “image-detection” in Desktop folder. Inside this also create 2 more folders.

i) image-detection/dataSet

  • Will be used for storing image data set

ii) image-detection/recognizer

  • Will be used for trained .yml model

Now Let’s look into the code.

  1. Create a DB where we will create the tables. (You can update the path of the DB)

2. Create a People table to store the data about the images which will be saved into dataSet folder. It will store the Unique ID, Name, age, gender and criminal status of the person whose image is uploaded.

3. Capture the image from the webcam and save multiple pictures of the faces. (Keep web cam at the right position).

a) It will open the webcam to capture the images

b) Will ask the details of the person, who is in front of the cam, and save that data into SQLite DB

c) And will store the images into dataSet folder, which we created earlier.

4. Train the model with our image database and create a .yml file from this. It will scan all the images stored in the dataSet folder.

a) It will store the trained model as trainningData.yml into the recognizer folder which we created earlier.

5. Finally It’s time to test our trained model and marking the image over a successful detection.

a) It will use the trainningData.yml to recognise the web cam input, and it will provide the ID of the matched image.

b) From the ID, SQLite DB would be queried, which will provide the person details and that info finally be displayed beside the rectangle over the face.

  • Run the above files exactly in the given below order.
  • This would be the final result screen.
Face detection and marking

Yayyyyyy !!!! It’s simple… right.. 😃 😄 😀.

I can understand it is not looking simple as of now, but once you will follow all the steps and see the above output, things would look very easy to you.

The results might be not be great but they are quite satisfying on a beginner level, also It will give a solid idea how things work in face detection and face recognition.

Here is git repo of working code :

https://github.com/gb103/face-match-python

That’s it for now. Let me know your thoughts in the comments. Happy Coding ~~

--

--

Gaurav Bansal

AVP Technical Architect in Sony Pictures, Ex Gaana. Interested to share what I learned and what I experienced and enthusiastic to learn more new things everyday