Coders Packet

Capturing face of a person using Python OpenCV.

By Chereddy Vivek Reddy

In this project, we will learn how to detect the face of a person and save photos of the face on our device using the Python OpenCV library.

Description:

The below python code takes pictures of the person in front of the camera and extracts face and saves images of the face of the person on the device. We give the username as input and finally a folder with username as its title is created in the specified path. We use the OpenCV library for this.

Step-1: Installing the require

Go to the command prompt and use ''pip install opencv-python'' command to install OpenCV.

Follow this link to install python and OpenCV.

 

Code:

Step-2: Importing required libraries.

OpenCV is imported as cv2.

 

Step-3: 

We take the username(name of the person whose face w are capturing) as input and then we send it to the "cap" function as an argument.

 

Step-4:

Cascades help in object detection haarcascade_frontalface is used to capture the face of the person in the entire image calling the required cascade from the file "cascades".

In the following piece of code, we initialise the face cascade which is useful to detect the face of the person in front of the camera. Every line of code is explained in the comments (#.....). A folder with the username as its name is made using mkdir in the specified path.

#defining cap fuction to capture and save images of user face, taking username as argument
def cap(username):
    #cascades help in object detection
    #haarcascade_frontalface is used to capture face of person in the entire image
    #calling the required cascade from the file "cascades"
    face_cascade = cv2.CascadeClassifier('cascades/data/haarcascade_frontalface_alt2.xml')

    #opening camera to capture video
    cam = cv2.VideoCapture(0)

    #making directory with username as its name
    id= username
    #parent directory is specified i.e the directory in which you are creating a file of images
    parent_dir = r"D:\code speedy\images"
    path = os.path.join(parent_dir, id)
    os.mkdir(path)

 

Step-5:

In this piece of python code, we initialise a loop and detect the face and take images of the face until a break statement is executed. Generally, more number of images are saved for more accurate results. The image captured is changed to a grey colour, if the change is not required the line can be omitted. Images are saved with specific names and while detecting faces a rectangular line is visible on-screen covering the portion of the face.

#initially the file is empty so sample number of images = 0
SampleNum=0
#initialising loop
while (1):
    #capturing image
    ret, img=cam.read()
    #converting image into gray color (this line can be omitted if colour change is not required)
    gray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #using face_cascade to detect and extract image of face in the whole image
    faces = face_cascade.detectMultiScale(gray,  minNeighbors=5)

    for (x,y,w,h) in faces :
        #sample number incriment per each image obtained
        SampleNum=SampleNum+1
        #naming and saving image
        cv2.imwrite("images/" + str(id) + "/" + str(id) + '.'+str(SampleNum) + ".jpg", gray[y:y+h,x:x+h])
        #drawing a rectangle around the face
        cv2.rectangle(img,(x,y),(x+y, y+h), (255,0,0), 1)
        cv2.waitKey(100)

 

Step-6:

This python code displays pictures of the face detected and releases cam once 25 pictures are taken and the process gets terminated.

    #displaying image
    cv2.imshow("Face",img)
    cv2.waitKey(1)
    #terminating loop after obtaining 25 sample images
    if SampleNum > 25: break

    cv2.imshow('img',img)
    if cv2.waitKey(1) & 0xFF == ord('q'): break

#releasing camera
cam.release()
cv2.destroyAllWindows()

 

Output:

We give the username as input (here ''Vivek'' is given as input)

Detecting and capturing the face of the user(sorry for the bad camera quality)

A folder with username as the name of the folder is created in the specified path.

Download project

Reviews Report

Submitted by Chereddy Vivek Reddy (Vivek)

Download packets of source code on Coders Packet