我正在关注 Murtaza 的研讨会 - 机器人与人工智能 (https://www.youtube.com/watch?v=p5Z_GGRCI5s&ab_channel=Murtaza%27sWorkshop-RoboticsandAI) 我收到一个错误:
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
([], [])
Traceback (most recent call last):
File "C:\Users\thepr\PycharmProjects\pythonProject2\finger.py", line 22, in <module>
if lmList[tipIds[id]][2] < lmList[tipIds[id] - 2][2]:
~~~~~~^^^^^^^^^^^^
IndexError: tuple index out of range
Process finished with exit code 1
这是我的代码:
import cv2
import time
from cvzone.HandTrackingModule import HandDetector
##import HandTrackingModule as htm
wcam, hcma = 640 ,480
cap = cv2.VideoCapture(0)
cap.set(3, wcam)
cap.set(4, hcma)
pTime = 0
detector = HandDetector(detectionCon=0.7, maxHands=5)
tipIds = [4,8,12,16,20]
while True:
sccuess, img = cap.read()
img = detector.findHands(img)
lmList = detector.findPosition(img, draw=False)
print(lmList)
if len(lmList) != 0:
fingers = []
for id in range (0,5):
if lmList[tipIds[id]][2] < lmList[tipIds[id] - 2][2]:
fingers.append(1)
else:
fingers.append(0)
cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime
cv2.putText(img,f'fps: {int(fps)}',(70,40), cv2.FONT_HERSHEY_PLAIN,3,(255,255,0),3)
cv2.imshow("imgge",img)
cv2.waitKey(1)
当我按照视频中的说明操作时,为什么会出现此错误?
看起来您正在使用 cv2 库的早期版本,该库仍然具有
findPosition
方法(参见 here)。但是,当查看您得到的输出 (([], [])
) 以及如何使用该方法时,在基本示例中:
lmList, bbox = detector.findPosition(img)
...看起来您不应该将返回值分配给
lmList
,而应将第一个元组成员分配给lmList
。
所以改变这一行:
lmList = detector.findPosition(img, draw=False)
至:
lmList,_ = detector.findPosition(img, draw=False)
我猜该视频是用该模块的更早版本制作的,其中返回值只是列表,正如我们在视频中清楚地看到一个列表的输出,而不是两个列表的元组。