A Mini Project Report On Age and Gender Detection-2
A Mini Project Report On Age and Gender Detection-2
Deep Learning
Mini project on
Submitted By
Class Fourth Year Branch Computer Engineering has successfully completed the work
asunciated with Deep Learning(410251) titled as
Implement Gender and Age Detection: predict if a person is a male or female and
also their age and has submitted the work book associated under my supervision, in
the partial fulfilment of Fourth Year Bachelor of Engineering (Choice Based Credit
System) (2019 course) of Savitribai Phule Pune University.
Date:
Place: Indapur
1. Shivani Dole
2. Shubhangi Shinde
3. Pratiksha Thorat
4. Bhagyashree Ghadage
Index
1 Abstract 3
2 Introduction 4
3 Methodology 5
5 Conclusion 10
ABSTRACT
DEEP LEARNING
Deep learning is an artificial intelligence (AI) technique that seeks to learn
from experience and resemble the human brain. Through a training
procedure, these representations are learned. To teach the software how to
detect an object, we must first train it with a large number of object images
that we categorize according to different classes. Deep learning-based
algorithms, on average, require a large amount of training data and take
longer to train than traditional machine learning methods.
Finding unique attributes when trying to recognize any object or character
in an image is time-consuming and complex. Unlike traditional machine
learning, where features are manually retrieved, problems can be solved
using deep learning approaches which extract important characteristics
from data automatically. A neural network with multiple hidden layers is
known as deep learning. They may build complicated notions from simple
concepts after an image has been passed through the network.
By integrating simple elements such as shape, edges, and corners, an image
can be trained in the network to learn items such as characters, faces, and
so on. As the image travels through the layers, each one gets a simple
property while moving on to the next. As the layers grow deeper, the
network may learn more complex features and eventually merge them to
identify the image. In the field of computer vision, deep learning has
discovered a wide range of uses. The domains that work with facial data are
among the most important computer vision applications.
FACE DETECTION
Face detection, also called facial detection, is an artificial intelligence (AI)-
based computer technology used to find and identify human faces in digital
images and video. Face detection technology is often used for surveillance
and tracking of people in real-time. It is used in various fields including
security, biometrics, law enforcement, entertainment, and social media.
Face detection uses Machine Learning (ML) and Artificial Neural Networks
(ANN) for training and plays an important role in face tracking, face
analysis, and facial recognition. In face analysis, face detection uses facial
expressions to identify which parts of an image or video should be focused
on to determine age, gender, and emotions. In a facial recognition system,
face detection gathers data required to generate a faceprint and match it with
other stored faceprints.
Upload Image
Face Detection
Result
Objective: To build a deep neural network model that can recognize gender and age
from the UTK Face dataset.
Theory:
• Collect and Prepare the Dataset:
To begin building a gender and age prediction model using deep learning, the
first step is to gather a dataset containing labelled facial images. In this project,
we utilize the UTK Face dataset, which includes over 20,000 images of human
faces of various ages, genders, and ethnicities. Each image filename encodes the
age and gender, making it easy to extract labels. Before feeding the data to the
model, we must perform preprocessing. This includes resizing the images to a
uniform dimension (e.g., 200x200 pixels), normalizing pixel values to improve
model convergence, and possibly limiting the maximum age to 100 years to
reduce the impact of outliers. Additionally, shuffling the dataset ensures that the
model doesn't learn any hidden order or bias present in the raw data.
• Split the Dataset:
After preparing the data, the next step is to split the dataset into training,
validation, and testing sets. A typical distribution is 80% for training, 10% for
validation, and 10% for testing. The training set is used to learn the model
parameters, the validation set helps fine-tune hyperparameters and prevent
overfitting, and the test set evaluates the final performance on unseen data. A
well-maintained split ensures that the model generalizes effectively and doesn't
just memorize the training examples.
• Define Data Generators:
Since loading the entire dataset into memory may be inefficient, especially with
large datasets, we use data generators to load the data in batches during training.
Keras provides an efficient tool called ImageDataGenerator, which can also
perform real-time data augmentation. This means that during training, the model
sees slightly altered versions of the images—through operations like rotation,
zoom, brightness adjustment, and horizontal flipping—which helps improve the
model’s robustness. For age and gender prediction, we need to ensure the
generator can yield multiple outputs (age and gender), which may involve
custom data generators or multi-output pipelines.
• Define the Neural Network Model:
The core of the system is a Convolutional Neural Network (CNN), which is
well-suited for image-related tasks. The CNN architecture typically begins with
several convolutional layers that detect spatial hierarchies in the data, followed
by pooling layers that reduce dimensionality. After a few layers of feature
extraction, the output is flattened and passed into dense (fully connected) layers.
The final layers branch out into two separate outputs: one for gender
classification (with a sigmoid activation for binary output: male/female) and
another for age regression (with a linear or ReLU activation to predict
continuous age values). The model is compiled using a multi-output loss
function, combining binary cross-entropy for gender and mean squared error
(MSE) for age. An optimizer such as Adam or RMSprop is used to adjust the
model’s weights efficiently during training.
• Train and Evaluate the Model:
After defining the model architecture, the training process begins by feeding in
the training and validation datasets through the generators. The model learns the
patterns in the facial features that correspond to different ages and genders.
During training, metrics such as accuracy (for gender) and mean absolute error
(for age) are monitored. Once the training is complete, the model’s performance
is assessed using the test dataset. This step is crucial to ensure the model
performs well on unseen data and isn’t overfitted.
• Save and Deploy the Model:
Once the model shows satisfactory performance, it is saved in a suitable format
(e.g., .h5 for Keras) for later use. The trained model can be deployed in real-
world applications such as face recognition systems, demographic analysis
tools, or embedded in mobile/desktop applications. A GUI or web interface can
also be built to allow users to upload images and get predictions in real-time.
Conclusion
This project successfully implements age and gender prediction using CNNs on the
UTK Face dataset, showcasing effective use of deep learning for facial analysis.
Source Code
script.py
import cv2
import argparse
def highlightFace(net, frame, conf_threshold=0.7):
frameOpencvDnn = frame.copy()
frameHeight = frameOpencvDnn.shape[0]
frameWidth = frameOpencvDnn.shape[1]
blob = cv2.dnn.blobFromImage(frameOpencvDnn, 1.0, (300, 300), [104, 117, 123],
swapRB=True, crop=False)
net.setInput(blob)
detections = net.forward()
faceBoxes = []
for i in range(detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > conf_threshold:
x1 = int(detections[0, 0, i, 3] * frameWidth)
y1 = int(detections[0, 0, i, 4] * frameHeight)
x2 = int(detections[0, 0, i, 5] * frameWidth)
y2 = int(detections[0, 0, i, 6] * frameHeight)
faceBoxes.append([x1, y1, x2, y2])
cv2.rectangle(frameOpencvDnn, (x1, y1), (x2, y2),
(0, 255, 0), int(round(frameHeight / 150)), 8)
return frameOpencvDnn, faceBoxes
parser = argparse.ArgumentParser()
parser.add_argument('--image', help='Path to image file', required=True)
args = parser.parse_args()
image = cv2.imread(args.image)
if image is None:
print("Could not read image. Please check the path.")
exit()
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"
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)
resultImg, faceBoxes = highlightFace(faceNet, image)
if not faceBoxes:
print("No face detected")
exit()
for faceBox in faceBoxes:
padding = 20
face = image[max(0, faceBox[1]-padding):min(faceBox[3]+padding,
image.shape[0]-1),
max(0, faceBox[0]-padding):min(faceBox[2]+padding, image.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}')
cv2.putText(resultImg, f'{gender}, {age}', (faceBox[0], faceBox[1]-10),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 255), 2, cv2.LINE_AA)
cv2.imshow("Detecting age and gender", resultImg)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output
.