Skip to content

Age & Gender Prediction using swatahVision (OpenVINO Engine)

This example demonstrates how to perform Age and Gender prediction using the age-gender-recognition-retail-0013 model through the
swatahVision framework, powered internally by the OpenVINO engine.

The script loads two face images, runs inference on CPU,
and prints the predicted age and gender in the terminal.

๐Ÿ“ฅ Model Download

Pretrained models for swatahVision are available in the Model Zoo.

๐Ÿ”— https://visionai4bharat.github.io/swatahVision/model_zoo/


๐Ÿ“ Folder Structure

age_gender_project/
โ”œโ”€โ”€ age-gender-recognition-retail-0013.xml
โ”œโ”€โ”€ age-gender-recognition-retail-0013.bin
โ”œโ”€โ”€ age_gender_prediction.py
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ image_old_man.jpg
โ””โ”€โ”€ image_boy.jpg

๐Ÿ”ง Requirements

  • Python 3.9+
  • NumPy
  • OpenCV
  • swatahVision

๐Ÿงฉ Installation

conda create -n swatah_env python=3.9 -y
conda activate swatah_env

Install Dependencies

pip install numpy
pip install opencv-python
pip install swatahVision

๐Ÿš€ How to Run

python age_gender_prediction.py

๐Ÿงช Complete Source Code

Below is the exact code used in this project:

import swatahVision as sv
import numpy as np
import cv2

MODEL_PATH = "age-gender-recognition-retail-0013.xml"
IMAGE_PATH_OLD = "image_old_man.jpg"
IMAGE_PATH_BOY = "image_boy.jpg"

model = sv.Model(
    model=MODEL_PATH,
    engine=sv.Engine.OPENVINO,
    hardware=sv.Hardware.CPU
)

# -----------------------------
# First Image Prediction
# -----------------------------
img = sv.Image.load_from_file(IMAGE_PATH_OLD)
outputs = model(img)[0]

gender_blob = outputs[0]
age_blob = outputs[1]

gender_id = int(np.argmax(gender_blob))
gender = "Male" if gender_id == 1 else "Female"
age = int(age_blob[0][0][0][0] * 100)

print("Predicted Age   :", age)
print("Predicted Gender:", gender)

# -----------------------------
# Second Image Prediction
# -----------------------------
image = sv.Image.load_from_file(IMAGE_PATH_BOY)
outputs = model(image)[0]
print(outputs)

gender_blob = outputs[0]
age_blob = outputs[1]

gender_id = int(np.argmax(gender_blob))
gender = "Male" if gender_id == 1 else "Female"
age = int(age_blob[0][0][0][0] * 100)

print("Predicted Age   :", age)
print("Predicted Gender:", gender)

๐Ÿ“ค Sample Output

Predicted Age   : 62
Predicted Gender: Male

Predicted Age   : 14
Predicted Gender: Male

๐Ÿง  Model Information

  • Model Name: age-gender-recognition-retail-0013
  • Framework: swatahVision
  • Inference Engine: OpenVINO (internal)
  • Hardware: CPU
  • Age Output: Normalized value ร— 100
  • Gender Output: Probability scores [Female, Male]

โš ๏ธ Notes

  • The model expects cropped face images.
  • Only single-face inference is supported.
  • Age prediction is an estimate.
  • Accuracy depends on lighting and image quality.