How to build a smile detector using IBM AI solutions
Building AI that detects emotions can be highly valuable in today’s world. If you are unable to detect the emotions of your customers, it will leave a negative impact on your business. So, you need to build powerful recommended systems using computer vision applications to understand the emotions.
IBM AI services are becoming extremely powerful and useful for enterprises especially in business functions such as customer services. Building an AI-powered application for your accurate business solutions will lower the staff requirement and be cost effective as well. With the help of different IBM AI API, you can build perfect applications which deliver everlasting output.
Here I am going to build an application which helps to detect the different emotional expressions on the faces. For this, you need to create 3 cascading files for detecting face, eyes, and smile on the faces.
Cascading XML file from front facial detection:
Haarcascade_frontalface_default.xml
<?xml version="1.0"?> <opencv_storage> <cascade type_id="opencv-cascade-classifier"><stageType>BOOST</stageType> <featureType>HAAR</featureType> <height>24</height> <width>24</width> <stageParams> <maxWeakCount>211</maxWeakCount></stageParams> <featureParams> <maxCatCount>0</maxCatCount></featureParams> <stageNum>25</stageNum> <stages> <_> <maxWeakCount>9</maxWeakCount> <stageThreshold>-5.0425500869750977e+00</stageThreshold> <weakClassifiers> <_> <internalNodes> 0 -1 0 -3.1511999666690826e-02</internalNodes> <leafValues> 2.0875380039215088e+00 -2.2172100543975830e+00</leafValues></_>
After creating the XML file from the front facial detection then you need to create XML files for detecting eyes and smile.
Cascading File For Eye Detection:
Haarcascade_eye.xml
BOOST HAAR 20 20 93 0 24 <_> 6 -1.4562760591506958e+00 <_> 0 -1 0 1.2963959574699402e-01 -7.7304208278656006e-01 6.8350148200988770e-01</_>
Now you need to create a cascade XML file which detects the emotions based on your facial expressions.
Cascading File For Smile Detection:
Haarcascade_smile.xml
BOOST HAAR 18 36 53 0 20 <_> 11 -1.2678639888763428e+00 <_> 0 -1 0 -4.8783610691316426e-04 5.9219348430633545e-01 -4.4163608551025391e-01</_>
Now you see the 3 cascading files are ready and you need to write the code i.e implement the logic by calling these cascading XML files. You need to import the libraries required, load all your cascading files, define the functions which will do detections and you can also do some face recognition with the webcam.
Code for detecting emotional expressions on ones faces:
Step1: # Import the libraries
import cv2
Step2: # Load the cascading XML files
You need to load all the cascading files for detecting face,eyes and smile on the faces.
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml') smile_cascade = cv2.CascadeClassifier('haarcascade_smile.xml')
Step3: # Defining a function that will do the detections
def detect(gray, frame): #defining the detect function faces = face_cascade.detectMultiScale(gray, 1.3, 5) #detecting the face with the rectangle coordinates. for (x, y, w, h) in faces: # Using the for loop for iterative detecting on multiple rectangles. cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2) roi_gray = gray[y:y+h, x:x+w] roi_color = frame[y:y+h, x:x+w] eyes = eye_cascade.detectMultiScale(roi_gray, 1.1, 22) #logic for detecting the eyes in the face with in the rectangular frame. for (ex, ey, ew, eh) in eyes: cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2) smiles = smile_cascade.detectMultiScale(roi_gray, 1.7, 22) #It is the logic for detecting the smiley expressions on the faces by setting the rectangle coordinates. for (sx, sy, sw, sh) in smiles: cv2.rectangle(roi_color, (sx, sy), (sx+sw, sy+sh), (0, 0, 255), 2) return frame Step4: # Face Recognition with the webcam video_capture = cv2.VideoCapture(0) while True: _, frame = video_capture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) canvas = detect(gray, frame) cv2.imshow('Video', canvas) if cv2.waitKey(1) & 0xFF == ord('q'): break video_capture.release() cv2.destroyAllWindows()
Because of the digital transformation in each and every industry, getting the best by using AI solutions had been emerging as very important. AI-powered applications or solutions are continuously learning to improve over time so if the companies that get AI to market first will have enormous advantages. Meanwhile, the competitors find it difficult to catch up. So use the evergreen solutions from IBM Watson for disruptive results in the market.
Leave A Comment