OpenCV is short for Open Source Computer Vision. Intuitively by the name, it is an open-source Computer Vision and Machine Learning library. This library is capable of processing real-time images and videos while also boasting analytical capabilities.
Today, we will use OpenCV to accurately identify the gender and age of a person from a single image of a face. The predicted gender may be one of ‘Male’ and ‘Female’, and the predicted age may be one of the following ranges- (0–2), (4–6), (8–12), (15–20), (25–32), (38–43), (48–53), (60–100).
The face detector produces the bounding box coordinates of the face in the image. Start by importing required libraries and finding the bounding box coordinates.
The getFaceBox() function is used to get the coordinates of the face. In the first three lines, we get the shallow copy of the frame and then we detect the height and width of the frame. Next, we construct a blob from it and pass the blob through the network to obtain the face detections. And finally, loop over the detections and extract the face coordinates. Use these coordinates to create a rectangle around the face.
In the next lines, define the path of the face detection, age detection, and gender detection models.
Initialize the mean values for the model and the lists of age ranges and genders to classify from.
After getting the face boxes, we create a 4-dimensional blob from the image. In doing this, we scale it, resize it, and pass in the mean values. We pass the blob through the gender detection model and get the confidence of the two-class (Male & Female). Whichever is higher, that is the gender of the person in the picture. Then follow the same procedure for age detection.