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

Yash Chauhan
3 min readNov 8, 2021

--

Introduction

Age and gender prediction are used extensively in the field of computer vision for surveillance. Advancement in computer vision makes this prediction even more practical and open to all. Significant improvements have been made in this research area due to its usefulness in intelligent real-world applications.

Application

A human face contains features that determine the identity, age, gender, emotions, and ethnicity of people. Among these features, age and gender classification can be especially helpful in several real-world applications including security and video surveillance, electronic customer relationship management, biometrics, electronic vending machines, human-computer interaction, entertainment, cosmetology, and forensic art.

Pre-Trained Models:

  • face proto: prototype text file for face detection.
  • face model: face detection pre-trained model.
  • age proto: Age Detection prototype text file.
  • ageModel: Age Detection pre-trained model.
  • genderProto: Gender Detection prototype text file.
  • genderModel: Gender Detection pre-trained model.

Classification:

Here Gender Model will classify images into Two Categories:

  • Male
  • Female

Age Model will classify images into Eight Categories:

  • (0–2)
  • (4–6)
  • (8–12)
  • (15–20)
  • (25–32)
  • (38–43)
  • (48–53)
  • (60–100)

Workflow:

Here we try to predict a person’s Gender and Age-based on his/her facial image so that’s why first we will try to find a face from the full image. After finding the ROI ( Region Of Interest ) i.e Face we will cut that face image from the picture and give that face image to our Gender and Age Predictor Models. So everything works in two steps:

(1) Find Faces ( ROI ) from the Picture

(2) Predict Age and Gender from ROI using Gender and Age Predictor

Code:

First We will import some libraries:

import cv2 as cv
import math
import time
import matplotlib.pyplot as plt

After Getting that we create the function which is used to create a bounding box on the face and also return coordinates of the face from the image.

getFaceBoxes Method

After that, we load our all pre-trained models with help of cv2.dnn.readNet(). which accepts two arguments first one is the prototype file and the second one is the weights of the model.

Load Network

NOTE: You have to give the path of your prototype file and your model file.

After that, We create our main function which will predict gender and age detector from the image.

Gender and Age Predictor

Here we simply give input to both models ( gender and age ) using setInput() method.

After that find prediction by just doing forward propagation i.e forward().

Finally getting the result I used argmax() to find the index of maximum probability value.

At the end add some text on the images using cv2.putText() method.

Finally, we create our last function show_results(). which is used to give us the final result by just simply passing the image path.

Predictions:

Here are some results getting from this above code. It also shows the confidence of a particular prediction.

--

--

No responses yet