Age & Gender Classification with OpenCV & Deep Learning on Raspberry Pi
Age & Gender Classification using OpenCV & Convolutional Neural Networks CNN Deep Learning Model on Raspberry Pi 4 Camera with Python Code
- Suitable forB.Tech/Diploma Raspberry Pi students
- Build time1-2 weeks
- LevelAdvanced
- Available atAjvika Labs, Kakinada
About this project
Overview:
This project is about
Age
&
Gender Classification
with
OpenCV
and Deep Learning (specifically
Convolutional Neural Networks
or CNNs) on
Raspberry Pi 4
Computer.
With the rise of social platforms, automatically determining
age
and
gender
from images has become increasingly important. However, the accuracy of current methods on real-world photos isn’t as good as the progress we’ve seen in
face recognition
. In this study, we demonstrate that using
deep-convolutional neural networks (CNN)
can significantly enhance the
accuracy
of age and gender classification. The model is based on
Gil Levi & Tal Hassner Model
published in 2015. It introduce a straightforward
CNN design
that’s effective even when there’s not a lot of data to train on.
In this guide, we’ll learn how to use
OpenCV
and
Deep Learning
to tell a person’s age and gender using a
Raspberry Pi 4
. OpenCV is a tool that helps computers understand pictures and videos.
Deep Learning
is a way for computers to
learn
from data, like how we learn from
experience
. When we combine these two, we can make a computer guess a person’s age and gender by looking at their picture. By the end of this guide, you’ll have a full idea of
Age & Gender Classification
Project with OpenCV & Deep Learning on Raspberry Pi.
Components Required
We need the following components for this project. You can purchase all items from given links:
S.N.
Components
Quantity
Purchase Link
Raspberry Pi 4
Amazon
SunFounder
Raspberry Pi Camera
Amazon
SunFounder
SD Card 16/32 GB
Amazon
SunFounder
5V, 3A DC Adapter for RPi
Amazon
SunFounder
LCD Display (Optional)
Amazon
SunFounder
Mouse & Keyboard (Optional)
Amazon
SunFounder
Raspberry Pi Camera Connection
The
Raspberry Pi Camera
is a peripheral device developed by the Raspberry Pi Foundation to be used with their series of Raspberry Pi single-board computers. The camera module provides a way to add
video/photo capabilities
to Raspberry Pi projects.
For this project, we can use a
5 mega-pixel
Raspberry Pi Camera.
Simply connect the Camera Module to the Raspberry Pi 4 Board using the Camera Connector.
To use the Camera you need to enable the Camera Module first. Open the Raspberry Pi Configuration Tool by typing
sudo raspi-config
in the terminal. Navigate to
Interfacing Options
>
Camera
and enable it.
Age & Gender Classification Method
The model proposed by
Levi and Hassner
in 2015 is a
convolutional neural network (CNN)
. CNNs are a category of neural networks that have proven very effective in areas such as
image recognition
and
classification
. Their model was trained on the Adience dataset, which is unfiltered and not aligned, making it suitable for
real-world
, unconstrained images.
The
classification process
is broken down into two primary steps:
Step 1: Face Detection
– The first step involves scanning the input video stream to detect faces. This is crucial as the subsequent classification is based on the facial features.
Step 2: Age and Gender Prediction
– Once a face is detected, the next step is to extract the face’s Region of Interest (ROI). This ROI is then fed into the age and gender detector algorithm. The algorithm analyzes the facial features and predicts both the age and gender of the individual.
The
gender prediction
will categorize the individual as either ‘
Male
‘ or ‘
Female
‘. For age prediction, the model classifies the individual into one of the
predefined age
brackets:
(0 – 2), (4 – 6), (8 – 12), (15 – 20), (25 – 32), (38 – 43), (48 – 53)
, or
(60 – 100)
Raspberry Pi Setup, Libraries & Dependencies Installation
OpenCV
is required for
Age
and
Gender Classification
, and other image processing tasks present in the code. Hence you need to
install the OpenCV
first. Follow the following guide to
install OpenCV
in your system:
How to Install & Setup OpenCV on Raspberry Pi 4
The next step is to install
picamera
. Therefore install it using
pip
pip3
install
picamera
Create a folder in the
Home directory
of your Raspberry Pi with any name such as “
Age_Gender_Classfication
“.
The code references multiple files used for age and gender classification. Download these files from the following links:
opencv_face_detector.pbtxt
opencv_face_detector_uint8.pb
age_deploy.prototxt
age_net.caffemodel
gender_deploy.prototxt
gender_net.caffemodel
Add these files to the folder you created above.
The setup part is complete now. We can move to the Age & Gender Classification Project with OpenCV & Deep Learning on Raspberry Pi.
Raspberry Pi Python Code for Age & Gender Classification
Now let’s develop a
Python Code
that helps in the classification of age and geneder using the
OpenCV library
and
Raspberry Pi Camera
Python Code
Open
Thonny IDE
and paste the following code to the Thonny Editor. Save this file with the any name such as “
classification.py
” to the folder that we created earlier. Ensure that these
all the downloaded files
are present in the directory mentioned in the code (
/home/mypi/Age_Gender_Classfication/
).
Here is the complete code.
import
cv2
def
highlightFace
(
net
frame
conf_threshold
=
0.7
)
:
frameOpencvDnn
=
frame
copy
(
)
frameHeight
=
frameOpencvDnn
shape
[
]
frameWidth
=
frameOpencvDnn
shape
[
]
blob
=
cv2
dnn
blobFromImage
(
frameOpencvDnn
1.0
(
)
[
]
True
False
)
net
setInput
(
blob
)
detections
=
net
forward
(
)
faceBoxes
=
[
]
for
i
in
range
(
detections
shape
[
]
)
:
confidence
=
detections
[
i
]
if
confidence
>
conf_threshold
:
x1
=
int
(
detections
[
i
]
*
frameWidth
)
y1
=
int
(
detections
[
i
]
*
frameHeight
)
x2
=
int
(
detections
[
i
]
*
frameWidth
)
y2
=
int
(
detections
[
i
]
*
frameHeight
)
faceBoxes
append
(
[
x1
y1
x2
y2
]
)
cv2
rectangle
(
frameOpencvDnn
(
x1
y1
)
(
x2
y2
)
(
)
int
(
round
(
frameHeight
/
)
)
)
return
frameOpencvDnn
faceBoxes
faceProto
=
"/home/mypi/Age_Gender_Classfication/opencv_face_detector.pbtxt"
faceModel
=
"/home/mypi/Age_Gender_Classfication/opencv_face_detector_uint8.pb"
ageProto
=
"/home/mypi/Age_Gender_Classfication/age_deploy.prototxt"
ageModel
=
"/home/mypi/Age_Gender_Classfication/age_net.caffemodel"
genderProto
=
"/home/mypi/Age_Gender_Classfication/gender_deploy.prototxt"
genderModel
=
"/home/mypi/Age_Gender_Classfication/gender_net.caffemodel"
MODEL_MEAN_VALUES
=
(
78.4263377603
87.7689143744
114.895847746
)
ageList
=
[
'(0-2)'
'(4-6)'
'(8-12)'
'(15-20)'
'(25-32)'
'(38-43)'
'(48-53)'
'(60-100)'
]
genderList
=
[
'Male'
'Female'
]
faceNet
=
cv2
dnn
readNet
(
faceModel
faceProto
)
ageNet
=
cv2
dnn
readNet
(
ageModel
ageProto
)
genderNet
=
cv2
dnn
readNet
(
genderModel
genderProto
)
video
=
cv2
VideoCapture
(
)
padding
=
hasFrame
frame
=
video
read
(
)
if
not
hasFrame
:
(
"Couldn't read a frame from the video."
)
exit
(
)
desired_window_width
=
ratio
=
desired_window_width
/
frame
shape
[
]
height
=
int
(
frame
shape
[
]
*
ratio
)
while
True
:
hasFrame
frame
=
video
read
(
)
if
not
hasFrame
:
break
frame
=
cv2
resize
(
frame
(
desired_window_width
height
)
)
resultImg
faceBoxes
=
highlightFace
(
faceNet
frame
)
for
faceBox
in
faceBoxes
:
face
=
frame
[
max
(
faceBox
[
]
padding
)
:
min
(
faceBox
[
]
+
padding
frame
shape
[
]
)
max
(
faceBox
[
]
padding
)
:
min
(
faceBox
[
]
+
padding
frame
shape
[
]
)
]
blob
=
cv2
dnn
blobFromImage
(
face
1.0
(
)
MODEL_MEAN_VALUES
swapRB
=
False
)
genderNet
setInput
(
blob
)
genderPreds
=
genderNet
forward
(
)
gender
=
gend
This project is about
Age
&
Gender Classification
with
OpenCV
and Deep Learning (specifically
Convolutional Neural Networks
or CNNs) on
Raspberry Pi 4
Computer.
With the rise of social platforms, automatically determining
age
and
gender
from images has become increasingly important. However, the accuracy of current methods on real-world photos isn’t as good as the progress we’ve seen in
face recognition
. In this study, we demonstrate that using
deep-convolutional neural networks (CNN)
can significantly enhance the
accuracy
of age and gender classification. The model is based on
Gil Levi & Tal Hassner Model
published in 2015. It introduce a straightforward
CNN design
that’s effective even when there’s not a lot of data to train on.
In this guide, we’ll learn how to use
OpenCV
and
Deep Learning
to tell a person’s age and gender using a
Raspberry Pi 4
. OpenCV is a tool that helps computers understand pictures and videos.
Deep Learning
is a way for computers to
learn
from data, like how we learn from
experience
. When we combine these two, we can make a computer guess a person’s age and gender by looking at their picture. By the end of this guide, you’ll have a full idea of
Age & Gender Classification
Project with OpenCV & Deep Learning on Raspberry Pi.
Components Required
We need the following components for this project. You can purchase all items from given links:
S.N.
Components
Quantity
Purchase Link
Raspberry Pi 4
Amazon
SunFounder
Raspberry Pi Camera
Amazon
SunFounder
SD Card 16/32 GB
Amazon
SunFounder
5V, 3A DC Adapter for RPi
Amazon
SunFounder
LCD Display (Optional)
Amazon
SunFounder
Mouse & Keyboard (Optional)
Amazon
SunFounder
Raspberry Pi Camera Connection
The
Raspberry Pi Camera
is a peripheral device developed by the Raspberry Pi Foundation to be used with their series of Raspberry Pi single-board computers. The camera module provides a way to add
video/photo capabilities
to Raspberry Pi projects.
For this project, we can use a
5 mega-pixel
Raspberry Pi Camera.
Simply connect the Camera Module to the Raspberry Pi 4 Board using the Camera Connector.
To use the Camera you need to enable the Camera Module first. Open the Raspberry Pi Configuration Tool by typing
sudo raspi-config
in the terminal. Navigate to
Interfacing Options
>
Camera
and enable it.
Age & Gender Classification Method
The model proposed by
Levi and Hassner
in 2015 is a
convolutional neural network (CNN)
. CNNs are a category of neural networks that have proven very effective in areas such as
image recognition
and
classification
. Their model was trained on the Adience dataset, which is unfiltered and not aligned, making it suitable for
real-world
, unconstrained images.
The
classification process
is broken down into two primary steps:
Step 1: Face Detection
– The first step involves scanning the input video stream to detect faces. This is crucial as the subsequent classification is based on the facial features.
Step 2: Age and Gender Prediction
– Once a face is detected, the next step is to extract the face’s Region of Interest (ROI). This ROI is then fed into the age and gender detector algorithm. The algorithm analyzes the facial features and predicts both the age and gender of the individual.
The
gender prediction
will categorize the individual as either ‘
Male
‘ or ‘
Female
‘. For age prediction, the model classifies the individual into one of the
predefined age
brackets:
(0 – 2), (4 – 6), (8 – 12), (15 – 20), (25 – 32), (38 – 43), (48 – 53)
, or
(60 – 100)
Raspberry Pi Setup, Libraries & Dependencies Installation
OpenCV
is required for
Age
and
Gender Classification
, and other image processing tasks present in the code. Hence you need to
install the OpenCV
first. Follow the following guide to
install OpenCV
in your system:
How to Install & Setup OpenCV on Raspberry Pi 4
The next step is to install
picamera
. Therefore install it using
pip
pip3
install
picamera
Create a folder in the
Home directory
of your Raspberry Pi with any name such as “
Age_Gender_Classfication
“.
The code references multiple files used for age and gender classification. Download these files from the following links:
opencv_face_detector.pbtxt
opencv_face_detector_uint8.pb
age_deploy.prototxt
age_net.caffemodel
gender_deploy.prototxt
gender_net.caffemodel
Add these files to the folder you created above.
The setup part is complete now. We can move to the Age & Gender Classification Project with OpenCV & Deep Learning on Raspberry Pi.
Raspberry Pi Python Code for Age & Gender Classification
Now let’s develop a
Python Code
that helps in the classification of age and geneder using the
OpenCV library
and
Raspberry Pi Camera
Python Code
Open
Thonny IDE
and paste the following code to the Thonny Editor. Save this file with the any name such as “
classification.py
” to the folder that we created earlier. Ensure that these
all the downloaded files
are present in the directory mentioned in the code (
/home/mypi/Age_Gender_Classfication/
).
Here is the complete code.
import
cv2
def
highlightFace
(
net
frame
conf_threshold
=
0.7
)
:
frameOpencvDnn
=
frame
copy
(
)
frameHeight
=
frameOpencvDnn
shape
[
]
frameWidth
=
frameOpencvDnn
shape
[
]
blob
=
cv2
dnn
blobFromImage
(
frameOpencvDnn
1.0
(
)
[
]
True
False
)
net
setInput
(
blob
)
detections
=
net
forward
(
)
faceBoxes
=
[
]
for
i
in
range
(
detections
shape
[
]
)
:
confidence
=
detections
[
i
]
if
confidence
>
conf_threshold
:
x1
=
int
(
detections
[
i
]
*
frameWidth
)
y1
=
int
(
detections
[
i
]
*
frameHeight
)
x2
=
int
(
detections
[
i
]
*
frameWidth
)
y2
=
int
(
detections
[
i
]
*
frameHeight
)
faceBoxes
append
(
[
x1
y1
x2
y2
]
)
cv2
rectangle
(
frameOpencvDnn
(
x1
y1
)
(
x2
y2
)
(
)
int
(
round
(
frameHeight
/
)
)
)
return
frameOpencvDnn
faceBoxes
faceProto
=
"/home/mypi/Age_Gender_Classfication/opencv_face_detector.pbtxt"
faceModel
=
"/home/mypi/Age_Gender_Classfication/opencv_face_detector_uint8.pb"
ageProto
=
"/home/mypi/Age_Gender_Classfication/age_deploy.prototxt"
ageModel
=
"/home/mypi/Age_Gender_Classfication/age_net.caffemodel"
genderProto
=
"/home/mypi/Age_Gender_Classfication/gender_deploy.prototxt"
genderModel
=
"/home/mypi/Age_Gender_Classfication/gender_net.caffemodel"
MODEL_MEAN_VALUES
=
(
78.4263377603
87.7689143744
114.895847746
)
ageList
=
[
'(0-2)'
'(4-6)'
'(8-12)'
'(15-20)'
'(25-32)'
'(38-43)'
'(48-53)'
'(60-100)'
]
genderList
=
[
'Male'
'Female'
]
faceNet
=
cv2
dnn
readNet
(
faceModel
faceProto
)
ageNet
=
cv2
dnn
readNet
(
ageModel
ageProto
)
genderNet
=
cv2
dnn
readNet
(
genderModel
genderProto
)
video
=
cv2
VideoCapture
(
)
padding
=
hasFrame
frame
=
video
read
(
)
if
not
hasFrame
:
(
"Couldn't read a frame from the video."
)
exit
(
)
desired_window_width
=
ratio
=
desired_window_width
/
frame
shape
[
]
height
=
int
(
frame
shape
[
]
*
ratio
)
while
True
:
hasFrame
frame
=
video
read
(
)
if
not
hasFrame
:
break
frame
=
cv2
resize
(
frame
(
desired_window_width
height
)
)
resultImg
faceBoxes
=
highlightFace
(
faceNet
frame
)
for
faceBox
in
faceBoxes
:
face
=
frame
[
max
(
faceBox
[
]
padding
)
:
min
(
faceBox
[
]
+
padding
frame
shape
[
]
)
max
(
faceBox
[
]
padding
)
:
min
(
faceBox
[
]
+
padding
frame
shape
[
]
)
]
blob
=
cv2
dnn
blobFromImage
(
face
1.0
(
)
MODEL_MEAN_VALUES
swapRB
=
False
)
genderNet
setInput
(
blob
)
genderPreds
=
genderNet
forward
(
)
gender
=
gend
Technologies used
Hardware components
- Raspberry Pi
- SD Card
Software & tools
- Thonny
What you get
- Working project / hardware kit, tested before delivery
- Complete source code with explanation
- Project demo and guidance for your viva
- Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed.
Payment is in two parts:
• 50% advance to start the project
• Remaining 50% before delivery
Note:
• Documentation and PPT are not included by default
• If you need documentation/PPT, it can be arranged for extra time and cost
We'll confirm the exact timeline and cost with you after reviewing your requirements.