Estimating Age, Gender, and Emotions
In this tutorial, you'll learn how to estimate age, gender, and emotions of a face using Face SDK. The result will be displayed next to the detected face. This tutorial expects that you already have a project with face detection in a video stream or on an image. You can learn how to detect faces in a video stream in the tutorial Face Detection and Tracking in a Video Stream. In this tutorial, we estimate age, gender, and emotions on the image.
You can find the tutorial project in Face SDK: examples/tutorials/age_gender_and_emotions
#
Estimating gender and age- Specify the path to the image in the field
QImage image
. Set the parameters of labels with information about gender, age, and emotions (size and style of text).
main.cpp
- Set the indent
margin_from_rect
between the face bounding rectangle and the text with information about a face. Set the distance between the elements "age group", "age", and "emotion"text_element_position
. These elements are displayed to the right of a detected face, one below the other.
main.cpp
- Use the method
pbio::FacerecService::createAgeGenderEstimator
to create the objectAgeGenderEstimator
and estimate gender and age. When calling this method, specify the configuration fileage_gender_estimator.xml
.
main.cpp
- Calculate the starting point
base_point
to display the information about gender, age, and emotions: get the face bounding rectangle from theRawSample
object, which stores the face information and calculate the point taking into account the indentmargin_from_rect
.
main.cpp
- Estimate gender and age of a face
(*sample)
using the methodpbio::AgeGenderEstimator
. Display the age group based on the estimated age. Four age groups are available:
- Kid (under 18 years)
- Young (18-37 years)
- Adult (37-55 years)
- Senior (55 years and older)
The age group is taken from the enumeration pbio::AgeGenderEstimator::Age
and the result is stored in the age_group_text
variable. Display the label with the age group using the method painter.drawText
. The label will be on the first line to the right of the starting point base_point
.
main.cpp
- Display the age in years on the second line (under the age group).
main.cpp
- Display the gender on the third line (under the age).
main.cpp
- Run the project. At this stage, you'll see the information about the age group, age and gender to the right of the detected face.
#
Estimating emotions- Use the method
pbio::FacerecService::createEmotionsEstimator
to create the objectEmotionsEstimator
and estimate emotions. When calling this method, specify the configuration fileemotions_estimator.xml
.
main.cpp
- Use the method
pbio::EmotionsEstimator::estimateEmotions
to estimate emotions of a detected face(*sample)
and get a confidence coefficient (from 0 to 1). The enumerationpbio::EmotionsEstimator::Emotion
includes all available emotions. Face SDK estimates four emotions:
- Neutral
- Happy
- Angry
- Surprised
Each emotion is assigned an index from 0 to 3. In this project, we display emotions as four columns of different colors (blue, green, red, yellow) with corresponding labels (Neutral, Happy, Angry, Surprise). If the confidence coefficient of emotion is high, then the column for this emotion is longer than three other columns. This clearly shows which emotion prevails. All parameters of emotions are stored in the dictionary emotions_params
.
main.cpp
- Set the starting point
emotions_base_point
, which we'll use to draw the columns with emotions (to the right of a face bounding rectangle with the labels). Also set the size of the columns with emotionsbar_base_size
and indentbar_offset
from the labels of emotions.
main.cpp
- Display the labels and columns. In the loop, calculate the starting point
emotion_row_base_point
to display the information about each emotion and the starting pointtext_base_point
to display the name of emotion. Then, get the name of the emotion from the dictionary and display the labelemotion_label
. Display the column for each emotion: calculate the starting point to display the columnbar_base_point
and calculate the length of the columnbar_size
. To do this, multiply the value ofbar_base_size.width
by the confidence coefficient. Color the columns according to the colors from the dictionary.
main.cpp
- Run the project. To the right of the face bounding rectangle, you'll see the information about emotions (the length of a column visualize the probability distribution in the space of described emotions).