Пример #1
0
def visualize_snake_detection(img, data):
    def blend_hm_img(hm, img):
        hm = np.max(hm, axis=0)
        h, w = hm.shape[:2]
        img = cv2.resize(img, dsize=(w, h), interpolation=cv2.INTER_LINEAR)
        hm = np.array([255, 255, 255
                       ]) - (hm.reshape(h, w, 1) * colors[0]).astype(np.uint8)
        ratio = 0.5
        blend = (img * ratio + hm * (1 - ratio)).astype(np.uint8)
        return blend

    img = img_utils.bgr_to_rgb(img)
    blend = blend_hm_img(data['ct_hm'], img)

    plt.imshow(blend)
    ct_ind = np.array(data['ct_ind'])
    w = img.shape[1] // snake_config.down_ratio
    xs = ct_ind % w
    ys = ct_ind // w
    for i in range(len(data['wh'])):
        w, h = data['wh'][i]
        x_min, y_min = xs[i] - w / 2, ys[i] - h / 2
        x_max, y_max = xs[i] + w / 2, ys[i] + h / 2
        plt.plot([x_min, x_min, x_max, x_max, x_min],
                 [y_min, y_max, y_max, y_min, y_min])
    plt.show()
Пример #2
0
def visualize_snake_octagon(img, extreme_points):
    img = img_utils.bgr_to_rgb(img)
    octagons = []
    bboxes = []
    ex_points = []
    for i in range(len(extreme_points)):
        for j in range(len(extreme_points[i])):
            bbox = get_bbox(extreme_points[i][j] * 4)
            octagon = snake_cityscapes_utils.get_octagon(extreme_points[i][j] *
                                                         4)
            bboxes.append(bbox)
            octagons.append(octagon)
            ex_points.append(extreme_points[i][j])
    _, ax = plt.subplots(1)
    ax.imshow(img)
    n = len(octagons)
    for i in range(n):
        x, y, x_max, y_max = bboxes[i]
        ax.add_patch(
            patches.Polygon(xy=[[x, y], [x, y_max], [x_max, y_max], [x_max,
                                                                     y]],
                            fill=False,
                            linewidth=1,
                            edgecolor='r'))
        octagon = np.append(octagons[i], octagons[i][0]).reshape(-1, 2)
        ax.plot(octagon[:, 0], octagon[:, 1])
        ax.scatter(ex_points[i][:, 0] * 4,
                   ex_points[i][:, 1] * 4,
                   edgecolors='w')
    plt.show()
Пример #3
0
def visualize_cp_detection(img, data):
    act_ind = data['act_ind']
    awh = data['awh']

    act_hm_w = data['act_hm'].shape[2]
    cp_h, cp_w = data['cp_hm'][0].shape[1], data['cp_hm'][0].shape[2]

    img = img_utils.bgr_to_rgb(img)
    plt.imshow(img)

    for i in range(len(act_ind)):
        act_ind_ = act_ind[i]
        ct = act_ind_ % act_hm_w, act_ind_ // act_hm_w
        w, h = awh[i]
        abox = np.array(
            [ct[0] - w / 2, ct[1] - h / 2, ct[0] + w / 2, ct[1] + h / 2])

        cp_ind_ = data['cp_ind'][i]
        cp_wh_ = data['cp_wh'][i]

        for j in range(len(cp_ind_)):
            ct = cp_ind_[j] % cp_w, cp_ind_[j] // cp_w
            x = ct[0] / cp_w * w
            y = ct[1] / cp_h * h
            x_min = (x - cp_wh_[j][0] / 2 + abox[0]) * snake_config.down_ratio
            y_min = (y - cp_wh_[j][1] / 2 + abox[1]) * snake_config.down_ratio
            x_max = (x + cp_wh_[j][0] / 2 + abox[0]) * snake_config.down_ratio
            y_max = (y + cp_wh_[j][1] / 2 + abox[1]) * snake_config.down_ratio
            plt.plot([x_min, x_min, x_max, x_max, x_min],
                     [y_min, y_max, y_max, y_min, y_min])

    plt.show()
