Home Backend Development Python Tutorial python3+dlib implements face recognition and emotion analysis

python3+dlib implements face recognition and emotion analysis

May 30, 2018 pm 02:10 PM
mood identify

This article explains in detail how python3 dlib implements face recognition and emotion analysis through specific codes and steps. Friends in need can refer to it.

1. Introduction

What I want to do is expression (emotion) analysis based on face recognition. I saw that there are many open source libraries available on the Internet, which provides great convenience for development. I chose the dlib library, which is currently used more frequently, for face recognition and feature calibration. Using python also shortens the development cycle.

The official website’s introduction to dlib is: Dlib contains a wide range of machine learning algorithms. All are designed to be highly modular, fast to execute, and extremely simple to use via a clean and modern C API. It is used in a variety of applications including robotics, embedded devices, mobile phones and large high-performance computing environments.

Although the applications are relatively high-end, it is still quite interesting to make a small sentiment analysis software on your PC.

Design the identification method according to your own ideas and ideas. Keras, which is also quite popular at the moment, seems to use changes in mouth shape as an indicator of emotional analysis.

My idea is to use the opening ratio of the mouth, the opening degree of the eyes, and the tilt angle of the eyebrows as three indicators of emotion analysis. However, due to the large differences in appearance between people and the wide range of facial features, my calculation method is also relatively simple. So the recognition efficiency is not very high.

Identification rules:

1. The greater the proportion of the distance between the mouth opening and the width of the facial recognition frame, the more excited the emotion is, which may be very happy, or it may be... Extremely angry.

2. The eyebrows are raised. The smaller the ratio between feature points 17-21 or 22-26 from the top of the facial recognition frame and the height of the recognition frame, it means the eyebrows are raised more strongly, which can express surprise and happiness. The tilt angle of the eyebrows. When you are happy, your eyebrows are usually raised. When you are angry, you frown, and at the same time, your eyebrows are pressed down more strongly.

3. Squint your eyes. People will unconsciously squint their eyes when they laugh heartily, and they will widen their eyes when they are angry or surprised.

System shortcomings: It cannot capture subtle changes in expressions, and can only roughly judge people's emotions, such as happiness, anger, surprise, and naturalness.

System advantages: simple structure and easy to use.

Application areas: smile capture, capture the beauty of the moment, alleviate children's autism, and develop interactive games.

Due to the complexity of human emotions, these expressions cannot completely represent a person's inner emotional fluctuations. To improve the accuracy of judgment, comprehensive evaluation such as heart rate detection and speech processing is required.

2. Development environment setup:

1. Install VS2015, because the latest version of dlib-19.10 requires this version of vscode

2. Install opencv (whl installation):

Download the required version of the whl file from pythonlibs, such as (opencv_python?3.3.0 contrib?cp36?cp36m?win_amd64.whl)
Then use pip locally install Installation. Pay attention to the file location (such as: C:\download\xxx.whl)

3. Install dlib (whl mode installation):

Download various versions of dlib here whl file, then open cmd in the root directory and install it directly.

But in order to learn to use various python example programs in dlib, you still need to download a dlib compressed package.

Visit dlib official website directly to download: http://dlib.net/ml.html

whl files of various versions of dlib: https://pypi.python. org/simple/dlib/

4. If you want to use face model feature calibration, you also need a face shape predictor. This can be trained with your own photos, or you can use the dlib author to A well-trained predictor:

Click to download: http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2

3. Implementation ideas

Four. Specific steps

First use dlib for face recognition:)

import cv2
import dlib
from skimage import io

# 使用特征提取器get_frontal_face_detector
detector = dlib.get_frontal_face_detector()
# dlib的68点模型,使用作者训练好的特征预测器
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
# 图片所在路径
img = io.imread("2.jpg")
# 生成dlib的图像窗口
win = dlib.image_window()
win.clear_overlay()
win.set_image(img)

# 特征提取器的实例化
dets = detector(img, 1)
print("人脸数:", len(dets))

for k, d in enumerate(dets):
    print("第", k+1, "个人脸d的坐标:",
       "left:", d.left(),
       "right:", d.right(),
       "top:", d.top(),
       "bottom:", d.bottom())

    width = d.right() - d.left()
    heigth = d.bottom() - d.top()

    print('人脸面积为:',(width*heigth))
Copy after login

Then instantiate A shape_predictor object uses the dlib author to train the facial feature detector and perform facial feature point calibration.

