Age Gender Detection
Age Gender Detection
Age Gender Detection
ABSTRACT
First introducing you with the terminologies used in this advanced python
project of gender and age detection –
What is 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.
What is a 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
perceptrons.
Existing System
Proposed System
Gender and Age Detection Python Project- Objective
To build a gender and age detector that can approximately guess the gender and
age of the person (face) in a picture using Deep Learning on the Adience
dataset.
MODULES
Detect faces
The Dataset
For this python project, we’ll use the Adience dataset; the dataset is available in
the public domain and you can find it here. This dataset serves as a benchmark
for face photos and is inclusive of various real-world imaging conditions like
noise, lighting, pose, and appearance. The images have been collected from
Flickr albums and distributed under the Creative Commons (CC) license. It has
a total of 26,580 photos of 2,284 subjects in eight age ranges (as mentioned
above) and is about 1GB in size. The models we will use have been trained on
this dataset.
Prerequisites
You’ll need to install OpenCV (cv2) to be able to run this project.
opencv_face_detector.pbtxt
opencv_face_detector_uint8.pb
age_deploy.prototxt
age_net.caffemodel
gender_deploy.prototxt
gender_net.caffemodel
2. 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.
3. For face, age, and gender, initialize protocol buffer and model.
4. Initialize the mean values for the model and the lists of age ranges and
genders to classify from.
5. Now, use the readNet() method to load the networks. The first parameter
holds trained weights and the second carries network configuration.
7. 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.
8. 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.
Create a blob from the shallow copy.
Set the input and make a forward pass to the network.
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.
9. 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.
10. 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.
12. We’ll add the gender and age texts to the resulting image and display it with
imshow().
Hardware Requirement: -
i3 Processor Based Computer
4GB-Ram
250 GB Hard Disk
Internet Connection
Software Requirement:
Windows 8 or higher
PYTHON INTERPRETER 3.6 ONWARDS
PYCHARM OR PYTHON IDLE
OPEN-CV PACKAGE
TECHNOLOGIES USED
PYTHON LANGUAGE
Python is a general-purpose interpreted, interactive, object-oriented, and high-
level programming language. It was created by Guido van Rossum during 1985-
1990. Like Perl, source code is also available under the GNU General Public
License (GPL). This tutorial gives enough understanding on Python
programming language.
Python is a high-level, interpreted, interactive and object-oriented scripting
language. Python is designed to be highly readable. It uses English keywords
frequently where as other languages use punctuation, and it has fewer
syntactical constructions than other languages.
Python is Interpreted − Python is processed at runtime by the interpreter. You
do not need to compile your program before executing it. This is similar to
PERL and PHP.
Python is Interactive − You can actually sit at a Python prompt and interact
with the interpreter directly to write your programs.
Python is Object-Oriented − Python supports Object-Oriented style or
technique of programming that encapsulates code within objects.
Python is a Beginner's Language − Python is a great language for the beginner-
level programmers and supports the development of a wide range of
applications from simple text processing to WWW browsers to games.
History of Python
Python was developed by Guido van Rossum in the late eighties and early
nineties at the National Research Institute for Mathematics and Computer
Science in the Netherlands.
Python is derived from many other languages, including ABC, Modula-3, C, C++,
Algol-68, SmallTalk, and Unix shell and other scripting languages.
Python is copyrighted. Like Perl, Python source code is now available under the
GNU General Public License (GPL).
Python is now maintained by a core development team at the institute,
although Guido van Rossum still holds a vital role in directing its progress.
Python Features
Python's features include −
Easy-to-learn − Python has few keywords, simple structure, and a clearly
defined syntax. This allows the student to pick up the language quickly.
Easy-to-read − Python code is more clearly defined and visible to the
eyes.
Easy-to-maintain − Python's source code is fairly easy-to-maintain.
A broad standard library − Python's bulk of the library is very portable
and cross-platform compatible on UNIX, Windows, and Macintosh.
Interactive Mode − Python has support for an interactive mode which
allows interactive testing and debugging of snippets of code.
Portable − Python can run on a wide variety of hardware platforms and
has the same interface on all platforms.
Extendable − You can add low-level modules to the Python interpreter.
These modules enable programmers to add to or customize their tools
to be more efficient.
Scalable − Python provides a better structure and support for large programs
than shell scripting.
Apart from the above-mentioned features, Python has a big list of good
features, few are listed below −
It supports functional and structured programming methods as well as OOP.
It can be used as a scripting language or can be compiled to byte-code for building
large applications.
It provides very high-level dynamic data types and supports dynamic type checking.
IT supports automatic garbage collection.
It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
$ python
>>>
Variables are nothing but reserved memory locations to store values. This
means that when you create a variable you reserve some space in
memory.Based on the data type of a variable, the interpreter allocates
memory and decides what can be
stored in the reserved memory. Therefore, by assigning different data types to
variables, you can store integers, decimals or characters in these variables.
The operand to the left of the = operator is the name of the variable and the
operand to the right of the = operator is the value stored in the variable. For
example −
#!/usr/bin/python
print counter
print miles
print name
Here, 100, 1000.0 and "John" are the values assigned to counter, miles, and
name variables,
100
1000.0
John
Multiple Assignment
a=b=c=1
Here, an integer object is created with the value 1, and all three variables are
assigned to the
same memory location. You can also assign multiple objects to multiple
variables. For example
a,b,c = 1,2,"john"
Here, two integer objects with values 1 and 2 are assigned to variables a and b
respectively, and one string object with the value "john" is assigned to the
variable c.
The data stored in memory can be of many types. For example, a person's age
is stored as a numeric value and his or her address is stored as alphanumeric
characters. Python has various
standard data types that are used to define the operations possible on them
and the storage method for each of them.
Numbers
String
List
Tuple
Dictionary
Python Numbers
Number data types store numeric values. Number objects are created when
you assign a value
var1 = 1
var2 = 10
You can also delete the reference to a number object by using the del
statement. The syntax of
del var1[,var2[,var3[....,varN]]]]
You can delete a single object or multiple objects by using the del statement.
For example −
del var
long (long integers, they can also be represented in octal and hexadecimal)
Examples
Here are some examples of numbers −
Python allows you to use a lowercase l with long, but it is recommended that
you use only an
uppercase L.
where x and y are the real numbers and j is the imaginary unit.
Python Strings
taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the
beginning of the
The plus (+) sign is the string concatenation operator and the asterisk (*) is the
repetition
#!/usr/bin/python
Hello World!
H
llo
llo World!
Hello World!TEST
Python Lists
Lists are the most versatile of Python's compound data types. A list contains
items separated
by commas and enclosed within square brackets ([]). To some extent, lists are
similar to arrays
in C. One difference between them is that all the items belonging to a list can
be of different
data type.
The values stored in a list can be accessed using the slice operator ([ ] and [:])
with indexes
starting at 0 in the beginning of the list and working their way to end -1. The
plus (+) sign is the
list concatenation operator, and the asterisk (*) is the repetition operator. For
example −
#!/usr/bin/python
abcd
[786, 2.23]
Python Tuples
A tuple is another sequence data type that is similar to the list. A tuple consists
of a number of values separated by commas. Unlike lists, however, tuples are
enclosed within parentheses.
The main differences between lists and tuples are: Lists are enclosed in
brackets ( [ ] ) and their elements and size can be changed, while tuples are
enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of
as read-only lists. For example −
#!/usr/bin/python
abcd
(786, 2.23)
#!/usr/bin/python
Python Dictionary
Python's dictionaries are kind of hash table type. They work like associative
arrays or hashes found in Perl and consist of key-value pairs. A dictionary key
can be almost any Python type, but are usually numbers or strings. Values, on
the other hand, can be any arbitrary Python object.
Dictionaries are enclosed by curly braces ({ }) and values can be assigned and
accessed using square braces ([]). For example −
#!/usr/bin/python
dict = {}
This is one
This is two
NUMPY
• NumPy stands for Numerical Python and is one of the most useful
scientific libraries in Python programming. It provides support for large
multidimensional array objects and various tools to work with
them. Various other libraries like Pandas, Matplotlib, and Scikit-
learn are built on top of this amazing library.
PANDAS
Pandas is an open-source library that is built on top of NumPy library. It
is a Python package that offers various data structures and operations for
manipulating numerical data and time series. It is mainly popular for importing
and analyzing data much easier. Pandas is fast and it has high-performance &
productivity for users.
Advantages
Fast and efficient for manipulating and analyzing data.
Data from different file objects can be loaded.
Easy handling of missing data (represented as NaN) in floating point as
well as non-floating point data
Size mutability: columns can be inserted and deleted from DataFrame
and higher dimensional objects
Data set merging and joining.
Flexible reshaping and pivoting of data sets
Provides time-series functionality.
Powerful group by functionality for performing split-apply-combine
operations on data sets.
Pandas generally provide two data structures for manipulating data,
They are:
Series,DataFrame
Series:
Pandas Series is a one-dimensional labelled array capable of holding data of
any type (integer, string, float, python objects, etc.). The axis labels are
collectively called indexes. Pandas Series is nothing but a column in an excel
sheet. Labels need not be unique but must be a hashable type. The object
supports both integer and label-based indexing and provides a host of
methods for performing operations involving the index.
DataFrame
Pandas DataFrame is a two-dimensional size-mutable, potentially
heterogeneous tabular data structure with labeled axes (rows and columns). A
Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular
fashion in rows and columns. Pandas DataFrame consists of three principal
components, the data, rows, and columns.
A Data frame is a two-dimensional data structure, i.e., data is aligned in a
tabular fashion in rows and columns. We can perform basic operations on
rows/columns like selecting, deleting, adding, and renaming.
Column Selection: In Order to select a column in Pandas DataFrame, we can
either access the columns by calling them by their columns name.
HANDLING CSV-FILES
Algorithm
Source code
import cv2
import math
import argparse
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')
##args=parser.parse_args()
#args = input('Enter file name : ')
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)
ageNet.setInput(blob)
agePreds=ageNet.forward()
age=ageList[agePreds[0].argmax()]
print(f'Age: {age[1:-1]} years')