在边界框中打印得分值

Posted

技术标签:

【中文标题】在边界框中打印得分值【英文标题】:Print score value in bounding box 【发布时间】:2021-11-08 16:47:52 【问题描述】:

我尝试在边界框上添加链接上对象跟踪器的分数:https://github.com/pinczakko/yolov4-deepsort

如何从 track 对象中获取分数?这里是处理检测的方法:

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()
        
        # 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)
        cv2.rectangle(frame, (int(bbox[0]), int(bbox[1]-30)), 
                (int(bbox[0])+(len(class_name)+len(str(track.track_id)))*17, int(bbox[1])), color, -1)
        cv2.putText(frame, class_name + "-" + str(track.track_id),(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, (int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3]))))
    return frame

【问题讨论】:

所以...您需要弄清楚哪个检测与哪个轨道相匹配?深入研究存储库的代码。 update 函数使用了一个 _match 函数。我建议更改代码,以便它返回匹配项,以便您可以使用信息。 【参考方案1】:

如果要打印置信度分数,请更改此行:

cv2.putText(frame, class_name + "-" + str(track.track_id),(int(bbox[0]), int(bbox[1]-10)),0, 0.5, (255,255,255), 1)

进入:

cv2.putText(frame, class_name + "-" + str(track.track_id) + str(scores[track.track_id]),(int(bbox[0]), int(bbox[1]-10)),0, 0.5, (255,255,255), 1)

=========

编辑:

经过一些本地测试,此功能可以正常工作:

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)

    # DEBUG: I do not know why there are tracks than detections objects its why i verify the length if the scores
    # print(len(detections), len(scores), len(tracker.tracks))
    # update tracks
    for i, track in enumerate(tracker.tracks):
        if not track.is_confirmed() or track.time_since_update > 1:
            continue
        bbox = track.to_tlbr()
        class_name = track.get_class()

        # 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)
        cv2.rectangle(frame, (int(bbox[0]), int(bbox[1] - 30)),
                      (int(bbox[0]) + (len(class_name) + len(str(track.track_id))) * 17, int(bbox[1])), color, -1)

        if i < len(scores):
            cv2.putText(frame,
                        class_name + "-" + str(track.track_id) + " - :.2f".format(float(scores[i])),
                        (int(bbox[0]),
                         int(bbox[1] - 10)), 0, 0.5, (255, 255, 255), 1)
        else:
            cv2.putText(frame,
                        class_name + "-" + str(track.track_id),
                        (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, (
                                                                                                    int(bbox[0]),
                                                                                                    int(bbox[1]),
                                                                                                    int(bbox[2]),
                                                                                                    int(bbox[3]))))
    return frame

【讨论】:

应用更改时出现如下错误: cv2.putText(frame, class_name + "-" + str(track.track_id) + str(scores[track.track_id]),(int (bbox[0]), int(bbox[1]-10)),0, 0.5, (255,255,255), 1) IndexError: index 5 is out of bounds for axis 0 with size 5 这是因为轨道索引不是检测索引。这个答案是一个值得尊敬的尝试,但它是错误的。 @ChristophRackwitz 事实上,就像你之前所说的那样,函数 match 对匹配的轨道和不匹配的轨道进行排序。不幸的是,我不知道如何获取正确的索引,因为它需要将这些存储在 deep_sort/trackers. py 中才能轻松访问。

以上是关于在边界框中打印得分值的主要内容,如果未能解决你的问题,请参考以下文章

边界框中的随机点(Lat、Lng)

C ++ / OpenGL - 2D - 如何在矩形边界框中剪切圆

OpenStreetMap pbf 文件 - 在边界框中提取所有街道/道路交叉点

从周围的边界框中提取车牌平行四边形?

如何从 C++ 人脸边界框中提取长度/宽度

在边界框中查询 lon/lat 的数据库/文件?谷歌地图