Age Gender Detection

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

AGE AND GENDER BASED DETECTION

ABSTRACT

First introducing you with the terminologies used in this advanced python
project of gender and age detection –

What is 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, analyzing, and understanding digital images to
extract high-dimensional data from the real world in order 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.

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

In the existing system every photo need to be watched carefully to understand


or classify and bifurcate the images

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.

Gender and Age Detection – About the Project


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.

The CNN Architecture


The convolutional neural network for this python project has 3 convolutional
layers:
Convolutional layer; 96 nodes, kernel size 7
Convolutional layer; 256 nodes, kernel size 5
Convolutional layer; 384 nodes, kernel size 3
It has 2 fully connected layers, each with 512 nodes, and a final output layer of
softmax type.

MODULES
 Detect faces

 Classify into Male/Female

 Classify into one of the 8 age ranges

 Put the results on the image and display it

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.

You can do this with pip-


pip install opencv-python
Other packages you’ll be needing are math and argparse, but those come as part
of the standard Python library.

opencv_face_detector.pbtxt
opencv_face_detector_uint8.pb
age_deploy.prototxt
age_net.caffemodel
gender_deploy.prototxt
gender_net.caffemodel

A few pictures to try the project on


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.

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.

6. Let’s capture video stream in case you’d like to classify on a webcam’s


stream. Set padding to 20.

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.

11. Then, we do the same thing for age.

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.

 Databases − Python provides interfaces to all major commercial


databases.
 GUI Programming − Python supports GUI applications that can be
created and ported to many system calls, libraries and windows systems,
such as Windows MFC, Macintosh, and the X

Window system of Unix.

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

Python 2.4.3 (#1, Nov 11 2010, 13:34:43)

[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>>

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.

Assigning Values to Variables

Python variables do not need explicit declaration to reserve memory space.


The declaration happens automatically when you assign a value to a variable.
The equal sign (=) is used to assign values to 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

counter = 100 # An integer assignment

miles = 1000.0 # A floating point

name = "John" # A string

print counter

print miles

print name

Here, 100, 1000.0 and "John" are the values assigned to counter, miles, and
name variables,

respectively. This produces the following result −

100

1000.0

John

Multiple Assignment

Python allows you to assign a single value to several variables simultaneously.


For example −

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.

Standard Data Types

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.

Python has five standard data types −

Numbers

String

List

Tuple

Dictionary

Python Numbers
Number data types store numeric values. Number objects are created when
you assign a value

to them. For example −

var1 = 1

var2 = 10

You can also delete the reference to a number object by using the del
statement. The syntax of

the del statement is −

del var1[,var2[,var3[....,varN]]]]

You can delete a single object or multiple objects by using the del statement.
For example −

del var

del var_a, var_b

Python supports four different numerical types −

int (signed integers)

long (long integers, they can also be represented in octal and hexadecimal)

float (floating point real values)

complex (complex numbers)

Examples
Here are some examples of numbers −

int long float complex

10 51924361L 0.0 3.14j

100 -0x19323L 15.20 45.j

-786 0122L -21.9 9.322e-36j

080 0xDEFABCECBDAECBFBAEl 32.3+e18 .876j

-0490 535633629843L -90. -.6545+0J

-0x260 -052318172735L -32.54e100 3e+26J

0x69 -4721885298529L 70.2-E12 4.53e-7j

Python allows you to use a lowercase l with long, but it is recommended that
you use only an

uppercase L to avoid confusion with the number 1. Python displays long


integers with an

uppercase L.

A complex number consists of an ordered pair of real floating-point numbers


denoted by x + yj,

where x and y are the real numbers and j is the imaginary unit.

Python Strings

Strings in Python are identified as a contiguous set of characters represented in


the quotation
marks. Python allows for either pairs of single or double quotes. Subsets of
strings can be

taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the
beginning of the

string and working their way from -1 at the end.

The plus (+) sign is the string concatenation operator and the asterisk (*) is the
repetition

operator. For example −

#!/usr/bin/python

str = 'Hello World!'

print str # Prints complete string

print str[0] # Prints first character of the string

print str[2:5] # Prints characters starting from 3rd to 5th

print str[2:] # Prints string starting from 3rd character

print str * 2 # Prints string two times

print str + "TEST" # Prints concatenated string

This will produce the following result −

Hello World!
H

llo

llo World!

Hello World!Hello 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

list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]

tinylist = [123, 'john']


print list # Prints complete list

print list[0] # Prints first element of the list

print list[1:3] # Prints elements starting from 2nd till 4th

print list[2:] # Prints elements starting from 3rd element

print tinylist * 2 # Prints list two times

print list + tinylist # Prints concatenated lists

This produce the following result −

['abcd', 786, 2.23, 'john', 70.200000000000003]

abcd

[786, 2.23]

[2.23, 'john', 70.200000000000003]

[123, 'john', 123, 'john']

['abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john']

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

tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )

tinytuple = (123, 'john')


print tuple # Prints complete list

print tuple[0] # Prints first element of the list

print tuple[1:3] # Prints elements starting from 2nd till 3rd

print tuple[2:] # Prints elements starting from 3rd element

print tinytuple * 2 # Prints list two times

print tuple + tinytuple # Prints concatenated lists

This produce the following result −

('abcd', 786, 2.23, 'john', 70.200000000000003)

abcd

(786, 2.23)

(2.23, 'john', 70.200000000000003)

(123, 'john', 123, 'john')

('abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john')

The following code is invalid with tuple, because we attempted to update a


tuple, which is not allowed. Similar case is possible with lists −

#!/usr/bin/python

tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )

list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]

tuple[2] = 1000 # Invalid syntax with tuple

list[2] = 1000 # Valid syntax with list

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 = {}

dict['one'] = "This is one"

dict[2] = "This is two"

tinydict = {'name': 'john','code':6734, 'dept': 'sales'}

print dict['one'] # Prints value for 'one' key

print dict[2] # Prints value for 2 key

print tinydict # Prints complete dictionary

print tinydict.keys() # Prints all the keys

print tinydict.values() # Prints all the values

This produce the following result −

This is one

This is two

{'dept': 'sales', 'code': 6734, 'name': 'john'}

['dept', 'code', 'name']

['sales', 6734, 'john']

Dictionaries have no concept of order among elements. It is incorrect to say


that the elements

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

• CSV (Comma Separated Values) is a simple file format used to store


tabular data, such as a spreadsheet or database. A CSV file stores tabular
data (numbers and text) in plain text. Each line of the file is a data
record. Each record consists of one or more fields, separated by
commas. The use of the comma as a field separator is the source of the
name for this file format.

Algorithm
Source code
import cv2
import math
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], True, 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')

##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"

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(args.image if args.image else 0)


#video=cv2.VideoCapture(args if args else 0)
video=cv2.VideoCapture(0)
padding=20
while cv2.waitKey(1)<0:
hasFrame,frame=video.read()
if not hasFrame:
cv2.waitKey()
break
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')

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)
Screen shots
Conclusion
In this python project, we implemented a CNN to detect gender and age from a
single picture of a face. Though many previous methods have addressed the
problems of age and gender classification, until recently, much of this work has
focused on constrained images taken in lab settings. Such settings do not
adequately reflect appearance variations common to the real-world images in
social websites and online repositories.

You might also like