When calibrating, use the circle method of opencv to add a watermark to the coordinates of the feature points. The content is the serial number and position of the feature points.

 # 利用预测器预测
    shape = predictor(img, d)
    # 标出68个点的位置
    for i in range(68):
      cv2.circle(img, (shape.part(i).x, shape.part(i).y), 4, (0, 255, 0), -1, 8)
      cv2.putText(img, str(i), (shape.part(i).x, shape.part(i).y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255))
    # 显示一下处理的图片,然后销毁窗口
    cv2.imshow('face', img)
    cv2.waitKey(0)
Copy after login

At this point, the information of 68 feature points has been obtained. Next, it is necessary to conduct a comprehensive calculation based on the coordinate information of these 68 feature points as a judgment indicator for each expression.

Based on my judgment indicators mentioned above, first calculate the opening ratio of the mouth. Due to the distance of the person from the camera, the size of the face recognition frame is different. , so the ratio is chosen as the judgment index.

Before selecting the standard value of the indicator, first analyze multiple photos of happy faces. Calculate the average mouth opening ratio when happy.

The following is a data processing method for intercepting human eyebrows. Linear fitting is performed on the five feature points on the left eyebrow, and a linear function straight line is fitted. The slope of the fitted straight line is used to approximately represent the inclination of the eyebrows. degree.

# 眉毛
          brow_sum = 0  # 高度之和
          frown_sum = 0  # 两边眉毛距离之和
          for j in range(17,21):
            brow_sum+= (shape.part(j).y - d.top()) + (shape.part(j+5).y- d.top())
            frown_sum+= shape.part(j+5).x - shape.part(j).x
            line_brow_x.append(shape.part(j).x)
            line_brow_y.append(shape.part(j).y)

          self.excel_brow_hight.append(round((brow_sum/10)/self.face_width,3))
          self.excel_brow_width.append(round((frown_sum/5)/self.face_width,3))
          brow_hight[0]+= (brow_sum/10)/self.face_width    # 眉毛高度占比
          brow_width[0]+= (frown_sum/5)/self.face_width    # 眉毛距离占比

          tempx = np.array(line_brow_x)
          tempy = np.array(line_brow_y)
          z1 = np.polyfit(tempx, tempy, 1) # 拟合成一次直线
          self.brow_k = -round(z1[0], 3)  # 拟合出曲线的斜率和实际眉毛的倾斜方向是相反的
Copy after login

我计算了25个人脸的开心表情的嘴巴张开比例、嘴巴宽度、眼睛张开程度、眉毛倾斜程度,导入excel表格生成折线图:

通过折线图能很明显的看出什么参数可以使用,什么参数的可信度不高,什么参数在那个范围内可以作为一个指标。

同样的方法,计算人愤怒、惊讶、自然时的数据折线图。

通过对多个不同表情数据的分析,得出每个指标的参考值,可以写出简单的表情分类标准:

