Learn how you can become a Python programmer in just 12 weeks.

    We respect your privacy. Unsubscribe at anytime.

    Batch Process Face Detection in 3 Steps with OpenCV

    What will you learn?

    You want to extract or identify faces on a bunch of images, but how do you do that without becoming a Machine Learning expert?

    Here you will learn how to do it without any Machine Learning skills.

    Many Machine Learning things are done so often you can just use pre-built Machine Learning models. Here you will learn the task of finding faces and extract locations of them.

    Step 1: Pre-built OpenCV models to detect faces

    When you think of detecting faces on images, you might get scared. I’ve been there, but there is nothing to be scared of, because some awesome people already did all the hard work for you.

    They built a model, which can detect faces on images.

    All you need to do, is, to feed it with images and let it do all the work.

    This boils down to the following.

    1. We need to know what model to use.
    2. How to feed it with images.
    3. How to use the results it brings and convert it to something useful.

    This is what the rest of this tutorial will teach you.

    We will use OpenCV and their pre-built detection model haarcascade.

    First you should download and install the requirements.

    This can be done either by cloning this repository.

    Or download the files as a zip-file and unpack them.

    You should install opencv-python library. This can be done as follows.

    pip install opencv-python

    You can also use the requirements.txt file to install it.

    pip install -r requirements.txt

    Step 2: Detect a face

    We will use this image to start with.

    The picture is part of the repository from step 1.

    Now let’s explore the code in face_detection.py.

    # importing opencv
    import cv2
    # using cv2.CascadeClassifier
    # See https://docs.opencv.org/3.4/db/d28/tutorial_cascade_classifier.html
    # See more Cascade Classifiers https://github.com/opencv/opencv/tree/4.x/data/haarcascades
    face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
    img = cv2.imread("sample_images/sample-00.jpg")
    # changing the image to gray scale for better face detection
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(
        scaleFactor=2,  # Big reduction
        minNeighbors=5  # 4-6 range
    # drawing a rectangle to the image.
    # for loop is used to access all the coordinates of the rectangle.
    for x, y, w, h in faces:
        cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 5)
    # showing the detected face followed by the waitKey method.
    cv2.imshow("image", img)

    First notice, that the opencv-python package is imported by import cv2.

    Then also, notice we need to run this code in from where the file haarcascade_frontalface_default.xml is located.

    After that you will read the image into the variable img. Notice, that this assumes you run the file like they are structure in the GitHub (downloaded in step 1).

    When you work with images, you often do not need the level of details given in it. Therefore, the first thing we doit to gray scale the image.

    After we have gray scaled the image we use the face detection model (face_cascade.detectMultiScale).

    This will give the result faces, which is an iterable.

    We want to insert rectangles of the images in the original image (not the gray scaled).

    Finally, we show the image and wait until someone hist a key.

    Step 3: Batch process face detection

    To batch process face detection, a great idea is to build a class to do the face detections. It could be designed in many ways. But the idea is to decouple the filename processing from the actual face detection.

    One way to do it could be as follows.

    import os
    import cv2
    class FaceDetector:
        def __init__(self, scale_factor=2, min_neighbors=5):
            self.face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
            self.scale_factor = scale_factor
            self.min_neighbors = min_neighbors
            self.img = None
        def read_image(self, filename):
            self.img = cv2.imread(filename)
        def detect_faces(self):
            gray = cv2.cvtColor(self.img, cv2.COLOR_BGR2GRAY)
            faces = self.face_cascade.detectMultiScale(
            # drawing a rectangle to the image.
            # for loop is used to access all the coordinates of the rectangle.
            for x, y, w, h in faces:
                cv2.rectangle(self.img, (x, y), (x + w, y + h), (0, 255, 0), 5)
            return self.img
    face_detector = FaceDetector()
    for filename in os.listdir('sample_images/'):
        img = face_detector.detect_faces()
        cv2.imshow("image", img)

    If you want to write the files to storage with face detections, you should exchange the the line cv2.imshow with the following.

        cv2.imwrite(filename, img)

    Want to learn more Machine Learning?

    You will surprised how easy Machine Learning has become. There are many great and easy to use libraries. All you need to learn is how to train them and use them to predict.

    If you want to learn more?

    Then I created this 10 hours free Machine Learning course, which will cover all you need.

    • 15 video lessons – which explain Machine Learning concepts, demonstrate models on real data, introduce projects and show a solution (YouTube playlist).
    • 30 JuPyter Notebooks – with the full code and explanation from the lectures and projects (GitHub).
    • 15 projects – with step guides to help you structure your solutions and solution explained in the end of video lessons (GitHub).

    Python for Finance: Unlock Financial Freedom and Build Your Dream Life

    Discover the key to financial freedom and secure your dream life with Python for Finance!

    Say goodbye to financial anxiety and embrace a future filled with confidence and success. If you’re tired of struggling to pay bills and longing for a life of leisure, it’s time to take action.

    Imagine breaking free from that dead-end job and opening doors to endless opportunities. With Python for Finance, you can acquire the invaluable skill of financial analysis that will revolutionize your life.

    Make informed investment decisions, unlock the secrets of business financial performance, and maximize your money like never before. Gain the knowledge sought after by companies worldwide and become an indispensable asset in today’s competitive market.

    Don’t let your dreams slip away. Master Python for Finance and pave your way to a profitable and fulfilling career. Start building the future you deserve today!

    Python for Finance a 21 hours course that teaches investing with Python.

    Learn pandas, NumPy, Matplotlib for Financial Analysis & learn how to Automate Value Investing.

    “Excellent course for anyone trying to learn coding and investing.” – Lorenzo B.

    Leave a Comment