Search code examples
pythonvisual-studio-codevalueerrormediapipecvzone

got a value error while working with opencv


this is my code

import cv2
from cvzone.HandTrackingModule import HandDetector
import keyboard
import time
from pynput.keyboard import Controller
import datetime
import pyautogui
wcam ,hcam = 640,480
framer = 150
cap = cv2.VideoCapture(0)
cap.set(3,wcam)
cap.set(4,hcam)
ptime = 0
detector = HandDetector(detectionCon=0.8, maxHands=2)
wsc,hsc = pyautogui.size()
print(wsc ,hsc)
while True:
    
    success, img = cap.read()
    img = detector.findHands(img)
    lmlist, bbox = detector.findPosition(img)

    if len(lmlist)!=0:
        x1,y1 = lmlist[8][1:]
        x2,y2 = lmlist[12][1:]
        print(x1,x2,y1,y2)

        fingers = detector.fingerUp()
        cv2.rectangle(img, (framer,framer),(wcam-framer , hcam-framer),0,2)
        if fingers[1]==1 and fingers[2]==0:

            x3 = np.interp(x1, (framer,wcam-framer), (0,wsc))
            y3 = np.interp(y1, (framer,hcam-framer), (0,hsc))

            mouse.move(x3,y3)
        if fingers[1]==1 and fingers[2]==1:
           length, img,_ = detector.findDistance(8,12,img)
           print(length)

        


    ctime = time.time()
    fps = 1/(ctime-ptime)
    ptime = ctime
    cv2.putText(img, str(int(fps)), (20,50), cv2.FONT_HERSHEY_PLAIN, 3, 0,5)
    cv2.imshow("window", img)
    cv2.waitKey(1)

this is the error i got

INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
Traceback (most recent call last):
  File "c:\Users\akki kisu\Desktop\gesture contoll\mouse control.py", line 24, in <module>
    x1,y1 = lmlist[8][1:]
ValueError: not enough values to unpack (expected 2, got 1)
[ WARN:1] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-1i5nllza\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
PS C:\Users\akki kisu\Desktop\gesture contoll> 

i was making a gesture contolled mouse and the got this vale error which i am unable to sovle i was not geeting this error but when i switched to from cvzone.HandTrackingModule import HandDetector from import HandTrackingModule as htm i got this error.


Solution

  • so instead of

    x1,y1 = lmlist[8][1:]
    x2,y2 = lmlist[12][1:]
    

    i had to write

    x1,y1 = lmlist[8],lmlist[1]
    x2,y2 = lmlist[12],lmlist[1]