【发布时间】: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