Build an Accurate Age and Gender Detection Model Using Python

By Rohit Sharma

Updated on Jul 30, 2025 | 5 min read | 1.48K+ views

Share:

Is it possible to determine someone's age and gender based solely on facial features? Yes. This project uses computer vision and pre-trained deep learning models to illustrate precisely that. In this project, we create an age and gender detection model that uses a facial image as input and makes the following predictions:

  • The individual's gender (male or female)
  • The age range, for example, 0–2, 4–6, 8–12, etc.

We will employ OpenCV's DNN module with pre-trained Caffe models to make predictions using a small sample set of facial images. There is no need for manual training. Real-world uses for this technique include interactive systems, surveillance, and targeted advertising.

See our Top 25+ Essential Data Science Projects GitHub to Explore in 2025 blog for more project ideas like this one.  

What Should You Know Beforehand?

It is better to have at least some background in:

Technologies and Libraries Used

For this project, the following tools and libraries will be used:

Tool/Library

Purpose

Python

Programming language for coding and scripting

OpenCV

For image processing, loading models, face detection, and making predictions

NumPy

Numerical operations & array manipulation

Google Colab

Cloud-based coding environment 

Pretrained Models Used for Prediction

Below are the pretrained models that we will be utilizing:

Model Name

File Used (.caffemodel)

Description

Age Prediction

age_net.caffemodel

CNN model trained to classify faces into 8 age ranges (Ex- 0–2, 4–6, etc.)

Gender Prediction

gender_net.caffemodel

CNN model trained to classify gender as Male or Female

Time Taken and Difficulty Level

On average, it will take about 1 to 2 hours to complete. Duration may increase/decrease depending on your familiarity with - Python, image processing, & ML concepts. It’s best for beginner to intermediate level.

How to Build an Age and Gender Detection Model

Let’s start building the project from scratch. We will start by:

  • Clone a sample facial image dataset from a GitHub repository
  • Load and explore these images for testing purposes
  • Use OpenCV to detect faces and prepare images for model input (resizing, normalization)
  • Load pre-trained Caffe models for gender and age prediction using OpenCV's DNN module
  • Run predictions on new images and display the results with bounding boxes and labels

Without any further delay, let’s start!

Step 1: Clone the Dataset from GitHub

Let’s clone the dataset from the public GitHub repository. This dataset contains thousands of labeled facial images organized into gender and age categories.

Here is the code to do so:

# Clone the dataset repository from GitHub
!git clone https://github.com/smahesh29/Gender-and-Age-Detection.git

# Navigate into the directory
%cd Gender-and-Age-Detection

Output:

Cloning into 'Gender-and-Age-Detection'...

remote: Enumerating objects: 72, done.

remote: Counting objects: 100% (25/25), done.

remote: Compressing objects: 100% (11/11), done.

remote: Total 72 (delta 20), reused 14 (delta 14), pack-reused 47 (from 1)

Receiving objects: 100% (72/72), 90.73 MiB | 17.76 MiB/s, done.

Resolving deltas: 100% (25/25), done.

/content/Gender-and-Age-Detection

The output tells us that - The GitHub repo has been successfully cloned and is now ready to be accessed in the Colab environment.

Step 2: Load the Pre-trained Models

In this step, we will load the Caffe-based deep learning models for Age and Gender Detection. The models are already trained and stored as .caffemodel and .prototxt files.

Use the below-mentioned code:

import cv2

# Define paths for model files
gender_model = 'gender_net.caffemodel'
gender_proto = 'gender_deploy.prototxt'
age_model = 'age_net.caffemodel'
age_proto = 'age_deploy.prototxt'

# Load the models
gender_net = cv2.dnn.readNet(gender_model, gender_proto)
age_net = cv2.dnn.readNet(age_model, age_proto)

Step 3: Define Label Lists and Image Preprocessing

Now, we will define the age and gender categories. Besides this, we will also set up a utility function to preprocess input images for the models.

Use the code given below to do so:

# Mean values used during model training
MODEL_MEAN_VALUES = (78.4263377603, 87.7689143744, 114.895847746)

# Define class labels
gender_list = ['Male', 'Female']
age_list = ['(0-2)', '(4-6)', '(8-12)', '(15-20)', 
            '(25-32)', '(38-43)', '(48-53)', '(60-100)']

# Image preprocessing function
def preprocess_image(image_path):
    img = cv2.imread(image_path)
    face_img = cv2.resize(img, (227, 227))  # Resize to model input size
    blob = cv2.dnn.blobFromImage(face_img, 1.0, (227, 227), MODEL_MEAN_VALUES, swapRB=False)
    return blob, img

