DL MiniProject Report
DL MiniProject Report
A PROJECT REPORT ON
Gender and Age Detection: predict if a person is a male or female and also their
age
SUBMITTED TOWARDS THE
PARTIAL FULFILLMENT OF THE REQUIREMENTS OF
1
Modern Education Society’s College of Engineering, Pune
CERTIFICATE
This is to certify that the Project Entitled
Gender and Age Detection: predict if a person is a male or female and also their
age
Submitted by
is a bonafide work carries out by students under the supervision of Prof. Dr. A.
K. Kale and is submitted towards the partial fulfilment of the requirements of
Bachelor of Engineering (Computer Engineering).
2
Index
1 Abstract 03
2 Introduction 04
3 Experimental Setup 05
4 Implementation 06
In this Python Project, we will use Deep Learning to accurately identify the gender and
age of a person from a single image of a face. We will use the models trained by Tal
Hassner and Gil Levi. 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) (8 nodes in the final SoftMax layer). It is
very difficult to accurately guess an exact age from a single image because of factors
like makeup, lighting, obstructions, and facial expressions. And so, we make this a
classification problem instead of making it one of regression. First introducing you with
the terminologies used in this advanced python project of gender and age detection
Introduction
Computer Vision
Computer Vision is the field of study that enables computers to see and identify digital
images and videos as a human would. The challenges it faces largely follow from the
limited understanding of biological vision. Computer Vision involves acquiring,
processing, analysing, and understanding digital images to extract high dimensional data
from the real world to generate symbolic or numerical information which can then be
used to make decisions. The process often includes practices like object recognition,
video tracking, motion estimation, and image restoration.
OpenCV
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 image and video while also boasting analytical capabilities. It supports the Deep
Learning frameworks TensorFlow, Caffe, and PyTorch.
CNN
A Convolutional Neural Network is a deep neural network (DNN) widely used for the
purposes of image recognition and processing and NLP. Also known as a ConvNet, a CNN
has input and output layers, and multiple hidden layers, many of which are convolutional.
In a way, CNNs are regularized multilayer perceptron.
It has 2 fully connected layers, each with 512 nodes, and a final output layer of SoftMax
type.
To go about the python project, we’ll:
• Detect faces
Experimental Setup
Hardware Specifications:
• Memory: 8 GB
• OS type: 64-bit
• Disk: 40 GB
Software Specifications:
• Operating system: Windows 10
• Python 3.7
• OpenCV2
Implementation
Steps for executing gender and age detection project
1. Download Dataset
• Adience dataset
2. Create files
A directory you’ll call gad. The contents of this directory are:
• opencv_face_detector.pbtxt
• opencv_face_detector_uint8.pb
• age_deploy.prototxt
• age_net.caffemodel
• gender_deploy.prototxt
• gender_net.caffemodel
For face detection, we have a .pb file- this is a protobuf file (protocol buffer); it holds the
graph definition and the trained weights of the model. We can use this to run the trained
model. And while a .pb file holds the protobuf in binary format, one with the .pbtxt
extension holds it in text format. These are TensorFlow files. For age and gender, the
.prototxt files describe the network configuration and the .caffemodel file defines the
internal states of the parameters of the layers.
3. We use the argparse library to create an argument parser so we can get the image
argument from the command prompt. We make it parse the argument holding the path to
the image to classify gender and age for.
4. For face, age, and gender, initialize protocol buffer and model.
5. Initialize the mean values for the model and the lists of age ranges and genders to
classify from.
6. Now, use the readNet() method to load the networks. The first parameter holds trained
weights and the second carries network configuration.
7. Let’s capture video stream in case you’d like to classify on a webcam’s stream. Set
padding to 20.
8. Now until any key is pressed, we read the stream and store the content into the names
hasFrame and frame. If it isn’t a video, it must wait, and so we call up waitKey() from cv2,
then break.
9. Let’s make a call to the highlightFace() function with the faceNet and frame parameters,
and what this returns, we will store in the names resultImg and faceBoxes. And if we got 0
faceBoxes, it means there was no face to detect. Here, net is faceNet- this model is the
DNN Face Detector and holds only about 2.7MB on disk.
• Create a shallow copy of frame and get its height and width.
• faceBoxes is an empty list now. for each value in 0 to 127, define the confidence
(between 0 and 1). Wherever we find the confidence greater than the confidence
threshold, which is 0.7, we get the x1, y1, x2, and y2 coordinates and append a list of those
to faceBoxes.
• Then, we put up rectangles on the image for each such list of coordinates and return two
things: the shallow copy and the list of faceBoxes
10.But if there are indeed faceBoxes, for each of those, we define the face, create a 4-
dimensional blob from the image. In doing this, we scale it, resize it, and pass in the mean
values.
11.We feed the input and give the network a forward pass to get the confidence of the two
class. Whichever is higher, that is the gender of the person in the picture.
13.We’ll add the gender and age texts to the resulting image and display it with imshow().
import cv2
import math
import argparse
parser=argparse.ArgumentParser()
parser.add_argument('--image')
args=parser.parse_args()
faceProto="opencv_face_detector.pbtxt"
faceModel="opencv_face_detector_uint8.pb"
ageProto="age_deploy.prototxt"
ageModel="age_net.caffemodel"
genderProto="gender_deploy.prototxt"
genderModel="gender_net.caffemodel"
faceNet=cv2.dnn.readNet(faceModel,faceProto)
ageNet=cv2.dnn.readNet(ageModel,ageProto)
genderNet=cv2.dnn.readNet(genderModel,genderProto)
resultImg,faceBoxes=highlightFace(faceNet,frame)
if not faceBoxes: print("No face detected")
for faceBox in faceBoxes:
face=frame[max(0,faceBox[1]-padding):
min(faceBox[3]+padding,frame.shape[0]-1),max(0,faceBox[0]-padding)
:min(faceBox[2]+padding, frame.shape[1]-1)]
blob=cv2.dnn.blobFromImage(face, 1.0, (227,227), MODEL_MEAN_VALUES,
swapRB=False) genderNet.setInput(blob) genderPreds=genderNet.forward()
gender=genderList[genderPreds[0].argmax()] print(f'Gender: {gender}')
ageNet.setInput(blob)
agePreds=ageNet.forward()
age=ageList[agePreds[0].argmax()]
print(f'Age: {age[1:-1]} years')
Output
Example 2:
Command
Output