Пример #1
0
    def test_frames2video(self):
        v = mmcv.VideoReader(self.video_path)
        frame_dir = tempfile.mkdtemp()
        v.cvt2frames(frame_dir)
        assert osp.isdir(frame_dir)
        for i in range(self.num_frames):
            filename = '{}/{:06d}.jpg'.format(frame_dir, i)
            assert osp.isfile(filename)

        out_filename = osp.join(tempfile.gettempdir(), 'mmcv_test.avi')
        mmcv.frames2video(frame_dir, out_filename)
        v = mmcv.VideoReader(out_filename)
        assert v.fps == 30
        assert len(v) == self.num_frames

        mmcv.frames2video(
            frame_dir,
            out_filename,
            fps=25,
            start=10,
            end=50,
            show_progress=False)
        v = mmcv.VideoReader(out_filename)
        assert v.fps == 25
        assert len(v) == 40

        for i in range(self.num_frames):
            filename = '{}/{:06d}.jpg'.format(frame_dir, i)
            os.remove(filename)
        os.removedirs(frame_dir)
        os.remove(out_filename)
Пример #2
0
def inference(
    detection_cfg,
    skeleton_cfg,
    dataset_cfg,
    gpus=1,
    worker_per_gpu=1,
):
    # get frame num
    video_file = dataset_cfg.video_file
    video_name = video_file.strip('/n').split('/')[-1]
    video_frames = mmcv.VideoReader(video_file)
    num_frames = len(video_frames)
    del video_frames

    data_cfg = skeleton_cfg.data_cfg
    if data_cfg.save_video:
        data_cfg.img_dir = os.path.join(data_cfg.save_dir,
                                        '{}.img'.format(video_name))

        if os.path.exists(data_cfg.img_dir):
            import shutil
            shutil.rmtree(data_cfg.img_dir)

        os.makedirs(data_cfg.img_dir)

    # cache model checkpoints
    cache_checkpoint(detection_cfg.checkpoint_file)
    cache_checkpoint(skeleton_cfg.checkpoint_file)

    # multiprocess settings
    context = mp.get_context('spawn')
    result_queue = context.Queue(num_frames)
    procs = []
    for w in range(gpus * worker_per_gpu):
        shred_list = list(range(w, num_frames, gpus * worker_per_gpu))
        p = context.Process(target=worker,
                            args=(video_file, shred_list, detection_cfg,
                                  skeleton_cfg, data_cfg, w % gpus,
                                  result_queue))
        p.start()
        procs.append(p)
    all_result = []
    print('\nPose estimation start:')
    prog_bar = ProgressBar(num_frames)
    for i in range(num_frames):
        t = result_queue.get()
        all_result.append(t)
        prog_bar.update()
    for p in procs:
        p.join()
    if len(all_result) == num_frames and data_cfg.save_video:
        print('\n\nGenerate video:')
        video_path = os.path.join(data_cfg.save_dir, video_name)
        mmcv.frames2video(data_cfg.img_dir,
                          video_path,
                          filename_tmpl='{:01d}.png')
        print('Video was saved to {}'.format(video_path))

    import IPython
    IPython.embed()
