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

AttributeError求助:'cv2.dnn.DetectionModel'对象无setInput属性(YOLO检测)

解决AttributeError: 'cv2.dnn.DetectionModel' object has no attribute 'setInput'
  • 错误原因:cv2.dnn.DetectionModel是OpenCV封装的高层目标检测模型类,它没有setInput方法——这个方法属于底层的cv2.dnn.Net类,你混淆了两类模型的调用流程。

  • 修正方案:

    1. 对于DetectionModel,不需要单独调用setInput,直接将图像传入detect()方法即可
    2. 先通过setInputParams()设置输入参数(比如尺寸、缩放因子、均值等),再执行检测

    示例修正代码:

    # 初始化DetectionModel
    model = cv2.dnn.DetectionModel(weightsPath, configPath)
    # 设置输入参数(根据你的YOLO版本调整)
    model.setInputParams(size=(640, 640), scale=1/255, swapRB=True)
    # 直接传入图像执行检测,不需要setInput
    classes, scores, boxes = model.detect(img, confThreshold=0.5, nmsThreshold=0.4)
    
  • 额外说明:如果坚持使用cv2.dnn.Net的流程,可改用cv2.dnn.readNetFromDarknet()加载模型,此时就能调用setInput(),再通过forward()获取输出,但后续需要自行处理检测结果(比如非极大值抑制),而DetectionModel已经封装了这些步骤。

内容的提问来源于stack exchange,提问作者Priya Priya Lakshmi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 15:03:09