Example #1
0
 def nondist_validation(self, dataloader, current_iter, tb_logger,
                        save_img):
     assert dataloader is None, 'Validation dataloader should be None.'
     self.test()
     result = tensor2img(self.output, min_max=(-1, 1))
     if self.opt['is_train']:
         save_img_path = osp.join(self.opt['path']['visualization'],
                                  'train', f'train_{current_iter}.png')
     else:
         save_img_path = osp.join(self.opt['path']['visualization'], 'test',
                                  f'test_{self.opt["name"]}.png')
     mmcv.imwrite(result, save_img_path)
     # add sample images to tb_logger
     result = (result / 255.).astype(np.float32)
     result = mmcv.bgr2rgb(result)
     if tb_logger is not None:
         tb_logger.add_image(
             'samples', result, global_step=current_iter, dataformats='HWC')
Example #2
0
    def predict(self, img, threshold):

        result = inference_detector(self.model, img)

        res_img = self.model.show_result(img, result, score_thr=threshold, show=False)

        _image_name = img.split('\\')[-1].split('.')[0]
        # maybe only happen for the first time initialization
        if not _image_name:
            return

        _image_name = _image_name + '.png'

        out = os.path.join('results/', _image_name)

        mmcv.imwrite(res_img, out)

        return _image_name
Example #3
0
    def forward_test(self,
                     lq,
                     gt=None,
                     meta=None,
                     save_image=False,
                     save_path=None,
                     iteration=None):
        """Testing forward function.

        Args:
            lq (Tensor): LQ Tensor with shape (n, c, h, w).
            gt (Tensor): GT Tensor with shape (n, c, h, w). Default: None.
            save_image (bool): Whether to save image. Default: False.
            save_path (str): Path to save image. Default: None.
            iteration (int): Iteration for the saving image name.
                Default: None.

        Returns:
            dict: Output results.
        """
        _model = self.generator_ema if self.is_use_ema else self.generator
        output = _model(lq)

        if self.test_cfg is not None and self.test_cfg.get(
                'metrics', None) and gt is not None:
            results = dict(eval_result=self.evaluate(output, gt))
        else:
            results = dict(lq=lq.cpu(), output=output.cpu())

        # save image
        if save_image:
            lq_path = meta[0]['lq_path']
            folder_name = osp.splitext(osp.basename(lq_path))[0]
            if isinstance(iteration, numbers.Number):
                save_path = osp.join(save_path, folder_name,
                                     f'{folder_name}-{iteration + 1:06d}.png')
            elif iteration is None:
                save_path = osp.join(save_path, f'{folder_name}.png')
            else:
                raise ValueError('iteration should be number or None, '
                                 f'but got {type(iteration)}')
            mmcv.imwrite(tensor2img(output), save_path)

        return results
Example #4
0
def main():
    args = parse_args()

    if not os.path.isfile(args.img_path):
        raise ValueError('It seems that you did not input a valid '
                         '"image_path". Please double check your input, or '
                         'you may want to use "restoration_video_demo.py" '
                         'for video restoration.')

    model = init_model(args.config,
                       args.checkpoint,
                       device=torch.device('cuda', args.device))

    output = restoration_inference(model, args.img_path)
    output = tensor2img(output)

    mmcv.imwrite(output, args.save_path)
    if args.imshow:
        mmcv.imshow(output, 'predicted restoration result')
Example #5
0
def process(data, img_path_prefix, out_dir):
    if data is None:
        return
    # Dirty hack for multi-processing
    img_path, words, word_bboxes, char_bbox_grps = data
    img_dir, img_name = os.path.split(img_path)
    img_name = os.path.splitext(img_name)[0]
    input_img = mmcv.imread(os.path.join(img_path_prefix, img_path))

    output_sub_dir = os.path.join(out_dir, img_dir)
    if not os.path.exists(output_sub_dir):
        try:
            os.makedirs(output_sub_dir)
        except FileExistsError:
            pass  # occurs when multi-proessing

    for i, word in enumerate(words):
        output_image_patch_name = f'{img_name}_{i}.png'
        output_label_name = f'{img_name}_{i}.txt'
        output_image_patch_path = os.path.join(output_sub_dir,
                                               output_image_patch_name)
        output_label_path = os.path.join(output_sub_dir, output_label_name)
        if os.path.exists(output_image_patch_path) and os.path.exists(
                output_label_path):
            continue

        word_bbox = word_bboxes[i]
        min_x, max_x = int(min(word_bbox[::2])), int(max(word_bbox[::2]))
        min_y, max_y = int(min(word_bbox[1::2])), int(max(word_bbox[1::2]))
        cropped_img = input_img[min_y:max_y, min_x:max_x]
        if cropped_img.shape[0] <= 0 or cropped_img.shape[1] <= 0:
            continue

        char_bbox_grp = np.array(char_bbox_grps[i])
        char_bbox_grp[:, ::2] -= min_x
        char_bbox_grp[:, 1::2] -= min_y

        mmcv.imwrite(cropped_img, output_image_patch_path)
        with open(output_label_path, 'w') as output_label_file:
            output_label_file.write(word + '\n')
            for cbox in char_bbox_grp:
                output_label_file.write('%d %d %d %d %d %d %d %d\n' %
                                        tuple(cbox.tolist()))
