DeepSORT中tracker.update报错TypeError: tuple indices must be integers
YOLOv8+DeepSORT跟踪报错:tuple indices must be integers or slices, not tuple
问题根源
报错发生在deep_sort/linear_assignment.py的min_cost_matching函数中,执行if col not in indices[:, 1]时触发TypeError,本质是indices不是预期的二维numpy数组,而是元组,导致无法使用数组切片语法。
输入合法性判断
你提到min_cost_matching的输入是两个numpy数组组成的元组——这肯定不符合原DeepSORT代码的要求。原逻辑中,indices参数应该是匈牙利算法返回的二维numpy数组(每行代表一组匹配的行、列索引),出现元组输入的原因通常是:
- 调用匈牙利算法时,直接传递了返回的元组(部分实现会返回
(行索引数组, 列索引数组)的元组),而非转换后的二维数组; - 代码中对匹配结果的处理逻辑出错,把数组转成了元组。
修复步骤
1. 转换匈牙利算法返回值为二维数组
找到调用linear_assignment(或hungarian)函数的代码,若返回的是元组,手动转换为二维数组:
# 假设原返回是(row_indices, col_indices) row_indices, col_indices = linear_assignment(cost_matrix) indices = np.array(list(zip(row_indices, col_indices)))
再将这个indices传入min_cost_matching。
2. 优化匹配列的查询逻辑
原代码用col not in indices[:, 1]效率极低,且依赖数组类型,改成集合查询更稳妥:
# 先提取所有已匹配的列索引,转成集合 matched_cols = set(indices[:, 1]) if col not in matched_cols: # 原有的未匹配处理逻辑
同时可以在min_cost_matching开头加个参数校验,避免后续踩坑:
def min_cost_matching(...): # 新增校验,确保indices是二维numpy数组 if not isinstance(indices, np.ndarray) or indices.ndim != 2: raise ValueError("indices必须是二维numpy数组") # 原函数逻辑
3. 对齐仓库版本兼容性
你混用了nwojke/deep_sort和computervisioneng/object-tracking-yolov8-deep-sort的代码,可能存在版本适配问题:
- 检查
deep_sort/linear_assignment.py里的linear_assignment函数,确认它返回的是二维数组还是元组; - 确保YOLOv8输出的检测结果
dets格式符合要求:必须是[x1, y1, x2, y2, score]结构的numpy数组。
环境信息
- Python版本:3.10.11
- 依赖仓库:nwojke/deep_sort、computervisioneng/object-tracking-yolov8-deep-sort
内容的提问来源于stack exchange,提问作者Toufiq
相关产品推荐
相关产品推荐

