Gender and Age Detection with OpenCV

Using image data, predict the gender and age range of an individual with Python.

Muskan Jindal
3 min readOct 28, 2021

Age and Gender have always been important features of our identity. It is also an important factor in our social life. Predictions of age and Gender made with AI can be applied to many areas such as intelligent human-machine interface development, security, cosmetics, electronic commerce.

Computer Vision is the field of study that enables computers to see and identify digital images and videos as a human would. It involves acquiring, processing, analyzing, and understanding digital images to extract high-dimensional data from the real world in order to generate symbolic or numerical information which can then be used to make decisions. The process often includes practices like object recognition, video tracking, motion estimation, and image restoration.

OpenCV is short for Open Source Computer Vision. Intuitively by the name, it is an open-source Computer Vision and Machine Learning library. This library is capable of processing real-time images and videos while also boasting analytical capabilities.

Today, we will use OpenCV to accurately identify the gender and age of a person from a single image of a face. The predicted gender may be one of ‘Male’ and ‘Female’, and the predicted age may be one of the following ranges- (0–2), (4–6), (8–12), (15–20), (25–32), (38–43), (48–53), (60–100).

Step 1: Detect face from the input image

The face detector produces the bounding box coordinates of the face in the image. Start by importing required libraries and finding the bounding box coordinates.

The getFaceBox() function is used to get the coordinates of the face. In the first three lines, we get the shallow copy of the frame and then we detect the height and width of the frame. Next, we construct a blob from it and pass the blob through the network to obtain the face detections. And finally, loop over the detections and extract the face coordinates. Use these coordinates to create a rectangle around the face.

Step 2: Load models

In the next lines, define the path of the face detection, age detection, and gender detection models.

Now, use the readNet() method to load the networks. The first parameter holds trained weights and the second carries network configuration.

Step 3: Defining the list of age buckets and gender

Initialize the mean values for the model and the lists of age ranges and genders to classify from.

Step 4: Function to predict the age and gender

After getting the face boxes, we create a 4-dimensional blob from the image. In doing this, we scale it, resize it, and pass in the mean values. We pass the blob through the gender detection model and get the confidence of the two-class (Male & Female). Whichever is higher, that is the gender of the person in the picture. Then follow the same procedure for age detection.

Step 5: Testing

Check the entire code here.

--

--