• 目标检测画图显示图


    这是我见过最好的代码了,摘录自u版本的yolov3里面的。
    首先看画出的效果图:

    每个类用不同颜色框,上面写出类别和分数这些信息,并且是填充。字体大小能够根据图片大小自动调整。

    def plot_one_box(x, img, color=None, label=None, line_thickness=None):
        # Plots one bounding box on image img
        tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1  # line/font thickness
        color = color or [random.randint(0, 255) for _ in range(3)]
        c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
        cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)
        if label:
            tf = max(tl - 1, 1)  # font thickness
            t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
            c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
            cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA)  # filled
            cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)
    
    
    labelmap = (  # always index 0
        'aeroplane', 'bicycle', 'bird', 'boat',
        'bottle', 'bus', 'car', 'cat', 'chair',
        'cow', 'diningtable', 'dog', 'horse',
        'motorbike', 'person', 'pottedplant',
        'sheep', 'sofa', 'train', 'tvmonitor')
    
    colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(labelmap))]
    
    
    ....
    ...
    for i in range(detections.size(1)):
        j = 0
        while detections[0, i, j, 0] >= 0.3:
            score = detections[0, i, j, 0]
            idx_class = i-1
            label_name = labelmap[idx_class]
            label_conf = '%s %.2f' % (label_name, score)
            pt = (detections[0, i, j, 1:]*scale).cpu().numpy()
            coords = (pt[0], pt[1], pt[2], pt[3])
            plot_one_box(coords, img_src, label=label_conf, color=colors[idx_class])
    
            pred_num += 1
            # with open(filename, mode='a') as f:
            #     f.write(str(pred_num)+' label: '+label_name+' score: ' +
            #             str(score) + ' '+' || '.join(str(c) for c in coords) + '
    ')
            j += 1
    
    cv2.imshow("img_src",img_src)
    cv2.waitKey(0)
    

    以上是把目标矩形框一个个画出来,最后再显示的。再来一张图

    好记性不如烂键盘---点滴、积累、进步!
  • 相关阅读:
    Perl Language 从入门到放弃 All In One
    Linux shell command line editor All In One
    TypeScript infer keyword All In One
    垂钓图解教程: 鱼钩 All In One
    CSS `@fontface` & fontfamily All In One
    Linux bash sed command All In One
    YouTube 标识名 All In One
    Leetcode 面试题 16.15. 珠玑妙算
    Leetcode 1385. 两个数组间的距离值
    Leetcode 984. 不含 AAA 或 BBB 的字符串(网友思路)
  • 原文地址:https://www.cnblogs.com/yanghailin/p/14894318.html
Copyright © 2020-2023  润新知