Real-Time Smile Detection OpenCV Python With Source Code
The Real-Time Smile Detection OpenCV Python was developed using Python OpenCV, In this article, we are going to build a smile detector using OpenCV which takes in a live feed from a webcam.
The smile/happiness detector that we are going to implement would be a raw one, there exist many better ways to implement it.
A Smile Detection OpenCV Python face data is stored as tuples of coordinates. Here, x and y define the coordinate of the upper-left corner of the face frame, and w and h define the width and height of the frame.
The cv2.rectangle
the function takes in the arguments frame, upper-left coordinates of the face, lower-right coordinates, the RGB code for the rectangle (that would contain within it the detected face), and the thickness of the rectangle.
The roi_gray
defines the region of interest of the face and roi_color
does the same for the original frame.
In this Real-Time Smile Detection OpenCV also includes a downloadable Project With Source Code for free, just find the downloadable source code below and click to start downloading.
By the way, if you are new to Python programming and don’t know what Python IDE to use, I have here a list of the Best Python IDE for Windows, Linux, and Mac OS that will suit you.
I also have here How to Download and Install the Latest Version of Python on Windows.
To start executing Real-Time Smile Detection OpenCV Python With Source Code, make sure that you have installed Python 3.9 and PyCharm on your computer.
Real-Time Smile Detection OpenCV Python With Source Code: Steps on how to run the project
Time needed: 5 minutes
These are the steps on how to run Real-Time Smile Detection OpenCV Python With Source Code
- Step 1: Download the given source code below.
First, download the given source code below and unzip the source code.
- Step 2: Import the project to your PyCharm IDE.
Next, import the source code you’ve downloaded to your PyCharm IDE.
- Step 3: Run the project.
Lastly, run the project with the command “py main.py”
Installed Libraries
import cv2
Complete Source Code
import cv2 # Load the cascade face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml') smile_cascade = cv2.CascadeClassifier('haarcascade_smile.xml') #faces = face_cascade.detectMultiScale(gray, 1.3, 5) def detect(gray, frame): faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: 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] smiles = smile_cascade.detectMultiScale(roi_gray, 1.8, 20) for (sx, sy, sw, sh) in smiles: cv2.rectangle(roi_color, (sx, sy), ((sx + sw), (sy + sh)), (0, 0, 255), 2) return frame video_capture = cv2.VideoCapture(0) while True: # Captures video_capture frame by frame _, frame = video_capture.read() # To capture image in monochrome gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # calls the detect() function canvas = detect(gray, frame) # Displays the result on camera feed cv2.imshow('Video', canvas) # The control breaks once q key is pressed if cv2.waitKey(1) & 0xff == ord('q'): break # Release the capture once all the processing is done. video_capture.release() cv2.destroyAllWindows()
Output:
Download the Source Code below
Summary
Emotion detectors are used in many industries, one being the media industry where it is important for the companies to determine the public reaction to their products.
In this article, we are going to build a smile detector using OpenCV which takes in a live feed from a webcam.
The smile/happiness detector that we are going to implement would be a raw one, there exist many better ways to implement it.
Related Articles
- Code For Game in Python: Python Game Projects With Source Code
- Best Python Projects With Source Code FREE DOWNLOAD
- How to Make a Point of Sale In Python With Source Code
- Python Code For Food Ordering System | FREE DOWNLOAD
- Inventory Management System Project in Python With Source Code
Inquiries
If you have any questions or suggestions about Real-Time Smile Detection OpenCV Python With Source Code, please feel free to leave a comment below.