Facial recognition for kids of all ages, part 3

Rahul Prabhu
Analytics Vidhya
Published in
4 min readOct 17, 2020


This is part 3 of a 3-part article series, where complex computer vision and facial recognition topics are broken down for beginners to follow.

Article 1: https://medium.com/@grokwithrahul/facial-recognition-for-kids-of-all-ages-part-1-c496040b2517
Article 2:

Now that we’ve understood the theory behind Haar cascades and the LBPH model, let’s implement it in code.

We’ll be working in Python in this article. Firstly, you’ll need to download the required packages. Open command prompt and use pip to download these following packages using the ‘install’ command:


All the programs and files can be conveniently downloaded. Scroll down to “How to use the code” and fork/download from the GitHub link. That way, you can work alongside my explanation of the code!

Facial Detection

In Part 1 of this series, we had discussed the Viola-Jones(Haar cascade) method of object classification. The reasons for choosing this particular method include its accuracy, efficiency, and speed. Right now, we will implement the haar cascade method in Python. (If you want to understand how the haar cascade method works, read through the first article in this series)

Flowchart representation of Facial detection program

If you are using an external webcam, try setting webCam = cv2.VideoCapture(1, cv2.CAP_DSHOW). Doing this will tell OpenCV to read from your second camera.

We are taking a frame from the camera and converting it into black-and-white. The Haar Cascade classifier can only operate on black-and-white images.

Then, we are detecting faces in the black and white image and saving them in the tuple ‘facesInCurrentFrame’. The data includes xCoord, yCoord, width, and height.

Finally, we draw a rectangle based on the data in facesInCurrentFrame.

Rectangle created in the image

Facial Recognition

Photo Capturing

First, we need to collect images for the LBPH module(discussed in article 2) to train on. We will collect these images with the help of the camera, and a trick to avoid cropping each photo to the face.

In each frame, we detect a face, crop the frame to that face, and save the cropped image. This process continues until 40 images are cropped and saved.

We then assign each user the next id available, based on the data in the CSV file (converted to a pandas dataframe) We then give each image the title

Finally, in the end, we add the new user into the dataframe, then export the dataframe into the CSV file. All of this gives us images for the LBPH model to train on.

LBPH Model Training

Next, we need to train the LBPH module with the images we have saved.

Over here, we are just taking the images, extracting the id from its name, and feeding the image and its ID to the LBPH model.

Recognize faces!

Now for the best part. With our trained model in hand, we can begin recognizing faces!

Most of the program is the same as the one for Facial detection. Also, we send the current image taken by the webcam to the LBPH model, which outputs an ID number. We then use the ID number to get the corresponding name from the CSV file. Finally, we display the name, along with the rectangle enclosing the face.

Great! You have finished the Facial Recognition article series! You can finally practically use the LBPH model and the Haar cascade method!

How to use the code

You can fork the GitHub repo over here (you can also download as a ZIP file): https://github.com/Redstomite/Facial-recognition-article-series

For facial recognition, make sure you first take the pictures, second train the model, and finally run the facial recognition program. To add more people, take images of the new user, then train the module. The more the users, the more the magic!

If you are facing any issues or have found any bugs in the program, you can create a new issue here: https://github.com/Redstomite/Facial-recognition-article-series/issues.

In conclusion, Facial Recognition is one of the most interesting fields in computer science. Its applications range from digital payments to ID systems to criminal identification. As a final note, facial recognition is entering a very challenging era — an era where masks are the new normal. To move forward, innovative solutions must be executed, and the boundaries must be broken.