top of page
  • Writer's pictureHackers Realm

Face Detection using Python | OpenCV - Computer Vision | Machine Learning Project Tutorial

Updated: May 31, 2023

Unleash the power of computer vision with Python! This tutorial explores face detection using OpenCV and machine learning. Dive into the fundamentals of image processing, learn to detect faces, and enhance your understanding of computer vision techniques. Develop real-world applications and elevate your skills in face detection with this hands-on project tutorial. #FaceDetection #Python #OpenCV #MachineLearning

face detection using opencv python with computer vision
Face Detection using OpenCV Python

In this project tutorial we will use various OpenCV and haarcascades functions to analyse and detect faces on images.

You can watch the video based tutorial with step by step explanation down below

Project Information

The project uses OpenCV module and haarcascades file to detect faces in the images.

Download the haarcascade file here

We will install the OpenCV module

pip install opencv-python

Import Modules

import cv2
import matplotlib.pyplot as plt
%matplotlib inline
  • cv2 - OpenCV module

  • matplotlib - used for data visualization and graphical plotting

HAAR Cascade File Path

First we must configure the HAAR Cascade file path

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

Load the Image

Next we load he test image to process the data

image = cv2.imread('test image.jpg')
# convert to rgb
img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
input image for face detection
Input Image for Face Detection
  • Now we can see the loaded image

  • In OpenCV, the image will be in BGR format, you must convert it into RGB format for further processing

  • There are five faces in the image, you can resize the image for better processing

We need to resize the image for better processing

# resize the image
image = cv2.resize(image, (400, 600))

Next we convert the image into grayscale for quicker processing

# convert to gray scale image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
plt.imshow(gray, cmap='gray')
grayscale image for face detection
Grayscale Image for Face Detection

Detect Faces

Finally, after preprocessing the images into a standard format to get better and faster results, we will proceed to the face detection method

faces = face_cascade.detectMultiScale(gray)
  • Function to analyze the image for face detection and store them



  • No. of faces that was detected

# diplay the faces in the image
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow("Faces ",image)
face detection using opencv python
Detecting Faces using OpenCV
  • Result of the display of the faces, not very accurate due to resolution of the image

  • rectangle() - Function to display a bounding box indicating where the face is being detected.

Final Thoughts

  • This is a simple image processing technique to detect faces in the image file.

  • You may also use a webcam or video recording for real time face detection using the same method.

  • You can apply facial recognition methods to expand the project for further work.

In this project tutorial, we have explored the Face detection project using OpenCV and the HAAR Cascade module. This is a basic image processing method to analyze the data using machine learning techniques.

Get the project notebook from here

Thanks for reading the article!!!

Check out more project videos from the YouTube channel Hackers Realm



bottom of page