Пример #3
0
def main():
    parser = ArgumentParser()
    parser.add_argument('--video', help='video file')
    parser.add_argument('--config', help='Config file')
    parser.add_argument('--checkpoint', help='Checkpoint file')
    parser.add_argument('--device',
                        default='cuda:0',
                        help='Device used for inference')
    parser.add_argument('--score-thr',
                        type=float,
                        default=0.3,
                        help='bbox score threshold')
    parser.add_argument('--theme',
                        type=str,
                        default='white',
                        help='themes to show detect results')
    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)

    # test a video and show the results
    video = mmcv.VideoReader(args.video)
    result_root = "./results"
    check_path_exist(result_root)
    frame_dir = osp.join(result_root, 'frame')
    check_path_exist(frame_dir)
    frame_id = 0
    interal = len(video) // 13
    key_frame_path = "./key_frame"
    check_path_exist(key_frame_path)
    key_frame_dir = osp.join(key_frame_path,
                             args.video.split('/')[-1].split('.')[0])
    check_path_exist(key_frame_dir)
    for frame in tqdm(video, ncols=64):
        result = inference_detector(model, frame)
        model.show_result(frame,
                          result,
                          score_thr=args.score_thr,
                          out_file=osp.join(frame_dir,
                                            '{:06d}.jpg'.format(frame_id)))
        if (frame_id + 1) % interal == 0:
            shutil.copyfile(src=osp.join(frame_dir,
                                         '{:06d}.jpg'.format(frame_id)),
                            dst=osp.join(key_frame_dir,
                                         '{:06d}.jpg'.format(frame_id)))
        frame_id += 1
    output_video_path = osp.join(result_root, args.video.split('/')[-1])
    mmcv.frames2video(frame_dir,
                      output_video_path,
                      fourcc='mp4v',
                      filename_tmpl='{:06d}.jpg')
    shutil.rmtree(frame_dir)
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--config', help='test config file path')
    parser.add_argument('--checkpoint', help='checkpoint file')
    parser.add_argument('--video_path', help='video_path')
    parser.add_argument('--frame_path', help='frame_path')
    parser.add_argument('--inference_path', help='inference image path')
    parser.add_argument('--avi_path', help='avi path')
    args = parser.parse_args()

    config_file = args.config
    checkpoint_file = args.checkpoint
    video_path = os.path.abspath(
        args.video_path
    )  # ex) video_path = ./data/CCTV-11_cctv11_0923_Daytime_CH01.avi
    frame_path = os.path.abspath(
        args.frame_path)  # frame으로 바꾼 이미지들, inference를 시킬 이미지들.
    inference_path = os.path.abspath(args.inference_path)  # inference 시킨 이미지들
    avi_path = os.path.abspath(args.avi_path)  # avi로 바꾼 이미지들

    ######### 1. video2frames
    video = mmcv.VideoReader(video_path)

    ##print(len(video)) # get the total frame number
    # print(video.width, video.height, video.resolution, video.fps)
    # ex) start = 150(기존 영상의 10초부터 시작), max_num = 785 -------------------> 기존 영상의 19초부터
    video.cvt2frames(
        frame_path, start=0, max_num=10
    )  # frame을 (H, W, C) = (960, 1280, 3)으로 만들게 됨, 0~600 frame이 40초 영상으로 변환됨

    ######### 2. implements inference
    model = init_detector(config_file, checkpoint_file, device='cuda:0')
    images = sorted(os.listdir(frame_path))
    integer = 0
    # images에 있는 image를 하나씩 뽑아가면서 inference 시켜야함
    for imgs in images:
        img_for_inference = frame_path + "/" + imgs
        result = inference_detector(model, img_for_inference)
        last_path = inference_path + "/{0:06d}".format(integer) + ".jpg"
        save_result_pyplot(model, img_for_inference, result, last_path)
        integer += 1

    ########## 3. frame2video
    mmcv.frames2video(inference_path, avi_path, fps=15)
    network = network.cuda()
    network.styler = network.styler.cuda()

    if args.parallel:
        network = nn.DataParallel(network)

    if not os.path.exists(args.save_dir):
        os.mkdir(args.save_dir)

    if not os.path.exists(args.log_dir):
        os.mkdir(args.log_dir)

    content_tf = train_transform()
    style_tf = train_transform()

    print('loading dataset done', flush=True)

    for bank in range(10):
        maxlen = 12
        for i in range(1, maxlen):
            print(i, bank, flush=True)
            path = '%05d.jpg' % (i)
            cimg = Image.open(
                os.path.join(
                    '/data/gaowei/datasest/videvo/mini_train/Waterfall1/',
                    path)).convert('RGB')
            cimg = content_tf(cimg).unsqueeze(0).cuda()
            out = network.evaluate(cimg, bank)
            save_image(out, 'output/%06d.jpg' % (i - 1 + bank * 11))
    mmcv.frames2video('output', 'mst_cat_flow.avi', fps=6)
