You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

手跟踪软件运行报错求助:TypeError: string indices must be integers

问题分析与修复

错误根源

  • 重复调用findHands:代码里连续两次调用detector.findHands(img),第二次调用会覆盖第一次的检测结果,且错误地将返回值赋值给lmList, bbox——findHands默认返回(hands, img)(hands是包含手信息的字典列表),而非关键点列表和边界框。
  • fingersUp参数错误:该方法需要传入单个手的字典(从hands列表中获取),而非字符串'*'。传入字符串后,方法内部尝试用字典索引访问字符串,触发TypeError。

修正后的代码

import cv2
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()
    # 仅调用一次findHands,获取检测到的手列表和绘制后的图像
    hands, img = detector.findHands(img)
    
    if hands:
        # 取检测到的第一只手(因maxHands=1)
        myHand = hands[0]
        # 传入手字典给fingersUp方法
        fingers = detector.fingersUp(myHand)
        print(fingers)
        # mySerial.sendData(fingers)
    
    cv2.imshow("Image", img)
    cv2.waitKey(1)

补充说明

如果需要获取关键点列表lmList和边界框bbox,可搭配detector.findPosition(img)方法使用:

if hands:
    lmList, bbox = detector.findPosition(img)
    myHand = hands[0]
    fingers = detector.fingersUp(myHand)
    print(fingers, lmList)

内容的提问来源于stack exchange,提问作者Aarav Anand

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 06:13:24