Example #6
0
def main():
    args = parse_args()
    os.makedirs(args.output_root, exist_ok=True)
    dataset = COCO(args.annotation_path)
    for img_id, img_info in tqdm(dataset.imgs.items()):
        filename = img_info["file_name"]
        img = mmcv.imread(osp.join(args.img_root, filename))
        ann_ids = dataset.getAnnIds(imgIds=[img_id])
        ann_info = dataset.loadAnns(ann_ids)
        for i, ann in enumerate(ann_info):
            x1, y1, w, h = map(int, ann["bbox"])
            category_id = ann["category_id"]
            crop = img[y1 : y1 + h, x1 : x1 + w]
            output_path = osp.join(args.output_root, f"{x1}_{y1}_{w}_{h}_{filename}")
            if w > 6 and h > 6:
                if args.from_predict:
                    if category_id == 2 and ann["score"] > SCORE_THRESHOLD:  # prediction
                        mmcv.imwrite(crop, output_path)
                elif category_id == 1 and img_info["source"] in ["arvalis_1", "arvalis_3", "rres_1", "inrae_1"]:
                    mmcv.imwrite(crop, output_path)
def dump_frames(vid_item):
    full_path, vid_path, vid_id = vid_item
    vid_name = vid_path.split('.')[0]
    out_full_path = osp.join(args.out_dir, vid_name)
    try:
        os.mkdir(out_full_path)
    except OSError:
        pass
    vr = mmcv.VideoReader(full_path)
    for i in range(len(vr)):
        if vr[i] is not None:
            mmcv.imwrite(vr[i],
                         '{}/img_{:05d}.jpg'.format(out_full_path, i + 1))
        else:
            print('[Warning] length inconsistent!'
                  'Early stop with {} out of {} frames'.format(i + 1, len(vr)))
            break
    print('{} done with {} frames'.format(vid_name, len(vr)))
    sys.stdout.flush()
    return True
Example #8
0
def main():
    args = parse_args()

    # rename_pth(args.checkpoint)
    # print('rename success')

    model = init_model(args.config,
                       args.checkpoint,
                       device=torch.device('cuda', args.device))

    for i in model.state_dict():
        print(i)

    pred_alpha = matting_inference(model, args.img_path,
                                   args.trimap_path) * 255

    # print(pred_alpha)
    mmcv.imwrite(pred_alpha, args.save_path)
    if args.imshow:
        mmcv.imshow(pred_alpha, 'predicted alpha matte')
Example #9
0
    def _pan2json(self, results, outfile_prefix):
        """Convert panoptic results to COCO panoptic json style."""
        label2cat = dict((v, k) for (k, v) in self.cat2label.items())
        pred_annotations = []
        outdir = os.path.join(os.path.dirname(outfile_prefix), 'panoptic')

        for idx in range(len(self)):
            img_id = self.img_ids[idx]
            segm_file = self.data_infos[idx]['segm_file']
            pan = results[idx]

            pan_labels = np.unique(pan)
            segm_info = []
            for pan_label in pan_labels:
                sem_label = pan_label % INSTANCE_OFFSET
                # We reserve the length of self.CLASSES for VOID label
                if sem_label == len(self.CLASSES):
                    continue
                # convert sem_label to json label
                cat_id = label2cat[sem_label]
                is_thing = self.categories[cat_id]['isthing']
                mask = pan == pan_label
                area = mask.sum()
                segm_info.append({
                    'id': int(pan_label),
                    'category_id': cat_id,
                    'isthing': is_thing,
                    'area': int(area)
                })
            # evaluation script uses 0 for VOID label.
            pan[pan % INSTANCE_OFFSET == len(self.CLASSES)] = VOID
            pan = id2rgb(pan).astype(np.uint8)
            mmcv.imwrite(pan[:, :, ::-1], os.path.join(outdir, segm_file))
            record = {
                'image_id': img_id,
                'segments_info': segm_info,
                'file_name': segm_file
            }
            pred_annotations.append(record)
        pan_json_results = dict(annotations=pred_annotations)
        return pan_json_results
