Mediapipe 的手部检测问题:NameError:未定义名称“results”

Hand detection issue with Mediapipe: NameError: name 'results' is not defined

提问人:Gerry 提问时间:10/21/2023 最后编辑:Christoph RackwitzGerry 更新时间:10/23/2023 访问量:41

问:

在 Windows 11 上,我正在使用 PyCharm,我是初学者。 我加载了 OpenCV vers.4.8.1.78 和 Mediapipe vers.0.10.7 包。我还安装了 Python 3.10 解释器。 我想做的是一个通过捕获网络摄像头来检测手部运动的程序。 我有这段代码,它显示一个窗口,我在其中从网络摄像头捕获图像,并且效果很好。

import cv2
import mediapipe as mp

mp_hands = mp.solutions.hands
hands = mp_hands.Hands(static_image_mode=False,
                       max_num_hands=2,
                       min_detection_confidence=0.5,
                       min_tracking_confidence=0.5,
                       )

mp_drawing = mp.solutions.drawing_utils

# if you have second camera you can set first parameter as 1
cap=cv2.VideoCapture(0,cv2.CAP_DSHOW)
# video dimension
cap.set(3,715)
cap.set(4,715)

if not (cap.isOpened()):
    print("Could not open video device")
while (True):

    # Capture the video frame by frame
    ret, frame = cap.read()

    # flip the image
    frame = cv2.cvtColor(cv2.flip(frame, 1), cv2.COLOR_BGR2RGB)
    # recoloring image
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    cv2.imshow('frame', frame)

    # the 'q' button is set as the quitting button, you may use any desired button of your choice
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# After the loop release the cap object
cap.release()
# Destroy all the windows
cv2.destroyAllWindows()

问题是,如果我插入代码行来检测手的运动,它会给我一个错误,说它无法识别“results.multi_hand_landmarks:”

框架 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

如果results.multi_hand_landmarks: 对于results.multi_hand_landmarks hand_landmarks: mp_drawing.draw_landmarks(frame, hand_landmarks, connections=mp_hands.HAND_CONNECTIONS)

cv2.imshow('帧', 帧)

错误消息是这样的: 文件“C:\Users\plipl\PycharmProjects\pythonProject\main.py”,第 35 行,在 如果results.multi_hand_landmarks: NameError:未定义名称“results”

有人可以帮我吗? 谢谢

Python 检测 名称Error MediaPipe

评论

0赞 Gerry 10/23/2023
我明白问题出在哪里。在调用它之前,我必须创建“结果”变量:frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) results = hands.process(frame) if results.multi_hand_landmarks: for hand_landmarks in results.multi_hand_landmarks: mp_drawing.draw_landmarks(frame, hand_landmarks, connections=mp_hands.HAND_CONNECTIONS) cv2.imshow('frame', frame)

答: 暂无答案