Picture Perfect: Face Recognition Without Machine Learning [Full Code]

Can you even do face recognition without machine learning?

Well, the answer depends on how you define “machine learning.”

If we define machine learning as machine-learned rules crafted from training data, then the answer is yes – we won’t need any of our own training data – and in this blog post, we will show you how! 

In this tutorial, we will use a library called OpenCV to detect faces in pictures.

This package comes with some XML files that define rules for our classifiers. We will load these into our classifier and use those to build our face recognition system.

We will be doing no training and won’t need any training data!

face recognition with opencv

How To Code A Face Recognition Algorithm Without Machine Learning

The code in its current format will use your computer’s camera and perform face recognition in real time!

This code can be converted for non-live feeds to be used with stored videos, images, and any other form of media with a face in it!

# import our packages
import pathlib
import cv2

# path to our xmls that hold the rules for our classifier
# we need these for our face recognition
xml = pathlib.Path(cv2.__file__).parent.absolute() / "data/haarcascade_frontalface_default.xml"

# initialize our classifier
classifier = cv2.CascadeClassifier(str(xml))

# I only have one camera, so I pass a zero here
camera = cv2.VideoCapture(0)

# run an "forever" loop, we create keys to break it at the end
while True:
    # we get some useless feedback, which is why we use _
    # we really only care about frame
    _, frame = camera.read()
    # lets convert what our camera reads in (in the backend)
    # to gray
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # lets try and detect
    is_face = classifier.detectMultiScale(
        # pass in our gray image
        # scale these images (from docs for double)
        # how strict we are with what we see 
        # higher the number, less faces you'll detect
        # anything smaller than 30,30 will be ignored
        # Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects. 
        # It is not used for a new cascade.
    # for each identified face (stored in is_face)
    for (x, y, width, height) in is_face:
        # lets plot a rectangle
        # BGR Color Scheme
        cv2.rectangle(frame, (x,y), (x+width, y+height), (125, 125, 0), 2)
    cv2.imshow("Detected Faces", frame)
    # how to quit
    # if we press x break out (switch to any key you like)
    if cv2.waitKey(1) == ord("x"):

# once we've quit, we need to release everything

Output From My Camera! It Found My Face!!

Why You Would Want To Build A Face Recognition Algorithm Without Machine Learning

It’s no secret that generating training data for face recognition is difficult and expensive.

You need access to many different faces at different angles to create a diverse dataset to train a highly accurate model.

And even then, the dataset will only be as good as the labels assigned to each face!

This is where CV2 comes in.

With only a few lines of code, CV2 can be used to build models that are much more accurate than those produced using traditional methods.

This is because CV2 comes with XML files with all the rules and information we need to start quickly.

Instead of spending hours and tons of money trying to train and build your own Convolutional Neural Network, why not leverage the free-to-use technology that already exists?

Using OpenCV results in a much better generalizable model from a small amount of data that comes with the package installation!

So if you’re looking to build a face recognition system, don’t waste your time with traditional methods.

Leverage CV2 and its stored XML files to build an accurate and efficient face recognition system!

Other Articles In Our “Without” Series

Here at Enjoymachinelearning.com, we’re exploring how things are interconnected. We have other articles in our “Without” series where we explore the technical differences between some of the common jargon within the data science and machine learning realm.

A few more of those articles are here:

Stewart Kaplan