Example #10
0
def process_level(
    src_img,
    annotation,
    dst_image_root,
    ignore_image_root,
    preserve_vertical,
    split,
    format,
    para_idx,
    img_idx,
    line_idx,
    word_idx=None,
):
    vertices = annotation['vertices']
    text_label = annotation['text']
    segmentation = [i for j in vertices for i in j]
    x, y, w, h = seg2bbox(segmentation)
    x, y = max(0, math.floor(x)), max(0, math.floor(y))
    w, h = math.ceil(w), math.ceil(h)
    dst_img = src_img[y:y + h, x:x + w]
    if word_idx:
        dst_img_name = f'img_{img_idx}_{para_idx}_{line_idx}_{word_idx}.jpg'
    else:
        dst_img_name = f'img_{img_idx}_{para_idx}_{line_idx}.jpg'
    if not preserve_vertical and h / w > 2 and split == 'train':
        dst_img_path = osp.join(ignore_image_root, dst_img_name)
    else:
        dst_img_path = osp.join(dst_image_root, dst_img_name)
    mmcv.imwrite(dst_img, dst_img_path)

    if format == 'txt':
        label = (f'{osp.basename(dst_image_root)}/{dst_img_name}'
                 f' {text_label}')
    elif format == 'jsonl':
        label = json.dumps({
            'filename': f'{osp.basename(dst_image_root)}/{dst_img_name}',
            'text': text_label
        })
    else:
        raise NotImplementedError
    return label
Example #11
0
def process_img(args, src_image_root, dst_image_root, ignore_image_root,
                preserve_vertical, split, format):
    # Dirty hack for multi-processing
    img_idx, img_info, anns = args
    src_img = mmcv.imread(osp.join(src_image_root, img_info['file_name']))
    labels = []
    for ann_idx, ann in enumerate(anns):
        text_label = ann['utf8_string']

        # Ignore illegible or non-English words
        if ann['language'] == 'not english':
            continue
        if ann['legibility'] == 'illegible':
            continue

        x, y, w, h = ann['bbox']
        x, y = max(0, math.floor(x)), max(0, math.floor(y))
        w, h = math.ceil(w), math.ceil(h)
        dst_img = src_img[y:y + h, x:x + w]
        dst_img_name = f'img_{img_idx}_{ann_idx}.jpg'

        if not preserve_vertical and h / w > 2 and split == 'train':
            dst_img_path = osp.join(ignore_image_root, dst_img_name)
        else:
            dst_img_path = osp.join(dst_image_root, dst_img_name)
        mmcv.imwrite(dst_img, dst_img_path)

        if format == 'txt':
            labels.append(f'{osp.basename(dst_image_root)}/{dst_img_name}'
                          f' {text_label}')
        elif format == 'jsonl':
            labels.append(
                json.dumps({
                    'filename':
                    f'{osp.basename(dst_image_root)}/{dst_img_name}',
                    'text': text_label
                }))
        else:
            raise NotImplementedError

    return labels
def vis_seg_ood(data, result, img_norm_cfg, save_dir, score_thr):

    img_tensor = data['img'][0]
    if len(img_tensor.shape) < 4:
        img_tensor = img_tensor.unsqueeze(0)
    try:
        img_metas = data['img_meta'][0].data[0]
    except:
        img_metas = data['img_meta'][0]

    imgs = tensor2imgs(img_tensor, **img_norm_cfg)
    assert len(imgs) == len(img_metas)

    for img, img_meta, cur_result in zip(imgs, img_metas, result):
        h, w, _ = img_meta['img_shape']
        img_show = img[:h, :w, :]

        ood_seg = F.interpolate(cur_result['ca_ood_seg'].unsqueeze(0).unsqueeze(0), img_show.shape[:2])[0,0]
        img_show[ood_seg>score_thr] = 0.7 * img_show[ood_seg>score_thr] + 0.3 * np.array((0, 0, 255))

        mmcv.imwrite(img_show, '{}'.format(save_dir))
Example #13
0
    def save_image(self, pred_alpha, meta, save_path, iteration):
        """Save predicted alpha to file.

        Args:
            pred_alpha (np.ndarray): The predicted alpha matte of shape (H, W).
            meta (list[dict]): Meta data about the current data batch.
                Currently only batch_size 1 is supported. Required keys in the
                meta dict are ``merged_path``.
            save_path (str): The directory to save predicted alpha matte.
            iteration (int | None): If given as None, the saved alpha matte
                will have the same file name with ``merged_path`` in meta dict.
                If given as an int, the saved alpha matte would named with
                postfix ``_{iteration}.png``.
        """
        image_stem = Path(meta[0]['merged_path']).stem
        if iteration is None:
            save_path = osp.join(save_path, f'{image_stem}.png')
        else:
            save_path = osp.join(save_path,
                                 f'{image_stem}_{iteration + 1:06d}.png')
        mmcv.imwrite(pred_alpha * 255, save_path)
