How to do Face Detection using OpenCV

How to do Face Detection using OpenCV

What Do I Need?

  • Any Dedicated or Virtual Server
  • Ubuntu
  • OpenCV
  • Python

What Is Face Detection?

OpenCV uses machine learning algorithms to search for faces within a picture. Because faces are so complicated, there isn’t one simple test that will tell you if it found a face or not. Instead, there are thousands of small patterns and features that must be matched. The algorithms break the task of identifying the face into thousands of smaller, bite-sized tasks, each of which is easy to solve. These tasks are also called classifiers.

For something like a face, you might have 6,000 or more classifiers, all of which must match for a face to be detected, within error limits, of course. But therein lies the problem: for face detection, the algorithm starts at the top left of a picture and moves down across small blocks of data, looking at each block, constantly asking, ‘Is this a face? … Is this a face? … Is this a face?’ Since there are 6,000 or more tests per block, you might have millions of calculations to do, which will grind your computer to a halt. To get around this, OpenCV uses cascades. What’s a cascade? The best answer can be found in the dictionary: ‘a waterfall or series of waterfalls.’

Like a series of waterfalls, the OpenCV cascade breaks the problem of detecting faces into multiple stages. For each block, it does a very rough and quick test. If that passes, it does a slightly more detailed test, and so on. The algorithm may have 30 to 50 of these stages or cascades, and it will only detect a face if all stages pass.

The advantage is that the majority of the picture will return a negative during the first few stages, which means the algorithm won’t waste time testing all 6,000 features on it. Instead of taking hours, face detection can now be done in real time. Though the theory may sound complicated, in practice it is quite easy. The cascades themselves are just a bunch of XML files that contain OpenCV data used to detect objects. You initialize your code with the cascade you want, and then it does the work for you.

Since face detection is such a common case, OpenCV comes with a number of built-in cascades for detecting everything from faces to eyes to hands to legs.

faceRecognition.py

strong>import cv2

import

 sys
# Get user supplied values
imagePath = sys.argv[1]
cascPath = "haarcascade_frontalface_default.xml"
# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)
# Read the image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags = cv2.CASCADE_SCALE_IMAGE
)
print("Found {0} faces!".format(len(faces)))
# Draw a rectangle around the faces

for

 (x, y, w, h)

in

 faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow("Faces found", image)
cv2.waitKey(0)

In order to get started, use the following command:

python3 faceRecognition.py group-of-people-of-mixed-ethnicity.jpg 
haarcascade_frontalface_default.xml

Followed by the following commands if using the images provided as part of the linked download pack:

python3 faceRecognition.py crowd-of-white-people.jpg haarcascade_frontalface_default.xml
python3 faceRecognition.py crowd-of-black-

and

-white-people.jpg haarcascade_frontalface_default.xml

When wanting to change to a different image for analysis ensure that you change line 5 of faceRecognition.py:

imagePath = "insert-name-of-image.jpg"

Source Images
How to do Face Detection using OpenCV

How to do Face Detection using OpenCV

How to do Face Detection using OpenCV

One of the things you’ll immediately notice is that there is a degree of error seen in these images. Although it managed to identify most of the faces in the photos, it also managed to misclassify a number of objects, therefore leading to erroneous object classification. By altering the scaleFactor parameter you can tweak the accuracy of the recognition matching.

Facial Recognition Script Output

default-source1.21.3-1.4
How to do Face Detection using OpenCVHow to do Face Detection using OpenCVHow to do Face Detection using OpenCV
How to do Face Detection using OpenCVHow to do Face Detection using OpenCVHow to do Face Detection using OpenCV
How to do Face Detection using OpenCVHow to do Face Detection using OpenCVHow to do Face Detection using OpenCV

Conclusion

Facial recognition is one of the most common uses for computer vision, particularly OpenCV. Building platforms using the facial recognition algorithms utilized in OpenCV is quick and efficient, however, it has to be remembered that you’ll be dealing with a lot of embedded bias. This is largely due to the training sets that were used to train the facial recognition algorithms. It can be, however, mitigated, but it takes time and patience to fix. Whenever building an application or algorithm for computer vision, particularly facial recognition, always first consider the ramifications of the technologies that you’re developing. Consider the consequences of the negative usage of it before building it. After all, once Pandora’s Box it’s opened, it can never be closed.

Download Face Detection Scripts and Images

How To Set up a VSFTPD Server on a CentOS 7 VPS or Dedicated Server

Brief Description FTP is usually insecure exposing clear-text passwords, userna
2 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

How To Set up a VSFTPD Server on an Ubuntu 16.04 VPS or Dedicated Server

Brief Description FTP data is usually insecure since information (usernames, pa
2 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

How to use phpMyAdmin to develop a website (without MySQL experience)

Brief description A web developer who is not well versed into coding websites f
2 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Install MySQL on a Windows Web Server Running Apache

This tutorial will show you how to install the MySQL database on a Windows serve
3 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top