def load_image(path: Path) -> np.array: # try: img = jpeg4py.JPEG(str(path)).decode() # except: # img = cv2.imread(str(path)) # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # print(path) # img = cv2.imread(str(path)) # if img.shape[0] == 512 and img.shape[1] == 512: # return img # height, width = 1024, 1024 # # h, w, c = img.shape # dy = (h - height) // 2 # dx = (w - width) // 2 # # y1 = dy # y2 = y1 + height # x1 = dx # x2 = x1 + width # # img = img[y1:y2, x1:x2, :] # if img.shape != (1024, 1024, 3): # print(path) # img = np.array(Image.open(str(path))) return img
def cmp_img_callback(self,msg): if self.input_image is None: img_cmp = np.fromstring(msg.data, np.uint8) img_bgr = jpeg.JPEG(img_cmp).decode() else: img_bgr = cv2.cvtColor(self.input_image,cv2.COLOR_GRAY2BGR) self.img_queue.put(img_bgr)
def random_manipulation(img, manipulation=None): if manipulation == None: manipulation = random.choice(MANIPULATIONS) if manipulation.startswith('jpg'): quality = int(manipulation[3:]) out = BytesIO() im = Image.fromarray(img) im.save(out, format='jpeg', quality=quality) im_decoded = jpeg.JPEG(np.frombuffer(out.getvalue(), dtype=np.uint8)).decode() del out del im elif manipulation.startswith('gamma'): gamma = float(manipulation[5:]) # alternatively use skimage.exposure.adjust_gamma # img = skimage.exposure.adjust_gamma(img, gamma) im_decoded = np.uint8(cv2.pow(img / 255., gamma) * 255.) elif manipulation.startswith('bicubic'): scale = float(manipulation[7:]) im_decoded = cv2.resize(img, (0, 0), fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC) else: assert False return im_decoded
def __getitem__(self, idx): img_name = self.name_list[idx, 1].strip() img_path = os.path.join(self.img_root_dir, 'images', img_name + '.jpg') image = jpeg.JPEG(img_path).decode() # [1, H, W] part_path = [os.path.join(self.part_root_dir, '%s' % x, 'images', img_name + '.jpg') for x in self.names] plabels_path = {x: [os.path.join(self.part_root_dir, '%s' % x, 'labels', img_name, img_name + "_lbl%.2d.png" % i) for i in self.label_id[x]] for x in self.names} parts_image = [io.imread(part_path[i]) for i in range(4)] plabels = {x: np.array([io.imread(plabels_path[x][i]) for i in range(len(self.label_id[x])) ]) for x in self.names } for x in self.names: bg = 255 - np.sum(plabels[x], axis=0, keepdims=True) # [1, 64, 64] plabels[x] = np.uint8(np.concatenate([bg, plabels[x]], axis=0)) # [L + 1, 64, 64] sample = {'image': image, 'parts': parts_image, 'parts_labels': plabels} if self.transform: sample = self.transform(sample) return sample
def get_image(image_name): # if datapath is not None: # image_name = ( # image_name if image_name.startswith(datapath) else # os.path.join(datapath, image_name) # ) img = None if has_jpeg4py and image_name.endswith(("jpg", "JPG", "jpeg", "JPEG")): try: img = jpeg4py.JPEG(image_name).decode() except Exception: print('jpeg4py error!') pass if img is None: img = cv2.imread(image_name) if img is None: print(red(image_name)) return None if len(img.shape) == 3: # BGR -> RGB img = img[:, :, ::-1] if len(img.shape) < 3: # grayscale img = np.expand_dims(img, -1) if img.shape[-1] != 3 and not grayscale: img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) return img
def random_manipulation(img, width, height, manipulation=None): if manipulation == None: manipulation = choice(MANIPULATIONS) #kaggel manipulations if manipulation.startswith('jpg'): quality = int(manipulation[3:]) out = BytesIO() im = Image.fromarray(img) im.save(out, format='jpeg', quality=quality) img = jpeg.JPEG(np.frombuffer(out.getvalue(), dtype=np.uint8)).decode() del out del im elif manipulation.startswith('gamma'): gamma = float(manipulation[5:]) img = exposure.adjust_gamma(img, gamma) elif manipulation.startswith('bicubic'): scale = float(manipulation[7:]) img = misc.imresize(img, scale, interp='bicubic') else: assert False #transform image img = transform_image(img) pil_im = Image.fromarray(img) img = np.array(pil_im.crop((0, 0, width, height))) return preprocess_input(img / 1.0)
def __getitem__(self, idx): # fix seed _fix_seeds(SEED) # upload image (numpy array) image_id = self.img_list[idx] image = jpeg.JPEG(self.files_path + image_id).decode() # upload mask (rle string) masks_dict = self.train_dict[image_id] labels = [] for cls in masks_dict: cur_rle = masks_dict[cls] if cur_rle != '': # get numpy array from string labels.append(1) else: labels.append(0) labels = np.array(labels) if self.transform is not None: aug_item = self.transform(image=image) return torch.from_numpy(aug_item['image']).permute(2, 0, 1), \ torch.from_numpy(labels).float()
def random_manipulation(img, manipulation=None, return_manip=False): if manipulation == None: manip_idx = np.random.randint(0, 8) manipulation = MANIPULATIONS[manip_idx] if manipulation.startswith('jpg'): quality = int(manipulation[3:]) out = BytesIO() # img = np.array(img) # FIXME assert len(img.shape) == 3, 'actual shape {}'.format(img.shape) im = Image.fromarray(img) im.save(out, format='jpeg', quality=quality) im_decoded = jpeg.JPEG(np.frombuffer(out.getvalue(), dtype=np.uint8)).decode() del out del im elif manipulation.startswith('gamma'): gamma = float(manipulation[5:]) # alternatively use skimage.exposure.adjust_gamma # img = skimage.exposure.adjust_gamma(img, gamma) im_decoded = np.uint8(cv2.pow(img / 255., gamma) * 255.) elif manipulation.startswith('bicubic'): scale = float(manipulation[7:]) im_decoded = cv2.resize(img, (0, 0), fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC) else: assert False if not return_manip: return im_decoded else: return im_decoded, manip_idx
def read_image(file_name, bgr=True): """ Reads jpeg by jpeg4py library. If jpeg4py is not installed or can't read the file, falls back to opencv imread() :param file_name: Image file :param bgr: Return BGR image instead of RGB :return: 3-channel image in RGB or BGR order """ def read_opencv(): image = cv2.imread(file_name, cv2.IMREAD_COLOR) if not bgr: image = image[..., ::-1] return image if jpeg: try: im = jpeg.JPEG(file_name).decode() if len(im.shape) == 2: im = np.stack((im, ) * 3, axis=-1) if bgr: im = im[..., ::-1] return im except jpeg.JPEGRuntimeError: # Fallback to read_opencv pass return read_opencv()
def __getitem__(self, idx): annotation = self.annotations['annotations'][idx] image_id = str(annotation['image_id']) try: image = jpeg4py.JPEG(self.path + '/images/COCO_' + self.mode + '2014_000000' + image_id.zfill(6) + '.jpg').decode() except: image = cv2.imread(self.path + '/images/COCO_' + self.mode + '2014_000000' + image_id.zfill(6) + '.jpg') image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = self.transform(image=image)['image'] question = torch.FloatTensor(self.questions[idx]) if self.mode != 'test': try: answer = self.ans_dict[annotation['multiple_choice_answer']] except: answer = -1 return (image, question, answer) else: return (image, question)
def __getitem__(self,idx): fname = self.fnames[idx] if self._test: print(fname) # img = cv2.imread(fname) img = jpeg.JPEG(fname).decode() img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) h, w, c = img.shape h = float(h/self.origin_img_size[1]) w = float(w/self.origin_img_size[0]) assert img is not None, print(fname) label = self.get_label_from_path(fname) if self.train: # add data augument seq_det = self.augmentation.to_deterministic() img = seq_det.augment_images([img])[0] img = self.transform(img) if self.with_file_path: return img, label, fname return img, label
def __call__(self, row): image_name = str(row[self.row_key]) if self.datapath is not None: image_name = ( image_name if image_name.startswith(self.datapath) else os.path.join(self.datapath, image_name)) img = None try: if image_name.endswith(("jpg", "JPG", "jpeg", "JPEG")): img = jpeg.JPEG(image_name).decode() except Exception: pass if img is None: img = cv2.imread(image_name) if len(img.shape) == 3: # BGR -> RGB img = img[:, :, ::-1] if len(img.shape) < 3: # grayscale img = np.expand_dims(img, -1) if img.shape[-1] != 3 and not self.grayscale: img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) result = {self.dict_key: img} return result
def imread(uri, grayscale=False, expand_dims=True, rootpath=None): """ Args: uri: {str, pathlib.Path, bytes, file} The resource to load the image from, e.g. a filename, pathlib.Path, http address or file object, see the docs for more info. grayscale: expand_dims: rootpath: Returns: """ if rootpath is not None: uri = (uri if uri.startswith(rootpath) else os.path.join( rootpath, uri)) if JPEG4PY_ENABLED and uri.endswith(("jpg", "JPG", "jpeg", "JPEG")): img = jpeg.JPEG(uri).decode() if grayscale: img = rgb2gray(img) else: img = imageio.imread(uri, as_gray=grayscale, pilmode="RGB") if expand_dims and len(img.shape) < 3: # grayscale img = np.expand_dims(img, -1) return img
def load_image(path, resize_dim): image = jpeg.JPEG(path).decode() image = Image.fromarray(image).convert('RGB') #image = torch.from_numpy(x) image = TF.resize(image, resize_dim) image = TF.to_tensor(image) return image
def __getitem__(self, idx): # fix seed _fix_seeds(SEED) # upload image (numpy array) image_id = self.img_list[idx] image = jpeg.JPEG(self.files_path + image_id).decode() # upload mask (rle string) masks_dict = self.train_dict[image_id] mask = [] for cls in masks_dict: cur_rle = masks_dict[cls] if cur_rle != '': # get numpy array from string cur_mask = rle2mask(cur_rle) mask.append(cur_mask) else: cur_mask = np.zeros((256,1600), dtype=np.uint8) mask.append(cur_mask) mask = np.array(mask).swapaxes(0, 1).swapaxes(1,2) if self.transform is not None: aug_item = self.transform(image=image, mask=mask) if not self.valid: return torch.from_numpy(aug_item['image']).permute(2, 0, 1), \ torch.from_numpy(aug_item['mask']).permute(2, 0, 1).float() else: return image_id, \ torch.from_numpy(aug_item['image']).permute(2, 0, 1), \ torch.from_numpy(aug_item['mask']).permute(2, 0, 1).float()
def read_image(image_name, datapath=None, grayscale=False): if datapath is not None: image_name = (image_name if image_name.startswith(datapath) else os.path.join(datapath, image_name)) img = None try: if image_name.endswith(("jpg", "JPG", "jpeg", "JPEG")): img = jpeg.JPEG(image_name).decode() except Exception: pass if img is None: img = cv2.imread(image_name) if len(img.shape) == 3: # BGR -> RGB img = img[:, :, ::-1] if len(img.shape) < 3: # grayscale img = np.expand_dims(img, -1) if img.shape[-1] != 3 and not grayscale: img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) return img
def prepare_roidb(imdb): """ Enrich the imdb's roidb by adding some derived quantities that are useful for training. This function precomputes the maximum overlap, taken over ground-truth boxes, between each ROI and each ground-truth box. The class with maximum overlap is also recorded. """ sizes = [tuple(reversed(jpeg.JPEG(imdb.image_path_at(i)).decode().shape[:2])) \ if imdb.image_path_at(i).endswith('.jpg') \ else PIL.Image.open(imdb.image_path_at(i)).size \ for i in xrange(imdb.num_images)] roidb = imdb.roidb for i in xrange(len(imdb.image_index)): if i % 500 == 0: print 'Preparing roidb {:06d}/{:06d}'.format(i, imdb.num_images) roidb[i]['image'] = imdb.image_path_at(i) roidb[i]['width'] = sizes[i][0] roidb[i]['height'] = sizes[i][1] # need gt_overlaps as a dense array for argmax gt_overlaps = roidb[i]['gt_overlaps'].toarray() # max overlap with gt over classes (columns) max_overlaps = gt_overlaps.max(axis=1) # gt class that had the max overlap max_classes = gt_overlaps.argmax(axis=1) roidb[i]['max_classes'] = max_classes roidb[i]['max_overlaps'] = max_overlaps # sanity checks # max overlap of 0 => class should be zero (background) zero_inds = np.where(max_overlaps == 0)[0] assert all(max_classes[zero_inds] == 0) # max overlap > 0 => class should not be zero (must be a fg class) nonzero_inds = np.where(max_overlaps > 0)[0] assert all(max_classes[nonzero_inds] != 0)
def _read_and_aug(self, dp, augmentor): fpath, im_info, img_id = dp #read image try: img = jpeg.JPEG(fpath).decode() except Exception as ex: print(f'cant open {fpath} by jpg, fall back to opencv reading') try: img = cv2.imread(fpath, cv2.IMREAD_COLOR) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) except cv2.error as ex: print(ex, fpath) #read keypoints keypoints = self.gt_points_to_array(im_info[0]['keypoints']) sample = {'image': img, 'pose': keypoints, 'fpath' : fpath} mean = np.array([0.485, 0.456, 0.406]).astype(np.float32) std = np.array([0.229, 0.224, 0.225]).astype(np.float32) #augment image and keypoint if augmentor: sample = augmentor(sample) if self.debug: sample['original_img'] = img sample['original_pose'] = keypoints #scale image and poses sample['image'] = sample['image'].astype(np.float32) sample, left_pad, top_pad, new_w, new_h = self.rescale_sample(sample, (self.config['in_height'], self.config['in_width']), mean) #create heatmap sample['heatmap'] = self._get_heatmap(sample['pose'], sample['image'].shape) #scale to network input sample['heatmap'] = cv2.resize(sample['heatmap'], (self.config['out_height'], self.config['out_width'])) #sample['heatmap'] = np.clip(sample['heatmap'], 0, 1) #sample['image'] = cv2.resize(sample['image'], (self.config['in_height'], self.config['in_width']), interpolation=cv2.INTER_AREA) #print(sample['heatmap'].shape) if self.debug: return sample #print('return') #print(sample['image']) sample['image'] = (sample['image']-mean)/(std) #return sample['image'], sample['heatmap'], img_id, img sample['img_id'] = img_id # if not self.train: sample['original_img'] = img sample['left_pad'] = left_pad sample['top_pad'] = top_pad sample['new_w'] = new_w sample['new_h'] = new_h return sample
def __getitem__(self, idx): if self.flip: flip = np.random.randint(10) == 1 else: flip = False image_id = self.fnames[idx] image_path = self.data_dir + '/{}.jpg'.format(image_id) img = jpeg.JPEG(image_path).decode() img = cv2.resize(img, (self.img_size[1], self.img_size[0])) if flip: img = hflip(img, p=1) annotations = self.df[self.df.image_id == image_id] # create label mask_size = (self.img_size[0]//self.model_scale, self.img_size[1]//self.model_scale) mask = np.zeros(mask_size, dtype='float32') regr = np.zeros([mask_size[0], mask_size[1], self.num_features], dtype='float32') for _, ann in annotations.iterrows(): r = int(ann['r_heatmap']) c = int(ann['c_heatmap']) if flip: ann['pitch_sin'] *= -1.0 ann['roll'] *= -1.0 ann['x'] *= -1.0 features = np.array([ann[col] for col in self.features]).astype(float) features = features.reshape(1, 1, len(features)) if 0 <= r < mask_size[0] and 0 <= c < mask_size[1]: kernel_size = 3 lower_r = max(r-1, 0) upper_r = min(r+2, mask.shape[0]) left_c = max(c-1, 0) right_c = min(c+2, mask.shape[1]) kernel = np.float32( [[0.5, 0.75, 0.5], [0.75, 1.0, 0.75], [0.5, 0.75, 0.5]]) kernel = kernel[lower_r-(r-1):kernel_size-((r+2)-upper_r), left_c-(c-1):kernel_size-((c+2)-right_c)] mask[lower_r:upper_r, left_c:right_c] = kernel regr[lower_r:upper_r, left_c:right_c] = features mask = mask.reshape(mask.shape[0], mask.shape[1], 1) mask_regr = np.concatenate([mask, regr], axis=2) if flip: mask_regr = np.array(mask_regr[:, ::-1]) augmented = self.transforms(image=img, mask=mask_regr) img = augmented['image'] mask_regr = augmented['mask'] mask_regr = mask_regr[0].permute(2, 0, 1) if self.return_fnames: return img, mask_regr, image_id else: return img, mask_regr
def __getitem__(self, idx): fname = self.fnames[idx] image_path = os.path.join(self.root, fname) image_path=image_path+'.jpg' img = jpeg.JPEG(image_path).decode() images = self.transforms(image=img)["image"] return fname, images
def imdecode(data): try: img = jpeg.JPEG(data).decode()[..., ::-1] # RGB -> BGR # pylint: disable=broad-except except Exception as ex: tf.logging.warning("Can't decode with jpeg4py (libjpeg-turbo): {0}. Will use OpenCV.".format(ex)) img = cv2.imdecode(data, cv2.IMREAD_COLOR) return img
def test_parse_header(self): raw = self.raw.copy() jp = jpeg.JPEG(raw) jp.parse_header() self.assertIsNotNone(jp.width) self.assertIsNotNone(jp.height) self.assertIsNotNone(jp.subsampling) return jp
def jpeg4py_loader(path): """ Image reading using jpeg4py https://github.com/ajkxyz/jpeg4py""" try: return jpeg4py.JPEG(path).decode() except Exception as e: print('ERROR: Could not read image "{}"'.format(path)) print(e) return None
def __getitem__(self, idx): image_id = self.fnames[idx] image_id = image_id + '.jpg' image_path = os.path.join(self.data_dir, image_id) img = jpeg.JPEG(image_path).decode()[1355:] img = cv2.resize(img, (self.img_size[1], self.img_size[0])) images = self.transforms(image=img)["image"] return image_id, images
def load_image(file, image_size=None, size_is_min=True): if _JPEG4PY: image = jpeg4py.JPEG(file).decode() else: image = cv2.imread(file) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if image_size is not None: image = resize(image, image_size, size_is_min) return image
def imread_jpeg4py(path): try: img = jpeg4py.JPEG(str(path)).decode() # lycon can't load gif file if img is None: return None return img.astype(np.float32) except BaseException: return None
def __getitem__(self, index): path = self.data.path[index] img = jpeg.JPEG(path).decode() label = self.data.label[index] img = self.additional_transforms(image=img)['image'] img = norm_augs(img) return img, label
def decode_jpeg_fast(img_path): """ Jpeg decoding method implemented by jpeg4py, available in https://github.com/ajkxyz/jpeg4py . This library binds the libjpeg-turbo C++ library (available in https://github.com/libjpeg-turbo/libjpeg-turbo/blob/master/BUILDING.md), and can be up to 9 times faster than the non SIMD implementation. Requires libjpeg-turbo, built and installed correctly. """ return jpeg.JPEG(img_path).decode()
def read_mask(image_id, mode): image_id = image_id + '.jpg' filter_path = os.path.join( '../input/pku-autonomous-driving/{}_masks/'.format(mode), image_id) if os.path.exists(filter_path): return jpeg.JPEG(str(filter_path)).decode()[:, :, 0] # return cv2.imread(str(filter_path))[:,:,0] else: return np.zeros((2710, 3384), dtype='uint8')
def __getitem__(self, idx): if self.split == "test": img_index = self.imgfiles[idx] # using jpeg4py instead of Image open image = jpeg.JPEG(os.path.join(self.imgdir, img_index + ".jpg")).decode() if self.transform_dtype == 'PIL': img = Image.fromarray(image).convert('RGB') # img = Image.open(os.path.join(self.imgdir, img_index + ".jpg")).convert('RGB') if self.input_transform is not None: img = self.input_transform(img) return img else: img_index = self.imgfiles[idx] # jpef4py > opencv > PIL open # Using jpeg4py instead of Image open # image = jpeg.JPEG(os.path.join(self.imgdir, img_index + ".jpg")).decode() image = cv2.imread(os.path.join(self.imgdir, img_index + ".jpg")) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # img = Image.open(os.path.join(self.imgdir, img_index + ".jpg")).convert('RGB') lab = Image.open(os.path.join(self.labdir, img_index + ".png")).convert('P') if self.transform_dtype == 'PIL': img = Image.fromarray(image).convert('RGB') if self.input_transform is not None: img = self.input_transform(img) if self.target_transform is not None: lab = self.target_transform(lab) # masks = [(lab == v) for v in range(21)] # mask = np.stack(masks, axis=0).astype('float32') lab = np.array(lab).astype('int32') return img, lab else: lab = np.array(lab) if self.input_transform is not None: image = self.input_transform(image) if self.target_transform is not None: lab = self.target_transform(lab) # masks = [(lab == v) for v in range(21)] # mask = np.stack(masks, axis=0).astype('float32') lab = np.array(lab).astype('int32') return image, lab