Esempio n. 1
0
def test_preprocess_image_accepts_array_input():
    inp_x = Image.open(PATH_SINGLE_IMAGE)
    inp_x = np.array(inp_x)
    target_size = (2, 2)
    ret_array = preprocess_image(inp_x, target_size=target_size, grayscale=True)
    assert isinstance(ret_array, np.ndarray)
    assert ret_array.shape == target_size
Esempio n. 2
0
    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
        )
Esempio n. 3
0
    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
Esempio n. 4
0
def test_preprocess_image_grayscale_false():
    inp_x = Image.open(PATH_SINGLE_IMAGE)
    target_size = (2, 2)
    ret_array = preprocess_image(inp_x, target_size=target_size, grayscale=False)
    assert isinstance(ret_array, np.ndarray)
    assert ret_array.shape == target_size + (3,)  # 3 for RGB
Esempio n. 5
0
def test_preprocess_image_wrong_input():
    inp = 'test_string'
    with pytest.raises(ValueError):
        preprocess_image(inp, target_size=(2, 2))
def get_image_array(pil_img):
    return preprocess_image(pil_img, target_size=(32, 32), grayscale=False)
Esempio n. 7
0
def get_image_phash(pil_img):
    array = preprocess_image(pil_img, target_size=TARGET_SIZE, grayscale=False)
    hash_mat = _hash_algo(array)
    hsh = _array_to_hash(hash_mat)
    return hsh