Facial recognition for kids of all ages, part 3

Rahul Prabhu
Oct 17 · 4 min read

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.

Image for post
Image for post

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)

Image for post
Image for post
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.

Image for post
Image for post
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.

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Sign up for Data Science Blogathon: Win Lucrative Prizes!

By Analytics Vidhya

Launching the Second Data Science Blogathon – An Unmissable Chance to Write and Win Prizesprizes worth INR 30,000+! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Rahul Prabhu

Written by

14 Year Old Coder, Writer, Teacher: Grokwithrahul.com

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Rahul Prabhu

Written by

14 Year Old Coder, Writer, Teacher: Grokwithrahul.com

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store