Friday , December 15 2017
Home / opencv / C++ / HAAR Casade in Opnecv C++ and Python

HAAR Casade in Opnecv C++ and Python

(Last Updated On: October 8, 2017)

HAAR is well known and famous face detection algorithm.Haar classifier is effective way for face detection. It is learning based algorithm where a training data is feeded which hundreds of positive and negative images. Initially a big number of positive images (images with face) and negative images (images without face) are used to train the classifier. After training data classifier is used to detect in this process classifier match each pixel of incoming frame with the trained data and in case of positive result it keeps the record and at the end of the input it decide on the bases of those results whether it matches with data or not. In this process if a full face is not detected then feature matching result is also given in which if a single features matches with trained data then it is also informed and calculated. As during feature matching process system is not much intelligent so it sometimes gives irrelevant results.

To solve issue of irrelevant feature matching  a tough and time consuming technique is applied after getting result from first phase top matching features are tested on all training data’s and a specific thresh hold is set which classify the positive and negative features. In thresh hold technique we ignore few range of pixels falling under a certain value in accumulator array so after thresh hold implementation we are assured that we are getting smooth and best values and noised data is ignored.

After thresh hold we select the features with low error and noise ratios and also with maximum matches. Then a new round of thresh hold is started with more high threshold so in this process we get a number of irrelevant and disqualified frames. This process run till the end of single frame and in each run we get the new weights and features. In final result the classifier is equal to the sum of positive and negative classifiers. Weak classifiers are those classifies who do not have big number of matches. While strong classifiers has more than 95% accuracy rates with a big number of matches. Just as example if we have 20000 features in first phase than in last phase we may have 3000-4000 features left as strong candidate. But this is also a big number which can take more time in next steps. For that purpose a new technique is applied. Before scanning of features it is checked that all features who have complete images to be rechecked for face detection as in last phase we were doing just feature mapping and detection but in next phase we must get features with complete image otherwise they will not qualify but will cost us in terms of time so a little scanning is done to remove features which does not contain main area or complete face. This is called cascade method in which we pass the data from several passes before passing it to final phase this process is little time consuming but it gives best results as compared to other algorithms.

Open CV come with trainer by default so it becomes very easy to use HAAR in opencv after configuration steps just add positive and negative samples and apply Haar strategy. If we do not want to include our custom samples Opencv already contains few predefined classifiers for face, eye, nose and other features. These samples are stored as XML form in opencv Haar cascade folder.



import numpy as np

import cv2

mainface_cscde = cv2.CascadeClassifier(‘haarcascade_frontalface_default.xml’)

nose_cascade = cv2.CascadeClassifier(‘haarcascade_nose.xml’)

image = cv2.imread(“user/vedios/input.mp4”)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

faces = mainface_cscde.detectMultiScale(gray, 1.3, 5)

for (x,y,w,h) in faces:


roi_gray = gray[y:y+h, x:x+w]

roi_color = image [y:y+h, x:x+w]

eyes = eye_cascade.detectMultiScale(roi_gray)

for (ex,ey,ew,eh) in eyes:


cv2.imshow(‘ image ‘, image)



About Admin

Check Also

Background Subtraction in Opencv C++

(Last Updated On: October 8, 2017)Change detection or background subtraction is the main and important …

Leave a Reply

Your email address will not be published. Required fields are marked *