Life2Coding
Face Detection on Videos using OpenCV Haar Cascades

This post will be helpful in learning OpenCV using Python programming. Here I will show how to implement OpenCV functions and apply it in various aspects using some examples. Then the output will be shown with some comparisons as well.

Requirements:

First, you need to setup your Python Environment with OpenCV. You can easily do it by following Life2Coding’s tutorial on YouTube: Linking OpenCV 3 with Python 3

Goals:

In this tutorial, I will show you how to detect the faces on webcam videos or normal videos using OpenCV Haar Cascades and Python coding.

Documentation:

Python: cv2.CascadeClassifier.detectMultiScale(image[, scaleFactor[, minNeighbors[, flags[, minSize[, maxSize]]]]])→ objects

Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles

Parameters:          

  • cascade – Haar classifier cascade (OpenCV 1.x API only). It can be loaded from XML or YAML file using Load(). When the cascade is not needed anymore, release it using cvReleaseHaarClassifierCascade(&cascade).
  • image – Matrix of the type CV_8U containing an image where objects are detected.
  • objects – Vector of rectangles where each rectangle contains the detected object, the rectangles may be partially outside the original image.
  • numDetections – Vector of detection numbers for the corresponding objects. An object’s number of detections is the number of neighboring positively classified rectangles that were joined together to form the object.
  • scaleFactor – Parameter specifying how much the image size is reduced at each image scale.
  • minNeighbors – Parameter specifying how many neighbors each candidate rectangle should have to retain it.
  • 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.

Steps:

  • Initialize webcam feed using cv2.VideoCapture()
  • Read webcam images using cv2.VideoCapture. read()
  • Load the Face Haar Cascade file using cv2.CascadeClassifier()
  • Detects the face on the image using cv2.CascadeClassifier.detectMultiScale()
  • Draw boundary around the face using cv2.rectangle()
  • Display each frames of the video using cv2.imshow()
  • Wait for keyboard button press using cv2.waitKey()
  • Release the VideoCapture using cv2.VideoCapture.release()
  • Exit window and destroy all windows using cv2.destroyAllWindows()

Example Code:

import cv2

scale_factor = 1.2
min_neighbors = 3
min_size = (50, 50)
webcam=True #if working with video file then make it 'False'

def detect(path):

    cascade = cv2.CascadeClassifier(path)
    if webcam:
        video_cap = cv2.VideoCapture(1) # use 0,1,2..depanding on your webcam
    else:
        video_cap = cv2.VideoCapture("videoFile.mp4")
    while True:
        # Capture frame-by-frame
        ret, img = video_cap.read()

        #converting to gray image for faster video processing
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        rects = cascade.detectMultiScale(gray, scaleFactor=scale_factor, minNeighbors=min_neighbors,
                                         minSize=min_size)
        # if at least 1 face detected
        if len(rects) >= 0:
            # Draw a rectangle around the faces
            for (x, y, w, h) in rects:
                cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)

            # Display the resulting frame
            cv2.imshow('Face Detection on Video', img)
            #wait for 'c' to close the application
            if cv2.waitKey(1) & 0xFF == ord('c'):
                break
    video_cap.release()

def main():
    cascadeFilePath="haarcascade_frontalface_alt.xml"
    detect(cascadeFilePath)
    cv2.destroyAllWindows()


if __name__ == "__main__":
    main()

Output:

videoFaceDetection Face Detection on Videos using OpenCV Haar Cascades

life2coding_icon [] Face Detection on Videos using OpenCV Haar Cascades

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.