def __call__(self, results): """Loading image(s) from file.""" if self.file_client is None: self.file_client = mmcv.FileClient(**self.file_client_args) image_file = results['image_file'] if isinstance(image_file, (list, tuple)): imgs = [] for image in image_file: img_bytes = self.file_client.get(image) img = mmcv.imfrombytes(img_bytes, flag=self.color_type, channel_order=self.channel_order) if self.to_float32: img = img.astype(np.float32) if img is None: raise ValueError(f'Fail to read {image}') imgs.append(img) results['img'] = imgs else: img_bytes = self.file_client.get(image_file) img = mmcv.imfrombytes(img_bytes, flag=self.color_type, channel_order=self.channel_order) if self.to_float32: img = img.astype(np.float32) if img is None: raise ValueError(f'Fail to read {image_file}') results['img'] = img return results
def test_petrel_backend(self): petrel_backend = FileClient('petrel') # input path is Path object with pytest.raises(NotImplementedError): petrel_backend.get_text(self.text_path) # input path is str with pytest.raises(NotImplementedError): petrel_backend.get_text(str(self.text_path)) # input path is Path object img_bytes = petrel_backend.get(self.img_path) img = mmcv.imfrombytes(img_bytes) assert img.shape == self.img_shape # input path is str img_bytes = petrel_backend.get(str(self.img_path)) img = mmcv.imfrombytes(img_bytes) assert img.shape == self.img_shape # `path_mapping` is either None or dict with pytest.raises(AssertionError): FileClient('petrel', path_mapping=1) # test `path_mapping` petrel_path = 's3://user/data' petrel_backend = FileClient( 'petrel', path_mapping={str(self.test_data_dir): petrel_path}) petrel_backend.client._client.Get = MagicMock( return_value=petrel_backend.client._client.Get(self.img_path)) img_bytes = petrel_backend.get(self.img_path) img = mmcv.imfrombytes(img_bytes) assert img.shape == self.img_shape petrel_backend.client._client.Get.assert_called_with( str(self.img_path).replace(str(self.test_data_dir), petrel_path))
def __call__(self, results): """Call functions to load image and get image meta information. Args: results (dict): Result dict from :obj:`mmseg.CustomDataset`. Returns: dict: The dict contains loaded image and meta information. """ if self.file_client is None: self.file_client = mmcv.FileClient(**self.file_client_args) if results.get('img_prefix') is not None: filename = osp.join(results['img_prefix'], results['img_info']['filename']) else: filename = results['img_info']['filename'] img_bytes = self.file_client.get(filename) img = mmcv.imfrombytes(img_bytes, flag=self.color_type, backend=self.imdecode_backend) if self.to_float32: img = img.astype(np.float32) if results.get('sequence_prefix') is not None: sequence_imgs = [] sequence = results['sequence_info']['frames'] for f in sequence: frame_filename = osp.join(results['sequence_prefix'], f) img_bytes = self.file_client.get(frame_filename) frame = mmcv.imfrombytes(img_bytes, flag=self.color_type, backend=self.imdecode_backend) if self.to_float32: frame = frame.astype(np.float32) sequence_imgs.append(frame) else: sequence = None sequence_imgs = None results['filename'] = filename results['ori_filename'] = results['img_info']['filename'] results['img'] = img results['img_shape'] = img.shape results['ori_shape'] = img.shape results['sequence_filename'] = sequence results['sequence_imgs'] = sequence_imgs # Set initial values for default meta_keys results['pad_shape'] = img.shape results['scale_factor'] = 1.0 num_channels = 1 if len(img.shape) < 3 else img.shape[2] results['img_norm_cfg'] = dict(mean=np.zeros(num_channels, dtype=np.float32), std=np.ones(num_channels, dtype=np.float32), to_rgb=False) return results
def _load_image_annotations_with_template(self, results, k): # clw modify results = results[k] if self.file_client is None: self.file_client = mmcv.FileClient(**self.file_client_args) if results['img_prefix'] is not None: filename = osp.join(results['img_prefix'], results['img_info']['filename']) else: filename = results['img_info']['filename'] img_bytes = self.file_client.get(filename) img = mmcv.imfrombytes(img_bytes, flag=self.color_type) template_filename = os.path.join( self.template_path, filename.split('/')[-1][:-4] + '_t.jpg') template_img_bytes = self.file_client.get(template_filename) img_t = mmcv.imfrombytes(template_img_bytes, flag=self.color_type) if self.to_float32: img = img.astype(np.float32) img_t = img_t.astype(np.float32) results['filename'] = filename results['ori_filename'] = results['img_info']['filename'] results['img'] = img results['img_t'] = img_t results['concat'] = True results['img_fields'] = ['img', 'img_t'] results['img_shape'] = img.shape results['ori_shape'] = img.shape # Set initial values for default meta_keys results['pad_shape'] = img.shape results['scale_factor'] = 1.0 num_channels = 1 if len(img.shape) < 3 else img.shape[2] results['img_norm_cfg'] = dict(mean=np.zeros(num_channels, dtype=np.float32), std=np.ones(num_channels, dtype=np.float32), to_rgb=False) results['img_fields'] = ['img'] if self.with_bbox: results = self._load_bboxes(results) if results is None: return None if self.with_label: results = self._load_labels(results) if self.with_mask: results = self._load_masks(results) if self.with_seg: results = self._load_semantic_seg(results) return results
def __call__(self, results): """Call functions to load image and get image meta information. Args: results (dict): Result dict from :obj:`mmdet.CustomDataset`. Returns: dict: The dict contains loaded image and meta information. """ if self.file_client is None: self.file_client = mmcv.FileClient(**self.file_client_args) if results['img_prefix'] is not None: filename = osp.join(results['img_prefix'], results['img_info']['filename']) else: filename = results['img_info']['filename'] template_filename = osp.basename(filename).split( '.')[0] + '_t' + '.jpg' if not isinstance(self.template_path, list): template_filename = osp.join(self.template_path, template_filename) else: if "20210204" in filename: template_filename = osp.join(self.template_path[0], template_filename) elif "20210208" in filename: template_filename = osp.join(self.template_path[1], template_filename) else: assert "no template image error" img_bytes = self.file_client.get(filename) img = mmcv.imfrombytes(img_bytes, flag=self.color_type) template_bytes = self.file_client.get(template_filename) template_img = mmcv.imfrombytes(template_bytes, flag=self.color_type) if self.to_float32: img = img.astype(np.float32) template_img = template_img.astype(np.float32) assert img.shape == template_img.shape results['filename'] = filename results['ori_filename'] = results['img_info']['filename'] results['img'] = img results['img_shape'] = img.shape results['ori_shape'] = img.shape results['img_fields'] = ['img'] results['template_filename'] = template_filename results['template_img'] = template_img return results
def __call__(self, results): """Call function to load multiple types annotations. Args: results (dict): Result dict from :obj:`mmseg.CustomDataset`. Returns: dict: The dict contains loaded semantic segmentation annotations. """ if self.file_client is None: self.file_client = mmcv.FileClient(**self.file_client_args) if results.get('seg_prefix', None) is not None: filename = osp.join(results['seg_prefix'], results['ann_info']['seg_map']) else: filename = results['ann_info']['seg_map'] if results['ann_info']['exist_label'] == False: # read the images and set the label map as the same size as the image if results.get('img_prefix') is not None: filename = osp.join(results['img_prefix'], results['img_info']['filename']) else: filename = results['img_info']['filename'] img_bytes = self.file_client.get(filename) img = mmcv.imfrombytes(img_bytes, flag='color', backend='cv2') # set the label values as ignore index gt_semantic_seg = (img[:, :, 1] * 0.0 + 255).astype(np.uint8) else: img_bytes = self.file_client.get(filename) gt_semantic_seg = mmcv.imfrombytes( img_bytes, flag='unchanged', backend=self.imdecode_backend).squeeze().astype(np.uint8) if 'leftImg8bit' in filename: # convert the cityscapes labels gt_semantic_seg = self._convert_cityscapes_label( gt_semantic_seg) # modify if custom classes if results.get('label_map', None) is not None: for old_id, new_id in results['label_map'].items(): gt_semantic_seg[gt_semantic_seg == old_id] = new_id # reduce zero_label if self.reduce_zero_label: # avoid using underflow conversion gt_semantic_seg[gt_semantic_seg == 0] = 255 gt_semantic_seg = gt_semantic_seg - 1 gt_semantic_seg[gt_semantic_seg == 254] = 255 results['gt_semantic_seg'] = gt_semantic_seg results['seg_fields'].append('gt_semantic_seg') return results
def __getitem__(self, index): if self.file_client is None: self.file_client = FileClient(self.io_backend_opt.pop('type'), **self.io_backend_opt) # random reverse if self.random_reverse and random.random() < 0.5: self.neighbor_list.reverse() scale = self.opt['scale'] gt_size = self.opt['gt_size'] key = self.keys[index] clip, seq = key.split('/') # key example: 00001/0001 # get the GT frame (im4.png) if self.is_lmdb: img_gt_path = f'{key}/im4' else: img_gt_path = self.gt_root / clip / seq / 'im4.png' img_bytes = self.file_client.get(img_gt_path, 'gt') img_gt = mmcv.imfrombytes(img_bytes).astype(np.float32) / 255. # get the neighboring LQ frames img_lqs = [] for neighbor in self.neighbor_list: if self.is_lmdb: img_lq_path = f'{clip}/{seq}/im{neighbor}' else: img_lq_path = self.lq_root / clip / seq / f'im{neighbor}.png' img_bytes = self.file_client.get(img_lq_path, 'lq') img_lq = mmcv.imfrombytes(img_bytes).astype(np.float32) / 255. img_lqs.append(img_lq) # randomly crop img_gt, img_lqs = paired_random_crop(img_gt, img_lqs, gt_size, scale, img_gt_path) # augmentation - flip, rotate img_lqs.append(img_gt) img_results = augment(img_lqs, self.opt['use_flip'], self.opt['use_rot']) img_results = totensor(img_results) img_lqs = torch.stack(img_results[0:-1], dim=0) img_gt = img_results[-1] # img_lqs: (t, c, h, w) # img_gt: (c, h, w) # key: str return {'lq': img_lqs, 'gt': img_gt, 'key': key}
def _load_image(self, key_tmpl, modality, idx): print("key_tmpl.format(idx)=", key_tmpl.format(idx)) if modality in ['RGB', 'RGBDiff']: img_bytes = self.txn.get(str(key_tmpl.format(idx)).encode()) return [mmcv.imfrombytes(img_bytes, flag='color')] elif modality == 'Flow': img_bytes = self.txn.get(key_tmpl.format('x', idx)) x_imgs = mmcv.imfrombytes(img_bytes, flag='grayscale') img_bytes = self.txn.get(key_tmpl.format('y', idx)) y_imgs = mmcv.imfrombytes(img_bytes, flag='grayscale') return [x_imgs, y_imgs] else: raise ValueError('Not implemented yet; modality' 'should be ["RGB", "RGBDiff", "Flow"]')
def _load_img_ann(self, results): # load image file_client_img = mmcv.FileClient(**self.file_client_args.copy()) if results.get('img_prefix') is not None: filename = osp.join(results['img_prefix'], results['img_info']['filename']) else: filename = results['img_info']['filename'] img_bytes = file_client_img.get(filename) img = mmcv.imfrombytes(img_bytes, flag=self.color_type, backend=self.img_imdecode_backend) if self.to_float32: img = img.astype(np.float32) results['filename'] = filename results['ori_filename'] = results['img_info']['filename'] results['img'] = img results['img_shape'] = img.shape results['ori_shape'] = img.shape # Set initial values for default meta_keys results['pad_shape'] = img.shape results['scale_factor'] = 1.0 num_channels = 1 if len(img.shape) < 3 else img.shape[2] results['img_norm_cfg'] = dict( mean=np.zeros(num_channels, dtype=np.float32), std=np.ones(num_channels, dtype=np.float32), to_rgb=False) # load annotation file_client_ann = mmcv.FileClient(**self.file_client_args.copy()) if results.get('seg_prefix', None) is not None: filename = osp.join(results['seg_prefix'], results['ann_info']['seg_map']) else: filename = results['ann_info']['seg_map'] img_bytes = file_client_ann.get(filename) gt_semantic_seg = mmcv.imfrombytes( img_bytes, flag='unchanged', backend=self.ann_imdecode_backend).squeeze().astype(np.uint8) # reduce zero_label if self.reduce_zero_label: # avoid using underflow conversion gt_semantic_seg[gt_semantic_seg == 0] = 255 gt_semantic_seg = gt_semantic_seg - 1 gt_semantic_seg[gt_semantic_seg == 254] = 255 results['gt_semantic_seg'] = gt_semantic_seg results['seg_fields'].append('gt_semantic_seg') return results
def __call__(self, results): """Call function. Args: results (dict): A dict containing the necessary information and data for augmentation. Returns: dict: A dict containing the processed data and information. """ filepath = str(results[f'{self.key}_path']) if self.file_client is None: self.file_client = FileClient(self.io_backend, **self.kwargs) if self.use_cache: if self.cache is None: self.cache = dict() if filepath in self.cache: img = self.cache[filepath] else: img_bytes = self.file_client.get(filepath) img = mmcv.imfrombytes(img_bytes, flag=self.flag, channel_order=self.channel_order, backend=self.backend) # HWC self.cache[filepath] = img else: img_bytes = self.file_client.get(filepath) img = mmcv.imfrombytes(img_bytes, flag=self.flag, channel_order=self.channel_order, backend=self.backend) # HWC if self.convert_to is not None: if self.channel_order == 'bgr' and self.convert_to.lower() == 'y': img = mmcv.bgr2ycbcr(img, y_only=True) elif self.channel_order == 'rgb': img = mmcv.rgb2ycbcr(img, y_only=True) else: raise ValueError('Currently support only "bgr2ycbcr" or ' '"bgr2ycbcr".') if img.ndim == 2: img = np.expand_dims(img, axis=2) results[self.key] = img results[f'{self.key}_path'] = filepath results[f'{self.key}_ori_shape'] = img.shape if self.save_original_img: results[f'ori_{self.key}'] = img.copy() return results
def __call__(self, results): """Perform the FrameSelector selecting given indices. Args: results (dict): The resulting dict to be modified and passed to the next transform in pipeline. """ mmcv.use_backend(self.decoding_backend) directory = results['frame_dir'] filename_tmpl = results['filename_tmpl'] modality = results['modality'] if self.file_client is None: self.file_client = FileClient(self.io_backend, **self.kwargs) imgs = list() if results['frame_inds'].ndim != 1: results['frame_inds'] = np.squeeze(results['frame_inds']) offset = results.get('offset', 0) for frame_idx in results['frame_inds']: frame_idx += offset if modality == 'RGB': filepath = osp.join(directory, filename_tmpl.format(frame_idx)) img_bytes = self.file_client.get(filepath) # Get frame with channel order RGB directly. cur_frame = mmcv.imfrombytes(img_bytes, channel_order='rgb') imgs.append(cur_frame) elif modality == 'Flow': x_filepath = osp.join(directory, filename_tmpl.format('x', frame_idx)) y_filepath = osp.join(directory, filename_tmpl.format('y', frame_idx)) x_img_bytes = self.file_client.get(x_filepath) x_frame = mmcv.imfrombytes(x_img_bytes, flag='grayscale') y_img_bytes = self.file_client.get(y_filepath) y_frame = mmcv.imfrombytes(y_img_bytes, flag='grayscale') imgs.extend([x_frame, y_frame]) else: raise NotImplementedError results['imgs'] = imgs results['original_shape'] = imgs[0].shape[:2] results['img_shape'] = imgs[0].shape[:2] return results
def __call__(self, results): """Call functions to load image and get image meta information. Args: results (dict): Result dict from :obj:`mmdet.CustomDataset`. Returns: dict: The dict contains loaded image and meta information. """ if self.file_client is None: self.file_client = mmcv.FileClient(**self.file_client_args) filenames = results['img_info']['filenames'] i = 0 results['img_fields'] = [] for filename in filenames: img_bytes = self.file_client.get(filename) img = mmcv.imfrombytes(img_bytes, flag=self.color_type) if self.to_float32: img = img.astype(np.float32) results['img{}'.format(i)] = img results['img_fields'].append('img{}'.format(i)) i += 1 results['filename'] = filename results['ori_filename'] = results['img_info']['filename'] results['img'] = img results['img_shape'] = img.shape results['ori_shape'] = img.shape return results
def __call__(self, results): img_key = results['img_info']['filename'] lmdb_path = results['img_prefix'] # lmdb env if self.env is None: self.env = lmdb.open( lmdb_path, max_readers=1, readonly=True, lock=False, readahead=False, meminit=False, ) # read image with self.env.begin(write=False) as txn: imgbuf = txn.get(img_key.encode('utf-8')) try: img = mmcv.imfrombytes(imgbuf, flag=self.color_type) except IOError: print('Corrupted image for {}'.format(img_key)) return None results['filename'] = img_key results['ori_filename'] = img_key results['img'] = img results['img_shape'] = img.shape results['ori_shape'] = img.shape results['img_fields'] = ['img'] return results
def __call__(self, results): if self.file_client is None: self.file_client = mmcv.FileClient(**self.file_client_args) if results['img_prefix'] is not None: filename = [ osp.join(results['img_prefix'], filename) for filename in results['img_info']['filename'] ] else: filename = results['img_info']['filename'] img = [] for name in filename: img_bytes = self.file_client.get(name) img.append(mmcv.imfrombytes(img_bytes, flag=self.color_type)) img = np.stack(img, axis=-1) if self.to_float32: img = img.astype(np.float32) results['filename'] = filename results['ori_filename'] = results['img_info']['filename'] results['img'] = img results['img_shape'] = img.shape results['ori_shape'] = img.shape results['img_fields'] = ['img'] return results
def _load_side_face_map(self, results): """loading side face map Args: results (dict): Result dict from :obj:`dataset`. Returns: dict: The dict contains loaded side face map annotations. """ if self.file_client is None: self.file_client = mmcv.FileClient(**self.file_client_args) filename = osp.join(results['side_face_prefix'], results['ann_info']['side_face_map']) img_bytes = self.file_client.get(filename) side_face_maps = mmcv.imfrombytes(img_bytes, flag='unchanged').squeeze() h, w = results['img_info']['height'], results['img_info']['width'] mask_num = len(results['ann_info']['masks']) gt_side_face_maps = BitmapMasks( [side_face_maps for _ in range(mask_num)], h, w) results['gt_side_face_maps'] = gt_side_face_maps results['side_face_fields'].append('gt_side_face_maps') return results
def test_http_backend(self): http_backend = FileClient('http') img_url = 'https://raw.githubusercontent.com/open-mmlab/mmcv/' \ 'master/tests/data/color.jpg' text_url = 'https://raw.githubusercontent.com/open-mmlab/mmcv/' \ 'master/tests/data/filelist.txt' # input is path or Path object with pytest.raises(Exception): http_backend.get(self.img_path) with pytest.raises(Exception): http_backend.get(str(self.img_path)) with pytest.raises(Exception): http_backend.get_text(self.text_path) with pytest.raises(Exception): http_backend.get_text(str(self.text_path)) # input url is http image img_bytes = http_backend.get(img_url) img = mmcv.imfrombytes(img_bytes) assert img.shape == self.img_shape # input url is http text value_buf = http_backend.get_text(text_url) assert self.text_path.open('r').read() == value_buf
def test_http_backend(self, backend, prefix): http_backend = FileClient(backend=backend, prefix=prefix) img_url = 'https://raw.githubusercontent.com/open-mmlab/mmcv/' \ 'master/tests/data/color.jpg' text_url = 'https://raw.githubusercontent.com/open-mmlab/mmcv/' \ 'master/tests/data/filelist.txt' # test `allow_symlink` attribute assert not http_backend.allow_symlink # input is path or Path object with pytest.raises(Exception): http_backend.get(self.img_path) with pytest.raises(Exception): http_backend.get(str(self.img_path)) with pytest.raises(Exception): http_backend.get_text(self.text_path) with pytest.raises(Exception): http_backend.get_text(str(self.text_path)) # input url is http image img_bytes = http_backend.get(img_url) img = mmcv.imfrombytes(img_bytes) assert img.shape == self.img_shape # input url is http text value_buf = http_backend.get_text(text_url) assert self.text_path.open('r').read() == value_buf # test `_get_local_path` # exist the with block and path will be released with http_backend.get_local_path(img_url) as path: assert mmcv.imread(path).shape == self.img_shape assert not osp.isfile(path)
def __call__(self, results): """Call functions to load image and get image meta information. Args: results (dict): Result dict from :obj:`mmdet.CustomDataset`. Returns: dict: The dict contains loaded image and meta information. """ if self.file_client is None: self.file_client = mmcv.FileClient(**self.file_client_args) if results['img_prefix'] is not None: filename = osp.join(results['img_prefix'], results['img_info']['filename']) else: filename = results['img_info']['filename'] img_bytes = self.file_client.get(filename) img = mmcv.imfrombytes(img_bytes, flag=self.color_type, channel_order=self.channel_order) if self.to_float32: img = img.astype(np.float32) results['filename'] = filename results['ori_filename'] = results['img_info']['filename'] results['img'] = img results['img_shape'] = img.shape results['ori_shape'] = img.shape results['img_fields'] = ['img'] return results
def __call__(self, results): if self.file_client is None: self.file_client = mmcv.FileClient(**self.file_client_args) if results['img_prefix'] is not None: filename = osp.join(results['img_prefix'], results['img_info']['filename']) else: filename = results['img_info']['filename'] # print(filename) img_bytes = self.file_client.get(filename) img = mmcv.imfrombytes(img_bytes, flag=self.color_type) if self.to_float32: img = img.astype(np.float32) results['filename'] = filename results['ori_filename'] = results['img_info']['filename'] results['img'] = img results['img_shape'] = img.shape results['ori_shape'] = img.shape # Set initial values for default meta_keys results['pad_shape'] = img.shape results['scale_factor'] = 1.0 num_channels = 1 if len(img.shape) < 3 else img.shape[2] results['img_norm_cfg'] = dict( mean=np.zeros(num_channels, dtype=np.float32), std=np.ones(num_channels, dtype=np.float32), to_rgb=False) results['img_fields'] = ['img'] return results
def __call__(self, results): """Call function. Args: results (dict): A dict containing the necessary information and data for augmentation. Returns: dict: A dict containing the processed data and information. """ if self.file_client is None: self.file_client = FileClient(self.io_backend, **self.kwargs) filepath = str(results[f'{self.key}_path']) img_bytes = self.file_client.get(filepath) img = mmcv.imfrombytes(img_bytes, flag=self.flag, channel_order=self.channel_order) # HWC results[self.key] = img results[f'{self.key}_path'] = filepath results[f'{self.key}_ori_shape'] = img.shape if self.save_original_img: results[f'ori_{self.key}'] = img.copy() return results
def __call__(self, results): """Call function to load multiple types annotations. Args: results (dict): Result dict from :obj:`mmseg.CustomDataset`. Returns: dict: The dict contains loaded semantic segmentation annotations. """ if self.file_client is None: self.file_client = mmcv.FileClient(**self.file_client_args) if results.get('seg_prefix', None) is not None: filename = osp.join(results['seg_prefix'], results['ann_info']['seg_map']) else: filename = results['ann_info']['seg_map'] img_bytes = self.file_client.get(filename) gt_semantic_seg = mmcv.imfrombytes( img_bytes, flag='unchanged', backend=self.imdecode_backend).squeeze().astype(np.uint8) # modify if custom classes if results.get('label_map', None) is not None: for old_id, new_id in results['label_map'].items(): gt_semantic_seg[gt_semantic_seg == old_id] = new_id # reduce zero_label if self.reduce_zero_label: # avoid using underflow conversion gt_semantic_seg[gt_semantic_seg == 0] = 255 gt_semantic_seg = gt_semantic_seg - 1 gt_semantic_seg[gt_semantic_seg == 254] = 255 results['gt_semantic_seg'] = gt_semantic_seg results['seg_fields'].append('gt_semantic_seg') return results
def test_imfrombytes(self): # backend cv2 mmcv.use_backend('cv2') with open(self.img_path, 'rb') as f: img_bytes = f.read() img_cv2 = mmcv.imfrombytes(img_bytes) assert img_cv2.shape == (300, 400, 3) # backend turbojpeg mmcv.use_backend('turbojpeg') with open(self.img_path, 'rb') as f: img_bytes = f.read() img_turbojpeg = mmcv.imfrombytes(img_bytes) assert img_turbojpeg.shape == (300, 400, 3) assert_array_equal(img_cv2, img_turbojpeg) mmcv.use_backend('cv2')
def test_ceph_backend(self): ceph_backend = FileClient('ceph') # input path is Path object with pytest.raises(NotImplementedError): ceph_backend.get_text(self.text_path) # input path is str with pytest.raises(NotImplementedError): ceph_backend.get_text(str(self.text_path)) # input path is Path object img_bytes = ceph_backend.get(self.img_path) img = mmcv.imfrombytes(img_bytes) assert img.shape == self.img_shape # input path is str img_bytes = ceph_backend.get(str(self.img_path)) img = mmcv.imfrombytes(img_bytes) assert img.shape == self.img_shape
def _load_image(self, filepath, flag='color'): value_buf = self.file_client.get(filepath) try: cur_frame = mmcv.imfrombytes(value_buf, flag) except Exception: print('imfrombytes error, reload backup') cur_frame = self.backup # cur_frame = mmcv.imread(filepath) return cur_frame
def _read_image(self, path): img_bytes = self.file_client.get(path) img = mmcv.imfrombytes( img_bytes, flag=self.color_type, channel_order=self.channel_order) if img is None: raise ValueError(f'Fail to read {path}') if self.to_float32: img = img.astype(np.float32) return img
def __call__(self, results): """Call functions to load image and get image meta information. Args: results (dict): Result dict from :obj:`mmseg.CustomDataset`. Returns: dict: The dict contains loaded image and meta information. """ if self.file_client is None: self.file_client = mmcv.FileClient(**self.file_client_args) #TODO to allow multi #curretn plna > for loop and concat img_prefix_list = results.get('img_prefix_list') for i, img_prefix in enumerate(img_prefix_list): if img_prefix is not None: filename = osp.join(img_prefix, results['img_info']['filename']) else: filename = results['img_info']['filename'] #print(filename) img_bytes = self.file_client.get(filename) img = mmcv.imfrombytes(img_bytes, flag=self.color_type, backend=self.imdecode_backend) if self.to_float32: img = img.astype(np.float32) #print(img) #concat img if len(img.shape) == 2: img = img[..., np.newaxis] if i == 0: img_concat = img else: img_concat = np.concatenate([img_concat, img], axis=2) img = img_concat #print(img.shape) results['filename'] = filename results['ori_filename'] = results['img_info']['filename'] results['img'] = img results['img_shape'] = img.shape results['ori_shape'] = img.shape # Set initial values for default meta_keys results['pad_shape'] = img.shape results['scale_factor'] = 1.0 num_channels = 1 if len(img.shape) < 3 else img.shape[2] results['img_norm_cfg'] = dict(mean=np.zeros(num_channels, dtype=np.float32), std=np.ones(num_channels, dtype=np.float32), to_rgb=False) return results
def __call__(self, results): """Call function. Args: results (dict): A dict containing the necessary information and data for augmentation. Returns: dict: A dict containing the processed data and information. """ if self.file_client is None: self.file_client = FileClient(self.io_backend, **self.kwargs) filepaths = results[f'{self.key}_path'] if not isinstance(filepaths, list): raise TypeError( f'filepath should be list, but got {type(filepaths)}') filepaths = [str(v) for v in filepaths] imgs = [] shapes = [] if self.save_original_img: ori_imgs = [] for filepath in filepaths: img_bytes = self.file_client.get(filepath) img = mmcv.imfrombytes(img_bytes, flag=self.flag, channel_order=self.channel_order) # HWC # convert to y-channel, if specified if self.convert_to is not None: if self.channel_order == 'bgr' and self.convert_to.lower( ) == 'y': img = mmcv.bgr2ycbcr(img, y_only=True) elif self.channel_order == 'rgb': img = mmcv.rgb2ycbcr(img, y_only=True) else: raise ValueError('Currently support only "bgr2ycbcr" or ' '"bgr2ycbcr".') if img.ndim == 2: img = np.expand_dims(img, axis=2) imgs.append(img) shapes.append(img.shape) if self.save_original_img: ori_imgs.append(img.copy()) results[self.key] = imgs results[f'{self.key}_path'] = filepaths results[f'{self.key}_ori_shape'] = shapes if self.save_original_img: results[f'ori_{self.key}'] = ori_imgs return results
def test_memcached_backend(self): mc_cfg = dict(server_list_cfg='', client_cfg='', sys_path=None) mc_backend = FileClient('memcached', **mc_cfg) # input path is Path object with pytest.raises(NotImplementedError): mc_backend.get_text(self.text_path) # input path is str with pytest.raises(NotImplementedError): mc_backend.get_text(str(self.text_path)) # input path is Path object img_bytes = mc_backend.get(self.img_path) img = mmcv.imfrombytes(img_bytes) assert img.shape == self.img_shape # input path is str img_bytes = mc_backend.get(str(self.img_path)) img = mmcv.imfrombytes(img_bytes) assert img.shape == self.img_shape
def __call__(self, results): """Call function. Args: results (dict): A dict containing the necessary information and data for augmentation. Returns: dict: A dict containing the processed data and information. """ if self.file_client is None: self.file_client = FileClient(self.io_backend, **self.kwargs) fg = results['fg'] alpha = results['alpha_f'] # alpha = results['alpha'].astype(np.float32) / 255. h, w = results['fg'].shape[:2] # randomly select fg if np.random.rand() < 0.5: idx = np.random.randint(len(self.fg_list)) fg2_bytes = self.file_client.get(self.fg_list[idx]) fg2 = mmcv.imfrombytes(fg2_bytes) alpha2_bytes = self.file_client.get(self.alpha_list[idx]) alpha2 = mmcv.imfrombytes(alpha2_bytes, flag='grayscale') alpha2 = alpha2.astype(np.float32) / 255. fg2 = mmcv.imresize(fg2, (w, h), interpolation=self.interpolation) alpha2 = mmcv.imresize(alpha2, (w, h), interpolation=self.interpolation) # the overlap of two 50% transparency will be 75% alpha_tmp = 1 - (1 - alpha) * (1 - alpha2) # if the result alpha is all-one, then we avoid composition if np.any(alpha_tmp < 1): # composite fg with fg2 fg = fg.astype(np.float32) * alpha[..., None] \ + fg2.astype(np.float32) * (1 - alpha[..., None]) alpha = alpha_tmp fg.astype(np.uint8) results['fg'] = fg results['alpha'] = (alpha * 255).astype(np.uint8) return results
def test_disk_backend(self): disk_backend = FileClient('disk') # input path is Path object img_bytes = disk_backend.get(self.img_path) img = mmcv.imfrombytes(img_bytes) assert self.img_path.open('rb').read() == img_bytes assert img.shape == self.img_shape # input path is str img_bytes = disk_backend.get(str(self.img_path)) img = mmcv.imfrombytes(img_bytes) assert self.img_path.open('rb').read() == img_bytes assert img.shape == self.img_shape # input path is Path object value_buf = disk_backend.get_text(self.text_path) assert self.text_path.open('r').read() == value_buf # input path is str value_buf = disk_backend.get_text(str(self.text_path)) assert self.text_path.open('r').read() == value_buf