Пример #4
0
    def visualize_ex(self, output, batch):
        inp = img_utils.bgr_to_rgb(
            img_utils.unnormalize_img(batch['inp'][0], mean,
                                      std).permute(1, 2, 0))
        ex = output['py']
        ex = ex[-1] if isinstance(ex, list) else ex
        ex = ex.detach().cpu().numpy() * snake_config.down_ratio

        fig, ax = plt.subplots(1, figsize=(20, 10))
        fig.tight_layout()
        ax.axis('off')
        ax.imshow(inp)

        colors = np.array([[31, 119, 180], [255, 127, 14], [46, 160, 44],
                           [214, 40, 39], [148, 103, 189], [140, 86, 75],
                           [227, 119, 194], [126, 126, 126], [188, 189, 32],
                           [26, 190, 207]]) / 255.
        np.random.shuffle(colors)
        colors = cycle(colors)
        for i in range(len(ex)):
            color = next(colors).tolist()
            poly = ex[i]
            poly = np.append(poly, [poly[0]], axis=0)
            ax.plot(poly[:, 0], poly[:, 1], color=color)

        plt.show()
Пример #5
0
    def visualize_training_box(self, output, batch, img_name):
        inp = img_utils.bgr_to_rgb(img_utils.unnormalize_img(batch['inp'][0], mean, std).permute(1, 2, 0))
        box = output['detection'][:, :4].detach().cpu().numpy() * snake_config.down_ratio
        ex = output['py']
        ex = ex[-1] if isinstance(ex, list) else ex
        ex = ex.detach().cpu().numpy() * snake_config.down_ratio

        #这里是查看显示结果的相关参数
        tmp_file = open('/home/tianhao.lu/code/Deep_snake/snake/Result/Contour/result.log', 'w', encoding='utf8')
        tmp_file.writelines("visualize training box -> inp:" + str(type(inp)) + "\n")
        tmp_file.writelines("visualize training box -> box:" + str(len(box)) + "\n")
        tmp_file.writelines("visualize training box -> ex:" + str(ex) + "\n")
        tmp_file.writelines("visualize training box -> detection:" + str(output['detection']) + "\n")
        # for tmp_data in train_loader:
        #     tmp_file.writelines("one train_loader data type:" + str(type(tmp_data)) + "\n")
        #     for key in tmp_data:
        #         tmp_file.writelines("one train_loader data key:" + str(key) + "\n")
        #         tmp_file.writelines("one train_loader data len:" + str(len(tmp_data[key])) + "\n")
        #     # tmp_file.writelines("one train_loader data:" + str(tmp_data) + "\n")
        #     break
        tmp_file.writelines(str("*************************************************************** \n"))
        tmp_file.close()
        fig, ax = plt.subplots(1, figsize=(20, 10))
        fig.tight_layout()
        ax.axis('off')
        ax.imshow(inp)

        colors = np.array([
            [31, 119, 180],
            [255, 127, 14],
            [46, 160, 44],
            [214, 40, 39],
            [148, 103, 189],
            [140, 86, 75],
            [227, 119, 194],
            [126, 126, 126],
            [188, 189, 32],
            [26, 190, 207]
        ]) / 255.
        np.random.shuffle(colors)
        colors = cycle(colors)
        for i in range(len(ex)):
            color = next(colors).tolist()
            poly = ex[i]
            poly = np.append(poly, [poly[0]], axis=0)
            ax.plot(poly[:, 0], poly[:, 1], color=color, linewidth=5)

            x_min, y_min, x_max, y_max = box[i]
            ax.plot([x_min, x_min, x_max, x_max, x_min], [y_min, y_max, y_max, y_min, y_min], color='w', linewidth=0.5)

        # filename = random.randint(0,100000)  #这个是名称取十万以内的随机数
        filename = img_name
        # fig = plt.gcf()
        img_width = 400
        img_height = 300
        fig.set_size_inches(img_width / 96.0, img_height / 96.0)  # 输出width*height像素
        plt.subplots_adjust(top=1, bottom=0, left=0, right=1, hspace=0, wspace=0)  # 输出图像#边框设置
        plt.margins(0, 0)
        plt.savefig("/home/tianhao.lu/code/Deep_snake/snake/Result/coco_test_result/%s.png"%filename,dpi=96.0,pad_inches=0.0)
Пример #6
0
def visualize_snake_evolution(img, data):
    img = img_utils.bgr_to_rgb(img)
    plt.imshow(img)
    for poly in data['i_gt_py']:
        poly = poly * 4
        poly = np.append(poly, [poly[0]], axis=0)
        plt.plot(poly[:, 0], poly[:, 1])
        plt.scatter(poly[0, 0], poly[0, 1], edgecolors='w')
    plt.show()
