You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

MediaPipe库报错AttributeError: module 'mediapipe' has no attribute 'solutions'的解决求助

MediaPipe库报错AttributeError: module 'mediapipe' has no attribute 'solutions'的解决求助

问题描述

我最近刚开始上手MediaPipe库做手势识别,跟着网上的教程一步步写的代码,教程里运行完全没问题,但我这边跑起来就报AttributeError: module 'mediapipe' has no attribute 'solutions'的错误,实在搞不懂哪里出问题了,麻烦各位大佬帮忙看看!

我的完整代码如下:

import cv2
import mediapipe as mp

cap = cv2.VideoCapture(0)

mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils

hands = mp_hands.Hands(min_detection_confidence=0.8, min_tracking_confidence=0.5)

tipIds = [4, 8, 12, 16, 20]

def countFingers(image, hand_landmarks, handNo=0):
    
    if hand_landmarks:
        # Get all Landmarks of the FIRST Hand VISIBLE
        landmarks = hand_landmarks[handNo].landmark
        # print(landmarks)

        # Count Fingers        
        fingers = []

        for lm_index in tipIds:
                # Get Finger Tip and Bottom y Position Value
                finger_tip_y = landmarks[lm_index].y 
                finger_bottom_y = landmarks[lm_index - 2].y

                # Check if ANY FINGER is OPEN or CLOSED
                if lm_index !=4:
                    if finger_tip_y < finger_bottom_y:
                        fingers.append(1)
                        print("FINGER with id ",lm_index," is Open")

                    if finger_tip_y > finger_bottom_y:
                        fingers.append(0)
                        print("FINGER with id ",lm_index," is Closed")

        # print(fingers)
        totalFingers = fingers.count(1)

        # Display Text
        text = f'Fingers: {totalFingers}'

        cv2.putText(image, text, (50,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0), 2)
 
def drawHandLanmarks(image, hand_landmarks):

    # Darw connections between landmark points
    if hand_landmarks:

      for landmarks in hand_landmarks:
               
        mp_drawing.draw_landmarks(image, landmarks, mp_hands.HAND_CONNECTIONS)


while True:
    success, image = cap.read()

    image = cv2.flip(image, 1)
    
    # Detect the Hands Landmarks 
    results = hands.process(image)

    # Get landmark position from the processed result
    hand_landmarks = results.multi_hand_landmarks

    # Draw Landmarks
    drawHandLanmarks(image, hand_landmarks)

    # Get Hand Fingers Position        
    countFingers(image, hand_landmarks)

    cv2.imshow("Media Controller", image)

    
    key = cv2.waitKey(1)
    if key == 32:
        break

cv2.destroyAllWindows()

运行后抛出的错误信息:

AttributeError: module 'mediapipe' has no attribute 'solutions'

我已经按照教程里的方式安装了MediaPipe,但就是出现这个问题,自己排查了半天也没找到原因,有没有遇到过类似问题的朋友能给点解决思路?比如是不是版本问题?或者安装方式不对?


专家解答

哈哈,这个问题我之前踩过坑!大概率是以下几个原因之一,你可以挨个排查试试:

  1. 脚本文件名和MediaPipe库重名了
    这个真的是新手常犯的错误!如果你把自己的代码文件命名成了mediapipe.py,那Python会优先导入你自己写的这个文件,而不是真正的MediaPipe库,自然就找不到solutions子模块了。
    赶紧检查一下你的脚本文件名,要是重名了立刻改掉,顺便把目录下自动生成的__pycache__文件夹删掉,再重新运行试试。

  2. MediaPipe版本过旧或安装不完整
    旧版本的MediaPipe确实没有solutions这个子模块,或者你安装过程中可能因为网络问题导致包没装全。先卸载现有版本,再重装最新版:

    pip uninstall -y mediapipe
    pip install mediapipe
    

    如果你用的是conda环境,也可以试试conda渠道安装:

    conda uninstall -y mediapipe
    conda install -c conda-forge mediapipe
    
  3. Python环境不匹配
    要是你同时有多个Python环境(比如系统默认Python、虚拟环境、Anaconda环境),很可能你在A环境装了MediaPipe,但却在B环境里运行代码。先确认当前环境是不是正确的:

    # 查看当前Python路径(Linux/macOS)
    which python
    # Windows系统用下面这个
    where python
    # 查看当前环境已安装的包
    pip list | grep mediapipe
    

    发现环境不对的话,切换到装了MediaPipe的环境再运行就好。

  4. 源码编译安装出问题
    如果你是从源码编译安装的MediaPipe,可能编译过程中漏了部分模块。这种情况建议直接用pip的预编译包,别自己折腾源码了,除非你有特殊定制需求。

优先排查第1点,这个解决起来最快,不行再试重装,基本就能搞定啦!

火山引擎 最新活动