PREDICTING GENDER AND AGE USING IMAGE DATA IN PYTHON

Dhwanipanjwani
3 min readOct 28, 2021

Computer Vision in the field of study enables computers to see and identify digital images and videos as a human would. The challenges it faces largely follow from the limited understanding of the biological vision. Computer Vision involves acquiring, processing, analyzing, and understanding digital images to extract high-dimensional data from the real world in order to generate symbolic or numerical information which can then be used to make decisions. The process often includes practices like object recognition, video tracking, motion estimation, and image restoration.

OpenCV is a cross-platform library using which we can develop real-time computer vision applications. It mainly focuses on image processing, video capture, and analysis including features like face detection and object detection. In this tutorial, we explain how you can use OpenCV in your applications.

Features of OpenCV Library

Using the OpenCV library, one can:

  • Read and write images
  • Capture and save videos
  • Process images (filter, transform)
  • Perform feature detection
  • Detect specific objects such as faces, eyes, cars, in the videos or images.
  • Analyze the video, i.e., estimate the motion in it, subtract the background, and track objects in it.

Getting Started

1. Importing OpenCV and other required packages.

2. Finding bounding box coordinates

3.Loading model and weight files

  • gender_net.caffemodel: It is the pre-trained model weights for gender detection. You can download it here.
  • deploy_gender.prototxt: is the model architecture for the gender detection model (a plain text file with a JSON-like structure containing all the neural network layer’s definitions). Get it here.
  • age_net.caffemodel: It is the pre-trained model weights for age detection. You can download it here.
  • deploy_age.prototxt: is the model architecture for the age detection model (a plain text file with a JSON-like structure containing all the neural network layer’s definitions). Get it here.
  • res10_300x300_ssd_iter_140000_fp16.caffemodel: The pre-trained model weights for face detection, download here.
  • deploy.prototxt.txt: This is the model architecture for the face detection model, download here.

4. Mentioning age and gender category list

5. Function to predict gender and age

6. Uploading image

How image is processed

  • First, it reads the image using the cv2.imread() method.
  • After the image is resized to the appropriate size, we use our get_faces() function to get all the detected faces from the image.
  • We iterate on each detected face image and call our get_age_predictions() and get_gender_predictions() to get the predictions.
  • We print the age and gender.
  • We draw a rectangle surrounding the face and also put the label that contains the age and gender text along with confidence on the image.
  • Finally, we show the image

Outputs:

Image 1:

Image 2:

Thank You

--

--