【问题标题】:Python opencv/cvzone - UnboundLocalErrorPython opencv/cvzone - UnboundLocalError
【发布时间】:2021-12-01 04:50:28
【问题描述】:

我正在尝试制作 ar 鼠标(相机会检测到您的手,而不是您的手指会成为鼠标)。但是当检查哪些手指在上面时,我得到了一个错误。在制作这个程序时,我遵循了这个教程:https://www.youtube.com/watch?v=8gPONnGIPgw&t=332s。这样做时,我做了一件不同的事情,那就是我没有像他那样制作一个名为 HandTrackingModule.py 的程序文件,而是导入它,但我只是从 cvzone.HandTrackingModule import HandDetector 导入,它应该工作相同。

这是错误:

fingers =检测器.fingersUp()

手指向上 返回手指

UnboundLocalError:在赋值之前引用了局部变量“fingers”

这是代码:

import cv2
import numpy as np
import time
import autopy
from cvzone.HandTrackingModule import HandDetector

wCam, hCam = 640, 480
frameR = 100 # Frame Reduction
smoothening = 7
 
pTime = 0
plocX, plocY = 0, 0
clocX, clocY = 0, 0
 
cap = cv2.VideoCapture(0)
cap.set(3, wCam)
cap.set(4, hCam)
detector = HandDetector(detectionCon=0.7,maxHands=1)
wScr, hScr = autopy.screen.size()
 
while True:
    # 1. Find hand Landmarks
    success, img = cap.read()
    img = detector.findHands(img)
    lmList, bbox = detector.findPosition(img)

    # 2. Get the tip of the index and middle fingers
    if len(lmList) != 0:
        x1, y1 = lmList[8][:1]
        x2, y2 = lmList[12][:1]
    
    # 3. Check which fingers are up
    fingers = detector.fingersUp() #------------------this is where the error happens
    print(fingers)
    cv2.rectangle(img, (frameR, frameR), (wCam - frameR, hCam - frameR),
    (255, 0, 255), 2)

    # 4. Only Index Finger : Moving Mode
    if fingers[1] == 1 and fingers[2] == 0:
        # 5. Convert Coordinates
        x3 = np.interp(x1, (frameR, wCam - frameR), (0, wScr))
        y3 = np.interp(y1, (frameR, hCam - frameR), (0, hScr))

        # 6. Smoothen Values
        clocX = plocX + (x3 - plocX) / smoothening
        clocY = plocY + (y3 - plocY) / smoothening
    
        # 7. Move Mouse
        autopy.mouse.move(wScr - clocX, clocY)
        cv2.circle(img, (x1, y1), 15, (255, 0, 255), cv2.FILLED)
        plocX, plocY = clocX, clocY
        
    # 8. Both Index and middle fingers are up : Clicking Mode
    if fingers[1] == 1 and fingers[2] == 1:

        # 9. Find distance between fingers
        length, img, lineInfo = detector.findDistance(8, 12, img)

        # 10. Click mouse if distance short
        if length < 40:
            cv2.circle(img, (lineInfo[4], lineInfo[5]),
            15, (0, 255, 0), cv2.FILLED)
            autopy.mouse.click()

    # 12. Display
    cv2.imshow("Image", img)
    cv2.waitKey(1)

【问题讨论】:

  • 这还不是全部代码。提供您尚未显示的代码(detector 的源代码)
  • 我告诉过你,我只是从 cvzone 导入 HandDetector,而不是自己构建它

标签: python opencv camera cvzone


【解决方案1】:

您的代码缩进错误。

# 2. Get the tip of the index and middle fingers
if len(lmList) != 0:
    x1, y1 = lmList[8][:1]
    x2, y2 = lmList[12][:1]

    # 3. Check which fingers are up
    fingers = detector.fingersUp() #------------------this is where the error happens
    print(fingers)
    cv2.rectangle(img, (frameR, frameR), (wCam - frameR, hCam - frameR),
    (255, 0, 255), 2)

    # 4. Only Index Finger : Moving Mode
    if fingers[1] == 1 and fingers[2] == 0:
        # 5. Convert Coordinates
        x3 = np.interp(x1, (frameR, wCam - frameR), (0, wScr))
        y3 = np.interp(y1, (frameR, hCam - frameR), (0, hScr))

        # 6. Smoothen Values
        clocX = plocX + (x3 - plocX) / smoothening
        clocY = plocY + (y3 - plocY) / smoothening
    
        # 7. Move Mouse
        autopy.mouse.move(wScr - clocX, clocY)
        cv2.circle(img, (x1, y1), 15, (255, 0, 255), cv2.FILLED)
        plocX, plocY = clocX, clocY
        
    # 8. Both Index and middle fingers are up : Clicking Mode
    if fingers[1] == 1 and fingers[2] == 1:

        # 9. Find distance between fingers
        length, img, lineInfo = detector.findDistance(8, 12, img)

        # 10. Click mouse if distance short
        if length < 40:
            cv2.circle(img, (lineInfo[4], lineInfo[5]),
            15, (0, 255, 0), cv2.FILLED)
            autopy.mouse.click()

【讨论】:

  • 感谢代码现在开始但是当它检测到手时我得到这个错误:x1, y1 = lmList[8][:1] ValueError: no enough values to unpack (expected 2, got 1) ,我尝试将 [8][:1] 添加到错误行并将 x1 和 y1 更改为 int 但是当我运行它时,鼠标只会沿对角线移动,有什么想法吗?
  • 试试这个x1,y1=lmList[8][:2] x2,y2=lmList[12][:2]
猜你喜欢
  • 2022-07-08
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
相关资源
最近更新 更多