Face Detection model on Image/Webcam/Video using Machine Learning OpenCV

Face detection is a computer technology that is being applied for many different applications that require the identification of human faces in digital images or video. It can be regarded as a specific case of object-class detection, where the task is to find the locations and sizes of all objects in an image that belongs to a given class. The technology is able to detect frontal or near-frontal faces in a photo, regardless of orientation, lighting conditions or skin color .

Not Face Recognition! It’s about Detection.

Face recognition describes a biometric technology that goes way beyond recognizing when a human face is present. It actually attempts to establish whose face it is. In this article, I’m not going deep into recognizing. I’ll keep that for a future blog article and for the time being, I’m going to explain how to run a simple Face  Detection program using your WebCam with Python Or we can run simple program on image as well .

We are dealing with below Cascade models for face  detection :

  • haarcascade_frontalface_default.xml ( Model of face detection using Haar Feature-based Cascade Classifiers)

What is Haar Cascades?

Object Detection using Haar feature-based cascade classifiers is an effective object detection method proposed by Paul Viola and Michael Jones in their paper, “Rapid Object Detection using a Boosted Cascade of Simple Features” in 2001.


I recommend you to save that Pdf and read it when you have a chance if you plan to stay in this face detection/recognition plus machine learning rock star! In this paper, they have introduced the concept of Cascade of Classifiers. Haar-cascade Detection in OpenCV

Haar-cascade Detection in OpenCV


OpenCV comes with a trainer as well as a detector. If you want to train your own classifier for any object like a car, planes, etc. you can use OpenCV to create one. Its full details are given here: Cascade Classifier Training.

Here we will deal with detection. OpenCV already contains many pre-trained classifiers for face, eyes, smile, etc. Those XML files are stored in opencv/data/haarcascades/ Folder. Let’s create a face detector with OpenCV.

Let’s CODE!

First, we need to load the required XML classifiers.
import cv2faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
This loads the face cascade into memory so it’s ready for use. Remember, the cascade is just an XML file that contains the data to detect faces.
video_capture = cv2.VideoCapture(0) // For Webcam // Give path of Video file if you want face detection in video . You just need to have ffmpeg library installedOR image = cv2.imread(imagePath) // For Image

This line sets the video source to the default webcam, which OpenCV can easily capture.
while True:
#Capture frame-by-frame
ret, frame = video_capture.read()

    In here, We’re capturing the video. The read() method reads one frame from the video source which is the Webcam and it returns:

  1. The actual video frame read (one frame on each loop)
  2. A return code

If we run out of frames by any chance, The return code will tell us but in our scenario, We are not reading from a file it’s our own Webcam so there’s no possibility for us to run of out of frames.
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

We just converted our Webcam feed to Grayscale. ( Most of the operations in OpenCV are done in grayscale.)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.5,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)

Here’s when our code detects faces in our frame. Let’s drill down from each of the above.
  1. The detectMultiScale function is a general function that detects objects. It detects faces since we’re using it on the face cascade.
  2. The first option is the grayscale image.
  3. The second is the scaleFactor. Since some faces may be closer to the camera, they would appear bigger than the faces in the back. The scale factor compensates for this. (You can change it to 1.5)
  4. The detection algorithm uses a moving window to detect objects. minNeighbors defines how many objects are detected near the current one before it declares the face found. minSize, meanwhile, gives the size of each window.

The function returns a list of rectangles in which it believes it found a face. Next, we will loop over where it thinks it found something.
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
  • This function returns 4 values: the x and y location of the rectangle, and the rectangle’s width and height (w , h).
  • We use these values to draw a rectangle using the built-in rectangle() function.
cv2.imshow('FaceDetection', frame)


This is the frame name (Kind of similar to the title of the Webcam popup)


Now, I want to take a photo of my frame by Pressing the Space bar and I need to exit my program by clicking ‘ESC’.
#ESC Pressed
if k%256 == 27:
break
#SPACE pressed
elif k%256 == 32:
img_name = "facedetect_webcam_{}.png".format(img_counter)
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
img_counter += 1

img_counter is a variable to count and increment after each photo was taken. We should declare this before declaring our While loop on the beginning (While=True)



To Clean everything up;
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()


You can save above code lines in a file and place haarcascade_frontalface_default.xml in current folder .
Run using python <filename> 
You will get to see output like :






You can access Git Repo using
 full code+XML file here For WebCam
OR 
full code + XML file for Image


You can access Youtube Video here:

OR


















Comments