Face Detection using Open-CV

Face Detection using Open-CVANKIT BHADORIYABlockedUnblockFollowFollowingApr 23This is a simple article on “Face detection “using OpenCV.

OpenCv is the most popular computer vision library.

And today we will be using it to create our simple module for face detection.

Once you successfully implement this module you can advance to my next articles for face recognition.

Before starting with the tutorial this is the list of Prerequisites you would need.

Prerequisites:-· Python (I am using version 3.

6.

4)· OpenCVOnce you have Python, You can simply Do It by typing following command in your command prompt.

pip install opencv-python(This will install the latest version of openCV in your system.

)· The Haar cascade file(Now the Haar Cascade is basically a classifier which is used to detect the object for which it has been trained for.

)We need .

xml file of frontal face Haar-Cascade.

This file along with Couple of more default cascades re provided with the OpenCv package itself.

So to find it follow up:-1.

Open Command prompt in windows /terminal or bash in other Os.

Now assuming that you have already installed python.

Start python in your prompt.

2.

Once the python is started type following to get the location of opencv.

Import cv2And then type following command to get the location .

Print(cv2.

__file__)Copy the location as shown and paste it into the explorer.

Inside the data folder you will find the Haar cascade file for frontal face that we will be usingCopy this file create a new folder on your desktop And paste this file in that folder .

Make sure that your python code and this file are in the same folder.

In this way it will be way simpler to give the path.

· Lets start the code Now:-1.

open python IDLE or any other IDE that you will be using , create a new file in the same folder where you Copied your har cascade.

· Import The libraries:-import cv2import numpy as np.

Detection part:-· Import the Haar Cascadedetector=cv2.

CascadeClassifier('haarcascade_frontalface_default.

xml').

Adding a video capture objectcap=cv2.

VideoCapture(0)The argument (0) in the above line indicates the default web cam you your are using USB-camera you can use the argument (1) it goes on increasing for the no.

of usb cameras connected to system.

· Lets read the Image from the video Capture object.

Which will return a TRUE/FALSE and the capture frame will be shown using imshow().

· This will be a while loop in which we will capture the image convert it to gray scale for face detection.

And then we will apply face detector on the Grayscale image.

Then we will extract the X,Y,W,H parameters from the image.

Where X, and Y are screen coordinates and W=width of the box for face H=height.

while(True):ret, img = cap.

read()gray = cv2.

cvtColor(img, cv2.

COLOR_BGR2GRAY)faces = detector.

detectMultiScale(gray, 1.

3, 5)for (x,y,w,h) in faces:cv2.

rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)once done with this we will show the image with face detector .

and I have added q as a key to break operation.

And waitkey is used for delay.

THE COMPLETE CODE:-import numpy as npimport cv2detect=cv2.

CascadeClassifier('haarcascade_frontalface_default.

xml')c = cv2.

VideoCapture(0)while(True):ret, img = c.

read()gray = cv2.

cvtColor(img, cv2.

COLOR_BGR2GRAY)faces = detect.

detectMultiScale(gray, 1.

3, 5)for (x,y,w,h) in faces:cv2.

rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)cv2.

imshow('frame',img)if cv2.

waitKey(1) & 0xFF == ord('q'):breakc.

release()cv2.

destroyAllWindows()Here are the results.

????Output 1.

Output 2.

I hope this was helpful.

In case you get any problem feel free to post your query I will try my best to solve it .

Keep Learning .

All the best.

.. More details

Leave a Reply