Age & Gender Prediction with DeepFace in Python

NeuralNine · Beginner ·💻 AI-Assisted Coding ·3y ago

Key Takeaways

This video demonstrates age and gender prediction using DeepFace, a lightweight Python framework for face recognition and facial attribute analysis, along with OpenCV for image loading and pandas for data set creation.

Full Transcript

what is going on guys welcome back in today's video we're going to learn how to easily detect or predict the age and gender of people based on photographs as well as some other interesting attributes so let us get right into it [Music] alright so for this video today we're going to use an external python package called Deep face in order to make predictions on attributes like the age and the gender of a person based on images based on photographs and deep face is essentially a lightweight python framework for face recognition and facial attribute analysis it's very easy to use and it allows us to predict attributes like gender age race and emotional state in a respective image using just one line of code using just one simple function call we can call one function to make predictions easily on all those attributes and the predictions are also most of the time quite accurate or at least when I look at these images I would say that the that the estimation the prediction is kind of reasonable most of the time so we're going to learn how to do this in this video today and then we're going to also look at a practical use case and a practical example where we're going to take multiple images and we're going to AI generate a data set from them so we're going to use deep face to generate a data set based on images we're going to have the respective attributes as columns in that data set so this is what we're going to do today and for that we're going to need a couple of preparation steps first of all you're going to need to have some images so I have a directory called faces with a couple of images here that I downloaded from the internet those are copyright free images you can use locally whatever images you want you can download images from the internet you can use your own images I have simple images like Alex jpeg Angela jpeg or something like Julie jpac Mike jpeg those are just some people some images of people that I downloaded from the internet and we're going to make the predictions on those now the reason they have names is because I want to use those file names here as the name of the person those are just made up names we're going to use them as the name column in our data set later on in the video so in addition to that we're also going to install three external python packages for this we're going to use pip we're going to say pip install deep face opencv Dash Python and pandas now deep face as I already mentioned is going to be used for the predictions opencv is going to be used in order to load the images into the script and pandas will be used in order to create the data sets so install those packages and once you have them installed we can get started with the coding we're going to start by importing CB2 which is opencv we're going to then say from Deep face import deep face and then all we need to do now in order to predict the gender age race and emotional state of a person based on an image is we need to load the image and we need to pass it to the analyze function of deep face so we just say image equals cb2.m reads then we provide the path to uh faces slash Alex dot jpeg and now what we do is we say result equals or results equals deep face dot analyze image and then we pass the actions tool so the actions Tuple defines what we're trying to predict here now by default we have all of the possible attributes selected we have a motion h gender race this is going to give us all the information if you want to just know the gender for example you could have you could say gender and you have to pass a comma here to make it a tuple otherwise it's just a string or you can also say here gender and H to just predict those two attributes and then once this is done you can print the results and we're going to see the structure here in a second now the first time you run this you're going to download the models Because deep face is actually using uh or is capable of using multiple different models in the background you can also specify those but whatever you use it has to First download the weights so in your case when you run this for the first time you're going to download the model weights and then you're going to make the prediction and in this case here I get this list with a dictionary inside of the list and the dictionary contains the key gender with another dictionary that has woman and man with the respective probabilities so in this case Alex this guy here has a 99.924 percent chance of being um of being a male of being a man and you can also see here the field dominant gender if you don't want to um if you don't want to pay attention to the probabilities you can just see the dominant gender is men uh and you can also see at the end here age 23 now I don't know what this guy uh how old this guy is but I think man is accurate and I think that 23 is not a bad estimation now we can do that here for the different faces we can say Angela jpeg and see what we get here's the result and of course what I can also do as I already mentioned here is we can add the different fields here like race and emotion and then we're going to also get some information about the facial expression and uh the race of that person at least a prediction so we can see here that Angela is a woman most likely 97.48 for that and also 23 years old so that's probably a coincidence so let's run this here again with all the four actions with all the four attributes that we're looking at ha and this is going to tell us that she's a woman we always get the probabilities and the dominant result so for the h we just get a number but for race you can see here we get a probability for every race that's possible here in that model we have Asian Indian black white middle eastern uh Latino Hispanic and then we get this dominant race which is Latino Hispanic now I don't know what the race of this girl is or if this woman is but uh it's at least something that could be the case here so we have a probability of 28.88 for that uh the emotion seems to be neutral I would agree with that it's not angry it's not disgust it's not fear it's not happy necessarily it's not sad uh it's not surprised it seems to be quite neutral now we also have some other facial expressions here now where is this maybe John John seems to be surprised in my opinion so let's go ahead and see what the results are for John and we should probably get surprised so John is first of all a man John is 32 years old I would say that's probably true something around that scale or range um race seems to be white I would agree with that and then we have the emotion the dominant emotion being surprised so this is quite accurate and now we can take this basic functionality and turn it into an automation test let's say we don't have just these couple of images let's say we have a thousand images and we want to create an AI label data set based on those images how do we do that now in order to do that we're going to import pandas SPD we're going to get rid of all this and we're going to say now here we're also going to import a core python module called OS in order to get all these images all the file names and what we want to do here is we want to create a dictionary data and we're going to have the fields H which is going to be an empty list we want to have gender which is going to be an empty list we want to have race which is going to be an empty list and we want to have uh finally the emotion which is also going to be an empty list and what we want to do now is we want to just iterate over the files load them extract information or make the predictions and then create a data frame a data set based on that so we can say here four file in OS list come on list directory and the path is going to be just faces for each file in the faces directory what we want to do is want to say result equals two incompetent to type right now uh the result is going to be equal to deep phase dot analyze CV2 imrit now we need to join the path so we need to say actually let's just use a formatted string faces slash and here file we're going to read that and we're going to predict everything so actions is going to be again H gender race and uh emotion now emotion is probably not very useful when I think about this um because emotion is always just in one particular image that's not necessarily something about a person so let's remove that but one thing that we need to add here is we need to add the name of the person so name which is going to be the file name is going to also be an empty list so let's do it like that name age gender race and let's just go with h gender race without using the emotional state and now all we need to do is we need to say data name dot append and we're going to append the file name uh without the file extension so we're just going to take file split it on the dot assuming that there's only one dot and then pick the first part of the split which is going to be the file name without the extension because the second part is going to be the extension and then we're going to say here data H this is going to be quite simple it's just append result index 0 to take the dictionary from the list and then we're going to just get DH from there and we're going to copy that we're going to change this change this to gender and race and all we need to change here is we need to say instead of H dominant gender dominant race and then we just say DF equals PD dot data frame based on the data dictionary we can then print it and we can also export it DF to underscore CSV we can export it to people.csv now this is going to take some time we're going to have to run through all the all the iterations here now we do have the problem gender oh of course we need to use uppercase characters here now I replace the quotation mark like that because of course the keys that we use here are the same keys that we Define up here so we need to keep this consistent uh but then it's going to run through the analysis for each of those images and we're going to get a data set as a result so we can see here we have Alex Angela Bob Emma John Julie Mike with the respective ages that were predicted with the gender and with the race now this does not have to be true this can be uh this data can contain mistakes but it is probably quite accurate in its estimation so we can look through the images Alex we already saw 23 years old man white probably true Angela 23 years old woman Latino Hispanic probably true Bob is um 47 I would say probably older but I don't know uh a man Middle Eastern could be I don't know if that's true uh Emma is uh 34 years old a man I don't know why that is the case um I think it's pretty obvious that this is a woman but maybe I'm wrong I don't know uh race black seems to be true then we have John which is uh who is 32 years old white and a man Julie being a 31 year old woman I would probably say younger but I'm not sure uh White and then we have Mike who's 23 I would also say probably a little bit older a man in Middle Eastern so it's not entirely what I would predict but I don't know also the true results so what would make sense here is to use a data set or maybe your own images where you already know the answer so use images of people you know uh where you know the age the gender and the race and then see if the amount performs quite well but this is something that can be automated you can also use different models here I think we can go into the code and you can see here that it has different base models that we can specify um I think we can specify those Indian analyze function here so I think we can use uh here we can just use the detector backend but for different functions we can use also the different models and you can play around here with that and see how accurate the results are but this is how you can easily predict attributes like the age gender race or even emotional state of a person using machine learning in Python so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you next video and bye [Music] thank you [Music]

Original Description

In this video, we learn how to predict attributes like the age and the gender of a person based on photographs, using DeepFace in Python. ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎙 Discord: https://discord.gg/JU4xr8U3dm 🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches how to use DeepFace and OpenCV to predict age and gender from images, and how to automate the process using machine learning in Python. The video covers the installation of required packages, loading images, and predicting attributes like gender, age, race, and emotional state.

Key Takeaways
  1. Install required packages: DeepFace, OpenCV, Dash, and pandas
  2. Import OpenCV and DeepFace
  3. Load image using OpenCV
  4. Pass image to DeepFace's analyze function
  5. Define actions tuple to predict attributes
  6. Run the code for the first time to download model weights
  7. Make predictions and print results
  8. Add fields for race and emotion prediction
  9. Create a dictionary data structure to store image information
  10. Iterate over files to load and extract information
💡 DeepFace is a lightweight Python framework that can be used for face recognition and facial attribute analysis, and can be integrated with OpenCV for image loading and pandas for data set creation.

Related Reads

Up next
Copilot Cowork: Setup, Skills, Plugins & Pricing
Matt Tutorials
Watch →