def test_load_image_alpha_channel_image_converts(preprocess_mocker): PATH_ALPHA_IMAGE = p.parent / 'data/alpha_channel_image.png' alpha_converted = Image.open(PATH_ALPHA_IMAGE).convert('RGBA').convert('RGB') load_image(PATH_ALPHA_IMAGE) preprocess_mocker.assert_called_once_with( alpha_converted, target_size=None, grayscale=False )
def test__get_cnn_features_single(cnn): img = load_image(TEST_IMAGE, target_size=(224, 224)) result = cnn._get_cnn_features_single(img) assert isinstance(result, np.ndarray) assert result.shape == (1, 1024)
def _data_generator( self, image_files: List[PosixPath]) -> Tuple[np.array, np.array]: """Generate data from samples in specified batch.""" # initialize images and labels tensors for faster processing X = np.empty((len(image_files), *self.target_size, 3)) invalid_image_idx = [] for i, image_file in enumerate(image_files): # load and randomly augment image img = load_image(image_file=image_file, target_size=self.target_size, grayscale=False) if img is not None: X[i, :] = img else: invalid_image_idx.append(i) self.invalid_image_idx.append(self.counter) self.counter += 1 if invalid_image_idx: X = np.delete(X, invalid_image_idx, axis=0) # apply basenet specific preprocessing # input is 4D numpy array of RGB values within [0, 255] X = self.basenet_preprocess(X) return X
def test_load_image_all_inputs_correct(): target_size = (8, 8) loaded_image = load_image( image_file=PATH_SINGLE_IMAGE, target_size=target_size, grayscale=True ) assert isinstance(loaded_image, np.ndarray) assert loaded_image.shape == target_size assert np.issubdtype( np.uint8, loaded_image.dtype ) # return numpy array dtype is uint8
def encode_image( self, image_file: Optional[Union[PurePath, str]] = None, image_array: Optional[np.ndarray] = None, ) -> np.ndarray: """ Generate CNN encoding for a single image. Args: image_file: Path to the image file. image_array: Optional, used instead of image_file. Image typecast to numpy array. Returns: encoding: Encodings for the image in the form of numpy array. Example: ``` from imagededup.methods import CNN myencoder = CNN() encoding = myencoder.encode_image(image_file='path/to/image.jpg') OR encoding = myencoder.encode_image(image_array=<numpy array of image>) ``` """ if isinstance(image_file, str): image_file = Path(image_file) if isinstance(image_file, PurePath): if not image_file.is_file(): raise ValueError( 'Please provide either image file path or image array!' ) image_pp = load_image( image_file=image_file, target_size=self.target_size, grayscale=False ) elif isinstance(image_array, np.ndarray): image_array = expand_image_array_cnn( image_array ) # Add 3rd dimension if array is grayscale, do sanity checks image_pp = preprocess_image( image=image_array, target_size=self.target_size, grayscale=False ) else: raise ValueError('Please provide either image file path or image array!') return ( self._get_cnn_features_single(image_pp) if isinstance(image_pp, np.ndarray) else None )
def test_encode_image(cnn): result = cnn.encode_image(TEST_IMAGE) assert isinstance(result, np.ndarray) assert result.shape == (1, 1024) # 1024 = 3*3*1024*2 result = cnn.encode_image(str(TEST_IMAGE)) assert isinstance(result, np.ndarray) assert result.shape == (1, 1024) # 1024 = 3*3*1024*2 with pytest.raises(ValueError): cnn.encode_image("") image_array = load_image(TEST_IMAGE) result = cnn.encode_image(image_array=image_array) assert result.shape == (1, 1024) # 1024 = 3*3*1024*2
def encode_image(self, image_file=None, image_array: Optional[np.ndarray] = None) -> str: """ Generate hash for a single image. Args: image_file: Path to the image file. image_array: Optional, used instead of image_file. Image typecast to numpy array. Returns: hash: A 16 character hexadecimal string hash for the image. Example: ``` from imagededup.methods import <hash-method> myencoder = <hash-method>() myhash = myencoder.encode_image(image_file='path/to/image.jpg') OR myhash = myencoder.encode_image(image_array=<numpy array of image>) ``` """ try: if image_file and os.path.exists(image_file): image_file = Path(image_file) image_pp = load_image(image_file=image_file, target_size=self.target_size, grayscale=True) elif isinstance(image_array, np.ndarray): check_image_array_hash( image_array) # Do sanity checks on array image_pp = preprocess_image(image=image_array, target_size=self.target_size, grayscale=True) else: raise ValueError except (ValueError, TypeError): raise ValueError( 'Please provide either image file path or image array!') return self._hash_func(image_pp) if isinstance(image_pp, np.ndarray) else None
def test_load_image_target_size_grayscale_true(preprocess_mocker): load_image(image_file=PATH_SINGLE_IMAGE, target_size=(8, 8), grayscale=True) preprocess_mocker.assert_called_once_with( Image.open(PATH_SINGLE_IMAGE), target_size=(8, 8), grayscale=True )
def test_load_image_wrong_image_format(): assert load_image(p.parent / 'data/formats_images/Iggy.1024.ppm') is None
def test_load_image_returns_none_wrong_input(): inp = 'test_string' assert load_image(inp) is None
def test_load_image_accepts_pil(mocker): preprocess_mocker = mocker.patch('imagededup.utils.image_utils.preprocess_image') load_image(PATH_SINGLE_IMAGE) preprocess_mocker.assert_called_once_with( Image.open(PATH_SINGLE_IMAGE), target_size=None, grayscale=False )