# 分情况讨论
            # 张嘴,可能是开心或者惊讶
            if round(mouth_higth >= 0.03):
              if eye_hight >= 0.056:
                cv2.putText(im_rd, "amazing", (d.left(), d.bottom() + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8,
                      (0, 0, 255), 2, 4)
              else:
                cv2.putText(im_rd, "happy", (d.left(), d.bottom() + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8,
                      (0, 0, 255), 2, 4)

            # 没有张嘴,可能是正常和生气
            else:
              if self.brow_k <= -0.3:
                cv2.putText(im_rd, "angry", (d.left(), d.bottom() + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8,
                      (0, 0, 255), 2, 4)
              else:
                cv2.putText(im_rd, "nature", (d.left(), d.bottom() + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8,
                      (0, 0, 255), 2, 4)
Copy after login

五、实际运行效果:

识别之后:


The above is the detailed content of python3+dlib implements face recognition and emotion analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
How to implement speech recognition and speech synthesis in C++? How to implement speech recognition and speech synthesis in C++? Aug 26, 2023 pm 02:49 PM

How to implement speech recognition and speech synthesis in C++? Speech recognition and speech synthesis are one of the popular research directions in the field of artificial intelligence today, and they play an important role in many application scenarios. This article will introduce how to use C++ to implement speech recognition and speech synthesis functions based on Baidu AI open platform, and provide relevant code examples. 1. Speech recognition Speech recognition is a technology that converts human speech into text. It is widely used in voice assistants, smart homes, autonomous driving and other fields. The following is the implementation of speech recognition using C++

Face detection and recognition technology implemented using Java Face detection and recognition technology implemented using Java Jun 18, 2023 am 09:08 AM

With the continuous development of artificial intelligence technology, face detection and recognition technology has become more and more widely used in daily life. Face detection and recognition technologies are widely used in various occasions, such as face access control systems, face payment systems, face search engines, etc. As a widely used programming language, Java can also implement face detection and recognition technology. This article will introduce how to use Java to implement face detection and recognition technology. 1. Face detection technology Face detection technology refers to the technology that detects faces in images or videos. in J

Win10 is recognizing the solution for being unable to connect to the internet Win10 is recognizing the solution for being unable to connect to the internet Jul 11, 2023 pm 06:21 PM

When you use a win10 computer, have you ever encountered a situation where you are unable to connect to the internet? If you don't know how to solve this problem, let's take a look at this article. 1. Use the Win+I key combination to bring up the settings window, click to select Network and Internet. 2. Click Ethernet on the left side of the Network and INTERNET window, and then click Change Adapter Options in the right window. 3. In the Network Connection window, right-click Ethernet (desktop, please treat mobile devices as appropriate), and click Disable in the menu. 4. After it is displayed as disabled, right-click the Ethernet mouse and execute the enable command. Once Ethernet is enabled, the problem should be resolved. Here is an introduction to Win10 network failure prompt identification without

An article talking about the traffic sign recognition system in autonomous driving An article talking about the traffic sign recognition system in autonomous driving Apr 12, 2023 pm 12:34 PM

What is a traffic sign recognition system? The traffic sign recognition system of the car safety system, whose English translation is: Traffic Sign Recognition, or TSR for short, uses a front-facing camera combined with a mode to recognize common traffic signs (speed limit, parking, U-turn, etc.). This feature alerts the driver to traffic signs ahead so they can obey them. The TSR function improves safety by reducing the likelihood that drivers will disobey traffic laws such as stop signs and avoid illegal left turns or other unintentional traffic violations. These systems require flexible software platforms to enhance detection algorithms and adjust to traffic signs in different areas. Traffic sign recognition principle Traffic sign recognition is also called TS

How to use Apple Health to track your mood - iOS 17 How to use Apple Health to track your mood - iOS 17 Sep 23, 2023 pm 01:41 PM

In iOS17, Apple has added the ability to track emotions every day in the Health App, giving you a comprehensive understanding of your mental health. Through Health App notifications or Apple Watch, you can record your mood at different times of the day and enter your overall mood for the day. Recording a mood displays a slider with options from very unpleasant to neutral to pleasant. The options are color-coded (very unpleasant is purple, neutral is blue, very pleasant is orange) and the idea is to drag the slider to the closest representation of your current mood. From there, Apple asks you what best describes the feeling and provides a list of emotion-related adjectives that you can choose from. For example, the emotion "very pleasant" has surprise

Huawei will launch the Xuanji sensing system in the field of smart wearables, which can assess the user's emotional state based on heart rate Huawei will launch the Xuanji sensing system in the field of smart wearables, which can assess the user's emotional state based on heart rate Aug 29, 2024 pm 03:30 PM

Recently, Huawei announced that it will launch a new smart wearable product equipped with Xuanji sensing system in September, which is expected to be Huawei's latest smart watch. This new product will integrate advanced emotional health monitoring functions. The Xuanji Perception System provides users with a comprehensive health assessment with its six characteristics - accuracy, comprehensiveness, speed, flexibility, openness and scalability. The system uses a super-sensing module and optimizes the multi-channel optical path architecture technology, which greatly improves the monitoring accuracy of basic indicators such as heart rate, blood oxygen and respiration rate. In addition, the Xuanji Sensing System has also expanded the research on emotional states based on heart rate data. It is not limited to physiological indicators, but can also evaluate the user's emotional state and stress level. It supports the monitoring of more than 60 sports health indicators, covering cardiovascular, respiratory, neurological, endocrine,

How to identify when a table is locked in an Oracle database? How to identify when a table is locked in an Oracle database? Mar 09, 2024 pm 01:09 PM

In Oracle database, table being locked is a common situation. How to identify and solve this problem is one of the challenges that database administrators often face. This article will introduce how to identify table locks in Oracle databases, and give specific code examples to help database administrators quickly locate and solve table lock problems. 1. How to identify when a table is locked? 1. View the V$LOCK view The V$LOCK view is an important view used to view lock information in the Oracle database. we can

Has facial recognition for pets been implemented? Has facial recognition for pets been implemented? Apr 13, 2023 pm 07:52 PM

Translator | Reviewed by Cui Hao | Sun Shujuan Technology that can accurately identify animals will help owners reunite with lost pets, farmers monitor their livestock, and researchers study wild animals. Based on the above application scenarios, microchips are the most popular pet identification method. However, implanting a chip requires invasive surgery, specialized equipment to read the chip, and the risk of a thief extracting the microchip. Another method is DNA analysis, which although accurate, is also very expensive and time-consuming. Today I want to introduce to you how to identify animals through facial recognition. 1. Pet facial recognition using computer vision software Pet facial recognition by computer vision solutions can be used as an alternative to the above solutions. Despite its shortcomings,

See all articles