real time face detection using python 2020

Real time Face Detection using OpenCV & Python



In this post I am sharing a very quick and easy way to detect multiple faces using HaarCascade_frontalface in OpenCV and Python.

Install openCV module if you haven't install it using the code below

pip install opencv-python

If u have already installed openCV you can check the version of installed openCV module by using the code below

cv2.__version__

Face detection utilizing HaarCascade is an AI based approach where a set of data is trained and prepared with a lot of information. OpenCV as of now contains numerous pre-prepared classifiers for face, eyes, grins, and so on Today we will utilize the face classifier. You can explore different avenues regarding different classifiers also.

First you need to download the trained classifier XML data file (haarcascade_frontalface_default.xml), which is also available in OpenCv’s GitHub repository.

Download: haarcascade_frontalface_default.xml

Save this file in your working directory

Now make a .py file by any name you preferred and start writting following code

import cv2
import sys

cascPath = sys.argv[0]
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

video_capture = cv2.VideoCapture(0)

while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.CASCADE_SCALE_IMAGE
    )

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

    # Display the resulting frame
    cv2.imshow('Video', frame)

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

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()

We use cv2.CascadeClassifier.detectMultiScale() to find faces or eyes, Where the parameters are:

image : Matrix of the type CV_8U containing an image where objects are detected. 

scaleFactor : Parameter specifying how much the image size is reduced at each image scale. ImageScale.png Picture source: Viola-Jones Face Detection This scale factor is used to create scale pyramid as shown in the picture. Suppose, the scale factor is 1.03, it means we're using a small step for resizing, i.e. reduce size by 3 %, we increase the chance of a matching size with the model for detection is found, while it's expensive. 

minNeighbors : Parameter specifying how many neighbors each candidate rectangle should have to retain it. This parameter will affect the quality of the detected faces: higher value results in less detections but with higher quality. We're using 5 in the code. 

flags : Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects. It is not used for a new cascade.

minSize : Minimum possible object size. Objects smaller than that are ignored. 

maxSize : Maximum possible object size. Objects larger than that are ignored.

For any queries ask in the comment section. Thank you.

Live tutorial:



Post a Comment

1 Comments

Recent Posts