How to Build Gender Wise Face Recognition and Counting Application With OpenCV

Gender Wise Face Recognition with OpenCV

In this article, we are going to learn how to build a computer vision and deep learning combination application. Which performs gender wise face recognition with opencv and counts the people in the image or in the video.

In other words with the help of deep learning and computer vision algorithms using python opencv as a modeling package, we will classify the gender  and count the faces for a given image/video.

Building these kinds of models makes life easier with the enhancements over the deep learning field.

Nowadays deep learning field is one of the most revolutionary technologies with rapid enhancement growth. It gives machines the ability to think and learn on their own. The key motivation for deep learning is to build algorithms that mimic the human brain.

You might have also heard about Computer Vision. Often abbreviated as OpenCV. Which is defined as a sub field of study that seeks to develop techniques to help computers “see” and understand the content of digital images such as photographs and videos.

Learn how to build a gender wise face recognition application with opencv #deeplearing #opencv #computervision #python 

Click to Tweet

With the high end research in deep learning, a lot of new applications of computer vision techniques have been introduced. Which have now become integral  parts of our everyday lives. 

These include

  • Face recognition and indexing
  • Photo stylization
  • Machine vision in self-driving cars

We are not going to build the high end computer vision and deep learning application in this article.

But we are going to leverage both computer vision and deep learning to build face detection applications with techniques of face recognition with opencv python. Which not only counts the faces but also separates the male and female faces.

Excited right ?

We are excited too 🙂 

Before we drive further. Let’s see what you are going to learn by the end of this article.

Let’s start the discussions with understanding various applications of face recognition.

Face Recognition or Detection Applications

Face Recognition Applications

Face recognition techniques are the most promising techniques in computer vision. Using which we can develop and solve many real world  problems. 

Anything which requires human supervision can leverage the help of computer vision algorithms to build applications to reduce the manual effort.

One such an application is counting the people. This inspires us to develop a model which is able to detect gender and also count faces separately.

Before going to build this. Let’s check a few more face recognition applications.

Using Face Detection In Daily Life

Daily Face Recognition Application

1. To unlock mobile, without passcode

Using face detection, we can unlock the phone without entering the password. The phone camera will detect your face and check if your face is matching with the  passcode face or not. If they find your face the phone lock will automatically open. 

2. Google photos grouping the same person photos

Using face detection and recognition, the google  photos can be grouped based on the different types of faces. A new album will be created according to the new face.

3. Facial recognition in surveillance

Facial recognition can use in forensic investigations by automatically capturing and recognizing individuals in security footage or other videos. This helps in pinpointing the persons behaviour in the footage.

4.Track attendance in any sector

Using face recognition has the potential to track students’ or employee’s attendance. In general, attendance sheets can allow students to sign another student, who is ditching class. 

So  face recognition applications ensures students aren’t skipping class. Face authentication is needed inorder to make attendance in class or office.

Phones or some electronic devices  are being used to scan  faces and match their photos against a database for validation.

5.Control access to sensitive areas

Face recognition is able to work as a main of access control. To make sure  that only authorized individuals get into some places like, boardrooms, bank vaults,labs, and other sensitive locations.

Face Detection High Level Application

We have seen some low level applications. Now let’s check some high level applications.

Face Detection High Level Application

1. Amazon Just Walk Out  Technology

The amazon just walk out technology  helps in identifying the person and detecting the amount from his amazon wallet for the items he/she collected from the shops.

2. Students Engagement

In western countries schools. Face recognition is extensively use to Identify the student faces and on top of that creates data points. Which helps in understanding  the student's activeness based on the type of class or lecture. Now the same will help in online courses too.

3.  Faster Payments

Face detection technology enables a more secure and faster  way to make payments. By selecting  into a pay-by-face technique, individuals can tie their face to normal payment methods. Which helps in fast  purchases using their faces as a payment method.

 Which reduces the offline and online fraud transaction. If we are still using the traditional way of payments, We need model to indentify the fraud activities identification models.

4.Finding Missing Persons

Face recognition can be used to find missing childrens or people. If missing individuals are added to a database. The system can become alerted as soon as they are recognized by face recognition. 

It can be attached in  airports, retail stores or other public space. Do you believe or not , in India 3000 missing children were discovered in just 4 days using face recognition.

We learned the potential application around face recognition models. Now let’s learn how we can build such face detection application with python opencv library.

Face Recognition Modeling Approach 

Building the face recognition counter is not a straightforward way. It's not easy like building randomforest model or building decision tree model. 

It required two main tasks.

One is to build a deep learning model which is able to detect gender of a person and also return the count of males and females. Next count of faces in an image or video.

As you already guessed the implementation. It will happen in two levels. Let's understand about this two stage modeling a bit deeper. 

Two Stage Modeling

We are going to build the face detection counting in two stages. 

  • Stage-1 - Model Training stage
  • Stage-2 - cascading and evaluation stage.

