Gender prediction and age detection in images using OpenCV

Posted by radley on Mon, 14 Feb 2022 03:47:01 +0100

1, Introduction

Facial analysis in photos has attracted extensive attention because it can help us solve various problems, including better customer advertising positioning, better content recommendation system, security monitoring and other fields.

Age and gender are important aspects of facial features and their identification is a prerequisite for such activities. Many enterprises use these technologies for a variety of reasons, including easier cooperation with customers, better adaptation to their needs and providing a good experience. People's gender and age make it easier to identify and predict their needs.

Even for us humans, it is difficult to detect gender and age from images because it is completely based on appearance, which is sometimes difficult to predict. The appearance of peers may be very different from what we expect.


In monitoring computer vision, age and gender prediction are often used. Advances in computer vision have made this prediction more practical and more acceptable to the public. Because of its practicability in the application of intelligent real world, this research topic has made great progress.

A person's identity, age, gender, emotion and race are all determined by the characteristics of their faces. Age and gender classifications are two of these features that are particularly useful in a variety of practical applications, including

  1. Security and video surveillance
  2. human-computer interaction
  3. Biometric technology
  4. entertainment

There are still a lot of it.


Now let's learn how to use the OpenCV Library in Python to determine age and gender through camera or image input.

The framework used is Caffe, which is used to create models using prototype files.

Let's get started. If we haven't installed OpenCV, please make sure it's installed.

$ pip install opencv-python numpy

Step 1: import library

# Import required modules
import cv2 as cv
import math
import time
from google.colab.patches import cv2_imshow

Step 2: find the bounding box coordinates in the frame

Using the following user-defined function, we can obtain the coordinates of the bounding box, or the position of the face in the image.

def getFaceBox(net, frame, conf_threshold=0.7):
    frameOpencvDnn = frame.copy()
    frameHeight = frameOpencvDnn.shape[0]
    frameWidth = frameOpencvDnn.shape[1]
    blob = cv.dnn.blobFromImage(frameOpencvDnn, 1.0, (300, 300), [104, 117, 123], True, False)    net.setInput(blob)
    detections = net.forward()
    bboxes = []
    for i in range(detections.shape[2]):
        confidence = detections[0, 0, i, 2]
        if confidence > conf_threshold:
            x1 = int(detections[0, 0, i, 3] * frameWidth)
            y1 = int(detections[0, 0, i, 4] * frameHeight)
            x2 = int(detections[0, 0, i, 5] * frameWidth)
            y2 = int(detections[0, 0, i, 6] * frameHeight)
            bboxes.append([x1, y1, x2, y2])
            cv.rectangle(frameOpencvDnn, (x1, y1), (x2, y2), (0, 255, 0), int(round(frameHeight/150)), 8)
    return frameOpencvDnn, bboxes

Step 3: load the model and weight file

The following files must be included in the project directory:

  • gender_net.caffemodel: pre training model weight for gender detection.
  • deploy_ gender. Protoxt: the model architecture of gender detection model.
  • age_net.caffemodel: pre training model weight for age detection.
  • deploy_ age. Protoxt: the model architecture of age detection model.
  • res10_300x300_ssd_iter_140000_fp16.caffemodel: pre training model weight for face detection.
  • deploy.prototxt.txt: model architecture of face detection model.

We have a for face detection pb file, which is a protobuf file (protocol buffer), which contains the graphic definition and training weight of the model. This is what we will use to execute the trained model. Although pb file contains protobuf in binary format, but pbtxt file contains protobuf in text format. Contains TensorFlow files The prototext file provides age and gender network configuration, while The caffemodel file defines the internal state of layer parameters.

Then, for the face, age and gender detection model, the weight and structural variables are defined.

faceProto = "/content/opencv_face_detector.pbtxt"
faceModel = "/content/opencv_face_detector_uint8.pb"
ageProto = "/content/age_deploy.prototxt"
ageModel = "/content/age_net.caffemodel"
genderProto = "/content/gender_deploy.prototxt"
genderModel = "/content/gender_net.caffemodel"

Step 4: list of age and gender categories

Set the average value of the model and the list of age groups and genders to classify from.

MODEL_MEAN_VALUES = (78.4263377603, 87.7689143744, 114.895847746)
ageList = ['(0-2)', '(4-6)', '(8-12)', '(15-20)', '(25-32)', '(38-43)', '(48-53)', '(60-100)']
genderList = ['Male', 'Female']

Step 5: load network

To load the network, use the readNet() method. The first parameter is used to store the training weight, and the second parameter is used to save the network configuration.

# Load network
ageNet = cv.dnn.readNet(ageModel, ageProto)
genderNet = cv.dnn.readNet(genderModel, genderProto)
faceNet = cv.dnn.readNet(faceModel, faceProto)

Step 6: predict gender and age as a function

The following user-defined function is pipline or we can say it is the implementation of the main workflow. In this workflow, the image enters the function to obtain the location and further predict the age range and gender.

def age_gender_detector(frame):
# Read frame
t = time.time()
frameFace, bboxes = getFaceBox(faceNet, frame)
for bbox in bboxes:
# print(bbox)
face = frame[max(0,bbox[1]-padding):min(bbox[3]+padding,frame.shape[0]-1),max(0,bbox[0]-padding):min(bbox[2]+padding, frame.shape[1]-1)]blob = cv.dnn.blobFromImage(face, 1.0, (227, 227), MODEL_MEAN_VALUES, swapRB=False)
 genderPreds = genderNet.forward()
 gender = genderList[genderPreds[0].argmax()]
 # print("Gender Output : {}".format(genderPreds))
 print("Gender : {}, conf = {:.3f}".format(gender, genderPreds[0].max()))ageNet.setInput(blob)
agePreds = ageNet.forward()
age = ageList[agePreds[0].argmax()]
print("Age Output : {}".format(agePreds))
print("Age : {}, conf = {:.3f}".format(age, agePreds[0].max()))label = "{},{}".format(gender, age)
cv.putText(frameFace, label, (bbox[0], bbox[1]-10), cv.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 255), 2, cv.LINE_AA)
    return frameFace

Step 7: Forecast

from google.colab import files
uploaded = files.upload()
input = cv.imread("2.jpg")
output = age_gender_detector(input)

Here, we can see that the confidence of gender prediction is 1 (100%), while the confidence of age prediction is lower because it is difficult to be accurate.

In this article, we learned how to create an age predictor that can also detect your face and highlight it with a border.