import cv2 as cv
import csv
from cvzone.HandTrackingModule import HandDetector
import cvzone as cvz
# Declaring the cap variable to start Camera activity
cap = cv.VideoCapture(0)
cap.set(3, 1280)
cap.set(4, 720)
detector = HandDetector(minTrackCon=0.8)
# Class to access the elements of Que.csv file via indexing
class Questions:
def __init__(self, data):
self.question = data[0]
self.choice1 = data[1]
self.choice2 = data[2]
self.choice3 = data[3]
self.choice4 = data[4]
self.answer = int(data[5])
self.userAnswer = None
# importing CSV file
getFile = 'Que.csv'
with open(getFile, newline='\n') as file:
reader = csv.reader(file)
datafile = list(reader)[1:]
# Creating Object for Each
mcqList = []
for que in datafile:
mcqList.append(Questions(que))
print(len(mcqList))
queNumber = 0
queTotal = len(datafile)
while True:
ret, frame = cap.read()
# To Flip the camera view to the original side
hands, frame = detector.findHands(frame, flipType=True)
mcq = mcqList[0]
frame, box = cvz.putTextRect(frame, mcq.question, [50, 50], 2, 2, offset=15, border=4)
frame, box1 = cvz.putTextRect(frame, mcq.choice1, [50, 115], 1, 2, offset=15, border=4)
frame, box2 = cvz.putTextRect(frame, mcq.choice2, [150, 115], 1, 2, offset=15, border=4)
frame, box3 = cvz.putTextRect(frame, mcq.choice3, [50, 165], 1, 2, offset=15, border=4)
frame, box4 = cvz.putTextRect(frame, mcq.choice4, [150, 165], 1, 2, offset=15, border=4)
# Checking for a Hand
if hands:
lmList = hands[0]['lmList']
length, info, frame = detector.findDistance(lmList[8],lmList[12],frame)
print(length)
cv.imshow('frame', frame)
if cv.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv.destroyAllWindows()
I have tried to solve this using different methods but the error still persists. The error message is shown below...
Traceback (most recent call last):
File "e:\CODE\.vscode\OpenCV\main.py", line 57, in <module>
length, info = detector.findDistance(lmList[0],lmList[12])
File "C:\Users\User1\AppData\Local\Programs\Python\Python310\lib\site-packages\cvzone\HandTrackingModule.py", line 143, in findDistance
x1, y1 = p1
ValueError: too many values to unpack (expected 2)
I have tried using different versions of mediapipe as well but still the issue exists....
I'm Expecting the code to detect the distance between my two finger's
For example in the above code I have selected my index finger tip that is lmList[8] and my middle finger tip that is lmList[12] and now i want the code to tell me the distance between both of them.
According to the cvzone HandTrackingModule module source code, each lmList
element contains three values: px
, py
, and pz
. So, you should pass the first two elements instead of all three elements:
...
length, info = detector.findDistance(lmList[0][:2],lmList[12][:2])
...
To see an example within the library itself, you can refer to HandTrackingModule.py#L189.