AttributeError: module 'mediapipe.python.solutions.holistic' has no attribute 'FaceLandmark'
时间: 2023-06-22 13:27:20 浏览: 252
这个错误是因为在mediapipe的版本更新中,FaceLandmark的位置发生了变化。在新版本的mediapipe中,FaceLandmark位于mediapipe.python.solutions.face_mesh.FaceMeshLandmark中。
以下是使用新版本mediapipe获取摄像头识别手部、嘴巴、眼睛并分别截取三个区域的Python代码:
```python
import cv2
import mediapipe as mp
# 初始化mediapipe的Hand, Face和Holistic模型
mp_hands = mp.solutions.hands
mp_face = mp.solutions.face_detection
mp_holistic = mp.solutions.holistic
mp_face_mesh = mp.solutions.face_mesh
# 初始化mediapipe的DrawingUtils模块
mp_drawing = mp.solutions.drawing_utils
# 初始化摄像头
cap = cv2.VideoCapture(0)
# 设置截取区域的大小和位置
hand_area = (100, 100, 300, 300) # (x, y, w, h)
face_area = (100, 100, 300, 300) # (x, y, w, h)
eye_area = (100, 100, 300, 300) # (x, y, w, h)
# 用于标记是否检测到手部、脸部和眼睛
hand_detected = False
face_detected = False
eye_detected = False
# 初始化Hand、Face和Holistic模型
with mp_hands.Hands() as hands, mp_face.FaceDetection() as face_detection, mp_holistic.Holistic() as holistic, mp_face_mesh.FaceMesh() as face_mesh:
while True:
# 从摄像头读取一帧图像
ret, image = cap.read()
if not ret:
break
# 将图像转换为RGB格式
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 检测手部
results_hands = hands.process(image)
if results_hands.multi_hand_landmarks:
# 获取手部关键点坐标
hand_landmarks = results_hands.multi_hand_landmarks[0]
# 绘制手部关键点
mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
# 截取手部区域图片
x, y, w, h = hand_area
hand_image = image[y:y+h, x:x+w]
hand_detected = True
# 检测脸部
results_face = face_detection.process(image)
if results_face.detections:
# 获取脸部关键点坐标
face_landmarks = results_face.detections[0].location_data.relative_bounding_box
# 绘制脸部矩形框
h, w, _ = image.shape
x, y, w, h = int(face_landmarks.xmin * w), int(face_landmarks.ymin * h), int(face_landmarks.width * w), int(face_landmarks.height * h)
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
# 截取脸部区域图片
x, y, w, h = face_area
face_image = image[y:y+h, x:x+w]
face_detected = True
# 检测眼睛
results_holistic = holistic.process(image)
if results_holistic.face_landmarks:
# 获取眼睛关键点坐标
left_eye = results_holistic.face_landmarks.landmark[mp_holistic.FACE_CONNECTIONS['left_eye'][0]]
right_eye = results_holistic.face_landmarks.landmark[mp_holistic.FACE_CONNECTIONS['right_eye'][0]]
# 绘制眼睛关键点
mp_drawing.draw_landmarks(image, [left_eye, right_eye], mp_holistic.FACE_CONNECTIONS)
# 截取眼睛区域图片
x, y, w, h = eye_area
eye_image = image[y:y+h, x:x+w]
eye_detected = True
# 检测嘴巴
results_face_mesh = face_mesh.process(image)
if results_face_mesh.multi_face_landmarks:
# 获取嘴巴关键点坐标
mouth_landmarks = results_face_mesh.multi_face_landmarks[0].landmark[mp_face_mesh.FaceMeshLandmark.MOUTH_TOP]
# 绘制嘴巴关键点
mp_drawing.draw_landmarks(image, [mouth_landmarks], mp_face_mesh.FACEMESH_CONTOURS)
# 截取嘴巴区域图片
x, y, w, h = face_area
mouth_image = image[y:y+h, x:x+w]
mouth_detected = True
# 显示图像
cv2.imshow('MediaPipe', image)
# 检查是否已经检测到手部、脸部、眼睛和嘴巴,如果是则退出循环
if hand_detected and face_detected and eye_detected and mouth_detected:
break
# 按下q键退出程序
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放摄像头并关闭所有窗口
cap.release()
cv2.destroyAllWindows()
```
在这个新的代码中,我们使用了mediapipe.python.solutions.face_mesh.FaceMeshLandmark来获取嘴巴的关键点坐标,并使用mediapipe.python.solutions.face_mesh.FACEMESH_CONTOURS绘制嘴巴的轮廓。
阅读全文