def augument(data_path, label, image_name, save_path, size=224, training = True): image_path = os.path.join(data_path, image_name) (name, extension) = splitfilename(image_name) extension = extension.lower() if extension not in IMG_EXTS: print('filered image: %s' % image_name) return try: img = image.imdecode(open(image_path, 'rb').read()).astype('float32') except Exception as ex: print("error: ", ex) return if label is not None: label_path = os.path.join(save_path, label) else: label_path = save_path mkdir(label_path) if training: aug1 = image.HorizontalFlipAug(0.5) aug2 = image.HorizontalFlipAug(.5) img = image.resize_short(img, size=384, interp=2) center_crop, _ = image.center_crop(img, size=(size, size)) new_name = "%s_%s%s" % (name, "0", extension) cv.imwrite(os.path.join(label_path, new_name), center_crop.asnumpy()) random_crop, _ = image.random_crop(img, size=(size, size)) new_name = "%s_%s%s" % (name, "1", extension) cv.imwrite(os.path.join(label_path, new_name), random_crop.asnumpy()) random_crop, _ = image.random_crop(img, size=(size, size)) new_name = "%s_%s%s" % (name, "2", extension) cv.imwrite(os.path.join(label_path, new_name), random_crop.asnumpy()) random_crop, _ = image.random_crop(img, size=(size, size)) new_name = "%s_%s%s" % (name, "3", extension) cv.imwrite(os.path.join(label_path, new_name), random_crop.asnumpy()) img_aug1 = aug1(random_crop).clip(0,255) new_name = "%s_%s%s" % (name, "4", extension) cv.imwrite(os.path.join(label_path, new_name), img_aug1.asnumpy()) img_aug2 = aug2(center_crop).clip(0, 255) new_name = "%s_%s%s" % (name, "5", extension) cv.imwrite(os.path.join(label_path, new_name), img_aug2.asnumpy()) img_resize = image.imresize(img, w=size, h=size, interp=2) new_name = "%s_%s%s" % (name, "6", extension) cv.imwrite(os.path.join(label_path, new_name), img_resize.asnumpy()) else: img = image.resize_short(img, size=size) img, _ = image.center_crop(img, size=(size, size)) new_name = "%s%s" % (name, extension) cv.imwrite(os.path.join(label_path, new_name), img.asnumpy())
def pre_processing(self, img_path): """ 对图片进行处理,先按照高度进行resize,resize之后如果宽度不足指定宽度,就补黑色像素,否则就强行缩放到指定宽度 :param img_path: 图片地址 :return: """ data_augment = False if self.phase == 'train' and np.random.rand() > 0.5: data_augment = True if data_augment: img_h = 40 img_w = 340 else: img_h = self.img_h img_w = self.img_w img = image.imdecode( open(img_path, 'rb').read(), 1 if self.img_channel == 3 else 0) h, w = img.shape[:2] ratio_h = float(img_h) / h new_w = int(w * ratio_h) if new_w < img_w: img = image.imresize(img, w=new_w, h=img_h) step = nd.zeros((img_h, img_w - new_w, self.img_channel), dtype=img.dtype) img = nd.concat(img, step, dim=1) else: img = image.imresize(img, w=img_w, h=img_h) if data_augment: img, _ = image.random_crop(img, (self.img_w, self.img_h)) return img
def __call__(self, img: np.ndarray): """ 对图片进行处理,先按照高度进行resize,resize之后如果宽度不足指定宽度,就补黑色像素,否则就强行缩放到指定宽度 :param img_path: 图片地址 :return: """ data_augment = False if self.phase == 'train' and np.random.rand() > 0.5: data_augment = True if data_augment: img_h = 40 img_w = 340 else: img_h = self.img_h img_w = self.img_w h, w = img.shape[:2] ratio_h = float(img_h) / h new_w = int(w * ratio_h) if new_w < img_w and self.pad: img = cv2.resize(img, (new_w, img_h)) if len(img.shape) == 2: img = np.expand_dims(img, 3) step = np.zeros((img_h, img_w - new_w, img.shape[-1]), dtype=img.dtype) img = np.column_stack((img, step)) else: img = cv2.resize(img, (img_w, img_h)) if data_augment: img = nd.array(img) img, _ = image.random_crop(img, (self.img_w, self.img_h)) img = img.asnumpy() return img
def forward(self, x): if self.pad: x_pad = np.pad(x.asnumpy(), self.pad, mode='constant', constant_values=0) return image.random_crop(nd.array(x_pad), *self._args)[0]
def forward(self, x): if not isinstance(self.size, (numbers.Number, tuple)): raise TypeError('Got inappropriate size arg') if isinstance(self.size, numbers.Number): size = (self.size, self.size) else: size = self.size return image.random_crop(x, size)[0]
def load_and_crop_image(root, files, crop_size): imgs = [] for file in files: fp = os.path.join(root, file) img = image.imread(fp) h, w = img.shape[:2] ratio = h / w if crop_size[0] / crop_size[1] < ratio: w = crop_size[1] h = w * ratio else: h = crop_size[0] w = h / ratio image.imresize(img, int(w), int(h)) img, rect = image.random_crop(img, crop_size) imgs.append(img) return imgs
def forward(self, x): return image.random_crop(x, *self._args)[0]
def rand_crop(data, label, shape): data, rect = image.random_crop(data, shape) label = image.fixed_crop(label, *rect) return data, label
def __voc_rand_crop(self, feature, label, height, width): feature, rect = image.random_crop(feature, (width, height)) label = image.fixed_crop(label, *rect) return feature, label
def voc_rand_crop(feature, label, height, width): """Random cropping for images of the Pascal VOC2012 Dataset.""" feature, rect = image.random_crop(feature, (width, height)) label = image.fixed_crop(label, *rect) return feature, label
def rand_crop(data, label, height, width): data, rect = random_crop(data, (width, height)) label = fixed_crop(label, *rect) return data, label
def voc_rand_crop(img, label, height, width): img, rect = image.random_crop(img, (width, height)) label = image.fixed_crop(label, *rect) return img, label
def RandomCrop(data, label, height, width): data, rect = image.random_crop(data,(height, width)) label = image.fixed_crop(label,*rect) return data, label
def voc_rand_crop(data, label, height, width): """Random cropping for images of the Pascal VOC2012 Dataset.""" data, rect = image.random_crop(data, (width, height)) label = image.fixed_crop(label, *rect) return data, label
def rand_crop(self, data, label, height, width): data, rect = image.random_crop(data, (width, height)) label = image.fixed_crop(label, *rect) return data, label
def voc_rand_crop(feature, label, height, width): """Randomly crop for both feature and label images.""" feature, rect = image.random_crop(feature, (width, height)) label = image.fixed_crop(label, *rect) return feature, label