示例#1
0
def visualize_network_output(output, tr_mask, mode='train'):

    vis_dir = os.path.join(cfg.vis_dir, cfg.exp_name + '_' + mode)
    if not os.path.exists(vis_dir):
        os.mkdir(vis_dir)
    b, c, _, _ = output.shape

    for i in range(b):
        predict = torch.sigmoid(output[i]).data.cpu().numpy()
        target = tr_mask[i].cpu().numpy()
        shows = list()
        for j in range(c):
            p = predict[j]
            t = target[:, :, j]
            tcl_pred = cav.heatmap(
                np.array(p / np.max(p) * 255, dtype=np.uint8))
            tcl_targ = cav.heatmap(
                np.array(t / np.max(t) * 255, dtype=np.uint8))
            show = np.concatenate([tcl_pred * 255, tcl_targ * 255], axis=0)
            shows.append(show)
        show_img = np.concatenate(shows, axis=1)

        show = cv2.resize(show_img, (256 * c, 512))
        path = os.path.join(vis_dir, '{}.png'.format(i))
        cv2.imwrite(path, show)
示例#2
0
def visualize_gt(image, contours, tr=None):
    image_show = image.copy()
    image_show = np.ascontiguousarray(image_show[:, :, ::-1])
    image_show = cv2.polylines(image_show, contours, True, (0, 0, 255), 3)
    if tr is not None:
        tr_map = cav.heatmap(np.array(tr[:, :, -1] / np.max(tr[:, :, -1]) * 255, dtype=np.uint8))
        h, w = tr_map.shape[:2]
        tr_map = cv2.resize(tr_map, (w*cfg.scale, h*cfg.scale))
        image_show = np.concatenate([image_show, np.array(tr_map*255, dtype=np.uint8)], axis=1)
        return image_show
    else:
        return image_show
示例#3
0
def visualize_detection(image, contours, tr=None):
    image_show = image.copy()
    image_show = np.ascontiguousarray(image_show[:, :, ::-1])
    cv2.drawContours(image_show, contours, -1, (0, 255, 0), 2)

    if tr is not None:
        tr_map = cav.heatmap(np.array(tr / np.max(tr) * 255, dtype=np.uint8))
        image_show = np.concatenate(
            [image_show, np.array(tr_map * 255, dtype=np.uint8)], axis=1)
        return image_show
    else:
        return image_show
示例#4
0
def visualize_detection(image, contours, tr=None):
    image_show = image.copy()
    image_show = np.ascontiguousarray(image_show[:, :, ::-1])

    cv2.drawContours(image_show, contours, -1, (0, 255, 0), 3)
    if tr is not None:
        tr_map = cav.heatmap(np.array(tr / np.max(tr) * 255, dtype=np.uint8))
        h,w = tr_map.shape[:2]
        tr_map = cv2.resize(tr_map, (w*cfg.scale, h*cfg.scale))
        #print(tr_map.shape)
        #print(image_show.shape)
        image_show = np.concatenate([image_show, np.array(tr_map*255, dtype=np.uint8)], axis=1)
        return image_show
    else:
        return image_show
示例#5
0
        img = img.transpose(1, 2, 0)
        img = ((img * stds + means) * 255).astype(np.uint8)
        print(idx, img.shape)
        top_map = radius_map[:, :, 0]
        bot_map = radius_map[:, :, 1]

        print(radius_map.shape)

        sin_map, cos_map = regularize_sin_cos(sin_map, cos_map)
        ret, labels = cv2.connectedComponents(tcl_mask[:, :,
                                                       0].astype(np.uint8),
                                              connectivity=8)
        cv2.imshow(
            "labels0",
            cav.heatmap(np.array(labels * 255 / np.max(labels),
                                 dtype=np.uint8)))
        print(np.sum(tcl_mask[:, :, 1]))

        t0 = time.time()
        for bbox_idx in range(1, ret):
            bbox_mask = labels == bbox_idx
            text_map = tcl_mask[:, :, 0] * bbox_mask

            boxes = bbox_transfor_inv(radius_map,
                                      sin_map,
                                      cos_map,
                                      text_map,
                                      wclip=(2, 8))
            # nms
            boxes = lanms.merge_quadrangle_n9(boxes.astype('float32'), 0.25)
            boxes = boxes[:, :8].reshape((-1, 4, 2)).astype(np.int32)
示例#6
0
    stds = (0.229, 0.224, 0.225)

    transform = Augmentation(
        size=640, mean=means, std=stds
    )

    trainset = Icdar15Text(
        data_root='../data/Icdar2015',
        is_training=True,
        transform=transform
    )

    # img, train_mask, tr_mask, tcl_mask, radius_map, sin_map, cos_map, meta = trainset[944]
    for idx in range(0, len(trainset)):
        t0 = time.time()
        img, train_mask, tr_mask = trainset[idx]
        img, train_mask, tr_mask = map(lambda x: x.cpu().numpy(), (img, train_mask, tr_mask))

        img = img.transpose(1, 2, 0)
        img = ((img * stds + means) * 255).astype(np.uint8)
        print(idx, img.shape)

        for i in range(tr_mask.shape[2]):
            cv2.imshow("tr_mask_{}".format(i),
                       cav.heatmap(np.array(tr_mask[:, :, i] * 255 / np.max(tr_mask[:, :, i]), dtype=np.uint8)))

        cv2.imshow("train_mask", cav.heatmap(np.array(train_mask * 255 / np.max(train_mask), dtype=np.uint8)))

        cv2.imshow('imgs', img)
        cv2.waitKey(0)
示例#7
0
    )
    # img, train_mask, tr_mask, tcl_mask, radius_map, sin_map, cos_map, meta = trainset[944]
    for idx in range(0, len(trainset)):
        t0 = time.time()
        img, train_mask, tr_mask, meta = trainset[idx]
        # img, train_mask, tr_mask = map(lambda x: x.cpu().numpy(), (img, train_mask, tr_mask))

        img = img.transpose(1, 2, 0)
        img = ((img * stds + means) * 255).astype(np.uint8)
        print(idx, img.shape)
        gt_contour = []
        for annot, n_annot in zip(meta['annotation'], meta['n_annotation']):
            if n_annot.item() > 0:
                gt_contour.append(np.array(annot[:n_annot], dtype=np.int))
        image_show = cv2.polylines(img, gt_contour, True, (0, 0, 255), 3)

        for i in range(tr_mask.shape[2]):
            heatmap = cav.heatmap(np.array(tr_mask[:, :, i] * 255 / np.max(tr_mask[:, :, i]), dtype=np.uint8))
            cv2.imshow("tr_mask_{}".format(i),heatmap)
            cv2.imwrite("{}.png".format(i), heatmap*255)
        mask = tr_mask[:, :, 0]
        # from scipy import ndimage as ndimg
        # dmp = ndimg.distance_transform_edt(mask)  # distance transform

        cv2.imwrite("mask1.png".format(i), np.array(mask*255, dtype=np.uint8))
        cv2.imwrite("mask0.png".format(i), np.array((mask>0) * 255, dtype=np.uint8))
        cv2.imshow('imgs', image_show)
        cv2.imwrite("imgs.png".format(i), image_show)
        cv2.waitKey(0)