Model Training Stage

In the first stage we are going to train the CNN (Convolutional Neural Networks)  model with both male and female images. In the next step we will save the trained model for the next stage.

Cascading and evaluation stage 

In the second stage we will  detect the faces using cascading files. If you don't know about the cascading files don’t worry. You are going to understand about it in the next section.

We load the cascading files and pass faces to the trained model. The well trained model returns whether the face is male or female.

As a final step we are going to use len() to find out the number of faces in an image or video.

Face Recognition Complete Workflow

This two stage approach makes more sense if we understand the models workflow architecture.

Before that Let’s understand what the cascade classifier means. 

What is Cascade Classifier

Cascade classifiers is an effective object detection method proposed by Paul Viola and Michael Jones in their paper, "Rapid Object Detection using a Boosted Cascade of Simple Features" in 2001. 

Cascading is a special case of ensemble learning. It is a combination of  several classifiers. Using all information taken from the output to a given classifier as extra information for the next classifier in the cascade.

Unlike voting or stacking ensembles, cascading is a multistage one.

Cascading classifiers are trained with several hundred "positive" images of a particular object and arbitrary "negative" images of the equal size. 

After the classifier is trained it can be applied to a region of an image and detect the object based on requirement.

Models Workflow Architecture

Cascade and CNN Model Architecture

Cascade and CNN Model Architecture

  1. Input image is passed to Cascade Classifier 
  2. ROI (region of interest) is  extracted from the given image
  3. ROI- these are samples within a data set identified for a particular purpose.
  4. Resize the image into 96 * 96
  5. Pass a resized image to trained CNN model , trained model is going to predict whether the face is a man or woman .
  6. With the help of the len() function , we can find out the number of faces in an image/frame.

Face Recognition with Opencv Python

Enough of theory. Now comes the fun part.

Let us implement the face recognition with opencv. Which is the popular python computer vision library.

Let’s start with the dataset we are going to use for building the model.

Dataset Description

The dataset consists of man and woman folders. Man folder is filled with 1173 images of the man and woman folder is filled with 1134 woman images respectively. we can find many similar datasets in different sources like kaggle, data.world etc.

You can also create your own dataset by using techniques like web scraping. Collect the set of man and woman images from the internet and save it into two different folders with folders names as man and woman.

 You can download this dataset from this link.

Files & Libraries Required

  • Haarcascade_frontalface_default.xml 
    • Haar Cascade is a machine learning-based approach where a lot of positive and negative images are used to train the classifier.
    • Positive images – These images contain the images which we want our classifier to identify.
    • Negative Images – Images of everything else, which do not contain the object we want to detect.
    • This file is used to find out  the faces in a given image.
  • Libraries
    • Tensorflow , keras, h5py ,numpy,and opencv.

Building Convolution Neural Network

By now we have the required files to build the model and we are also having the required libraries information. 

Now we are going to create a model using Convolutional Neural Networks. As every model building starts with importing libraries.

Let’s import all the necessary python libraries.

Data preprocessing

We are going to design how our input images are to be modified . We need to resize the images according to our model specifications.

In deep learning , it expects data to be in number format so that we convert our input image into an array with the help of keras library.

And next we need to split the data into training and validation data based on the given splitting ratio.

Defining the initial parameters for further use ,

  • Learning rate: learning rate is a tuning parameter in an optimization algorithm that determines the step size at each iteration.
  • Batch size: number of training examples passed  in one iteration.
  • Epochs: an epoch refers to one cycle through the full training dataset. 
  • Image dimensions: we need to specify the image dimensions, here i’m taking (96,96).
  • Data and labels are two lists used to store images and its corresponding labels.
  • Image_files stores all the images given by the path.

Here, we are 

  • Loading each image and resizing according to our specified image dimensions.
  • Converting resized image into array
  • Appending that image  into data list
  • Extracting the label name from path using slicing eg:C:\Desktop\gender_dataset\woman\face_113.jpg →  [-2] returns woman
  • Labelling man =0 and woman =1 and storing it into labels list.
  • Repeating above steps for every image present in image_files.

Here, we are normalizing the data and converting labels into an array.

Then we are splitting the data into training and testing in the ratio of 80% training data and 20% testing data .

Data Augmentation

Data augmentation is the process of increasing the amount and diversity of data. It is used to increase the amount of data.

By adding slightly modified copies of already existing data or newly created synthetic data from existing data.

It helps reduce overfitting when training a model.

Here, 

we are performing various operations  like 

  • rotation_range : degree range for random rotations.
  • width_shift_range : fraction of total width
  • height_shift_range : fraction of total height
  • shear_range : shear angle in counter-clockwise direction in degrees.
  • zoom_range : range for random zoom
  • Horizontal_flip : randomly flip inputs horizontally.
  • fill_mode : Points outside the boundaries of the input are filled according to the given mode on images to increase the data size .

