Classifying with a Classifier: HAAR CASCADE
Updated: Mar 14, 2021
Haar Cascade is an object detection algorithm, that uses a classifier which is trained to detect an object and does this by superimposing a positive image over a set of negative images in a cumulative series of stages.
This procedure of machine learning technique follows a heavy training of data sets. Each example available online have their own focus of object detection. They are obtained by the black and white boxes below which act as convolutional kernels.
The features are more specifically single values received by subtracting the sum of pixels under the white rectangles from the sum of pixels under the black rectangles. “Haar” sounds a bit weird , but it's intuitive similarity with Haar wavelets which are these :
The image to be used is divided into different kinds of sub-windows and multiple Haar-like features to compute it at different scales and positions for each sub-window. The main features are selected using the Adaboost algorithm. Then each sub-window is checked for the presence or absence of face using a cascade of classifiers , which finds an optimal threshold for classifying the training images correctly.
The feature selection takes place as per our imagination. Trying different Haar features and see which of those produce the largest value for the difference between the sums of pixels between the black and white rectangles. The example shown above where optimal Haar features have been found. The eyes features are darker whereas the under-eyes area are lighter, and thus a horizontal rectangle with black at top and white at below is pretty accurate. Lastly, the nose is always lighter than the eye areas and as such a Haar feature with a vertical white box in the middle is the way to go. OpenCV has it’s own trainer and detector which can be used to train your own classifier, which in our case would need to track the left eye of a person over a series of three images.
face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml') eye_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_eye.xml') glasses_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_eye_tree_eyeglasses.xml') smile_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_smile.xml')
This is basically how features are scaled using HAAR CASCADE CLASSIFIER , to detect objects, images as a major application of OpenCV.
import keras import cv2 import numpy as np import urllib from keras.models import load_model classifier=cv2.CascadeClassifier(r"C:/Users/Natasha/Desktop/project1/proj/haarcascade_frontalface_default.xml") URL="http://192.168.43.1:8080/shot.jpg" model = load_model('ABCD.h5')
I used it with frontal face detection .