Python mmcv.imwrite() Examples

The following are 4 code examples of mmcv.imwrite(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module mmcv , or try the search function .
Example #1
Source File: build_rawframes.py    From mmaction with Apache License 2.0 6 votes vote down vote up
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 #2
Source File: test_io.py    From mmcv with Apache License 2.0 5 votes vote down vote up
def test_imwrite(self):
        img = mmcv.imread(self.img_path)
        out_file = osp.join(tempfile.gettempdir(), 'mmcv_test.jpg')
        mmcv.imwrite(img, out_file)
        rewrite_img = mmcv.imread(out_file)
        os.remove(out_file)
        self.assert_img_equal(img, rewrite_img)

        ret = mmcv.imwrite(
            img, './non_exist_path/mmcv_test.jpg', auto_mkdir=False)
        assert ret is False 
Example #3
Source File: image2skeleton.py    From mmskeleton with Apache License 2.0 5 votes vote down vote up
def save(image, det_image, pred, name):
    batch_size = pred.shape[0]
    num_joints = pred.shape[1]
    cimage = np.expand_dims(image, axis=0)
    cimage = torch.from_numpy(cimage)
    pred = torch.from_numpy(pred)
    cimage = cimage.permute(0, 3, 1, 2)
    pred_vis = torch.ones((batch_size, num_joints, 1))
    ndrr = save_batch_image_with_joints(cimage, pred, pred_vis)
    mask = ndrr[:, :, 0] == 255
    mask = np.expand_dims(mask, axis=2)
    out = ndrr * mask + det_image * (1 - mask)
    mmcv.imwrite(out, name) 
Example #4
Source File: image2skeleton.py    From mmskeleton with Apache License 2.0 4 votes vote down vote up
def worker(video_file, index, detection_cfg, skeleton_cfg, skeleon_data_cfg,
           device, result_queue):
    os.environ["CUDA_VISIBLE_DEVICES"] = str(device)
    video_frames = mmcv.VideoReader(video_file)

    # load model
    detection_model_file = detection_cfg.model_cfg
    detection_checkpoint_file = get_mmskeleton_url(
        detection_cfg.checkpoint_file)
    detection_model = init_detector(detection_model_file,
                                    detection_checkpoint_file,
                                    device='cpu')
    skeleton_model_file = skeleton_cfg.model_cfg
    skeletion_checkpoint_file = skeleton_cfg.checkpoint_file
    skeleton_model = init_twodimestimator(skeleton_model_file,
                                          skeletion_checkpoint_file,
                                          device='cpu')

    detection_model = detection_model.cuda()
    skeleton_model = skeleton_model.cuda()

    for idx in index:
        skeleton_result = dict()
        image = video_frames[idx]
        draw_image = image.copy()
        bbox_result = inference_detector(detection_model, image)

        person_bbox, labels = VideoDemo.bbox_filter(bbox_result,
                                                    detection_cfg.bbox_thre)

        if len(person_bbox) > 0:
            person, meta = VideoDemo.skeleton_preprocess(
                image[:, :, ::-1], person_bbox, skeleon_data_cfg)
            preds, maxvals = inference_twodimestimator(skeleton_model,
                                                       person.cuda(), meta,
                                                       True)
            results = VideoDemo.skeleton_postprocess(preds, maxvals, meta)
            if skeleon_data_cfg.save_video:
                file = os.path.join(skeleon_data_cfg.img_dir,
                                    '{}.png'.format(idx))
                mmcv.imshow_det_bboxes(draw_image,
                                       person_bbox,
                                       labels,
                                       detection_model.CLASSES,
                                       score_thr=detection_cfg.bbox_thre,
                                       show=False,
                                       wait_time=0)
                save(image, draw_image, results, file)

        else:
            preds, maxvals = None, None
            if skeleon_data_cfg.save_video:
                file = os.path.join(skeleon_data_cfg.img_dir,
                                    '{}.png'.format(idx))
                mmcv.imwrite(image, file)
        skeleton_result['frame_index'] = idx
        skeleton_result['position_preds'] = preds
        skeleton_result['position_maxvals'] = maxvals
        result_queue.put(skeleton_result)