Guide to make Custom Haar Cascade XML file for Object detection with OpenCV

In this blog, we are training any custom Haar Cascade XML file for object detection on an image /real-time


With the current technology trends, computer vision has become an important entity in the technology field leading to limitless computer innovations. Think about computer vision from this perspective: As humans, our eyes are an important part of the body and so is embedding vision to computers/machines so as to allow them to see.

So to do that we are going to use Haar cascade method lets see first what is haar cascade shortly

Haar Cascade is a machine learning object detection algorithm used to identify objects in an image or video and based on the concept of ​​ features proposed by Paul Viola and Michael Jones in their paper “Rapid Object Detection using a Boosted Cascade of Simple Features” in 2001.

It is a machine learning-based approach where a cascade function is trained from a lot of positive and negative images. It is then used to detect objects in other images.

A Haar​ feature considers adjacent rectangular regions at a specific location in a detection window, sums up the pixel intensities in each region, and calculates the difference between these sums. some the Haar features are shown in the following image

To learn more about Haar features follow following link


  1. Operating System :- Windows 7/8/10
  2. Environment :- Python, anaconda (environment setup)

Note:- If You don’t have any environment setup refer my previous blog to setup environment link here

lets get Started with Installing OpenCV

So to install OpenCV on your machine follow the following steps:

For python environment users :-

  • 1. Goto Start->search for ( cmd ) open it->type python and hit Enter-> you should see the following output then your python is successfully installed on your machine.
  • 2. then type exit() and then hit Enter button
  • 3. To install OpenCV you just have to type the following command
pip install numpy
pip install opencv-python

Note :- Type this code on command prompt and hit enter Wait for some time (time will proportional to download speed of your internet ) this command will download and install Opencv on your machine

For Anaconda environment users:-

  • 1. Goto Start->search for ( Anaconda Prompt ) open it->type following command and hit enter
python -m pip install --upgrade pip

Then you should see something like this output

Note:- if you have any particular environment then Activate it first and then follow the above process again.

  • 2. To install OpenCV type the following command
pip install numpy 
pip install opencv-python
NOTE: I have already install OpenCV on my pc that’s why it is showing requirement already satisfied if you are installing it the first time then it shows successfully installed OpenCV

To train custom Haar classifier from scratch

  • To train Harr cascade from scratch you will require software for it. So we are going to use Cascade-Trainer-GUI here is a link to download a software
  • After downloading it install it and lunch it then you will see window something like this
  • Then minimize this window.
  • For training custom object detector using haar cascade, we require a dataset of it so that why we are going to create it using a python library named icrawler.
  • To install icrawler type following command on cmd /anaconda prompt
pip install icrawler
  • Hit enter and this command will install icrawler in your system.
  • To create dataset XML file we require positive images i.e. object we want to predict and negative images i.e. images except for the object we want to predict
  • To create a dataset of requiring images you have to run following commands here we are downloading positive images
  • to collect negative images we are using the same command but using n as a folder after root directory (check line no 10 in both the codes )
Congratulation Guy’s We Successfully build a dataset from scratch for any object detection

To train object detection classifier follow following simple steps

  1. Goto start->search for (Cascade-Trainer-GUI)->open it
  2. create a new directory on Desktop lets say named temp and create a new folder inside it named p to copy the positive images we download previously to this folder. refer to the following image.

3. create a new folder named it with n in the temp folder and copy-paste negative images we download previously.

4. when all done open the folder temp on desktop you will se two folders named n and p like this.

5. open the cascade trainer GUI paste the previously copied path into sample folder location also count the no of negative images you have and put it in negative image count.

6. Click on the common section this will open a new page in the same window. Select no of stages carefully try to put it low if you put it so high then it will take a long time to train.but also maybe it will inc accuracy.

My suggestion is that try to put no of stages= 15 and click on the button start on the right bottom corner. you can refer the following image

7. Then go back to temp folder you will see a new file named classifier open that folder you will find a new file named cascade.xml copy path to this file

check the classifier we build using python code

  • copy-paste this code
  • Note:- Replace the cascadeClassifier path to your own cascade.xml e.g. cv2.CascadeClassifier(r“your_own_cascade.xml_file_path) file and put your own test file path in cv2.imread(r“your_own_testing_image_path”)
  • run this code u will see new popup window and detected image is marked with a green box
  • Here is my output to close output click any keyboard key on that tab this will terminate the code.
  • you will find all these files on GitHub which link is mention here

Congratulation!! you have successfully made a custom object detection classifier using Haar cascade Algorithm if you want to learn more about haar cascade please refer to this link here.

In our next blog, we are going to start the object recognition with the help of TensorFlow API’s CNN’s models. On that blog, we are going to discuss some of the drawbacks of haar cascade classifiers and how we can able to avoid it using CNN’s.

  • Quiz: I always suggest peoples try to practice more because practice makes a man perfect. So I am going to give one challenge try to create horse images dataset from scratch and try object classification on that using techniques we learn in this blog.

About Me

Hi everyone I am Vipul Gote




If you want to ask me some questions, report any mistake, suggest improvements, give feedback you are free to do so via the chatbox on the website or by emailing me at —


Ask me About AI/ML stuff, Anime, Cosmology, Physics , Games