Example #14
0
def main():
    flag_train = False
    if flag_train:
        for j in range(1,6):
            data_path = osp.join(data_dir, 'data_batch_' + str(j)) # data_batch_1, data_batch_2, data_batch_3, data_batch_4, data_batch_5
            train_data = unpickle(data_path)
            print(data_path + ' is processing ... ')

            for i in range(10000):
                img = np.reshape(train_data[b'data'][i], (3, 32, 32)).transpose(0, 1, 2)
                label_num = str(train_data[b'labels'][i])
                output_dir = osp.join(train_output, label_num)
                
                if not osp.isdir(output_dir):
                    os.makedirs(output_dir)
                
                img_name = label_num + '_' + str(i + (j - 1) * 10000) + '.png'
                img_path = osp.join(output_dir, img_name)
                mmcv.imwrite(img, img_path)
            print (data_path + 'loaded.')
    else:
        print ('test_batch is processing ...')

        test_data_path = osp.join(data_dir, 'test_batch')
        test_data = unpickle(test_data_path)

        for i in range(10000):
            img = np.reshape(test_data[b'data'][i], (3, 32, 32))
            img = img.transpose(1, 2, 0)

            label_num = str(test_data[b'labels'][i])
            output_dir = osp.join(test_output, label_num)
            
            if not osp.exists(output_dir):
                os.makedirs(output_dir)
            
            img_name = label_num + '_' + str(i) + '.png'
            img_path = osp.join(output_dir, img_name)
            mmcv.imwrite(img, img_path)
        print ('test_batch' + 'loaded.')
Example #15
0
def main():
    parser = ArgumentParser()
    parser.add_argument('img', help='Image file.')
    parser.add_argument('config', help='Config file.')
    parser.add_argument('checkpoint', help='Checkpoint file.')
    parser.add_argument('save_path', help='Path to save visualized image.')
    parser.add_argument('--device',
                        default='cuda:0',
                        help='Device used for inference.')
    parser.add_argument('--imshow',
                        action='store_true',
                        help='Whether show image with OpenCV.')
    args = parser.parse_args()

    # build the model from a config file and a checkpoint file
    model = init_detector(args.config, args.checkpoint, device=args.device)
    if model.cfg.data.test['type'] == 'ConcatDataset':
        model.cfg.data.test.pipeline = model.cfg.data.test['datasets'][
            0].pipeline

    # test a single image
    print(args.img)
    s = time.time()
    with open(args.img, 'r') as ff:
        lines = ff.readlines()
    for i, line in enumerate(lines):
        imgPath = line.strip()
        imgPath = f"qtests/{imgPath}"
        imgName = imgPath.split('/')[-1]
        print(imgPath)
        result = model_inference(model, imgPath)
        print(result)

        #show the results
        img = model.show_result(imgPath, result, out_file=None, show=False)
        print("time", time.time() - s)
        mmcv.imwrite(img, f"{args.save_path}/{imgName}.jpg")

        if args.imshow:
            mmcv.imshow(img, 'predicted results')
Example #16
0
def generate_ann(root_path, split, image_infos, format):

    dst_image_root = osp.join(root_path, 'crops', split)
    dst_label_file = osp.join(root_path, f'{split}_label.{format}')
    os.makedirs(dst_image_root, exist_ok=True)

    lines = []
    for image_info in image_infos:
        index = 1
        src_img_path = osp.join(root_path, 'imgs', image_info['file_name'])
        image = mmcv.imread(src_img_path)
        src_img_root = image_info['file_name'].split('.')[0]

        for anno in image_info['anno_info']:
            word = anno['word']
            dst_img = crop_img(image, anno['bbox'], 0, 0)

            # Skip invalid annotations
            if min(dst_img.shape) == 0:
                continue

            dst_img_name = f'{src_img_root}_{index}.png'
            index += 1
            dst_img_path = osp.join(dst_image_root, dst_img_name)
            mmcv.imwrite(dst_img, dst_img_path)

            if format == 'txt':
                lines.append(f'{osp.basename(dst_image_root)}/{dst_img_name} '
                             f'{word}')
            elif format == 'jsonl':
                lines.append(
                    json.dumps({
                        'filename':
                        f'{osp.basename(dst_image_root)}/{dst_img_name}',
                        'text': word
                    }))
            else:
                raise NotImplementedError

    list_to_file(dst_label_file, lines)