Step 4: Load Models and Predict Age & Gender

In this step, we will - 

  • Load the pre-trained models.
  • Predict age and gender.
  • Display the result on the image.

Use the below-mentioned code to accomplish the same:

import cv2
import matplotlib.pyplot as plt

# Load the models
age_net = cv2.dnn.readNetFromCaffe(
    "/content/Gender-and-Age-Detection/age_deploy.prototxt",
    "/content/Gender-and-Age-Detection/age_net.caffemodel"
)
gender_net = cv2.dnn.readNetFromCaffe(
    "/content/Gender-and-Age-Detection/gender_deploy.prototxt",
    "/content/Gender-and-Age-Detection/gender_net.caffemodel"
)

# Define age and gender lists
AGE_LIST = ['(0-2)', '(4-6)', '(8-12)', '(15-20)',
            '(25-32)', '(38-43)', '(48-53)', '(60-100)']
GENDER_LIST = ['Male', 'Female']
MODEL_MEAN_VALUES = (78.4263377603, 87.7689143744, 114.895847746)

# Preprocess function
def preprocess_image(image_path):
    img = cv2.imread(image_path)
    if img is None:
        raise ValueError(f"Image not found: {image_path}")
    face_img = cv2.resize(img, (227, 227))
    blob = cv2.dnn.blobFromImage(face_img, 1.0, (227, 227), MODEL_MEAN_VALUES, swapRB=False)
    return blob, img

# Run prediction
image_path = "/content/Gender-and-Age-Detection/man1.jpg"  # Change to another image if needed
blob, original_img = preprocess_image(image_path)

# Predict gender
gender_net.setInput(blob)
gender_pred = gender_net.forward()
gender = GENDER_LIST[gender_pred[0].argmax()]

# Predict age
age_net.setInput(blob)
age_pred = age_net.forward()
age = AGE_LIST[age_pred[0].argmax()]

# Annotate image
label = f"{gender}, {age}"
cv2.putText(original_img, label, (10, 30),
            cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
cv2.rectangle(original_img, (5, 5), (220, 40), (0, 255, 0), 1)

# Convert BGR to RGB for displaying
rgb_img = cv2.cvtColor(original_img, cv2.COLOR_BGR2RGB)

# Show image
plt.figure(figsize=(6, 6))
plt.imshow(rgb_img)
plt.axis('off')
plt.title("Prediction Result")
plt.show()

Output:

Conclusion

We predicted age and gender from photos using OpenCV and pre-trained Caffe models. After processing a face, the model produced the following result: Male, 60–100 years old. The system predicts one set of labels per image, despite being trained to classify faces into two gender categories and eight age groups. For ease of interpretation, the results were superimposed on the picture. This demonstrates that the model has a respectable level of accuracy in identifying and categorizing facial features.

background

Liverpool John Moores University

MS in Data Science

Dual Credentials

Master's Degree17 Months

Placement Assistance

Certification6 Months

Unlock the power of data with our popular Data Science courses, designed to make you proficient in analytics, machine learning, and big data!

Elevate your career by learning essential Data Science skills such as statistical modeling, big data processing, predictive analytics, and SQL!

Stay informed and inspired with our popular Data Science articles, offering expert insights, trends, and practical tips for aspiring data professionals!

Reference:
https://colab.research.google.com/drive/15M_eSIfZ1QxhUSzROhMTJYtlHRufaG6U?usp=sharing

Frequently Asked Questions (FAQs)

1. What is Age and Gender Detection?

2. Which algorithms are used for Age and Gender Detection?

3. What are the real-world applications of Age and Gender Detection?

4. What tools or libraries can be used for this project?

5. What are some limitations of Age and Gender Detection systems?

Rohit Sharma

802 articles published

Rohit Sharma is the Head of Revenue & Programs (International), with over 8 years of experience in business analytics, EdTech, and program management. He holds an M.Tech from IIT Delhi and specializes...

Speak with Data Science Expert

+91

By submitting, I accept the T&C and
Privacy Policy

Start Your Career in Data Science Today

Top Resources

Recommended Programs

upGrad Logo

Certification

3 Months

Liverpool John Moores University Logo
bestseller

Liverpool John Moores University

MS in Data Science

Dual Credentials

Master's Degree

17 Months

IIIT Bangalore logo
bestseller

The International Institute of Information Technology, Bangalore

Executive Diploma in Data Science & AI

Placement Assistance

Executive PG Program

12 Months