In this Python OpenCV article i want to show you Smile Detection with HaarCascade Classifier. so in this article iam using an image, you need to have an image in your working directory.
What Is HaarCascade Classifier?
So Haar Cascade is basically a classifier which is used to detect the object for which it has been trained for, from the source. also The Haar Cascade is trained by superimposing the positive image over a set of negative images. The training is generally done on a server and on various stages. Better results are obtained by using high quality images and increasing the amount of stages for which the classifier is trained.
you can also used predefined Haar Cascades which are available on github
Smile Detection in Python OpenCV with HaarCascade
Let’s create our example, so now this is the complete code for Smile Detection In Python OpenCV With HaarCascade
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# Import the OpenCV library import cv2 # Load image from a file image = cv2.imread("smile.jpg") # Load pre-trained Haar cascade for smile detection smile_cascade = cv2.CascadeClassifier('haarcascade_smile.xml') # Detect smiles in the image smiles = smile_cascade.detectMultiScale(image, scaleFactor=1.8, minNeighbors=20) # Loop through the list of detected smiles for (sx, sy, sw, sh) in smiles: # Draw a rectangle around each detected smile cv2.rectangle(image, (sx, sy), (sx + sw, sy + sh), (0, 255, 0), 5) # Display the image with detected smiles # Parameters: window name, image cv2.imshow("Smile Detected", image) # Wait for a key event indefinitely cv2.waitKey(0) # Destroy all OpenCV windows cv2.destroyAllWindows() |
So in this line of code we have loaded the image, you need to have an image in your working directory.
1 |
image = cv2.imread("smile.jpg") |
This is for loading our Haar Cascade Classifier that we have already copied in our directory.
1 |
smile_cascade = cv2.CascadeClassifier('haarcascade_smile.xml') |
detecMultiScale() function is for detecting objects if it finds a face in the image it will return in the form of x,y,w,h. and it needs some parameters.
ScaleFactor: This is parameter is for specifying how much the image size is reduced at each image scale.
minNeighbors: Parameter specifying how many neighbors each candidate rectangle should have to retain it, this parameter will affect the quality of the detected faces.
1 |
smiles = smile_cascade.detectMultiScale(image, scaleFactor = 1.8, minNeighbors = 20) |
In this code we want to draw rectangle to the smiles in the image.
1 2 |
for (sx, sy, sw, sh) in smiles: cv2.rectangle(image, (sx, sy), ((sx + sw), (sy + sh)), (0, 255,0), 5) |
In this line of code we want to show our image
1 |
cv2.imshow("Smile Detected", image) |
Run the complete code and this will be the result
FAQs:
What is the algorithm for detecting smiles?
Algorithm for detecting smiles is based on the use of Haar-like features and a cascade of classifiers, known as the Haar Cascade algorithm. This method works as follows:
- Haar-like Features: The algorithm uses simple rectangular features to capture the essential characteristics of a smile.
- Integral Image: To quickly compute these features over different areas of the image.
- AdaBoost: An algorithm that selects the most relevant features and constructs a strong classifier as combination of weighted weak classifiers.
- Cascade of Classifiers: A series of increasingly complex classifiers applied to different regions of the image. Early stages quickly eliminate non-smile regions, while later stages apply more detailed analysis.
What is Haarcascade in Python?
Haarcascade or Haar Cascade in Python is a machine learning approach for object detection, it is provided by OpenCV library. It uses a series of pre trained classifiers to detect objects, such as faces, eyes and smiles inside images. These classifiers are stored in XML files and can be loaded using OpenCV’s CascadeClassifier class. Haar Cascade method is efficient and effective for real time object detection.
This is an example of loading Haar Cascade classifier in Python:
1 2 |
import cv2 smile_cascade = cv2.CascadeClassifier('haarcascade_smile.xml') |
Can Haar Cascade detect multiple faces?
Yes, Haar Cascade can detect multiple faces in an image. detectMultiScale method of the CascadeClassifier class is used to detect objects such as faces in the image. This method can return multiple bounding boxes, each corresponding to a detected face. This algorithm scans the image at multiple scales and locations, and it allows you to detect multiple instances of the target object.
Subscribe and Get Free Video Courses & Articles in your Email