Пример #7
0
 def visualize(self, output, batch):
     inp = img_utils.bgr_to_rgb(img_utils.unnormalize_img(batch['inp'][0], mean, std).permute(1, 2, 0))
     box = output['detection'][0, :, :4].detach().cpu().numpy() * tless_config.down_ratio
     score = output['detection'][0, :, 4].detach().cpu().numpy()
     plt.imshow(inp)
     for i in range(len(box)):
         x_min, y_min, x_max, y_max = box[i]
         plt.plot([x_min, x_min, x_max, x_max, x_min], [y_min, y_max, y_max, y_min, y_min])
     plt.show()
Пример #8
0
    def visualize_training_box(self, output, batch):
        import cv2 
        inp = img_utils.bgr_to_rgb(img_utils.unnormalize_img(batch['inp'][0], mean, std).permute(1, 2, 0))
        h, w, _ = inp.shape
        box = output['detection'][:, :4].detach().cpu().numpy() * snake_config.down_ratio
        #box = output['detection'][:, :4].detach().cpu().numpy()
        nms_ct_hm = output['nms_ct_hm'].detach().cpu().numpy()
        nms_ct_hm = nms_ct_hm[0,0,...]
        nms_ct_hm = Image.fromarray(nms_ct_hm)
        nms_ct_hm = nms_ct_hm.resize((w, h))


        ex = output['py']
        ex = ex[-1] if isinstance(ex, list) else ex
        ex = ex.detach().cpu().numpy() * snake_config.down_ratio
        #ex = ex.detach().cpu().numpy()

        fig, ax = plt.subplots(1, figsize=(10, 10))
        fig.tight_layout()
        ax.axis('off')
        ax.imshow(inp)
        ax.imshow(np.array(nms_ct_hm))

        colors = np.array([
            [31, 119, 180],
            [255, 127, 14],
            [46, 160, 44],
            [214, 40, 39],
            [148, 103, 189],
            [140, 86, 75],
            [227, 119, 194],
            [126, 126, 126],
            [188, 189, 32],
            [26, 190, 207]
        ]) / 255.
        np.random.shuffle(colors)
        colors = cycle(colors)
        for i in range(len(ex)):
            color = next(colors).tolist()
            poly = ex[i]
            poly = np.append(poly, [poly[0]], axis=0)
            ax.plot(poly[:, 0], poly[:, 1], color=color, linewidth=1)

            x_min, y_min, x_max, y_max = box[i]
            ax.plot([x_min, x_min, x_max, x_max, x_min], [y_min, y_max, y_max, y_min, y_min], color='w', linewidth=0.5)

            print("box:", box[i])

        plt.show()
Пример #9
0
    def visualize_training_box(self, output, batch):
        inp = img_utils.bgr_to_rgb(
            img_utils.unnormalize_img(batch['inp'][0], mean,
                                      std).permute(1, 2, 0))
        box = output['detection'][:, :4].detach().cpu().numpy(
        ) * snake_config.down_ratio
        # box = output['cp_box'][:, :4].detach().cpu().numpy() * snake_config.down_ratio

        _, ax = plt.subplots(1)
        ax.imshow(inp)
        n = len(box)
        for i in range(n):
            x_min, y_min, x_max, y_max = box[i]
            ax.plot([x_min, x_min, x_max, x_max, x_min],
                    [y_min, y_max, y_max, y_min, y_min])
        plt.show()
Пример #10
0
    def visualize_gt(self, output, batch, index):
        inp = img_utils.bgr_to_rgb(
            img_utils.unnormalize_img(batch['inp'][0], mean,
                                      std).permute(1, 2, 0))
        box = output['detection'][:, :4].detach().cpu().numpy(
        ) * snake_config.down_ratio

        ex = output['i_gt_4py']
        ex = ex[-1] if isinstance(ex, list) else ex
        ex = ex.detach().cpu().numpy() * snake_config.down_ratio

        fig, ax = plt.subplots(1, figsize=(20, 10))
        fig.tight_layout()
        ax.axis('off')
        ax.imshow(inp)

        colors = np.array([[31, 119, 180], [255, 127, 14], [46, 160, 44],
                           [214, 40, 39], [148, 103, 189], [140, 86, 75],
                           [227, 119, 194], [126, 126, 126], [188, 189, 32],
                           [26, 190, 207]]) / 255.
        np.random.shuffle(colors)
        colors = cycle(colors)
        for i in range(len(ex)):
            color = next(colors).tolist()
            poly = ex[i]
            poly = np.append(poly, [poly[0]], axis=0)
            ax.plot(poly[:, 0], poly[:, 1], color=color, linewidth=3)

            x_min, y_min, x_max, y_max = box[i]
            ax.plot([x_min, x_min, x_max, x_max, x_min],
                    [y_min, y_max, y_max, y_min, y_min],
                    color='w',
                    linewidth=0.5)

        plt.show()
        # os.mkdir()
        os.makedirs("KINS_SNAKE_GT_VIS/", exist_ok=True)
        out_png_path = "KINS_SNAKE_GT_VIS/" + str(index) + ".jpg"

        # plt.savefig(out_png_path, format='jpg', transparent=True, dpi=100, pad_inches = 0)
        plt.savefig(out_png_path,
                    format='jpg',
                    transparent=True,
                    dpi=100,
                    bbox_inches='tight')