Пример #6
0
def single_gpu_test(model,
                    data_loader,
                    show=False,
                    out_dir=None,
                    fps=3,
                    show_score_thr=0.3):
    """Test model with single gpu.

    Args:
        model (nn.Module): Model to be tested.
        data_loader (nn.Dataloader): Pytorch data loader.
        show (bool, optional): If True, visualize the prediction results.
            Defaults to False.
        out_dir (str, optional): Path of directory to save the
            visualization results. Defaults to None.
        fps (int, optional): FPS of the output video.
            Defaults to 3.
        show_score_thr (float, optional): The score threshold of visualization
            (Only used in VID for now). Defaults to 0.3.

    Returns:
        dict[str, list]: The prediction results.
    """
    model.eval()
    results = defaultdict(list)
    dataset = data_loader.dataset
    prev_img_meta = None
    prog_bar = mmcv.ProgressBar(len(dataset))
    for i, data in enumerate(data_loader):
        with torch.no_grad():
            result = model(return_loss=False, rescale=True, **data)

        batch_size = data['img'][0].size(0)
        if show or out_dir:
            assert batch_size == 1, 'Only support batch_size=1 when testing.'
            img_tensor = data['img'][0]
            img_meta = data['img_metas'][0].data[0][0]
            img = tensor2imgs(img_tensor, **img_meta['img_norm_cfg'])[0]

            h, w, _ = img_meta['img_shape']
            img_show = img[:h, :w, :]

            ori_h, ori_w = img_meta['ori_shape'][:-1]
            img_show = mmcv.imresize(img_show, (ori_w, ori_h))

            if out_dir:
                out_file = osp.join(out_dir, img_meta['ori_filename'])
            else:
                out_file = None

            model.module.show_result(
                img_show,
                result,
                show=show,
                out_file=out_file,
                score_thr=show_score_thr)

            # Whether need to generate a video from images.
            # The frame_id == 0 means the model starts processing
            # a new video, therefore we can write the previous video.
            # There are two corner cases.
            # Case 1: prev_img_meta == None means there is no previous video.
            # Case 2: i == len(dataset) means processing the last video
            need_write_video = (
                prev_img_meta is not None and img_meta['frame_id'] == 0
                or i == len(dataset))
            if out_dir and need_write_video:
                prev_img_prefix, prev_img_name = prev_img_meta[
                    'ori_filename'].rsplit(os.sep, 1)
                prev_img_idx, prev_img_type = prev_img_name.split('.')
                prev_filename_tmpl = '{:0' + str(
                    len(prev_img_idx)) + 'd}.' + prev_img_type
                prev_img_dirs = f'{out_dir}/{prev_img_prefix}'
                prev_img_names = sorted(os.listdir(prev_img_dirs))
                prev_start_frame_id = int(prev_img_names[0].split('.')[0])
                prev_end_frame_id = int(prev_img_names[-1].split('.')[0])

                mmcv.frames2video(
                    prev_img_dirs,
                    f'{prev_img_dirs}/out_video.mp4',
                    fps=fps,
                    fourcc='mp4v',
                    filename_tmpl=prev_filename_tmpl,
                    start=prev_start_frame_id,
                    end=prev_end_frame_id,
                    show_progress=False)

            prev_img_meta = img_meta

        for key in result:
            if 'mask' in key:
                result[key] = encode_mask_results(result[key])

        for k, v in result.items():
            results[k].append(v)

        for _ in range(batch_size):
            prog_bar.update()

    return results
