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

如何将文件名传入cv2.imread?解决Vehicle_Counting参数错误

问题解决思路

错误原因

你调用Thesis.VehicleCounting.Vehicle_Counting(win, self.filename)时触发TypeError,核心原因是Vehicle_Counting类的__init__方法只定义了2个位置参数(self+window),但你实际传入了3个(隐式传递的self加上显式的win和self.filename),参数数量不匹配。

解决方法

方法1:适配现有类结构调用

  • 先仅传入win参数实例化Vehicle_Counting类:
    vc_instance = Thesis.VehicleCounting.Vehicle_Counting(win)
    
  • 再调用专门接收文件名的Counter方法:
    vc_instance.Counter(self.filename)
    
    这样就能让Counter方法拿到文件名,执行cv2.imread(self.filename)操作。

方法2:修改Vehicle_Counting类的初始化逻辑

如果希望在实例化类时就传入文件名,可以修改类的__init__方法,新增filename参数:

class Vehicle_Counting:
    def __init__(self, window, filename):
        self.window = window
        self.filename = filename  # 将文件名存为实例属性
    
    def Counter(self):
        # 直接使用实例属性self.filename读取图像
        img = cv2.imread(self.filename)
        # 后续车辆计数逻辑

修改后,就可以按你最初的方式调用类:

Thesis.VehicleCounting.Vehicle_Counting(win, self.filename)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 22:01:08