七仔的博客

七仔的博客GithubPages分博

0%

【“魔镜”系列】基于dlib的情绪识别

首先dlib库进行人脸识别与特征标定获得人脸对应的68个特征点,然后通过嘴巴张开距离占面部识别框宽度的比例、眉毛上扬程度、眯眼睛程度对用户情绪进行判定

【“魔镜”系列】基于dlib的情绪识别

原理

首先dlib库进行人脸识别与特征标定获得人脸对应的68个特征点,然后通过嘴巴张开距离占面部识别框宽度的比例、眉毛上扬程度、眯眼睛程度对用户情绪进行判定
参考:python3+dlib人脸识别及情绪分析

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class EmotionRecognition:
def __init__(self):
self.detector = dlib.get_frontal_face_detector() # 使用特征提取器get_frontal_face_detector
self.predictor = dlib.shape_predictor(DATA_PATH) # dlib的68点模型,使用训练好的特征预测器

def learning_face(self, path):
im_read = cv2.imread(path, 1)
face_list = self.detector(cv2.cvtColor(im_read, cv2.COLOR_RGB2GRAY), 0) # 对灰度图像使用检测器检测
if len(face_list) != 0: # 检测器检测到人脸
for i in range(len(face_list)): # 获取68个特征点
for k, d in enumerate(face_list): # 获取索引和数据,k为索引,d为对象
box_width = d.right() - d.left() # 计算人脸框边长
appear = self.predictor(im_read, d) # 获取68个特征点坐标
mouth_higth = (appear.part(66).y - appear.part(62).y) / box_width
# 眉毛直线拟合列表
brow_x = []
brow_y = []
for j in range(17, 21):
brow_x.append(appear.part(j).x)
brow_y.append(appear.part(j).y)
z1 = np.polyfit(np.array(brow_x), np.array(brow_y), 1)
brow_k = -round(z1[0], 3) # 拟合成一条直线
eye_hight = ((appear.part(41).y - appear.part(37).y + appear.part(40).y - appear.part(38).y +
appear.part(47).y - appear.part(43).y + appear.part(46).y - appear.part(44).y)
/ 4) / box_width # 获取眼睛睁开大小
upload_qiniu(path) # 上传至图床
if round(mouth_higth >= 0.03):
if eye_hight >= 0.056:
send_face_data_save(path, "3") # 惊讶
return json.dumps({"state": "success", "result": "amazing"})
else:
send_face_data_save(path, "4") # 开心
return json.dumps({"state": "success", "result": "happy"})
else:
if brow_k <= -0.3:
send_face_data_save(path, "1") # 生气
return json.dumps({"state": "success", "result": "angry"})
else:
send_face_data_save(path, "2") # 正常
return json.dumps({"state": "success", "result": "nature"})
break # 只识别一张人脸
return json.dumps({"state": "error", "result": "no face"}) # 未识别到人脸

此为博主副博客,留言请去主博客,转载请注明出处:https://www.baby7blog.com/myBlog/75.html

欢迎关注我的其它发布渠道