Data Science | Age and Gender Prediction using Python

Nandish Bhayani
3 min readOct 29, 2021

AIM:- Using image data, predict the gender and age range of an individual in Python. Test the data science model using your own image.

This blog is about how we predict gender and age from a given image using OpenCV and the machine learning library.

What is OpenCV in machine learning?

OpenCV is a library of programming functions mainly aimed at real-time computer vision. Originally developed by Intel, it was later supported by Willow Garage then Itseez. The library is cross-platform and free for use under the open-source Apache 2 License.

The library has more than 2500 optimized algorithms, which includes a comprehensive set of both classic and state-of-the-art computer vision and machine learning algorithms. These algorithms can be used to detect and recognize faces, identify objects, classify human actions in videos, track camera movements, track moving objects, extract 3D models of objects, produce 3D point clouds from stereo cameras, stitch images together to produce a high-resolution image of an entire scene, find similar images from an image database, remove red eyes from images taken using flash, follow eye movements, recognize scenery and establish markers to overlay it with augmented reality, etc. OpenCV has more than 47 thousand people in the user community and an estimated number of downloads exceeding 18 million. The library is used extensively in companies, research groups, and governmental bodies.

Now let start to implement this project step by step,

Step-1: Command for installing OpenCV

pip Install OpenCV

Step-2: Command for installing argparse

pip install argparse

For this python project, I had used the Adience dataset; the dataset is available in the public domain and you can find it here. This dataset serves as a benchmark for face photos and is inclusive of various real-world imaging conditions like noise, lighting, pose, and appearance. The images have been collected from Flickr albums and distributed under the Creative Commons (CC) license. It has a total of 26,580 photos of 2,284 subjects in eight age ranges (as mentioned above) and is about 1GB in size. The models I used had been trained on this dataset.

After that how we create a rectangle outside the face for code is given in the below image.

Here I am running the project using command prompt as below

python detect.py — image <image name with extension>

Output:-

--

--