Пример #7
0
def main():
    parser = ArgumentParser()
    parser.add_argument('config', help='Config file')
    parser.add_argument('--input', help='input video file')
    parser.add_argument('--output', help='output video file (mp4 format)')
    parser.add_argument('--checkpoint', help='Checkpoint file')
    parser.add_argument('--device',
                        default='cuda:0',
                        help='Device used for inference')
    parser.add_argument('--show',
                        action='store_true',
                        default=False,
                        help='whether to show visualizations.')
    parser.add_argument('--color',
                        default=(0, 255, 0),
                        help='Color of tracked bbox lines.')
    parser.add_argument('--thickness',
                        default=3,
                        type=int,
                        help='Thickness of bbox lines.')
    parser.add_argument('--fps', help='FPS of the output video')
    parser.add_argument('--gt_bbox_file', help='The path of gt_bbox file')
    args = parser.parse_args()

    # load images
    if osp.isdir(args.input):
        imgs = sorted(filter(lambda x: x.endswith(('.jpg', '.png', '.jpeg')),
                             os.listdir(args.input)),
                      key=lambda x: int(x.split('.')[0]))
        IN_VIDEO = False
    else:
        imgs = mmcv.VideoReader(args.input)
        IN_VIDEO = True

    # define output
    if args.output is not None:
        if args.output.endswith('.mp4'):
            OUT_VIDEO = True
            out_dir = tempfile.TemporaryDirectory()
            out_path = out_dir.name
            _out = args.output.rsplit(os.sep, 1)
            if len(_out) > 1:
                os.makedirs(_out[0], exist_ok=True)
        else:
            OUT_VIDEO = False
            out_path = args.output
            os.makedirs(out_path, exist_ok=True)
    fps = args.fps
    if args.show or OUT_VIDEO:
        if fps is None and IN_VIDEO:
            fps = imgs.fps
        if not fps:
            raise ValueError('Please set the FPS for the output video.')
        fps = int(fps)

    # build the model from a config file and a checkpoint file
    model = init_model(args.config, args.checkpoint, device=args.device)

    prog_bar = mmcv.ProgressBar(len(imgs))
    # test and show/save the images
    for i, img in enumerate(imgs):
        if isinstance(img, str):
            img = osp.join(args.input, img)
            img = mmcv.imread(img)
        if i == 0:
            if args.gt_bbox_file is not None:
                bboxes = mmcv.list_from_file(args.gt_bbox_file)
                init_bbox = list(map(float, bboxes[0].split(',')))
            else:
                init_bbox = list(cv2.selectROI(args.input, img, False, False))

            # convert (x1, y1, w, h) to (x1, y1, x2, y2)
            init_bbox[2] += init_bbox[0]
            init_bbox[3] += init_bbox[1]

        result = inference_sot(model, img, init_bbox, frame_id=i)
        if args.output is not None:
            if IN_VIDEO or OUT_VIDEO:
                out_file = osp.join(out_path, f'{i:06d}.jpg')
            else:
                out_file = osp.join(out_path, img.rsplit('os.sep', 1)[-1])
        else:
            out_file = None
        model.show_result(img,
                          result,
                          show=args.show,
                          wait_time=int(1000. / fps) if fps else 0,
                          out_file=out_file,
                          thickness=args.thickness)
        prog_bar.update()

    if args.output and OUT_VIDEO:
        print(
            f'\nmaking the output video at {args.output} with a FPS of {fps}')
        mmcv.frames2video(out_path, args.output, fps=fps, fourcc='mp4v')
        out_dir.cleanup()
