Skip to main content

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

Popular posts from this blog

Long-Polling vs WebSockets vs Server-Sent Events

Long-Polling vs WebSockets vs Server-Sent Events  Long-Polling, WebSockets, and Server-Sent Events are popular communication protocols between a client like a web browser and a web server. First, let’s start with understanding what a standard HTTP web request looks like. Following are a sequence of events for regular HTTP request: Client opens a connection and requests data from the server. The server calculates the response. The server sends the response back to the client on the opened request. HTTP Protocol Ajax Polling Polling is a standard technique used by the vast majority of AJAX applications. The basic idea is that the client repeatedly polls (or requests) a server for data. The client makes a request and waits for the server to respond with data. If no data is available, an empty response is returned. Client opens a connection and requests data from the server using regular HTTP. The requested webpage sends requests to the server at

MonoLithic Vs Microservice Architecture | which Architecture should i choose ?

From last few years ,microservices are an accelerating trend . Indeed, microservices approach offers tangible benefits including an increase in scalability, flexibility, agility, and other significant advantages. Netflix, Google, Amazon, and other tech leaders have successfully switched from monolithic architecture to microservices. Meanwhile, many companies consider following this example as the most efficient way for business growth. On the contrary, the monolithic approach is a default model for creating a software application. Still, its trend is going down because building a monolithic application poses a number of challenges associated with handling a huge code base, adopting a new technology, scaling, deployment, implementing new changes and others. So is the monolithic approach outdated and should be left in the past? And is it worth shifting the whole application from a monolith to microservices ? Will developing a microservices application help you reach you