def apply_transform(self, x, transform_parameters): """Applies a transformation to an image according to given parameters. # Arguments x: 3D tensor, single image. transform_parameters: Dictionary with string - parameter pairs describing the transformation. Currently, the following parameters from the dictionary are used: - `'theta'`: Float. Rotation angle in degrees. - `'tx'`: Float. Shift in the x direction. - `'ty'`: Float. Shift in the y direction. - `'shear'`: Float. Shear angle in degrees. - `'zx'`: Float. Zoom in the x direction. - `'zy'`: Float. Zoom in the y direction. - `'flip_horizontal'`: Boolean. Horizontal flip. - `'flip_vertical'`: Boolean. Vertical flip. - `'channel_shift_intencity'`: Float. Channel shift intensity. - `'brightness'`: Float. Brightness shift intensity. # Returns A transformed version of the input (same shape). """ # x is a single image, so it doesn't have image number at index 0 img_row_axis = self.row_axis - 1 img_col_axis = self.col_axis - 1 img_channel_axis = self.channel_axis - 1 x = apply_affine_transform(x, transform_parameters.get('theta', 0), transform_parameters.get('tx', 0), transform_parameters.get('ty', 0), transform_parameters.get('shear', 0), transform_parameters.get('zx', 1), transform_parameters.get('zy', 1), row_axis=img_row_axis, col_axis=img_col_axis, channel_axis=img_channel_axis, fill_mode=self.fill_mode, cval=self.cval, order=self.interpolation_order) if transform_parameters.get('channel_shift_intensity') is not None: x = apply_channel_shift( x, transform_parameters['channel_shift_intensity'], img_channel_axis) if transform_parameters.get('flip_horizontal', False): x = flip_axis(x, img_col_axis) if transform_parameters.get('flip_vertical', False): x = flip_axis(x, img_row_axis) if transform_parameters.get('brightness') is not None: x = apply_brightness_shift(x, transform_parameters['brightness']) return x
def test_apply_brightness_shift_error(monkeypatch): monkeypatch.setattr(affine_transformations, 'ImageEnhance', None) with pytest.raises(ImportError): affine_transformations.apply_brightness_shift(0, [0])
def create_augment_videos() -> None: images_paths = sorted( imutils.paths.list_images(f'{HOME}/.keras/large-mpi-db') ) for code in ['islf', 'kabf', 'lekf', 'milf', 'silf', 'cawm', 'chsm', 'jakm', 'juhm', 'mamm']: for label in LABELS: filtered_images_paths = list( filter( lambda image_path: image_path.split(os.path.sep)[-1].startswith(code) and image_path.split(os.path.sep)[-2] == label, images_paths ) ) for t_count, transformation in enumerate(get_random_transformations()): path = f'{HOME}/.keras/datasets/cec-videos-augmented/{label}' video_path = f'{path}/{code}_{label}_{t_count:02d}.avi' cap = cv2.VideoCapture(video_path) if cap.get(cv2.CAP_PROP_FRAME_COUNT) == len(filtered_images_paths): print(f'Skipping: {video_path}') continue os.makedirs(path, exist_ok=True) out = cv2.VideoWriter( video_path, cv2.VideoWriter_fourcc(*'XVID'), 25, (224, 224) ) print(f'Creating: {video_path}') for i_count, image_path in enumerate(filtered_images_paths): x = cv2.imread(image_path) x = cv2.resize(x, (224, 168)) x = cv2.copyMakeBorder(x, 28, 28, 0, 0, cv2.BORDER_CONSTANT) if i_count == 0: cv2.imwrite(f'{path}/transformation_oo.png', x) x = apply_affine_transform( x, transformation.get('theta', 0), transformation.get('tx', 0), transformation.get('ty', 0), transformation.get('shear', 0), transformation.get('zx', 1), transformation.get('zy', 1) ) if transformation.get('channel_shift_intensity') is not None: x = apply_channel_shift( x, transformation['channel_shift_intensity'] ) if transformation.get('flip_horizontal', False): x = flip_axis(x, 1) if transformation.get('flip_vertical', False): x = flip_axis(x, 0) if transformation.get('brightness') is not None: x = apply_brightness_shift(x, transformation['brightness']) x = np.uint8(x) if transformation.get('grayscale', False): x = cv2.cvtColor(x, cv2.COLOR_RGB2GRAY) x = np.repeat(x[:, :, np.newaxis], 3, axis=2) if i_count == 0: cv2.imwrite(f'{path}/transformation_{t_count:02d}.png', x) out.write(x) out.release() return