Example #17
0
def _draw_lines_ls_points_ls(img,
                             lines_ls,
                             points_ls=None,
                             line_colors='green',
                             point_colors='red',
                             out_file=None,
                             line_thickness=1,
                             point_thickness=1,
                             box=False):
    if lines_ls is not None:
        assert isinstance(lines_ls, list)
        if lines_ls is not None and isinstance(line_colors, str):
            line_colors = [line_colors] * len(lines_ls)
        if not isinstance(line_thickness, list):
            line_thickness = [line_thickness] * len(lines_ls)

    if points_ls is not None:
        assert isinstance(points_ls, list)
        if points_ls is not None and isinstance(point_colors, str):
            point_colors = [point_colors] * len(points_ls)
        if not isinstance(point_thickness, list):
            point_thickness = [point_thickness] * len(points_ls)

    img = _read_img(img)
    if lines_ls is not None:
        for i, lines in enumerate(lines_ls):
            img = _draw_lines(img,
                              lines,
                              line_colors[i],
                              line_thickness[i],
                              box=box)

    if points_ls is not None:
        for i, points in enumerate(points_ls):
            img = _draw_points(img, points, point_colors[i],
                               point_thickness[i])
    if out_file is not None:
        mmcv.imwrite(img, out_file)
        print('\n', out_file)
    return img
    def gen_pack_zbuf_render(self):
        for line in tqdm.tqdm(self.lines):
            pattern, num_str = line
            base = osp.join(self.data_root, pattern)
            num = int(num_str)
            scene = Scene(base, num)
            left = scene.left
            right = scene.right

            bgr = self._gen_pack_zbuf_render_side(left)
            fname = osp.join(self.save_dir, f"{pattern}/{num_str}.left.png")
            if bgr is not None:
                mmcv.imwrite(bgr, fname)
            else:
                print(f"{fname} is None.")

            bgr = self._gen_pack_zbuf_render_side(right)
            fname = osp.join(self.save_dir, f"{pattern}/{num_str}.right.png")
            if bgr is not None:
                mmcv.imwrite(bgr, fname)
            else:
                print(f"{fname} is None.")
def vis_gt_known_ood(data, gt, img_norm_cfg, save_dir, palette='cityscapes', last_cls=19):
    img_tensor = data['img'][0]
    seg_color_map = get_color_map(palette)

    if len(img_tensor.shape) < 4:
        img_tensor = img_tensor.unsqueeze(0)
    try:
        img_metas = data['img_meta'][0].data[0]
    except:
        img_metas = data['img_meta'][0]

    imgs = tensor2imgs(img_tensor, **img_norm_cfg)
    assert len(imgs) == len(img_metas)

    for img, img_meta, img_gt in zip(imgs, img_metas, gt):
        h, w, _ = img_meta['img_shape']
        img_show = img[:h, :w, :]

        img_gt = torch.tensor(img_gt).unsqueeze(0).unsqueeze(0)
        full_seg = F.interpolate(img_gt.float(),
                                 img.shape[:2], mode='nearest')[0,0]
        full_seg = full_seg.cpu().numpy().astype(np.uint8)
        full_seg = full_seg[:h, :w]

        known_seg = full_seg.copy()
        ood_seg = np.zeros_like(full_seg)
        known_seg[full_seg > last_cls-1] = 255
        ood_seg[full_seg == last_cls] = 1

        seg_show = img_show.copy()

        for k, v in seg_color_map.items():
            cur_mask = (known_seg==k)
            seg_show[cur_mask] = seg_show[cur_mask] * 0.5 + np.asarray(v[::-1], np.uint8) * 0.5

        img_show[ood_seg==1] = 0.5 * img_show[ood_seg==1] + 0.5 * np.array((255, 0, 0)[::-1])

        mmcv.imwrite(img_show, '{}'.format(save_dir))
        mmcv.imwrite(seg_show, '{}'.format(save_dir.replace('ood', 'seg')))
Example #20
0
def process_img(args, src_image_root, dst_image_root):
    # Dirty hack for multi-processing
    img_idx, img_info, anns = args
    src_img = mmcv.imread(osp.join(src_image_root, img_info['file_name']))
    labels = []
    for ann_idx, ann in enumerate(anns):
        text_label = ann['utf8_string']

        # Ignore illegible or non-English words
        if text_label == '.':
            continue

        x, y, w, h = ann['bbox']
        x, y = max(0, math.floor(x)), max(0, math.floor(y))
        w, h = math.ceil(w), math.ceil(h)
        dst_img = src_img[y:y + h, x:x + w]
        dst_img_name = f'img_{img_idx}_{ann_idx}.jpg'
        dst_img_path = osp.join(dst_image_root, dst_img_name)
        mmcv.imwrite(dst_img, dst_img_path)
        labels.append(f'{osp.basename(dst_image_root)}/{dst_img_name}'
                      f' {text_label}')
    return labels
