当然可以,我们只需要计算眉毛的中心点和嘴巴的中心点,然后计算两个中心点之间的夹角即可。以下是修改后的代码:
```python
import cv2
import math
# 加载人脸检测器和关键点检测器
face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
keypoint_detector = cv2.face.createFacemarkLBF()
keypoint_detector.loadModel('lbfmodel.yaml')
# 读取图像
img = cv2.imread('test.jpg')
# 转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 检测人脸
faces = face_detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
# 如果没有检测到人脸,则退出程序
if len(faces) == 0:
print("No faces found")
exit()
# 提取第一个人脸
(x, y, w, h) = faces[0]
# 提取人脸ROI
face_roi = gray[y:y+h, x:x+w]
# 检测人脸关键点
_, landmarks = keypoint_detector.fit(gray, faces)
# 将关键点转换为numpy数组
landmarks = landmarks[0][0]
# 计算左眼和右眼的中心点坐标
left_eye_center = (int((landmarks[36][0] + landmarks[39][0]) / 2), int((landmarks[36][1] + landmarks[39][1]) / 2))
right_eye_center = (int((landmarks[42][0] + landmarks[45][0]) / 2), int((landmarks[42][1] + landmarks[45][1]) / 2))
# 计算眉毛的中心点坐标
left_eyebrow_center = (int((landmarks[17][0] + landmarks[21][0]) / 2), int((landmarks[17][1] + landmarks[21][1]) /