Smile Detection using Opencv-Python
Smile Detection using Opencv-Python
Welcome back to a another Blog on Opencv.
In this Blog we will recognize Smile on face using an Image.
so lets looks at requirements.
Requirements.
- Opencv (pip install opencv-python)
- numpy (automatically install with opencv)
- python 3.x
So lets firsts code and then Understand its working.
# Dynamic Coding
# Smile detection using Opencv-Python
# Import library
import cv2
#lets include the haar cascade xml file.
detection = cv2.CascadeClassifier('haarcascade_smile.xml');
#reading the image
image = cv2.imread('tom.png')
#converting to gray scale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = detection.detectMultiScale(gray, 1.3, 20)
#loop for drawing rectangle
for(x, y, w, h) in faces:
#drawing rectangle
cv2.rectangle(image, (x,y), (x+w, y+h), (0,255,0), 2)
#displaying image in new window
cv2.imshow('WINDOW', image)
#Saving image to folder
#cv2.imwrite('tom_modified.jpg', image)
cv2.waitKey()
Output.
In the first 2 line we first import the module and make a instance for cascade classifier class which takes haarcascade_smile.xml as parameter.
import cv2
#lets include the haar cascade xml file.
detection = cv2.CascadeClassifier('haarcascade_smile.xml');
the haarcascade_smile.xml is the core file for our whole code because it is used to recognize the smile on image.
Now read the Image using imread method of opencv and then convert the colored image into gray scale image for better results.
image = cv2.imread('tom.png')
#converting to gray scale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
After reading and converting image into gray scale used the cascade classifier object with Detectmultiscale method which detects the smile according to the data present in the haarcascade_smile.xml file and it returns 4 values which are the coordinates of the image and we use those 4 returned values for creating a rectangle over the image , means over the smile.
faces = detection.detectMultiScale(gray, 1.3, 20)
#loop for drawing rectangle
for(x, y, w, h) in faces:
#drawing rectangle
cv2.rectangle(image, (x,y), (x+w, y+h), (0,255,0), 2)
now all the work is done so lets see our image using imshow method and waitkey method for holding the image screen on the Window.
cv2.imshow('WINDOW', image)
#Saving image to folder
#cv2.imwrite('tom_modified.jpg', image)
cv2.waitKey()
I hope you like the Blog and learn some thing new from it .
if you have any doubts and errors please comments we will reply you as soon as possible.
No comments