Ready to take Python coding to a new level? Explore our Python Code Generator. The perfect tool to get your code up and running in no time. Start now!
In this tutorial, we will combine gender detection and age detection tutorials to develop a single code that detects both.
Let's get started. If you haven't OpenCV already installed, make sure to do so:
Open up a new file. Importing the libraries:
Next, defining the variables of weights and architectures for face, age, and gender detection models:
Below are the necessary files to be included in the project directory:
gender_net.caffemodel
: It is the pre-trained model weights for gender detection. You can download it here.deploy_gender.prototxt
: is the model architecture for the gender detection model (a plain text file with a JSON-like structure containing all the neural network layer’s definitions). Get it here.age_net.caffemodel
: It is the pre-trained model weights for age detection. You can download it here.deploy_age.prototxt
: is the model architecture for the age detection model (a plain text file with a JSON-like structure containing all the neural network layer’s definitions). Get it here.res10_300x300_ssd_iter_140000_fp16.caffemodel
: The pre-trained model weights for face detection, download here.deploy.prototxt.txt
: This is the model architecture for the face detection model, download here.Next, loading the models:
Before trying to detect age and gender, we need a function to detect faces first:
Building a real-time automatic number plate recognition system using YOLO and OpenCV library in Python
Download EBookThe get_faces()
function was grabbed from the face detection tutorial, so check it out if you want more information.
Below is a function for simply displaying an image:
Below are is a function for dynamically resizing an image, we're going to need it to resize the input images when exceeding a certain width:
Now that everything is ready, let's define our two functions for age and gender detection:
The get_gender_predictions()
and get_age_predictions()
perform prediction on the gender_net
and age_net
models to infer the gender and age of the input image respectively.
Finally, we write our main function:
The main function does the following:
cv2.imread()
method.get_faces()
function to get all the detected faces from the image.get_age_predictions()
and get_gender_predictions()
to get the predictions.Let's call it:
Done, let's run the script now (testing on this image):
Output in the console:
The resulting image:
Here is another example:
Or this:
Awesome! If you see the text in the image is large or small, make sure to tweak the
font_scale
floating-point variable on your image in the predict_age_and_gender()
function.
For more detail on how the gender and age prediction works, I suggest you check the individual tutorials:
If you want to use your camera, I made a Python script to read images from your webcam and perform inference in real time.
Check the full code here.
Building a real-time automatic number plate recognition system using YOLO and OpenCV library in Python
Download EBookHappy coding ♥
Take the stress out of learning Python. Meet our Python Code Assistant – your new coding buddy. Give it a whirl!
View Full Code Generate Python Code
Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!