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

使用Python+cvzone做手指追踪遇TypeError,摄像头无法启动

手指追踪功能开发报错解决

我正在用Python结合cvzone、OpenCV库开发手指追踪功能,代码如下:

import cvzone
import cv2

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

while True:
    success, img = cap.read()
    img = detector.findHands(img)
    lmList, bbox = detector.findPosition(img)

    cv2.imshow("Image", img)
    cv2.waitKey(0)

运行代码时出现以下错误:

TypeError: create_int(): incompatible function arguments. The following argument types are supported:
    1. (arg0: int) -> mediapipe.python._framework_bindings.packet.Packet

Invoked with: 0.7

Process finished with exit code 1

摄像头无法正常启动,解决方法如下:

  • 错误根源:detectionCon参数在当前cvzone版本中要求传入整数类型,你传入的0.7是浮点数,触发了类型不匹配错误。该参数实际接收的是百分比形式的检测置信度(比如70代表70%)。
  • 修复操作:
    1. 将detectionCon=0.7修改为detectionCon=70
    2. 把cv2.waitKey(0)改为cv2.waitKey(1),否则画面会一直卡住无法实时刷新
    3. 补充摄像头资源释放代码,避免程序退出后摄像头被占用

修复后的完整代码:

import cvzone
import cv2

cap = cv2.VideoCapture(1)
detector = cvzone.HandDetector(maxHands=1, detectionCon=70)

while True:
    success, img = cap.read()
    if not success:
        break
    img = detector.findHands(img)
    lmList, bbox = detector.findPosition(img)

    cv2.imshow("Image", img)
    # 按下q键退出程序
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# 释放摄像头和窗口资源
cap.release()
cv2.destroyAllWindows()

内容的提问来源于stack exchange,提问作者Mohamed Belaid

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 02:17:10