Пример #8
0
def main():
    args = parse_args()

    assert args.out or args.eval or args.format_only or args.show \
        or args.show_dir, \
        ('Please specify at least one operation (save/eval/format/show the '
         'results / save the results) with the argument "--out", "--eval"'
         ', "--format-only", "--show" or "--show-dir"')

    if args.eval and args.format_only:
        raise ValueError('--eval and --format_only cannot be both specified')

    if args.out is not None and not args.out.endswith(('.pkl', '.pickle')):
        raise ValueError('The output file must be a pkl file.')

    # config 파일 읽기
    cfg = Config.fromfile(args.config)
    # 코드 돌릴 때 --cfg-options 설정하면 기존 config 파일에 내용 합치는 코드
    if args.cfg_options is not None:
        cfg.merge_from_dict(args.cfg_options)
    # import modules from string list.
    if cfg.get('custom_imports', None):
        from mmcv.utils import import_modules_from_strings
        import_modules_from_strings(**cfg['custom_imports'])
    # set cudnn_benchmark
    if cfg.get('cudnn_benchmark', False):
        torch.backends.cudnn.benchmark = True
    cfg.model.pretrained = None
    if cfg.model.get('neck'):
        if isinstance(cfg.model.neck, list):
            for neck_cfg in cfg.model.neck:
                if neck_cfg.get('rfp_backbone'):
                    if neck_cfg.rfp_backbone.get('pretrained'):
                        neck_cfg.rfp_backbone.pretrained = None
        elif cfg.model.neck.get('rfp_backbone'):
            if cfg.model.neck.rfp_backbone.get('pretrained'):
                cfg.model.neck.rfp_backbone.pretrained = None

    # in case the test dataset is concatenated
    if isinstance(cfg.data.test, dict):  # cfg.data.test가 dict 타입이면 실행
        cfg.data.test.test_mode = True  # coco_detection.py 랑 비슷한 form에 test_mode를 설정해줄 수 있나봄
    elif isinstance(cfg.data.test, list):  # cfg.data.test가 list 타입이면 실행
        for ds_cfg in cfg.data.test:
            ds_cfg.test_mode = True

    # init distributed env first, since logger depends on the dist info.
    if args.launcher == 'none':
        distributed = False
    else:
        distributed = True
        init_dist(args.launcher, **cfg.dist_params)

    # build the dataloader
    samples_per_gpu = cfg.data.test.pop('samples_per_gpu', 1)
    if samples_per_gpu > 1:
        # Replace 'ImageToTensor' to 'DefaultFormatBundle'
        cfg.data.test.pipeline = replace_ImageToTensor(cfg.data.test.pipeline)

    ########################################################################################
    """ 여기에서 cctv 영상을 이미지로 변환해서 폴더에 넣고 cfg.data.test 부분에 폴더 경로를 추가"""
    video = mmcv.VideoReader("/home/minjae/mjseong/mmdetection/data/test.avi")
    print(len(video))  # get the total frame number
    print(video.width, video.height, video.resolution, video.fps)
    video.cvt2frames(
        "/home/minjae/mjseong/mmdetection/data/KRRI_Video_cvt2frames",
        start=0,
        max_num=10000)
    cfg.data.test.img_prefix = "data_root" + "/home/minjae/mjseong/mmdetection/data/KRRI_Video_cvt2frames/"
    # cfg.data.test.ann_file =
    ########################################################################################

    dataset = build_dataset(cfg.data.test)
    data_loader = build_dataloader(dataset,
                                   samples_per_gpu=samples_per_gpu,
                                   workers_per_gpu=cfg.data.workers_per_gpu,
                                   dist=distributed,
                                   shuffle=False)

    # build the model and load checkpoint
    model = build_detector(cfg.model, train_cfg=None, test_cfg=cfg.test_cfg)
    fp16_cfg = cfg.get('fp16', None)
    if fp16_cfg is not None:
        wrap_fp16_model(model)
    checkpoint = load_checkpoint(model, args.checkpoint, map_location='cpu')
    if args.fuse_conv_bn:
        model = fuse_conv_bn(model)
    # old versions did not save class info in checkpoints, this walkaround is
    # for backward compatibility
    if 'CLASSES' in checkpoint['meta']:
        model.CLASSES = checkpoint['meta']['CLASSES']
    else:
        model.CLASSES = dataset.CLASSES

    if not distributed:
        model = MMDataParallel(model, device_ids=[0])
        outputs = single_gpu_test(model, data_loader, args.show, args.show_dir,
                                  args.show_score_thr)
    else:
        model = MMDistributedDataParallel(
            model.cuda(),
            device_ids=[torch.cuda.current_device()],
            broadcast_buffers=False)
        outputs = multi_gpu_test(model, data_loader, args.tmpdir,
                                 args.gpu_collect)

    rank, _ = get_dist_info()
    if rank == 0:
        if args.out:
            print(f'\nwriting results to {args.out}')
            mmcv.dump(outputs, args.out)
        kwargs = {} if args.eval_options is None else args.eval_options
        if args.format_only:
            dataset.format_results(outputs, **kwargs)
        if args.eval:
            eval_kwargs = cfg.get('evaluation', {}).copy()
            # hard-code way to remove EvalHook args
            for key in ['interval', 'tmpdir', 'start', 'gpu_collect']:
                eval_kwargs.pop(key, None)
            eval_kwargs.update(dict(metric=args.eval, **kwargs))
            print(dataset.evaluate(outputs, **eval_kwargs))

    ########################################################################################
    """여기에서 box쳐진 test 결과의 이미지를 다시 video로 변환 시켜주기"""
    mmcv.frames2video(
        "/home/minjae/mjseong/mmdetection/data/KRRI_Video_cvt2frames",
        "/home/minjae/mjseong/mmdetection/data/test.avi")
