Age Prediction in Images-An OpenCV approach
By: Pratishtha Kapoor
Tags: Opencv, Computer Vision, Deep Learning, Age Detection, Image Recognition

The face detector in the previous step detected 6 faces from the input image. For all these faces which are detected in the image, we extract the ROI. We create a blob from this ROI and pass it to ageNet detector which predicts the age bucket and the age confidence.

For the above input image, the OpenCV age detector has predicted the age of all the faces in the age bracket of 25–32 with confidence score ranging from 63 % — 97 % which is quite accurate.

We will go ahead and try the age detection model on a few more images and analyze the prediction made by our age detector

Image from Harry Potter and Philospher’s stone

The next image as input is of Harry Potter from philosopher’s stone and during that movie, the character was 11 years old and our age detector has detected it correctly where it predicts the characters age to fall in the bracket range of 8–12 years and a confidence score around 54%. Till now our model has performed quite well in the age brackets estimation and the confidence score. But the model is prone to make the wrong prediction as the model has been trained on an imbalanced dataset. Where the data is heavily biased towards the age group of 25–32 and this affects the result adversely.

Let’s take another example to understand the problem stated above.

Image of Hermione Granger from Philosopher’s stone

Consider this input image where the character age of Hermione Granger is estimated in the age range of 4–6 years and as we know this where the model has predicted the age bracket totally wrong.

So what can be done to improve the age prediction results?

The first approach can be to use data augmentation. Another one can be to implement additional regularization when training the model. Finally, we can use the approach of Face alignment to improve the prediction results

Stage 3 — Face Alignment

In this step, the face alignment module identifies the geometric structures of faces and then attempts to obtain a canonical alignment of the face based on translation, scale, and rotation. The face alignment can be seen as data normalization. Where we rely on the facial landmarks mostly on the eye regions to obtain a normalized rotation, translation, and scale representation of the face.

Given a set of facial landmarks (the input coordinates) our goal is to warp and transform the image into an output coordinate space.

In this output coordinate space, all faces across an entire dataset should:

Be centered in the image. Be rotated that such the eyes lie on a horizontal line (i.e., the face is rotated such that the eyes lie along the same y-coordinates). Be scaled such that the size of the faces are approximately identical.

Face aligner source code

For the face alignment, we are going to use the FaceAligner from the imutils module.

We load the input image and resize the input to maintain an aspect ratio to have a width of 800 pixels. To detect the faces from the input images we use the dlib’s face detector, which returns a list of bounding boxes that the detector is found, next we iterate through the detection and align each face.

We pass these aligned images to the NN age detector to predict the estimated age brackets

Image of Hermione Granger from Philosopher’s stone Age detection on the aligned images got after applying the Face Aligner

As we can see now that after the application of the Face Aligner the prediction result of the age detector has significantly improved and it has predicted the characters age correctly between the age bracket of 8–12 years.

To sum up, of how all our 3 stages in the age detection model work together we use a simple flowchart visualization

Image by Author: Flowchart of the age estimation process

Conclusion

So in this blog, we learned how to perform age detection with OpenCV and deep learning, we utilized a pre-trained model from Levi and Hassner. We also came across the limitation of the model as it is heavily biased to the age bracket of 25–32 years because of the imbalanced dataset, and how can we overcome this limitation using the Face Alignment technique which can improve our age detector results significantly in most of the cases, but we should keep in mind that age prediction is a challenging problem.

Therefore, we must assess all age prediction results in terms of perceived age rather than actual age. As there are a number of factors that determine how old a person visually appears, including their lifestyle, work/job, smoking habits, and most importantly, genetics.