我要執行程式的時候會出現這個錯誤 AttributeError: module 'mediapipe' has no attribute 'solutions'
但我查過網路上的方法像是檔名的問題、版本的問題等等,我試過之後還是一樣,有沒有人有其他的辦法~?
我的code:
import cv2
import mediapipe as mp
import random
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands
cap = cv2.VideoCapture(0)
with mp_hands.Hands(
model_complexity=0,
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as hands:
if not cap.isOpened():
    print("Cannot open camera")
    exit()
run = True         
while True:
    ret, img = cap.read()
    if not ret:
        print("Cannot receive frame")
        break
    img = cv2.resize(img,(540,320))  
    size = img.shape   
    w = size[1]        
    h = size[0]       
    if run:
        run = False    
        rx = random.randint(50,w-50)    
        ry = random.randint(50,h-100)   
        print(rx, ry)
    img2 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)   
    results = hands.process(img2)                 
    if results.multi_hand_landmarks:
        for hand_landmarks in results.multi_hand_landmarks:
            x = hand_landmarks.landmark[7].x * w   
            y = hand_landmarks.landmark[7].y * h   
            print(x,y)
            if x>rx and x<(rx+80) and y>ry and y<(ry+80):
                run = True
            
            mp_drawing.draw_landmarks(
                img,
                hand_landmarks,
                mp_hands.HAND_CONNECTIONS,
                mp_drawing_styles.get_default_hand_landmarks_style(),
                mp_drawing_styles.get_default_hand_connections_style())
    cv2.rectangle(img,(rx,ry),(rx+80,ry+80),(0,0,255),5)   
    cv2.imshow('oxxostudio', img)
    if cv2.waitKey(5) == ord('q'):
        break   
cap.release()
cv2.destroyAllWindows()
我執行沒有問題,確定有WebCam嗎?
cap = cv2.VideoCapture(0)
我的mediapipe版本是0.8.11,升級至最新版也OK。
1.可能是沒有安裝最新的 MediaPipe 模組或是版本不相容的問題。
https://google.github.io/mediapipe/getting_started/install.html
2.將 solutions 改為 holistic。
import cv2
import mediapipe as mp
import random
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_holistic = mp.solutions.holistic
cap = cv2.VideoCapture(0)
with mp_holistic.Holistic(
model_complexity=0,
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as holistic:
if not cap.isOpened():
    print("Cannot open camera")
    exit()
run = True         
while True:
    ret, img = cap.read()
    if not ret:
        print("Cannot receive frame")
        break
    img = cv2.resize(img,(540,320))  
    size = img.shape   
    w = size[1]        
    h = size[0]       
    if run:
        run = False    
        rx = random.randint(50,w-50)    
        ry = random.randint(50,h-100)   
        print(rx, ry)
    img2 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)   
    results = holistic.process(img2)                 
    if results.pose_landmarks:
        for hand_landmarks in results.pose_landmarks:
            x = hand_landmarks.landmark[7].x * w   
            y = hand_landmarks.landmark[7].y * h   
            print(x,y)
            if x>rx and x<(rx+80) and y>ry and y<(ry+80):
                run = True
            
            mp_drawing.draw_landmarks(
                img,
                hand_landmarks,
                mp_holistic.POSE_CONNECTIONS,
                mp_drawing_styles.get_default_pose_landmarks_style(),
                mp_drawing_styles.get_default_pose_connections_style())
    cv2.rectangle(img,(rx,ry),(rx+80,ry+80),(0,0,255),5)   
    cv2.imshow('oxxostudio', img)
    if cv2.waitKey(5) == ord('q'):
        break   
cap.release()
cv2.destroyAllWindows()