ByteTrack传入检测结果报错:TypeError元组索引类型错误
问题
无法将YOLOv5的检测结果传入ByteTrack,按官方指引尝试后失败,代码如下:
# Perform object detection with YOLOv5 results = model(frame) #print(results) # Process detection results bbs = [] for det in results.pandas().xyxy[0].iterrows(): _, detection = det xmin = float(detection.xmin) ymin = float(detection.ymin) xmax = float(detection.xmax - xmin) ymax = float(detection.ymax - ymin) confidence = float(detection.confidence) class_id = int(detection.name) if class_id >= 0 and class_id < len(class_labels): class_label = class_labels[class_id] if confidence > conf_threshold: # 0.4 # Append detection to the list of bounding boxes bbs.append((xmin, ymin, xmax, ymax, confidence, class_id)) #print(bbs) dets = (bbs, num_classes) #print(dets) # Pass detection results to BYTETracker online_targets = tracker.update(dets, img_size) #, info_imgs
报错信息:
TypeError Traceback (most recent call last) Cell In[17], line 77 73 dets = (bbs, num_classes) 74 #print(dets) 75 76 # Pass detection results to BYTETracker ---> 77 online_targets = tracker.update(dets, img_size) #, info_imgs 79 # Process tracking results 80 for target in online_targets: File ~\AppData\Local\anaconda3\envs\bytetrackpypi\lib\site-packages\bytetracker\byte_tracker.py:186, in BYTETracker.update(self, dets, _) 183 lost_stracks = [] 184 removed_stracks = [] ---> 186 xyxys = dets[:, 0:4] 187 xywh = xyxy2xywh(xyxys) 188 confs = dets[:, 4] TypeError: tuple indices must be integers or slices, not tuple
解决方案
问题根源
- 传入ByteTrack的
dets格式错误:ByteTrack的update方法需要Numpy数组格式的检测结果,你却传了一个包含列表和num_classes的元组,导致代码尝试对元组做数组切片操作时报错。 - 边界框格式错误:你把
xmax和ymax计算成了宽高(xmax - xmin、ymax - ymin),但ByteTrack需要的是原始的xmax、ymax坐标,即(xmin, ymin, xmax, ymax)格式。
修改步骤
- 先导入Numpy(如果未导入):
import numpy as np
- 修正边界框计算逻辑:
将代码中这两行:
xmax = float(detection.xmax - xmin) ymax = float(detection.ymax - ymin)
修改为:
xmax = float(detection.xmax) ymax = float(detection.ymax)
- 转换检测结果为Numpy数组,无需打包num_classes:
将:
dets = (bbs, num_classes)
修改为:
# 若有检测结果则转成Numpy数组,无结果则传空数组 dets = np.array(bbs) if bbs else np.empty((0, 6))
- 正常调用ByteTrack的update方法:
online_targets = tracker.update(dets, img_size)
说明
ByteTrack的update方法要求dets是形状为(N,6)的Numpy数组,每一行对应一个检测框的xmin, ymin, xmax, ymax, confidence, class_id,修改后即可符合要求,解决报错。
内容的提问来源于stack exchange,提问作者Yiannis
相关产品推荐
相关产品推荐

