【问题标题】:Python, cvzone - why do i get this ValueError?Python,cvzone - 为什么我会得到这个 ValueError?
【发布时间】:2021-12-04 05:13:33
【问题描述】:

我正在尝试使用两只手放大图片(手势控制图像缩放),但是当尝试使用两只手时,我得到了这个错误,但我不知道为什么。在制作我的程序时,我遵循了本教程: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)

【问题讨论】:

  • 试试print(detector.findHands(img))len(detector.findHands(img)),你看到了什么输出?
  • findHands方法的源代码是here。它确实应该返回两个项目。执行 kinskukdua 的建议并使用 cvzone.__version__ 检查您的库版本是否较旧。还可以使用 help(detector.findHands) 查看其文档以查看其参数签名及其返回的内容。
  • 当我使用 len(detector.findHands(img)) 时输出为 480
  • 我使用的版本是 cvzone==1.4.1,因为最新版本没有 HandTrackingModule 选项,我需要这个选项来更轻松地制作其他项目

标签: python opencv computer-vision gesture-recognition cvzone


【解决方案1】:

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

我在其他版本中尝试了您的代码,但遇到了与您类似的错误,但在 1.5.0 版本中,您的代码运行良好。

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

【讨论】:

  • 我使用的是 1.4.1 版本,因为我需要它用于其他项目。因为他们从 1.5.0 中删除了 HandTrackingModule 选项,所以我的代码是否有可能适用于我的版本?
  • 不,您必须将函数与 cvzone 的原始代码分离为 2 种不同的方法..
  • 好的,谢谢你的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-12
  • 1970-01-01
相关资源
最近更新 更多