AttributeError: module 'mediapipe.python.solutions.holistic' has no attribute 'FACE_CONNECTIONS'
时间: 2023-11-05 17:56:06 浏览: 36
报错信息"AttributeError: module 'mediapipe.python.solutions.holistic' has no attribute 'FACE_CONNECTIONS'"是由于MediaPipe库在某个版本的更新中将'FACE_CONNECTIONS'改为了'FACEMESH_CONTOURS'所致。解决这个问题的方法是将代码中的'FACE_CONNECTIONS'改为'FACEMESH_CONTOURS'即可正常运行。
相关问题
AttributeError: module 'mediapipe.python.solutions.face_mesh' has no attribute 'FACE_CONNECTIONS'
当你在使用MediaPipe库中的人脸面部检测功能时,出现了AttributeError: module 'mediapipe.python.solutions.face_mesh' has no attribute 'FACE_CONNECTIONS'的错误。根据资料,这个错误通常是由于MediaPipe库的更新导致的。在某个版本的更新中,FACE_CONNECTIONS被改为了FACEMESH_CONTOURS。
要解决这个问题,你需要将你的代码中的FACE_CONNECTIONS改为FACEMESH_CONTOURS。这样更改后,你的代码应该可以正常运行了。请确保你的MediaPipe库是最新版本,以确保你的代码与库的版本兼容。
总结起来,解决AttributeError: module 'mediapipe.python.solutions.face_mesh' has no attribute 'FACE_CONNECTIONS'的方法是将代码中的FACE_CONNECTIONS改为FACEMESH_CONTOURS,并确保使用的MediaPipe库是最新版本。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [AttributeError: module 'tensorflow.compat.v1' has no attribute '](https://download.csdn.net/download/qq_38766019/86272235)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [解决module ‘mediapipe.python.solutions.face_mesh‘ has no attribute ‘FACE_CONNECTIONS‘问题的办法](https://blog.csdn.net/Superman980527/article/details/126942219)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [[MediaPipe] 解决AttributeError: module mediapipe.python.solutions.holistic has ... FACE_CONNECTIONS...](https://blog.csdn.net/u013978102/article/details/130714176)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
AttributeError: module 'mediapipe.python.solutions.holistic' has no attribute 'FaceLandmark'
这个错误是因为在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绘制嘴巴的轮廓。