Пример #9
0
def main():
    parser = ArgumentParser()
    parser.add_argument('config', help='config file')
    parser.add_argument('--input', help='input video file or folder')
    parser.add_argument('--output',
                        help='output video file (mp4 format) or folder')
    parser.add_argument('--checkpoint', help='checkpoint file')
    parser.add_argument('--device',
                        default='cuda:0',
                        help='device used for inference')
    parser.add_argument('--show',
                        action='store_true',
                        help='whether show the results on the fly')
    parser.add_argument('--backend',
                        choices=['cv2', 'plt'],
                        default='cv2',
                        help='the backend to visualize the results')
    parser.add_argument('--fps', help='FPS of the output video')
    args = parser.parse_args()
    assert args.output or args.show
    # load images
    if osp.isdir(args.input):
        imgs = sorted(os.listdir(args.input))
        IN_VIDEO = False
    else:
        imgs = mmcv.VideoReader(args.input)
        IN_VIDEO = True
    # define output
    if args.output is not None:
        if args.output.endswith('.mp4'):
            OUT_VIDEO = True
            out_dir = tempfile.TemporaryDirectory()
            out_path = out_dir.name
            _out = args.output.rsplit('/', 1)
            if len(_out) > 1:
                os.makedirs(_out[0], exist_ok=True)
        else:
            OUT_VIDEO = False
            out_path = args.output
            os.makedirs(out_path, exist_ok=True)

    fps = args.fps
    if args.show or OUT_VIDEO:
        if fps is None and IN_VIDEO:
            fps = imgs.fps
        if not fps:
            raise ValueError('Please set the FPS for the output video.')
        fps = int(fps)

    # build the model from a config file and a checkpoint file
    model = init_model(args.config, args.checkpoint, device=args.device)

    prog_bar = mmcv.ProgressBar(len(imgs))
    # test and show/save the images
    for i, img in enumerate(imgs):
        if isinstance(img, str):
            img = osp.join(args.input, img)
        result = inference_mot(model, img, frame_id=i)
        if args.output is not None:
            if IN_VIDEO or OUT_VIDEO:
                out_file = osp.join(out_path, f'{i:06d}.jpg')
            else:
                out_file = osp.join(out_path, img.rsplit('/', 1)[-1])
        else:
            out_file = None
        model.show_result(img,
                          result,
                          show=args.show,
                          wait_time=int(1000. / fps) if fps else 0,
                          out_file=out_file,
                          backend=args.backend)
        prog_bar.update()

    if OUT_VIDEO:
        print(f'making the output video at {args.output} with a FPS of {fps}')
        mmcv.frames2video(out_path, args.output, fps=fps)
        out_dir.cleanup()
Пример #10
0

if __name__ == "__main__":
    # cut_video('data/green-video', 'data/green-video/green-background')

    # video = mmcv.VideoReader('data/GS_Video/VID_20200727_140923.mp4')
    # video.cvt2frames('data/GS_Video/gsvideo_image', filename_tmpl='{:06d}.png')

    # mmcv.frames2video('data/GS_Video/results/ngs-iter_78000-SAD-7.081.pth/fg-merged', 'data/GS_Video/results/ngs-iter_78000-SAD-7.081.pth/fg-merged.avi', filename_tmpl='{:06d}.png')

    #concat_video('data/GS_Video/results/ngs-iter_78000-SAD-7.081.pth/img-merged.avi', 'data/GS_Video/results/ngs-iter_78000-SAD-7.081.pth/fg-merged.avi', 'data/GS_Video/results/ngs-iter_78000-SAD-7.081.pth')

    # concat_video('data/GS_Video/results/ngs-iter_78000-SAD-7.081.pth/SDK_raw_index.avi', 'data/GS_Video/results/ngs-iter_78000-SAD-7.081.pth/EIG.mp4', 'data/GS_Video/results/ngs-iter_78000-SAD-7.081.pth/fg-merged.avi', 'data/GS_Video/results/ngs-iter_78000-SAD-7.081.pth/submit')

    # video = mmcv.VideoReader('data/GS_Video/motion_blur/sorce.avi')
    # video.cvt2frames('data/GS_Video/motion_blur/sorce', filename_tmpl='{:06d}.png')

    # mmcv.frames2video('data/GS_Video/motion_blur/results/ngs-iter_78000-SAD-7.081.pth/fg-merged', 'data/GS_Video/motion_blur/results/ngs-iter_78000-SAD-7.081.pth/fg-merged/fg-merged.avi', filename_tmpl='{:06d}.png')
    # mmcv.frames2video('data/GS_Video/motion_blur/results/ngs-iter_78000-SAD-7.081.pth/img-merged', 'data/GS_Video/motion_blur/results/ngs-iter_78000-SAD-7.081.pth/fg-merged/img-merged.avi', filename_tmpl='{:06d}.png')
    # mmcv.frames2video('data/GS_Video/motion_blur/results/ngs-iter_78000-SAD-7.081.pth/fg-merged-low', 'data/GS_Video/motion_blur/results/ngs-iter_78000-SAD-7.081.pth/fg-merged-low.avi', filename_tmpl='{:06d}.png')

    # concat_video('data/GS_Video/motion_blur/results/compare/IMG_1967_EIG.mp4', 'data/GS_Video/motion_blur/results/compare/IMG_1967_indexnet_ft_result.avi', 'data/GS_Video/motion_blur/results/ngs-iter_78000-SAD-7.081.pth/fg-merged-low.avi', 'data/GS_Video/motion_blur/results/compare')

    mmcv.frames2video(
        'data/GS_Video/man/results/port+purebg/fg-merged',
        'data/GS_Video/man/results/port+purebg/port+purebg-epoch30.avi',
        filename_tmpl='{:06d}.png')

    # concat_2video('data/GS_Video/motion_blur/results/ngs-iter_78000-SAD-7.081.pth/fg-merged-low.avi', 'data/GS_Video/motion_blur/results/ngs-iter_78000-SAD-7.081.pth/ofd0.1.avi', 'data/GS_Video/motion_blur/results/ngs-iter_78000-SAD-7.081.pth')

    # cut_video('data/green-video/video/other_color', 'data/portrait+purebg/bg')