Example #21
0
def show_result(img, result, dataset='coco', score_thr=0.3, out_file=None):
    img = mmcv.imread(img)
    class_names = get_classes(dataset)
    if isinstance(result, tuple):
        bbox_result, segm_result = result
    else:
        bbox_result, segm_result = result, None
    bboxes = np.vstack(bbox_result)
    f = open('tmp.txt', 'w')
    for i in range(bboxes.shape[0]):
        write_str = "{:.2f},{:.2f},{:.2f},{:.2f},{:.2f}\n".format(
            bboxes[i][0], bboxes[i][1], bboxes[i][2], bboxes[i][3],
            bboxes[i][4])
        f.write(write_str)
    f.close()

    # draw segmentation masks
    if segm_result is not None:
        segms = mmcv.concat_list(segm_result)
        inds = np.where(bboxes[:, -1] > score_thr)[0]
        for i in inds:
            color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8)
            mask = maskUtils.decode(segms[i]).astype(np.bool)
            img[mask] = img[mask] * 0.5 + color_mask * 0.5
    # draw bounding boxes
    labels = [
        np.full(bbox.shape[0], i, dtype=np.int32)
        for i, bbox in enumerate(bbox_result)
    ]
    labels = np.concatenate(labels)
    # mmcv.imshow_det_bboxes(
    #     img.copy(),
    #     bboxes,
    #     labels,
    #     class_names=class_names,
    #     score_thr=score_thr,
    #     show=out_file is None)

    mmcv.imwrite(img, 'test1.jpg')
Example #22
0
def process_img(args, dst_image_root, ignore_image_root, preserve_vertical,
                split, format):
    # Dirty hack for multi-processing
    img_idx, img_info, anns = args
    src_img = mmcv.imread(img_info['file_name'])
    labels = []
    for ann_idx, ann in enumerate(anns):
        segmentation = []
        for x, y in ann['points']:
            segmentation.append(max(0, x))
            segmentation.append(max(0, y))
        xs, ys = segmentation[::2], segmentation[1::2]
        x, y = min(xs), min(ys)
        w, h = max(xs) - x, max(ys) - y
        text_label = ann['transcription']

        dst_img = src_img[y:y + h, x:x + w]
        dst_img_name = f'img_{img_idx}_{ann_idx}.jpg'

        if not preserve_vertical and h / w > 2 and split == 'train':
            dst_img_path = osp.join(ignore_image_root, dst_img_name)
        else:
            dst_img_path = osp.join(dst_image_root, dst_img_name)
        mmcv.imwrite(dst_img, dst_img_path)

        if format == 'txt':
            labels.append(f'{osp.basename(dst_image_root)}/{dst_img_name}'
                          f' {text_label}')
        elif format == 'jsonl':
            labels.append(
                json.dumps({
                    'filename':
                    f'{osp.basename(dst_image_root)}/{dst_img_name}',
                    'text': text_label
                }))
        else:
            raise NotImplementedError

    return labels
Example #23
0
def main():
    """ Demo for video interpolation models.

    Note that we accept video as input(output), when 'input_dir'('output_dir')
    is set to the path to the video. But using videos introduces video
    compression, which lower the visual quality. If you want actual quality,
    please save them as separate images (.png).
    """

    args = parse_args()

    model = init_model(args.config,
                       args.checkpoint,
                       device=torch.device('cuda', args.device))

    output, fps = video_interpolation_inference(model, args.input_dir,
                                                args.start_idx, args.end_idx,
                                                args.batch_size)

    if args.fps_multiplier:
        assert args.fps_multiplier > 0, '`fps_multiplier` cannot be negative'
        assert fps > 0, 'the input is not a video'
        fps = args.fps_multiplier * fps
    else:
        fps = args.fps if args.fps > 0 else fps

    file_extension = os.path.splitext(args.output_dir)[1]
    if file_extension in VIDEO_EXTENSIONS:  # save as video
        h, w = output[0].shape[:2]
        fourcc = cv2.VideoWriter_fourcc(*'mp4v')
        video_writer = cv2.VideoWriter(args.output_dir, fourcc, fps, (w, h))
        for img in output:
            video_writer.write(img)
        cv2.destroyAllWindows()
        video_writer.release()
    else:  # save as images
        for i, img in enumerate(output):
            save_path = f'{args.output_dir}/{args.filename_tmpl.format(i)}'
            mmcv.imwrite(img, save_path)
