
Face Detection is a technique that identifies human faces in a digital image. Face detection is a relatively mature technology – remember back in the good old days of your digital camera when you looked through the viewfinder? You saw rectangles surrounding the faces of the people in the viewfinder.
Face detection is the technique you need to learn before you can perform face recognition, which is trying to put a name to a face.
For face detection, one of the most famous algorithms is known as the Viola-Jones Face Detection technique, commonly known as Haar cascades. Haar cascades were invented long before deep learning was popular and is one of the most commonly used techniques for detecting faces.
Ethical Considerations for Face Detection/Recognition
While the ability to detect and recognize faces is definitely cool, it certainly has a lot of ethical implications. There are several concerns that you need to take note before you get too ahead of yourself when implementing facial recognition into your projects. Concerns such as privacy (face detection can be used to track people’s movements without their consent), bias (face detection can be biased towards individual of different race, gender, or age), and misuse (faces captured may be used for other illegal uses or malicious purposes). So while this article focuses on the technical capability for face detection, you should consider carefully the moral and ethical implications before you implement it in your work.
Here are some low-risk projects where face detection/recognition can be implemented:
- Attendance tracking – you can use face recognition in schools or workplace to take attendance.
- Personalization – using face recognition to personalize services. A good example is for entertainment services such as recommending specific TV shows based on a user’s watching history.
- Security – use of face recognition to unlock non-critical systems, such as smartphones and computers.
However, using face recognition in certain applications has severe moral implications. Here are some examples:
- Law enforcement – while face recognition can be useful for law enforcement, there are some serious concerns about its inaccuracies and biases.
- Surveillance – face recognition technologies have been used in some countries for monitoring and tracking its citizens, particularly dissidents. Some companies also uses face recognition to monitor employees’ productivity, which is a direct infringement of their privacies.
Here are some articles that you can read to learn more about the legal and moral concerns of face recognition:
- Facial Recognition in the United States: Privacy Concerns and Legal Developments – https://www.asisonline.org/security-management-magazine/monthly-issues/security-technology/archive/2021/december/facial-recognition-in-the-us-privacy-concerns-and-legal-developments/
- Privacy and security issues associated with facial recognition software – https://www.techrepublic.com/article/privacy-and-security-issues-associated-with-facial-recognition-software/
- 10 reasons to be concerned about facial recognition technology – https://www.privacycompliancehub.com/gdpr-resources/10-reasons-to-be-concerned-about-facial-recognition-technology/
How Do Haar Cascades Work?
A Haar cascade classifier is used to detect the object for which it has been trained. If you’re interested in a detailed explanation of the mathematics behind how Haar cascade classifiers work, check out the paper by Paul Viola and Michael Jones at https://www.cs.cmu.edu/~efros/courses/LBMV07/Papers/viola-cvpr-01.pdf.
Here is a high-level overview of how a Haar classifier for face works:
- First, a set of positive images (images of faces) and a set of negative images (images with faces) are used to train the classifier.
- You then extract the features from the images. The following figure shows some of the features that are extracted from images containing faces.

- To detect faces from an image, you look for the presence of the various features that are usually found on human faces (see the following figure), such as the eyebrow, where the region above the eyebrow is lighter than the region below it.