Пример #11
0
def main():
    parser = ArgumentParser()
    parser.add_argument('config', help='Config file')
    parser.add_argument('--input', help='input video file')
    parser.add_argument('--output', help='output video file (mp4 format)')
    parser.add_argument('--checkpoint', help='Checkpoint file')
    parser.add_argument(
        '--device', default='cuda:0', help='Device used for inference')
    parser.add_argument(
        '--show',
        action='store_true',
        default=False,
        help='whether to show visualizations.')
    parser.add_argument(
        '--score-thr', type=float, default=0.8, help='bbox score threshold')
    parser.add_argument(
        '--thickness', default=3, type=int, help='Thickness of bbox lines.')
    parser.add_argument('--fps', help='FPS of the output video')
    args = parser.parse_args()

    # load images
    if osp.isdir(args.input):
        imgs = sorted(os.listdir(args.input))
        IN_VIDEO = False
    else:
        imgs = mmcv.VideoReader(args.input)
        IN_VIDEO = True

    # define output
    if args.output is not None:
        if args.output.endswith('.mp4'):
            OUT_VIDEO = True
            out_dir = tempfile.TemporaryDirectory()
            out_path = out_dir.name
            _out = args.output.rsplit('/', 1)
            if len(_out) > 1:
                os.makedirs(_out[0], exist_ok=True)
        else:
            OUT_VIDEO = False
            out_path = args.output
            os.makedirs(out_path, exist_ok=True)
    fps = args.fps
    if args.show or OUT_VIDEO:
        if fps is None and IN_VIDEO:
            fps = imgs.fps
        if not fps:
            raise ValueError('Please set the FPS for the output video.')
        fps = int(fps)

    # build the model from a config file and a checkpoint file
    model = init_model(args.config, args.checkpoint, device=args.device)

    prog_bar = mmcv.ProgressBar(len(imgs))
    # test and show/save the images
    for i, img in enumerate(imgs):
        if isinstance(img, str):
            img = osp.join(args.input, img)
            img = mmcv.imread(img)

        result = inference_vid(model, img, frame_id=i)
        if args.output is not None:
            if IN_VIDEO or OUT_VIDEO:
                out_file = osp.join(out_path, f'{i:06d}.jpg')
            else:
                out_file = osp.join(out_path, img.rsplit('/', 1)[-1])
        else:
            out_file = None
        model.show_result(
            img,
            result,
            score_thr=args.score_thr,
            show=args.show,
            wait_time=int(1000. / fps) if fps else 0,
            out_file=out_file,
            thickness=args.thickness)
        prog_bar.update()

    if OUT_VIDEO:
        print(
            f'\nmaking the output video at {args.output} with a FPS of {fps}')
        mmcv.frames2video(out_path, args.output, fps=fps, fourcc='mp4v')
        out_dir.cleanup()
Пример #12
0
import mmcv
import os

path = os.path.abspath("./data/image_inference_result02/")
mmcv.frames2video(path,
                  "/home/minjae/mjseong/mmdetection/data/test02.avi",
                  fps=15)
