Post · 6 min read· pp. 354–359

Age and Gender Prediction with Deep Learning in OpenCV

Researchers of the Swiss Federal Institute of Technology in Zurich (ETH Zurich) built a state-of-the-art age and gender prediction models. Besides, they adopted the open source culture and shared the model structure and pre-trained weights of the production model for Caffe framework. So, we will mention how to run pre-built age and gender Caffe model within OpenCV in Python in this post.

Stark Family in the Game of Thrones

Vlog

You can either follow this blog post or watch the following video to make age and gender predictions with deep learning within OpenCV in Python.

▶ YouTube video: Age and Gender Prediction with OpenCV in Python

Age and Gender Prediction with OpenCV in Python

DeepFace library for python covers age prediction as well. You can run age estimation with a few lines of code.

▶ YouTube video: Age Estimation in Python with DeepFace

Age Estimation in Python with DeepFace

Previous study

I couldn’t convert Caffe weights to Keras / TensorFlow pair before and I decide to re-train the age and gender models from scratch based on the published paper. Here, you can find my age and gender prediction models.

The original study was built on regular VGG model but I adopted VGG-Face as a base model. Even though they have both same structure, regular VGG trained on imagenet data set whereas VGG-Face trained on face data set. In other words, VGG-Face comes with an advantage to recognize some facial patterns. Because this study is mainly based on transfer learning and we will freeze early layers and we will not update weights of those locked ones. Training will be handled just in final fully connected layers.

Loading pre-built models

We can use the built model structure and pre-trained weights for Caffe framework within OpenCV. Besides, you don’t have to have Caffe framework in your environment. Here, you can find required files. I pasted the exact URLs of requires files as comments below. Then, we can load pre-built Caffe models within OpenCV.

python
#age model
#model structure: https://data.vision.ee.ethz.ch/cvl/rrothe/imdb-wiki/static/age.prototxt
#pre-trained weights: https://data.vision.ee.ethz.ch/cvl/rrothe/imdb-wiki/static/dex_chalearn_iccv2015.caffemodel
age_model = cv2.dnn.readNetFromCaffe("age.prototxt", "dex_chalearn_iccv2015.caffemodel")

#gender model
#model structure: https://data.vision.ee.ethz.ch/cvl/rrothe/imdb-wiki/static/gender.prototxt
#pre-trained weights: https://data.vision.ee.ethz.ch/cvl/rrothe/imdb-wiki/static/gender.caffemodel
gender_model = cv2.dnn.readNetFromCaffe("gender.prototxt", "gender.caffemodel")
Face detection

OpenCV haar cascade is the easiest way to detect faces. Alternatively, some overperforming deep learning based approaches exist.

python
img = cv2.imread("img.jpg")

haar_detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml"")

def detect_faces(img):
   gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
   faces = haar_detector.detectMultiScale(gray, 1.3, 5)
   return faces

faces = detect_faces(img)
for x, y, w, h in faces:
   detected_face = img[int(y):int(y+h), int(x):int(x+w)]
Pre-procesing

As I mentioned before, age and gender prediction models are based on VGG and the model expects 224×224 shaped inputs.

The structure of VGG

You can confirm the expected input shape in the model structure files having prototxt extensions. I copied the 1st 5 rows of the prototxt file below. This means that Caffe model expexts (1, 3, 224, 224) shaped inputs.

python
input = "data"
input_dim = 1
input_dim = 3
input_dim = 224
input_dim = 224

Caffe model expects (1, 3, 224, 224) shaped inputs but OpenCV loads (224, 224, 3) shaped images even if we resize. blobFromImage function under deep neural networks module of OpenCV transforms read images to the expected shape.

python
detected_face = cv2.resize(detected_face, (224, 224)) #img shape is (224, 224, 3) now
img_blob = cv2.dnn.blobFromImage(detected_face ) # img_blob shape is (1, 3, 224, 224)
Predictions

We can feed pre-processed input images to the built model and make predictions. OpenCV requires to set input and call forward commands respectively to make predictions.

python
age_model.setInput(img_blob)
age_dist = age_model.forward()[0]

gender_model.setInput(img_blob)
gender_class = gender_model.forward()[0]

Firslty, age model returns 101 dimensional vector. In the original study, element wise multiplication is applied to the output vector and index vector.

python
output_indexes = np.array([i for i in range(0, 101)])
apparent_predictions = round(np.sum(age_dist * output_indexes), 2)

In addition, Gender model returns 2 dimensional model. If the 1st dimension value is greater than the 2nd one, then it is a woman. Vice versa, if the 2nd dimension value is greater than the 1st one, then it it is a man.

python
gender = 'Woman ' if np.argmax(gender_class) == 0 else 'Man'
Testings

You can see the testing results of pre-built age and gender models on game of thrones casting. We can obviously monitor how they grew up in years from season 1 to 8.

Test results

Age and gender in deepface

What’s more, deepface package for python offers age and gender prediction with a few lines of code.

python
#!pip install deepface
from deepface import DeepFace
demography = DeepFace.analyze("img4.jpg", actions = ['age', 'gender'])
print("Age: ", demography["age"])
print("Gender: ", demography["gender"])

Furthermore, you can watch a demo for age and gender prediction in python with deepface.

▶ YouTube video: Facial Attribute Analysis with Deep Learning in Python: Emotion, Age, Gender, Race

Facial Attribute Analysis with Deep Learning in Python: Emotion, Age, Gender, Race

Besides, deepface can access your webcam and run real time age and gender analysis as well.

▶ YouTube video: Real-Time Face Recognition and Facial Attribute Analysis (Age, Gender, Emotion) in Python

Real-Time Face Recognition and Facial Attribute Analysis (Age, Gender, Emotion) in Python

Real time implementation

We can run age and gender prediction in real time. Here, you can find the source code of this task.

▶ YouTube video: Real Time Age and Gender Prediction with Deep Learning in Python (TensorFlow + Keras + OpenCV)

Real Time Age and Gender Prediction with Deep Learning in Python (TensorFlow + Keras + OpenCV)

Also, deepface has its React JS ui for facial attribute analysis purposes.

▶ YouTube video: Face Analysis with ReactJS & DeepFace: Age, Gender, Emotion, Race

Face Analysis with ReactJS & DeepFace: Age, Gender, Emotion, Race

Conclusion

So, we’ve mentioned how to run pre-built Caffe models for age and gender prediction within OpenCV in Python. This one is built on regular VGG whereas its keras implementation is built on VGG-Face. My experiments show that VGG-Face based one overperforms than regular VGG based one.

Finally, I pushed the source code of this study to GitHub as a notebook. You can support this study if you star⭐️ the repo.