How to Build a Webcam Face Detector Using OpenCV & Python
On , by
Computer vision, the sub-division of artificial intelligence concerned with enabling computers to ‘see’, is one of the hottest areas of AI research in the world today. In digital terms, images are, of course, nothing but a set of pixel intensities organized in a multi-dimensional array of values. For conventional reasons, these pixel intensity values are in the range 0 to 255. Computer vision technology hence, essentially enables machines to understand, manipulate and extract useful information from images, simply by performing operations on their pixel intensities.
Computer Vision has become one of the hottest areas of Artificial Intelligence research.
OpenCV (short for Open-Source Computer Vision Library) is an open-source library available to Python users for image processing and computer vision tasks. The value of OpenCV is two-fold: firstly, being open-source, it is available to every computer vision practitioner free-of-cost, and is being continually improved and updated by a passionate developer community. Secondly, because of the package’s C/C++ development background, it is highly optimized for real-time speed and efficiency, meaning it is possible to obtain very good performance using OpenCV even on less powerful systems. The library is supported by Windows/MacOS/Linux and has bindings available for C++/Java/Python, making it the standard computer vision library of choice for developers all over the world.
OpenCV is the computer vision library of choice for developers all over the world.
This short post will go into how to use OpenCV’s black-box functions to build your own Face Detector using nothing more than the laptop’s web camera. The face detector is surprisingly accurate, and can even detect faces in pictures shown to the webcam, such as faces on photographs and ID cards.
First, you’ll need to ensure you have a version of Python 3 (preferably Python 3.6+), and install OpenCV. Simply go to your Terminal (Command Line) and type:
pip install opencv-python
Then, open up a Python 3 session, and try:
If this works and does not throw an error, that means OpenCV is installed and you can go ahead with building the application.
Building The Face Detector Application
This face detector utilizes an XML file written by OpenCV, known as a Haar Cascade. A Haar Cascade is a classifier that is used to identify specific objects from a source image. The particular XML file used for this application: “Haar Cascade: Frontal Face Default“, is used to detect frontal faces. It is open-source and can be downloaded from the OpenCV GitHub Repository. This Haar Cascade XML needs to be present in the same directory as the Python script being written for the application.
An example of a frontal-direction face, which can easily be detected with the Haar Cascades made available by OpenCV.
Moving ahead with the code for the application:
import cv2; # Using the Haar Cascade from OpenCV face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml');
Now, in order to capture the webcam’s video feed, OpenCV has a function called cv2.VideoCapture():
video = cv2.VideoCapture(0);
Now, the operations being performed on the frame captured by the webcam need to repeatedly be performed on every single frame being captured by the video feed. This needs to happen until the system detects a request to break this continuous loop, at which point, the loop is exited and video capture can be released to shut down the webcam. That calls for a While loop:
Extracting the frame from the video feed can be done with:
check, frame = video.read();
Now, in order to apply the face detection algorithm to the frame from the video feed:
faces = face_cascade.detectMultiScale(frame, scaleFactor = 1.1, minNeighbors = 5);
The scaleFactor argument in the function above is an operation that diminishes the image by a percentage related to the value provided, and repeats the search for a face then. It does this repeatedly, in order to improve the likelihood of detecting facial features. A value of 1.1, for example, is a 10% reduction in the size of the image.
Once the machine has detected a facial feature, we would like to draw a green rectangular bounding box around the facial area. The faces variable stores information about the detected face in terms of its upper left-hand corner, and the width and height of the detected face. So:
for x,y,w,h in faces: frame = cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3);
The actual display of this image needs to be created by using OpenCV’s cv2.imshow() function:
cv2.imshow('Face Detector', frame);
OpenCV, by default, does not continually keep this display open. It would be ideal to ask the system to keep this display open and have it wait for a key-press to close the display. Hence:
key = cv2.waitKey(1);
And now, to code for the key that will close the display, we will use the ‘q’ key:
if key == ord('q'): break;
On detecting the ‘q’ key being pressed while the OpenCV window is active, the window will close and the continuous while loop is broken out of. Once this happens, outside the code for the while loop, we can write code to ask the system to release the video capture mechanism (shut down the webcam) and have OpenCV close all open windows. So:
So the final Python script is as below:
import cv2; face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml'); video = cv2.VideoCapture(0); while True: check, frame = video.read(); faces = face_cascade.detectMultiScale(frame, scaleFactor=1.1, minNeighbors=5); for x,y,w,h in faces: frame = cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3); cv2.imshow('Face Detector', frame); key = cv2.waitKey(1); if key == ord('q'): break; video.release(); cv2.destroyAllWindows();
Running this Python script shows an application that can easily detect frontal faces by drawing green bounding boxes around that specific area:
The application is also accurate enough to detect minuscule faces on cards and other photographs:
Et voilá! That’s all you require to create your own facial detection application! Through the power of open-source libraries and object-oriented programming, Python makes it possible to create an application such as this in less than 25 lines of code. This face detector is a good place to start to eventually create more complex applications such as Object Detection/Segmentation or even Facial Recognition Software, which can identify people based on their faces if trained with good quality data. Computer Vision has revolutionized the world of AI, and the tools are all available online for any interested developer to start DIY or business projects of their own.