# mmcv.frames2video("/home/minjae/mjseong/mmdetection/data/KRRI_Video_cvt2frames_6000", "/home/minjae/mjseong/mmdetection/data/6000test.avi", fps= 20)
Пример #13
0
def main():
    args = parse_args()

    assert args.show or args.out_dir, \
        ('Please specify at least one operation (show the results '
         '/ save the results) with the argument "--show" or "--out-dir"')

    if not args.result_file.endswith(('.pkl', 'pickle')):
        raise ValueError('The result file must be a pkl file.')

    if args.out_dir is not None:
        os.makedirs(args.out_dir, exist_ok=True)

    print_log('This script visualizes the error for multiple object tracking. '
              'By Default, the red bounding box denotes false positive, '
              'the yellow bounding box denotes the false negative '
              'and the blue bounding box denotes ID switch.')

    cfg = Config.fromfile(args.config)
    dataset = build_dataset(cfg.data.val, dict(test_mode=True))
    results = mmcv.load(args.result_file)

    # create index from frame_id to filename
    filenames_dict = dict()
    for data_info in dataset.data_infos:
        video_name = data_info['filename'].split('/', 1)[0]
        frame_id = int(data_info['filename'].rsplit('/', 1)[-1].split('.')[0])
        if video_name not in filenames_dict:
            filenames_dict[video_name] = dict()
        filenames_dict[video_name][frame_id] = data_info['filename']

    # format the results to txts
    resfiles, video_names, tmp_dir = dataset.format_results(
        results, None, ['track'])

    for video_name in video_names:
        print_log(f'Start processing video {video_name}')

        acc, res, gt = compare_res_gts(resfiles, dataset, video_name)

        frames_id_list = sorted(
            list(set(acc.mot_events.index.get_level_values(0))))
        for frame_id in frames_id_list:
            # events in the current frame
            events = acc.mot_events.xs(frame_id)
            cur_res = res.loc[frame_id] if frame_id in res.index else None
            cur_gt = gt.loc[frame_id] if frame_id in gt.index else None
            # path of image
            img = osp.join(dataset.img_prefix,
                           filenames_dict[video_name][frame_id])
            fps = events[events.Type == 'FP']
            fns = events[events.Type == 'MISS']
            idsws = events[events.Type == 'SWITCH']

            bboxes, ids, error_types = [], [], []
            for fp_index in fps.index:
                hid = events.loc[fp_index].HId
                bboxes.append([
                    cur_res.loc[hid].X, cur_res.loc[hid].Y,
                    cur_res.loc[hid].X + cur_res.loc[hid].Width,
                    cur_res.loc[hid].Y + cur_res.loc[hid].Height,
                    cur_res.loc[hid].Confidence
                ])
                ids.append(hid)
                # error_type = 0 denotes false positive error
                error_types.append(0)
            for fn_index in fns.index:
                oid = events.loc[fn_index].OId
                bboxes.append([
                    cur_gt.loc[oid].X, cur_gt.loc[oid].Y,
                    cur_gt.loc[oid].X + cur_gt.loc[oid].Width,
                    cur_gt.loc[oid].Y + cur_gt.loc[oid].Height,
                    cur_gt.loc[oid].Confidence
                ])
                ids.append(-1)
                # error_type = 1 denotes false negative error
                error_types.append(1)
            for idsw_index in idsws.index:
                hid = events.loc[idsw_index].HId
                bboxes.append([
                    cur_res.loc[hid].X, cur_res.loc[hid].Y,
                    cur_res.loc[hid].X + cur_res.loc[hid].Width,
                    cur_res.loc[hid].Y + cur_res.loc[hid].Height,
                    cur_res.loc[hid].Confidence
                ])
                ids.append(hid)
                # error_type = 2 denotes id switch
                error_types.append(2)
            if len(bboxes) == 0:
                bboxes = np.zeros((0, 5), dtype=np.float32)
            else:
                bboxes = np.asarray(bboxes, dtype=np.float32)
            ids = np.asarray(ids, dtype=np.int32)
            error_types = np.asarray(error_types, dtype=np.int32)
            imshow_mot_errors(
                img,
                bboxes,
                ids,
                error_types,
                show=args.show,
                out_file=osp.join(args.out_dir,
                                  f'{video_name}/{frame_id:06d}.jpg')
                if args.out_dir else None,
                backend=args.backend)

        print_log(f'Done! Visualization images are saved in '
                  f'\'{args.out_dir}/{video_name}\'')

        mmcv.frames2video(f'{args.out_dir}/{video_name}',
                          f'{args.out_dir}/{video_name}.mp4',
                          fps=args.fps,
                          fourcc='mp4v',
                          start=frames_id_list[0],
                          end=frames_id_list[-1],
                          show_progress=False)
        print_log(
            f'Done! Visualization video is saved as '
            f'\'{args.out_dir}/{video_name}.mp4\' with a FPS of {args.fps}')