Пример #11
0
def visualize_snake_evolution(img, data):
    img = img_utils.bgr_to_rgb(img)

    i_gt_py = data['i_gt_py']
    i_it_py = data['i_it_py']

    plt.imshow(img)
    for k in range(len(i_gt_py)):
        poly = i_gt_py[k] * 4
        poly = np.append(poly, [poly[0]], axis=0)

        it_poly = i_it_py[k] * 4
        it_poly = np.append(it_poly, [it_poly[0]], axis=0)

        plt.plot(poly[:, 0], poly[:, 1], 'g')
        plt.plot(it_poly[:, 0], it_poly[:, 1], 'r')
        #plt.scatter(poly[0, 0], poly[0, 1], edgecolors='w')
    #plt.show()
    plt.savefig('b.png')
    plt.close()
Пример #12
0
    def visualize_ex(self, output, batch):
        inp = img_utils.bgr_to_rgb(
            img_utils.unnormalize_img(batch['inp'][0], mean,
                                      std).permute(1, 2, 0))

        detection = output['detection']
        score = detection[:, 4].detach().cpu().numpy()
        label = detection[:, 5].detach().cpu().numpy().astype(int)
        cp_ind = output['cp_ind'].detach().cpu().numpy().astype(int)
        py = output['py'][-1].detach().cpu().numpy() * snake_config.down_ratio

        if len(py) == 0:
            return

        ct_ind = np.unique(cp_ind)
        score = score[ct_ind]
        label = label[ct_ind]
        ind_group = [
            np.argwhere(ct_ind[i] == cp_ind).ravel()
            for i in range(len(ct_ind))
        ]
        py = [[py[ind] for ind in inds] for inds in ind_group]

        fig, ax = plt.subplots(1, figsize=(20, 10))
        fig.tight_layout()
        ax.axis('off')
        ax.imshow(inp)

        colors = np.array([[31, 119, 180], [255, 127, 14], [46, 160, 44],
                           [214, 40, 39], [148, 103, 189], [140, 86, 75],
                           [227, 119, 194], [126, 126, 126], [188, 189, 32],
                           [26, 190, 207]]) / 255.
        # colors = cycle(colors)
        for i in range(len(py)):
            color = colors[np.random.randint(len(colors))]
            # color = next(colors).tolist()
            for poly in py[i]:
                poly = np.append(poly, [poly[0]], axis=0)
                ax.plot(poly[:, 0], poly[:, 1], color=color, linewidth=3)

        plt.show()
Пример #13
0
    def visualize_heatmap_on_img(self, output, batch, vis_file=None):
        import cv2 
        inp = img_utils.bgr_to_rgb(img_utils.unnormalize_img(batch['inp'][0], mean, std).permute(1, 2, 0))
        h, w, _ = inp.shape
        raw_ct_hm = output['ct_hm'].detach().cpu().numpy()
        raw_ct_hm = raw_ct_hm[0,0,...]
        raw_ct_hm = Image.fromarray(raw_ct_hm)
        raw_ct_hm = raw_ct_hm.resize((w, h))
        nms_ct_hm = output['nms_ct_hm'].detach().cpu().numpy()
        nms_ct_hm = nms_ct_hm[0,0,...]
        nms_ct_hm = Image.fromarray(nms_ct_hm)
        nms_ct_hm = nms_ct_hm.resize((w, h))

        raw_ct_hm = np.array(raw_ct_hm)
        plt.imshow(inp)
        plt.imshow(raw_ct_hm, alpha=0.8, cmap='rainbow')
        plt.axis('off')
        if vis_file is not None:
            plt.savefig(vis_file,format='png',dpi=600)
            plt.close()
        else:
            plt.show()