Defining the  Convolutional Model

Here , we are 

  • Defining input shape  and its dimensions.
  • Creating a Sequential model with 5 hidden conv2D layers with the sequence of 32,64,64,128 and 256 hidden neurons respectively. Along with relu activation function, batchnormalization,maxpooling2D and Dropout.
  • Flattening the data- (Flattening is converting the data into a 1-dimensional array for inputting it to the next layer).
  • Dense layer with having 1024 neurons, relu activation function, batch normalization and 50% dropout ratio.
  • Final output layer with 2 neurons and sigmoid activation function.

Above  all key terms are related to Convolutional Neural Networks. We used various activation functions please go through the activation functions link inorder to understand those keywords. 

Here my main focus is creating a convolutional neural network.

Now compile the defined model having Adam optimizer. We are  defining

  • learning rate
  • binary_crossentropy loss
  •  accuracy metric

Then , we are fitting the model with 100 epochs  on  train and validation data.

Last, we are saving the model with the name of ‘gender_predictor’  by using the save() function.

Gender Wise Face Counting via ComputerVision

Loading the gender prediction model

In this we are going to

  •  import necessary libraries like cv2, tensorflow and numpy
  • Load model and frontal_face haar cascading file
  • Defining label names in a list form.

Gender Face Detection Counting Function

Here, we are 

  • Firstly we took the passed input image.
  • Then convert it into grayscale and save into a new variable named ‘gray_image’.
  • detectMultiScale method is used to detect objects of different sizes in the input image.
  • The detected objects are returned as a list of rectangles  and saved into the faces variable.
  • len(faces) results number of faces in an image.
  • Now, iterate each and every face which is  detected in above and draw a rectangle on top of it.  cv2.rectangle() method is used to draw a rectangle on any image
  • Crop the input image. Resize it, normalize, convert it into an array and expand the shape of an array.
  • Then pass it to the trained model and save the predicted outcome in the result variable.
  • Use argmax for finding the class with the largest predicted probability from the result.
  • If label = women, increment f variable , else increment m variable.
  • cv2.putText() method is used to draw a text string on any image.
  • With the help of this function display the resulting counts on an image.

Face Recognition Model Testing 

We build the complete pipeline for detecting the faces and identifying whether the face is male or female.

Now let’s test our model.  For that we are going to show how we can evaluate both on images and videos (real time webcam).

We are not using any classification evaluation metrics or regression evaluation metrics here.

All we do is load a image and check Whether the faces are detected properly or not.

Model Evaluation On Images

Here , we are

  • Reading input image with the help of imread() function
  • Maintaining separate counter variables for both males and females
  • Passing an input image along with counter variables.
  • Finally, closing all the active windows or tabs.

Face Recognition Demo

From the above picture. We can observe, model accurately classified gender and return the exact count of both males and females and total faces in an image.

Model evaluation with webcam

Here , we are

  • Opening a webcam with VideoCapture(). 0 means local machines webcam, if you have an external camera change number to 1 or 2 . 
  • Read frame by frame from live streaming video.
  •  Maintaining separate counter variables for both males and females as x and y
  • Passing frame by frame  to the gender_facecounter() function along with  x and y variables.
  • Exit the current tab when user presses enter key 
  • Finally, closing all the active windows or tabs.

What next

You can check code in our dataaspirant GitHub repo. Please feel free to check it out. You can ignore the code_for_blog.py file and consider all the other files. 

We have plenty of applications with deep learning. In recent times there is huge research going on deep learning and computer vision.

We can solve any problem with the help of deep learning. We have showed an  simple use case in deep learning and computer vision.

You  can try more creative projects  like

  • Vehicles detection
  • Hand Gesture recognition
  • DNA pattern recognition etc on your own.

Conclusion

In this article we learn how to build face recognition with opencv python package. We also learnt how we can leverage it to build face detection and counter application end to end. We also learn how the cascading works. 

As we said before don’t limit to this. Use this code and do the modification to solve various object detection applications.

Let’s say for a grocery kind of domain. A good application could detect the items in the shop and count the items to send a report to the store staff so they can plan to order required items ahead. This reduces a huge manual effort.

Recommended Computer Vision Courses

Recommended
Master-Computer-Vision™-OpenCV4-in-Python-with-Deep-Learning-Course

Master Computer Vision With OpenCV

Rating: 4.5/5

Computer vision quick start

Face Recognition OpenCV Quick Start 

Rating: 4/5

Deep-learning-for-computer-vision2.png

Learn Deep Learning and Computer Vision

Rating: 4.6/5

Follow us:

FACEBOOKQUORA |TWITTERGOOGLE+ | LINKEDINREDDIT FLIPBOARD | MEDIUM | GITHUB

I hope you like this post. If you have any questions ? or want me to write an article on a specific topic? then feel free to comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *

>