Data Science👨‍💻: Gender and Age Prediction Project

Manthan Bhikadiya 💡
DataDrivenInvestor
Published in
4 min readOct 28, 2021

--

Welcome to the Data Science Blog Series. This is the Final Blog in this series. Do check out my all previous blog from the data science blog series here.

Today is your opportunity to build the Tomorrow you want.

~ Ken Poirot

Overview:

Age and gender, two of the key facial attributes, play a very 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.

Here in this blog I will how you can make your own Gender and Age Detector in Just a Few lines…

Here I used some pre-trained models rather than created from scratch.

You can create from scratch here.

I found that accuracy by using a pre-trained model is high than creating from scratch.

Pre-Trained Models:

You must have to download all pre-trained models mentioned here.

You can download all the pre-trained models from the My GitHub Repository or you can download from the below official sources:

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.

show results

Predictions:

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

LinkedIn:

Github:

Thanks for reading! If you enjoyed this article, please hit the clap 👏button as many times as you can. It would mean a lot and encourage me to keep sharing my knowledge. If you like my content follow me on medium I will try to post as many blogs as I can.

--

--

Beyond the code lies magic. 🪄 Unveiling AI's potential with Generative AI, ML, DL, NLP, CV. Explore my blog's insights!