Table of contents
1.
Introduction
2.
CODE
3.
OUTPUT
4.
Frequently Asked Questions
5.
Key Takeaways
Last Updated: Mar 27, 2024

Smile Detection

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Facial expressions are a great way to determine a person’s state of mind. A very basic and beautiful facial expression is a smile. With the growing era of technology, we have made machines detect smiles from a person’s face.

 

I’ll use the OpenCV (Open Computer Vision) library to detect a smile from an input image in this tutorial.

 

The first step is to install the OpenCV library in the python environment. For this, just move to the system's command prompt and enter the command ‘pip install opencv-python’.

 

Using the imread() function, we’ll read the input image.

 

Then, using the CascadeClassifer() method, we can use the already pre-trained models and find a smile on the person’s face. All the haar-cascades can be found in this link. To detect a smile, we’ll use smile.xml harr-cascade.

 

Next, for detecting the smile, we’ll use the detectMultiScale() method, it’ll store all the smile coordinates (x, y, w, h) in a variable called smiles.

 

Finally, using a for loop, we’ll traverse through the coordinates and create a rectangle around the smile.

 

Check out below the simple & concise code for detecting a smile in an image.

CODE

import cv2

image = cv2.imread('dravid.jpg')
smile_cascade=cv2.CascadeClassifier("smile.xml")

smiles  = smile_cascade.detectMultiScale(image, scaleFactor = 1.8, minNeighbors = 20)

for (sx, sy, sw, sh) in smiles:
           cv2.rectangle(image, (sx, sy), ((sx + sw), (sy + sh)), (0, 255,0), 5)

cv2.imshow("Smile Detected", image)

cv2.waitKey(0)
cv2.destroyAllWindows()
You can also try this code with Online Python Compiler
Run Code

OUTPUT

 

Also read, Sampling and Quantization

Frequently Asked Questions

  1. What are the applications of the OpenCV library?
    The OpenCV library has many use-cases like
    1. Face Detection
    2. Object Detection
    3. Image Processing
    4. Face Recognition
     
  2. What are Haar-Cascades?
    We use Harr-Cascades to detect facial expressions from a person’s face. These are classifiers in XML files and store the pre-defined patterns over face segments.
     
  3. What are the different types of haar-cascades available in OpenCV?
    OpenCV has many haar-cascades corresponding to different parts:
    1. Face
    2. Eye
    3. Full-Body
    4. Lower-Body
    5. Upper-Body
    6. Smile
    7. Frontal-Face

Key Takeaways

Congratulations on finishing the blog!! Below, I have some blog suggestions for you. Go ahead and take a look at these informative articles.

 

In today’s scenario, more & more industries are adapting to AutoML applications in their products; with this rise, it has become clear that AutoML can be the next boon in the technology. Check this article to learn more about AutoML applications.

 

Check out this link if you are a Machine Learning enthusiast or want to brush up your knowledge with ML blogs.

 

If you are preparing for the upcoming Campus Placements, don't worry. Coding Ninjas has your back. Visit this link for a carefully crafted and designed course on-campus placements and interview preparation.

Live masterclass