INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
Traceback (most recent call last):
  File "e:\Coding\Python\Face_Reg\test.py", line 13, in <module>
    fingers = detector.fingersUp('*')
  File "C:\Users\aarav\AppData\Local\Programs\Python\Python310\lib\site-packages\cvzone\HandTrackingModule.py", line 111, in fingersUp
    myHandType = myHand["type"]
TypeError: string indices must be integers

this is my code:


import cv2
import cvzone.SerialModule
from cvzone.HandTrackingModule import HandDetector

cap = cv2.VideoCapture(0)
detector = HandDetector(maxHands = 1, detectionCon = 0.7)
#mySerial = cvzone.SerialObject("COM3", 9600, 1)
while True:
    success, img = cap.read()
    hands, img = detector.findHands(img)
    lmList, bbox = detector.findHands(img)
    if lmList:
        fingers = detector.fingersUp('*')
        print(fingers)
       # mySerial.sendData(fingers)
    cv2.imshow("Image", img)
    cv2.waitKey(1)

Hi! I was making a hand tracking software. When I click run the camera pops up but as soon as I put my hand in frame the thing shuts and gives error

2

There are 2 best solutions below

0
Seon On

HandDetector.fingersUp expects a dictionary representing a hand, as returned by HandDetector.findHands.

A working example is provided below:

import cv2
import cvzone.SerialModule
from cvzone.HandTrackingModule import HandDetector

cap = cv2.VideoCapture(0)
detector = HandDetector(maxHands = 1, detectionCon = 0.7)
while True:
    success, img = cap.read()
    # The 'flipType' parameter flips the image, making it easier for some detections
    hands, img = detector.findHands(img, flipType=True)
    if hands:
        # Gets fingers for the first detected hand
        fingers = detector.fingersUp(hands[0])
        print(fingers)
       # mySerial.sendData(fingers)
    cv2.imshow("Image", img)
    cv2.waitKey(1)

Note: You might also want to take a look at the project's github, as it contains detailed examples for hand tracking.

0
Bastien BRUNEL On

The error you're seeing originates from this part of your code:

fingers = detector.fingersUp('*')

The error message:

TypeError: string indices must be integers

indicates that the fingersUp function is trying to access string indices with non-integer values.

The problem is likely how you're calling the fingersUp method. Typically, fingersUp would need the landmarks of the detected hand, but you're passing it a string ('*').

The proper way to call the fingersUp method is by passing the landmarks of the detected hand.

Here's a corrected version of your code:

import cv2
import cvzone.SerialModule
from cvzone.HandTrackingModule import HandDetector

cap = cv2.VideoCapture(0)
detector = HandDetector(maxHands=1, detectionCon=0.7)

while True:
    success, img = cap.read()
    hands, img = detector.findHands(img)

    # This will give the landmarks of the detected hand
    lmList, bboxInfo = detector.findPosition(img)
    
    if lmList:
        fingers = detector.fingersUp()
        print(fingers)
    cv2.imshow("Image", img)
    cv2.waitKey(1)

Notice I removed the argument from fingersUp() since the method uses the most recent hand landmarks internally.

Run the modified code and see if it works for you. If any other issues arise, double-check the documentation or examples from your CV course to ensure you're using the methods correctly.