A Simple Face Detection System in Using OpenCV & Python

sajith dilshan
Geek Culture
Published in
3 min readMay 9, 2022
Face detection

we are going to detect faces in videos. As we know videos are basically Is being created of frames, which are still images. then we perform the face detection for each frame in a video

First of all, you install OpenCV in your machines. If you don’t know how to install OpenCV. you can follow my previous article Installing OpenCV for Python on Windows using Anaconda

Face detection using Haar cascades is a machine learning-based approach where a cascade function is trained with a set of input data. OpenCV already contains many pre-trained classifiers for face, eyes, smiles, etc… Today we will be using the face classifier. You can experiment with other classifiers as well.

You should download the trained classifier XML file in GitHub (haarcascade_frontalface_default.xml), which is available in OpenCV’s GitHub repository. you can go haarcascade_frontalface_default.xml Raw page and right-click it and go save as and save that file into your working location.

after that, Launch the Anaconda prompt from the Start menu

launching Anaconda prompt

To open the Jupyter Notebook we need to type the following command at the prompt:

Jupyter Notebook

Launch Jupyter notebook

When the notebook opens in your browser, you will see the Notebook Dashboard, which will show a list of the notebooks, files, and subdirectories in the directory where the notebook server was started.

Jupyter Notebook Dashboard

choose the file path and click the top of the right-side button called “New” and choose python 3 for creating a notebook file

creating a Jupyter notebook file

after that, you can see the Jupyter notebook file

JupyterEmptyNotebook

after that, you can copy and paste the below code into that empty notebook file

To detect faces in the video:

after copy and paste in code into a notebook, you can run that file by clicking the “Run” button

run button in Jupyter notebook

after clicking the run button youcan see the program work using the webcam, detecting faces in webcam video.

A few things to note:

  • The detection works only on grayscale images. So it is important to convert the color image to grayscale. (line 15)
  • detectMultiScale function (line 17) is used to detect the faces. It takes 3 arguments — the input image, scaleFactor, and minNeighbours. scaleFactor specifies how much the image size is reduced with each scale. minNeighbours specifies how many neighbors each candidate rectangle should have to retain it. You can read about it in detail here. You may have to tweak these values to get the best results.
  • faces contain a list of coordinates for the rectangular regions where faces were found. We use these coordinates to draw the rectangles in our image.
  • we use an infinite loop to loop through each frame in the video. We use cap.read() to read each frame. The first value returned is a flag that indicates if the frame was read correctly or not. We don’t need it. The second value returned is the still frame on which we will be performing the detection.

Social Media: LinkedIn, Twitter, Instagram, YouTube

--

--

sajith dilshan
Geek Culture

Software Engineer | Technical Writer | Tech Enthusiast