Face Detection Using OpenCV & Python

Afaf Athar
Analytics Vidhya
Published in
3 min readJan 14, 2020

The moment you hear of face recognition, you think of surveillance in videos, that you are being watched. Over the last few decades, face recognition has become a popular area of computer vision. Face recognition systems use computer algorithms to pick out specific, distinctive details about a person’s face.

In this post, you will learn how to perform a basic implementation of facial recognition using OpenCV and Python. OpenCV essentially stands for Open Source Computer Vision Library. By the end, you will learn to detect faces in image and video.

Implementation of face Detection using OpenCV:

1. Install OpenCV, if not installed then:

“pip install opencv-python”

2. If you are using PyCharm then:

· Go to File -> Settings

Settings->Project Interpreter

· Then click on Project Interpreter and click “+” icon

Click on + icon on the right corner of the dialog box and add opencv and install

· After “+”, add opencv-python and click to install button at the bottom left.

Face Detection uses haarcascade which is a machine learning approach where cascade function is trained with some set of input data. Opencv has a pre-trained classifier for face, eyes, etc.

Firstly, You need to download an image and the trained classifier XML file (haarcascade_frontalface_default.xml), which is available in OpenCV’s GitHub repository.

To Detect Faces in the image:

Steps to the program:

1. Import cv2

2. Load the cascade (Line 3)

3. Read the input image using imread function(Line 5)

4. Transform an image to grayscale using cvtColor (Line 7)

5. Detect the faces (Line 9)

6. Draw rectangles around the faces (Line11- Line 12)

7. Display the output (Line 14- Line 15)

Points to remember:

· Convert the image into grayscale as the detection works on grayscale only.

· detectMultiScale function is used to detect faces. It takes three input arguments (input image, scale factor, and minNeighbours)

· We draw rectangles in our images where faces were found. Faces contain a list of coordinates for the rectangular regions (where faces are detected).

Output Of face detection in an image:

Likewise, we can detect faces in videos. As we know videos are made up of frames, which are still images.

Given below is the code:

Steps to the program:

  1. Import cv2
  2. Load the cascade (Line 3)

3. Read the Video from webcam using VideoCapture (Line 5)

4. Run a loop to read the image from video and convert video frame to grayscale (Line 12)

5. Detect the faces taking the input(Line 14)

6. Draw rectangles around the faces, specifying the width and color of the rectangle (Line15- Line 17)

7. Run to detect your face as the output (Line 18- Line 24)

Face Recognition in video

The only difference in image and video recognition i.e An infinite loop is used through each frame in the video. We use cap.read() to read each frame, the initial value returned is a flag that indicates if the frame is read correctly or not. The second value returned is the still frame on which we will perform the detection.

I hope this helps!!

For more help, you can visit my Github:- https://github.com/Afaf-Athar/FaceDetection_openCv

--

--

Afaf Athar
Analytics Vidhya

I Do Data. I write what I wish I could have read when I was younger