Example #24
0
def generate_ann(root_path, split, image_infos):
    """Generate cropped annotations and label txt file.

    Args:
        root_path (str): The relative path of the totaltext file
        split (str): The split of dataset. Namely: training or test
        image_infos (list[dict]): A list of dicts of the img and
            annotation information
    """

    dst_image_root = osp.join(root_path, 'dst_imgs', split)
    if split == 'training':
        dst_label_file = osp.join(root_path, 'train_label.txt')
    elif split == 'test':
        dst_label_file = osp.join(root_path, 'test_label.txt')
    os.makedirs(dst_image_root, exist_ok=True)

    lines = []
    for image_info in image_infos:
        index = 1
        src_img_path = osp.join(root_path, 'imgs', image_info['file_name'])
        image = mmcv.imread(src_img_path)
        src_img_root = osp.splitext(image_info['file_name'])[0].split('/')[1]

        for anno in image_info['anno_info']:
            word = anno['word']
            dst_img = crop_img(image, anno['bbox'])

            # Skip invalid annotations
            if min(dst_img.shape) == 0 or word == '###':
                continue

            dst_img_name = f'{src_img_root}_{index}.png'
            index += 1
            dst_img_path = osp.join(dst_image_root, dst_img_name)
            mmcv.imwrite(dst_img, dst_img_path)
            lines.append(f'{osp.basename(dst_image_root)}/{dst_img_name} '
                         f'{word}')
    list_to_file(dst_label_file, lines)
Example #25
0
    def __call__(self, img, boxes, labels):
        img = img.astype(np.float32)
        ##debug
        DEGUG = False
        if DEGUG:
            global COUNT
            image_before = img.copy()
            for idx, bbox in enumerate(boxes):
                image_before = visualize_bbox(image_before, bbox, labels[idx],
                                              {1: 'positive'})
            mmcv.imwrite(
                image_before, "/home/wanglichao/mmdetection/output/img_" +
                str(COUNT + 1) + "_before.jpg")

        for transform in self.transforms:
            img, boxes, labels = transform(img, boxes, labels)

        ##debug
        boxes = np.array(boxes).astype(np.float32)
        labels = np.array(labels).astype(np.int64)

        if len(boxes.shape) == 1:
            print("@" * 100)
            boxes = np.expand_dims(boxes, axis=0)

        if DEGUG:
            print("\nafter", "*" * 50)
            image_after = img.copy()
            for idx, bbox in enumerate(boxes):
                image_after = visualize_bbox(image_after, bbox, labels[idx],
                                             {1: 'positive'})
            mmcv.imwrite(
                image_after, "/home/wanglichao/mmdetection/output/img_" +
                str(COUNT + 1) + "_after.jpg")
            COUNT = COUNT + 1
            if COUNT > 20:
                exit(1)
        return img, boxes, labels
Example #26
0
def dump_frames(vid_item):
    full_path, vid_path, vid_id = vid_item
    vid_name = vid_path.split('.')[0]
    out_full_path = osp.join(args.out_dir, vid_name)

    try:
        os.mkdir(out_full_path)
    except OSError:
        pass

    vr = mmcv.VideoReader(full_path)
    video_length = 0
    while vr.vcap.isOpened():
        ret, frame = vr.vcap.read()
        if ret:
            mmcv.imwrite(frame, '{}/img_{:05d}.jpg'.format(out_full_path, video_length + 1))
        else:
            break
        video_length += 1
    print('{} done with {} frames'.format(vid_name, video_length))
    sys.stdout.flush()
    vr.vcap.release()
    return True
