Predict Age and Gender using Convolutional Neural Network and OpenCV

Predict Age and Gender using Convolutional Neural Network and OpenCV

Age&gender-usecase

Introduction

The two key facial attributes, age and gender, play a foundational role in social interactions, making age and gender estimation from a single face image an important task in intelligent applications, such as access control, human-computer interaction, law enforcement, marketing intelligence and visual surveillance, etc.

Problem Statement

Used YouTube URL link for data, recognizing age, gender and face detection by pre trained models.

Architecture

Explanation

We will be doing face recognition, face detection stuff and furthermore. We will be using CNN (Convolutional Neural Networks) for age and gender predictions from a YouTube video. You don’t need to download the video, just the video URL is fine. The interesting part will be the usage of CNN for age and gender predictions on video URLs.

Steps to follow :

1. Get the video URL from YouTube.

2. Face detection with Haar cascades.

3. Gender Recognition with CNN.

4. Age Recognition with CNN.

Process:

1. Get a video URL from YouTube:

Get the Youtube video URL and try to get the attributes of the video using pafy as explained above.

2. Face detection with Haar cascades:

This is a the part most of us at least have heard of. OpenCV/JavaCV provide direct methods to import Haar-cascades and use them to detect faces. We will not be explaining this part in depth. You can refer this article to know more about face detection using OpenCV.

3. Gender Recognition with CNN:

Gender recognition using OpenCV’s fisherfaces implementation is quite popular and some of you must have tried or read about it. But, in this example, we will be using a different approach to recognize gender.

4. Age Recognition with CNN

This is almost similar to the gender detection part except that the corresponding prototxt file and the caffe model file are “deploy_agenet.prototxt” and “age_net.caffemodel”. Furthermore, the CNN’s output layer (probability layer) in this CNN consists of 8 values for 8 age classes (“0–2”, “4–6”, “8–13”, “15–20”, “25–32”, “38–43”, “48–53” and “60-”)

A caffe model has 2 associated files,

1 .prototxt — The definition of CNN goes in here. This file defines the layers in the neural network, each layer’s inputs, outputs and functionality.

2 .caffemodel — This contains the information of the trained neural network (trained model).

Steps:

Step 1: Import all the required libraries.

Step 2: Get the Youtube video URL and create an object ‘play’ which contains the best resolution of the video in webm/mp4 format.

Step 3: Often, we have to capture live stream with a camera. OpenCV provides a very simple interface to this. We can capture the video from the camera, convert it into grayscale video and display it. Just a simple task to get started.

Step 4: Using set() we’ll set the height and width of our video frame. cap.set(propId, value)

Step 5: Create 3 separate lists for storing Model_Mean_Values, Age, and Gender.

Step 6: We have defined a function to load caffemodel and prototxt of both age and gender detector. These are basically pre-trained CNN models which will do the detection.

Step 7: Now we will perform Face detection, Age detection, and Gender detection and for that create a function video_detector(age_net, gender_net) inside your main function and pass age_net and gender_net as its parameters.

Step 8: Read the cap object which is created from VideoCapture() in step 3.

Step 9: Convert the image to gray image as OpenCV face detector expects gray images.

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

Step 10: Load the pre-built model for facial detection.

face_cascade = cv2.CascadeClassifier(‘haarcascade_frontalface_alt.xml’)

Step 11: Now, how do we detect a face from an image using the CascadeClassifier

Below are arguments which should pass to detectMultiScale().

This is a general function to detect objects, in this case, it’ll detect faces since we called in the face cascade. If it finds a face, it returns a list of positions of said face in the form “Rect(x,y,w,h).”, if not, then returns “None”.

Image: The first input is the grayscale image.

Step 12: Loop through the list of faces and draw rectangles on the human faces in the video. Here basically we’re finding faces, splitting the faces, their sizes, and drawing rectangles.

Step 13 : OpenCV provides a function to facilitate image preprocessing for deep learning classification: blobFromImage(). It performs :

Step 14: Predict the gender.

#Predict Gender

gender_net.setInput(blob)

gender_preds = gender_net.forward()

Step 15: Predict the Age.

#Predict Age

age_net.setInput(blob)

age_preds = age_net.forward()

Step 16: Now we have to put text on our output frame using putText() module of openCV.

Step 17: Finally print your final output.

Outputs

Results of Dicky-Fuller test

Login

Register