Face Detection on recorded videos using OpenCV in Python — Windows and macOS

Venkatesh Chandra
Analytics Vidhya
Published in
5 min readJan 2, 2020
Video source — Linked here

Face detection is a computer technology which leverages the power of AI to locate the presence of human faces in an image or a video. With the advancement of open-source projects, it is now possible to identify human faces irrespective of skin color, tone, the position of face and movements.

We will learn how to build a face detection system which takes a pre-recorded video as an input and identifies the faces in it. This project is similar to real-time face detection project (link here) — the difference is that the latter takes live webcam stream as the input. You may find some overlap in the content.

Libraries to be installed in pip (Windows + macOS)

  1. OpenCV (Open Source Computer Vision) library: It is built to help developers carry out tasks related to computer vision.
install opencv-python

Next, we need to install face recognition APIs

2. dlib library: dlib is built through pre-trained models to locate the facial landmarks.

pip install dlib

3. face_recognition library: face_recognition is an open-source project, which is also known as the most straightforward API for facial recognition.

pip install face_recognition

Let’s get into some action in Python

Now, we have the required libraries installed. We will start implementing the codes as shown below. I have explained each block of code to help you understand what is happening in the background.

Feel free to skip to the end of this page to get the link to full code.

Step 1: Open Spyder

Step 2: Import the libraries

import cv2
import face_recognition

Step 3: Reference your video file saved on your hard drive (mp4 format)

cap= cv2.VideoCapture(<enter file path.mp4>)

Step 4: Initialize the required variables. These variables will be populated later on in the code

face_locations = []

Step 5: The way it works is that the video is divided into frames and the code reads one frame at a time. In each frame, we detect the location of the face using the APIs which we have imported above. For each face detected, we locate the coordinates and draw a rectangle around it and release the video to the viewer.

The full code is shown below — explanations follow below the code

while True:
# Grab a single frame of video
ret, frame = cap.read()
# Convert the image from BGR color (which OpenCV uses) to RGB
# color (which face_recognition uses)
rgb_frame = frame[:, :, ::-1]
# Find all the faces in the current frame of video
face_locations = face_recognition.face_locations(rgb_frame)
for top, right, bottom, left in face_locations:
# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0,
255), 2)
# Display the resulting image
cv2.imshow('Video', frame)


# Wait for Enter key to stop
if cv2.waitKey(25) == 13:
break

Block 1:

# Grab a single frame of video
ret, frame = cap.read()
# Convert the image from BGR color (which OpenCV uses) to RGB color #(which face_recognition uses)
rgb_frame = frame[:, :, ::-1]

Here, we process one frame at a time. The frame is extracted using cv2 library which captures the frame in BGR (Blue-Green-Red) colors, while the face recognition library uses RGB (Red-Green-Blue) format. Hence we flip the color code of the frame.

Block 2:

face_locations = face_recognition.face_locations(rgb_frame)

Here, we locate the coordinates of the faces present in the frame. The list face_locations is populated with the x, y coordinates, and the width and height of the faces detected.

Block 3:

for top, right, bottom, left in face_locations:
# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

Here, we draw a rectangle around each of the faces captured. The rectangle starts at the x and y coordinates (left and top in this case), and extend up to the width and height of the face detected (right and bottom in this case).

Block 4:

cv2.imshow(‘Video’, frame)if cv2.waitKey(25) == 13:
break

The resulting image (frame) is released to the viewer and the loop continues to run until the user hits the Enter key on the keyboard.

Step 6: All captured videos must be released.

video_capture.release()
cv2.destroyAllWindows()

Run the program in the command line

The next step is to save the file in .py format and run it in command line/Anaconda prompt.

I ran it in Anaconda prompt by first navigating to the folder using the command cd.

I ran it in Anaconda prompt by first navigating to the folder using the command cd.

cd <folder path>

Run the python file

python filename.py

You will see a pop-up window with the video playing. The video might be slow and it is because the number of frames is usually large in OpenCV. However, if you save the video on your hard drive, the written video is not slow and matches the fps (frames per second) of the input video.

You can download more royalty-free videos here.

And Yes! You have successfully written a code that detects human faces in a video.

Try experimenting on videos with Snapchat filters on and let me know how it goes in the comments section below.

If you face any issues, let me know in the comments section.

Codes

Related links

Real-Time Face Detection System in Python — Windows and macOS

Vehicle Detection in Python using OpenCV — Windows and macOS

Pedestrian Detection in Python using OpenCV — Windows and macOS

Saving output of object recognition in macOS

Where to find me 🤓

  1. Connect with me on LinkedIn/ GitHub / My website
  2. Feeling generous? Buy me a coffee here ☕️

--

--