Example #27
0
def vis_combine(dir_a, dir_b, combine_dir, split_txt=None, alpha=None):
    import itertools
    from glob import glob

    import mmcv

    paths_a = glob(os.path.join(dir_a, '**', '*.jpg'), recursive=True)+glob(os.path.join(dir_a, '**', '*.png'), recursive=True)+glob(os.path.join(dir_a, '**', '*.jpeg'), recursive=True)
    

    paths_b = []
    for path_a in paths_a:
        print(path_a)
        if split_txt is not None:
            name = path_a.split(split_txt)[-1]
        else:
            name = osp.basename(path_a)
        path_b = osp.join(dir_b, name)
        if not osp.exists(path_b):
            path_b = path_b.replace('.png', '.jpg')

        assert osp.exists(path_b), path_b
        paths_b.append(path_b)
        
    for pa, pb in zip(paths_a, paths_b):
        if osp.basename(pa).split('.')[0]!=osp.basename(pb).split('.')[0]: continue
        ima = mmcv.imread(pa)
        h, w= ima.shape[:2]
        imb = mmcv.imread(pb)
        imb = mmcv.imresize(imb, (w,h))
        if alpha is None:
            imab = np.concatenate([ima, imb], 1)
        else:
            imab = (0.5*ima + 0.5*imb).astype('uint8')
        # name = osp.basename(pa)
        name = pa.split(split_txt)[-1]
        out_path = osp.join(combine_dir, name)
        mmcv.imwrite(imab, out_path)
    def _save_image(meta, iteration, save_path, output):
        """Save the image.

        Args:
            meta (list[dict]): Meta data, such as path of target file.
                Default: None. These dictionaries should contain
                'target_path' (str of a path) or 'inputs_path' (list of str)
            iteration (int): Iteration for the saving image name.
                Default: None.
            save_path (str): Path to save image. Default: None.
            output (Tensor): Output image.
        """

        if output.ndim == 4:  # an image
            img_name = meta[0]['key'].replace('/', '_')
            if isinstance(iteration, numbers.Number):
                save_path = osp.join(save_path,
                                     f'{img_name}-{iteration + 1:06d}.png')
            elif iteration is None:
                save_path = osp.join(save_path, f'{img_name}.png')
            else:
                raise ValueError('iteration should be number or None, '
                                 f'but got {type(iteration)}')
            mmcv.imwrite(tensor2img(output), save_path)
        elif output.ndim == 5:  # a sequence
            folder_name = meta[0]['key'].split('/')[0]
            for i in range(0, output.size(1)):
                if isinstance(iteration, numbers.Number):
                    save_path_i = osp.join(save_path, folder_name,
                                           f'{i:08d}-{iteration + 1:06d}.png')
                elif iteration is None:
                    save_path_i = osp.join(save_path, folder_name,
                                           f'{i:08d}.png')
                else:
                    raise ValueError('iteration should be number or None, '
                                     f'but got {type(iteration)}')
                mmcv.imwrite(tensor2img(output[:, i, :, :, :]), save_path_i)
Example #29
0
def single_test(model, data_loader, cfg):
    model.eval()
    results = []
    dataset = data_loader.dataset
    prog_bar = mmcv.ProgressBar(len(dataset))
    for i, data in enumerate(data_loader):
        with torch.no_grad():
            result, seg, img, meta = model(return_loss=False, **data)
            #result = model(return_loss=False, **data)
            #'''
            for frame in range(8):
                for idx_in_batch in range(seg.shape[0]):
                    seg0 = seg.argmax(1)[idx_in_batch, frame, :, :]
                    img0 = mmcv.imdenormalize(
                        img[idx_in_batch, :,
                            frame * 4, :, :].transpose([1, 2, 0]),
                        mean=np.array(cfg.img_norm_cfg.mean).reshape(1, 1, 3),
                        std=np.array(cfg.img_norm_cfg.std).reshape(1, 1, 3),
                        to_bgr=cfg.img_norm_cfg.to_rgb)

                    out_dir = os.path.join('outputs_ntucentercrop_1028',
                                           meta[0]['img_path'],
                                           'setting_%02d' % idx_in_batch)
                    if not os.path.isdir(out_dir):
                        os.makedirs(out_dir)
                    mmcv.imwrite(
                        img0, os.path.join(out_dir, 'img_%05d.png' % (frame)))
                    mmcv.imwrite(
                        seg0 * 255,
                        os.path.join(out_dir, 'seg_%05d.png' % (frame)))
            #'''
        results.append(result)

        batch_size = data['img_group_0'].data[0].size(0)
        for _ in range(batch_size):
            prog_bar.update()
    return results
Example #30
0
def main():
    parser = ArgumentParser()
    parser.add_argument('img', help='Image file.')
    parser.add_argument('config', help='Config file.')
    parser.add_argument('checkpoint', help='Checkpoint file.')
    parser.add_argument('out_file', help='Path to save visualized image.')
    parser.add_argument('--device',
                        default='cuda:0',
                        help='Device used for inference.')
    parser.add_argument('--imshow',
                        action='store_true',
                        help='Whether show image with OpenCV.')
    args = parser.parse_args()

    # build the model from a config file and a checkpoint file
    model = init_detector(args.config, args.checkpoint, device=args.device)
    if model.cfg.data.test['type'] == 'ConcatDataset':
        model.cfg.data.test.pipeline = model.cfg.data.test['datasets'][
            0].pipeline

    # test a single image
    result = model_inference(model, args.img)
    print(f'result: {result}')

    # show the results
    img = model.show_result(args.img,
                            result,
                            out_file=args.out_file,
                            show=False)

    if img is None:
        img = mmcv.imread(args.img)

    mmcv.imwrite(img, args.out_file)
    if args.imshow:
        mmcv.imshow(img, 'predicted results')