Face Detection using Open-CV

Face Detection using Open-CV

This is a simple article on “Face detection “using OpenCV. OpenCv is the most popular computer vision library. And today we will be using it to create our simple module for face detection.

This is a simple article on “Face detection “using OpenCV. OpenCv is the most popular computer vision library. And today we will be using it to create our simple module for face detection.

Once you successfully implement this module you can advance to my next articles for face recognition.

Before starting with the tutorial this is the list of Prerequisites you would need.


· *Python *(I am using version 3.6.4)

· OpenCV

Once you have Python, You can simply Do It by typing following command in your command prompt.

pip install opencv-python

(This will install the latest version of openCV in your system.)

· The** Haar cascade** file

(Now the Haar Cascade is basically a classifier which is used to detect the object for which it has been trained for.)

We need .xml file of frontal face Haar-Cascade.

This file along with Couple of more default cascades re provided with the OpenCv package itself. So to find it follow up:-

  1. Open Command prompt in windows /terminal or bash in other Os.

Now assuming that you have already installed python.

Start python in your prompt.

  1. Once the python is started type following to get the location of opencv.

Import cv2

And then type following command to get the location .


Copy the location as shown and paste it into the explorer.

Inside the data folder you will find the Haar cascade file for frontal face that we will be using

Copy this file create a new folder on your desktop And paste this file in that folder .

Make sure that your python code and this file are in the same folder. In this way it will be way simpler to give the path.

· Lets start the code Now:-

  1. open python IDLE or any other IDE that you will be using , create a new file in the same folder where you Copied your har cascade.

·** Import The libraries:-**

import cv2
import numpy as np

.** Detection part:-**

· Import the Haar Cascade


.Adding a video capture object


The argument (0) in the above line indicates the default web cam you your are using USB-camera you can use the argument (1) it goes on increasing for the no. of usb cameras connected to system.

· Lets read the Image from the video Capture object.

Which will return a *TRUE/FALSE *and the capture frame will be shown using imshow().

· This will be a while loop in which we will capture the image convert it to gray scale for face detection.

And then we will apply face detector on the Grayscale image.

Then we will extract the X,Y,W,H parameters from the image. Where X, and Y are screen coordinates and W=width of the box for face H=height.

ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:

once done with this we will show the image with face detector .and I have added q as a key to break operation. And waitkey is used for delay.


import numpy as np
import cv2
c = cv2.VideoCapture(0)
ret, img = c.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detect.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
if cv2.waitKey(1) & 0xFF == ord('q'):

Here are the results. 😊

Output 1.

Output 2.

I hope this was helpful. In case you get any problem feel free to post your query I will try my best to solve it .

Keep Learning . All the best.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

OpenCV Python Tutorial: Computer Vision With OpenCV In Python

OpenCV Python Tutorial: Computer Vision With OpenCV In Python: Learn Vision Includes all OpenCV Image Processing Features with Simple Examples. Face Detection, Face Recognition. Computer Vision is an AI based, that is, Artificial Intelligence based technology that allows computers to understand and label images. Use OpenCV to work with image files. Create Face Detection Software. Detect Objects, including corner, edge, and grid detection techniques with OpenCV and Python. Use Python and Deep Learning to build image classifiers. Use Python and OpenCV to draw shapes on images and videos. Create Color Histograms with OpenCV

OpenCV Python Tutorial - Computer Vision With OpenCV In Python

In this OpenCV Python Tutorial article, we will be covering various aspects of Computer Vision using OpenCV in Python. OpenCV has been a vital part in the development of software for a long time. Learning OpenCV is a good asset to the developer to improve aspects of coding and also helps in building a software development career.

OpenCV Python for Beginners - Learn Computer Vision with OpenCV 2020

OpenCV Python for Beginners - Learn Computer Vision with OpenCV in 10 Hours (2020). You'll learn: Introduction to OpenCV; How to Install OpenCV for Python on Windows 10; How to Read, Write, Show Images in OpenCV; How to Read, Write, Show Videos from Camera in OpenCV; matplotlib with OpenCV; Image Pyramids with Python and OpenCV; Canny Edge Detection in OpenCV; Image Blending using Pyramids in OpenCV; Face Detection using Haar Cascade Classifiers ...