- When an image contains a combination of all these features, it is deemed to contain a face.
Fortunately, without needing to know how Haar cascades work, OpenCV can perform face detection out of the box using a pre-trained Haar cascade, along with other Haar cascades for recognizing other objects. The list of predefined Haar cascades is available on GitHub at https://github.com/opencv/opencv/tree/master/data/haarcascades.
Open Source Computer Vision (OpenCV) is an open source computer vision and machine learning software library originally developed by Intel. It was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products. OpenCV ships with several pre-trained Haar cascades that can detect eyes, faces, Russian car plates, smiles, and more.
For face detection, you’ll need the haarcascade_frontalface_default.xml
file that you can download from the GitHub link in the previous paragraph.
Installing OpenCV
Let’s try out face detection using Opencv. First, you need to use the following command to install it:
!pip install opencv-python
For the example in this article, you need to create a file named face_detection.py. Start by populating it with the following statement to import the OpenCV library:
import cv2
Reading from the Webcam
The next thing would be to connect to your Webcam and display the image on the screen:
import cv2
# default webcam
stream = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
(grabbed, frame) = stream.read()
# Show the frame
cv2.imshow("Image", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"): # Press q to break out of the loop
break
# Cleanup
stream.release()
cv2.waitKey(1)
cv2.destroyAllWindows()
cv2.waitKey(1)
To reference your webcam, use the VideoCapture
class and pass it a number indicating your webcam instance (0
for first webcam, 1
for second webcam and so on):
stream = cv2.VideoCapture(0)
To continuously capture the inputs from the webcam, use an infinite loop (while(True)
) and read each frame and then show it:
# Capture frame-by-frame
(grabbed, frame) = stream.read()
# Show the frame
cv2.imshow("Image", frame)
To allow the program to gracefully exit, wait for the user to press a key on the keyboard. When the "q" key is pressed, the loop terminates:
key = cv2.waitKey(1) & 0xFF
if key == ord("q"): # Press q to break out of the loop
break
You can then perform your cleanup:
# Cleanup
stream.release()
cv2.waitKey(1)
cv2.destroyAllWindows()
cv2.waitKey(1)
To run the program, go to Terminal and type:
$ python face_detection.py
You should now see your face:

Detecting Face
Here comes the fun part – detecting face(s). First, create an instance of the CascadeClassifier
class and pass it the haarcascade_frontalface_default.xml file:
import cv2
# for face detection
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
You need to copy the haarcascade_frontalface_default.xml file and put it in the same folder as the face_detection.py file. You can download the XML file from https://github.com/opencv/opencv/tree/master/data/haarcascades.
You can now use it to detect faces using the detectMultiScale()
function:
while(True):
# Capture frame-by-frame
(grabbed, frame) = stream.read()
#===============DETECTING FACES============
# Convert to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Try to detect faces in the webcam
faces = face_cascade.detectMultiScale(gray,
scaleFactor=1.3,
minNeighbors=5)
# for each faces found
for (x, y, w, h) in faces:
# Draw a rectangle around the face
color = (0, 255, 255) # in BGR
stroke = 5
cv2.rectangle(frame, (x, y), (x + w, y + h),
color, stroke)
#===============DETECTING FACE=============
# Show the frame
cv2.imshow("Image", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"): # Press q to break out of the loop
break
Take note of the the following parameters in the detectMultiScale()
function:
- The
scaleFactor
parameter allows you to rescale the capture image to a new dimension so that the face can be detected by the algorithm. - The
minNeighbors
parameter specifies how many neighbors each candidate rectangle should have to retain it. This parameter affects the quality of the detected faces. Higher value results in less detections but with higher quality. Usually, 4 to 6 is a good number.
You can vary the values of these two parameters to ensure that faces are detected correctly.
When faces are detected, you want to draw rectangles around them:
# for each faces found
for (x, y, w, h) in faces:
# Draw a rectangle around the face
color = (0, 255, 255) # in BGR
stroke = 5
cv2.rectangle(frame, (x, y), (x + w, y + h),
color, stroke)
When you rerun the face_detection.py file again, you should now be able to detect faces:

The entire content for face_detection.py file is as follows:
import cv2
# for face detection
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# default webcam
stream = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
(grabbed, frame) = stream.read()
# Convert to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Try to detect faces in the webcam
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
# for each faces found
for (x, y, w, h) in faces:
# Draw a rectangle around the face
color = (0, 255, 255) # in BGR
stroke = 5
cv2.rectangle(frame, (x, y), (x + w, y + h),
color, stroke)
# Show the frame
cv2.imshow("Image", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"): # Press q to break out of the loop
break
# Cleanup
stream.release()
cv2.waitKey(1)
cv2.destroyAllWindows()
cv2.waitKey(1)
If you like reading my articles and that it helped your career/study, please consider signing up as a Medium member. It is $5 a month, and it gives you unlimited access to all the articles (including mine) on Medium. If you sign up using the following link, I will earn a small commission (at no additional cost to you). Your support means that I will be able to devote more time on writing articles like this.
Summary
I hope this short article provided you with a simple way to detect faces using Python and your webcam. Be sure to download the haar cascades XML file and put it into the same folder as your Python file. In future articles, I will show you some techniques to perform face recognition. Stay tuned!