Facial emotion identification was a familiar problem in the world of computer vision. Although we have already many libraries that can help create an emotion detection system more convenient than in the past, we will depend on the ability of that and its pre-trained models. So, if you want to expand the recognition capabilities to your liking in the easiest way, welcome to my project. I will choose a very humanistic theme that is "Smiles".
The main idea here is that we will define the shape of the mouth and the threshold instead of using advanced algorithms like Machine Learning or Deep Learning for smile detection.
I hope you can understand some of the formulas below:
-
How to calculate length of vector? Formula for the Length of a Vector
-
Công thức tính điểm trung bình đoạn thẳng
-
Dlib
pip install dlib
-
Opencv
pip install opencv-python
-
Dowload the file shape_predictor_68_face_landmarks.dat tại đây
The landmark file that look like this :Each white point in the image is a landmark for your face, the effect of this file will give us a model that defines 68 landmarks on the face, we will use the markers to define the shape of the mouth!
import dlib
import cv2
from math import sqrt
predictor = dlib.shape_predictor('./shape_predictor_68_face_landmarks.dat')
detector = dlib.get_frontal_face_detector()
vs = cv2.VideoCapture(0)
while True:
check, frame = vs.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
for rect in dets:
x = rect.left()
y = rect.top()
w = rect.right()
h = rect.bottom()
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 1)
landmark = predictor(gray, rect)
for k, d in enumerate(landmark.parts()):
if(k>=60 and k<=68):
lines.append((d.x,d.y))
...