Python,cvzone为什么我会得到这个值错误?

2024-05-19 15:53:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用双手放大图片(手势控制的图像缩放),但当我尝试用双手放大图片时,我发现了这个错误,但我不知道为什么。制作程序时,我遵循了以下教程:https://www.youtube.com/watch?v=VPaFV3QBsEw&t=675s。这很奇怪,因为这个程序对他有效

这是我得到的错误:

  hands, img = detector.findHands(img)

ValueError: too many values to unpack (expected 2)

这是我的代码:

import cv2
from cvzone.HandTrackingModule import HandDetector
 
cap = cv2.VideoCapture(0)
cap.set(3, 1280)
cap.set(4, 720)
 
detector = HandDetector(detectionCon=0.7)
startDist = None
scale = 0
cx, cy = 500,500

while True:
    success, img = cap.read()
    hands, img = detector.findHands(img)
    img1 = cv2.imread("kung_fu_panda.png")
 
    if len(hands) == 2:
        
        if detector.fingersUp(hands[0]) == [1, 1, 0, 0, 0] and \
                detector.fingersUp(hands[1]) == [1, 1, 0, 0, 0]:
         
            lmList1 = hands[0]["lmList"]
            lmList2 = hands[1]["lmList"]
            # point 8 is the tip of the index finger
            if startDist is None:
                length, info, img = detector.findDistance(hands[0]["center"], hands[1]["center"], img)
                startDist = length
 
            length, info, img = detector.findDistance(hands[0]["center"], hands[1]["center"], img)
 
            scale = int((length - startDist) // 2)
            cx, cy = info[4:]
            print(scale)
    else:
        startDist = None
 
    try:
        h1, w1, _= img1.shape
        newH, newW = ((h1+scale)//2)*2, ((w1+scale)//2)*2
        img1 = cv2.resize(img1, (newW,newH))
 
        img[cy-newH//2:cy+ newH//2, cx-newW//2:cx+ newW//2] = img1
    except:
        pass
 
    cv2.imshow("Image", img)
    cv2.waitKey(1)

Tags: noneimgcv2detectorlengthimg1capcenter
1条回答
网友
1楼 · 发布于 2024-05-19 15:53:20

cvzone库每次都会不断更新其库。正如您在视频开头看到的,当他导入cvzone包时,他使用cvzone版本1.5.0

我在其他版本中尝试了你的代码,得到了一个与你类似的错误,但是在1.5.0版本中,你的代码运行得非常好

您可以使用my answer here将项目中cvzone库的版本更改为1.5.0

相关问题 更多 >