YOLOv4-DeepSort目标跟踪如何从track对象取分数并打印到边界框
解决方法
你使用的YOLOv4-DeepSort项目中,Track对象默认已经存储了匹配到的检测目标置信度,直接通过track.conf属性即可获取。如果需要在边界框上标注该值,只需修改你现有代码中绘制文本的逻辑即可,修改后的代码如下:
def process_detections(tracker, detections, nms_max_overlap, frame): """ Process objects detected by YOLO with deepsort Returns processed frame """ #initialize color map cmap = plt.get_cmap('tab20b') colors = [cmap(i)[:3] for i in np.linspace(0, 1, 20)] # run non-maxima supression boxs = np.array([d.tlwh for d in detections]) scores = np.array([d.confidence for d in detections]) classes = np.array([d.class_name for d in detections]) indices = preprocessing.non_max_suppression(boxs, classes, nms_max_overlap, scores) detections = [detections[i] for i in indices] # Call the tracker tracker.predict() tracker.update(detections) # update tracks for track in tracker.tracks: if not track.is_confirmed() or track.time_since_update > 1: continue bbox = track.to_tlbr() class_name = track.get_class() # 新增:获取跟踪置信度,保留两位小数 track_conf = round(track.conf, 2) # draw bbox on screen color = colors[int(track.track_id) % len(colors)] color = [i * 255 for i in color] cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), color, 1) # 拼接包含置信度的标注文本 label_text = f"{class_name}-{track.track_id} 置信度:{track_conf}" # 调整顶部填充框宽度适配新增的文本长度 cv2.rectangle(frame, (int(bbox[0]), int(bbox[1]-30)), (int(bbox[0]) + len(label_text)*17, int(bbox[1])), color, -1) cv2.putText(frame, label_text,(int(bbox[0]), int(bbox[1]-10)),0, 0.5, (255,255,255), 1) # if enable info flag then print details about each track if FLAGS.info: print("Tracker ID: {}, Class: {}, 置信度: {}, BBox Coords (xmin, ymin, xmax, ymax): {}".format(str(track.track_id), class_name, track_conf, (int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])))) return frame
如果你使用的版本没有内置track.conf属性,可以手动补充该属性:
- 找到项目下
deep_sort/track.py文件,在Track类的__init__方法中新增一行self.conf = 0.0 - 找到同文件中的
update方法,在匹配到检测框的逻辑分支下新增一行self.conf = detection.confidence即可。
内容的提问来源于stack exchange,提问作者user3410517
相关产品推荐
相关产品推荐

