Face and Eye Detection in Python using OpenCV.

Bilal K.
1 min readJul 24, 2021

OpenCV is an open-source computer vision library that uses machine learning algorithms for face detection, object tracking or colors detection. In this blog post, we’ll learn how to use the Haar Cascade algorithm to detect faces and eyes in an image or real-time video. Let’s dive in.

Approach:

  • Capture the video from the webcam.
  • Convert the frames to gray scale since cascade algorithm works only with gray scale.
  • Detect the faces using multi-scale. It takes 3 arguments — the input image, scaleFactor and minNeighbors.Scale Factor specify how much the image size is reduced at each image scale and min neighbors specify how many neighbors each candidate rectangle should have to retain it. You can read more about the arguments on this stack overflow post.
  • Draw the blue rectangle on the faces.
  • Draw the green rectangle on the eyes.

Don’t forget to import the libraries.

Make sure you have Python, OpenCV and Numpy already installed. I personally prefer Anaconda distribution but you are good to go without Anaconda if you have mentioned libraries installed.

--

--

Bilal K.

Software Engineer, I share tips and tools to help fellow developers level up.