We will use the Haar cascade classifier for smile detection in an image. A haar cascade classifier is an effective object detection method. It is a machine learning based approach. To train a haar cascade classifier for smile detection, the algorithm init...
We will use the Haar cascade classifier for smile detection in an image. A haar cascade classifier is an effective object detection method. It is a machine learning based approach. To train a haar cascade classifier for smile detection, the algorithm initially needs a lot of positive images (images with smile) and negative images (images without smile). Then the classifier is trained from these positive and negative images. It is then used to detect smiles in other images.
We can use already trained haar cascades for smile detection. For smile detection in the input image, we need two haar cascades one for face detection and other for smile detection. We will use the following two haar cascades −
haarcascade_frontalface_default.xml
haarcascade_smile.xml
You can find different haarcascades following the GitHub website address −
https://github.com/opencv/opencv/tree/master/data/haarcascades
To download the smile haarcascade, click on the haarcascade_smile.xml file. Open it in raw format, right click and s**e.
To detect smile in an image, you can follow the steps given below −
Import the required library. In all the following examples, the required Python library is OpenCV. Make sure you h**e already installed it.
Read the input image using cv2.imread(). Specify the full image path. Convert the image to grayscale image.
Initiate the Haar cascade classifier objects face_cascade = cv2.CascadeClassifier() for face detection and smile_cascade = cv2.CascadeClassifier for smile detection. Pass the full path of the haar cascade xml files. You can use the haar cascade file haarcascade_frontalface_default.xml to detect faces in the image and haarcascade_smile.xml to detect smiles.
Detect faces in the input image using face_cascade.detectMultiScale(). It returns the coordinates of detected faces in (x,y,w,h) format.
Define face roi as image[y:y+h, x:x+w] for the detected face. Now detect the smile within the detected face area (roi). Use smile_cascade.detectMultiScale(). It also returns the coordinate of the bounding rectangle of eyes in (sx,sy,sw,sh) format.
Draw the bounding rectangles around the detected smile (mouth) in the original image using cv2.rectangle().
Display the image with the drawn bounding rectangles around the mouth.
Let's h**e a look at some examples for more clear understanding.
In this Python program, we perform smile detection in the input image using haar cascade.
We will use this image as the Input File for this program −
On execution, it will produce the following output −
Number of detected faces: 1 smile detected
And we get the following output window −