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.
(
self,
image_file: Optional[Union[PurePath, str]] = None,
image_array: Optional[np.ndarray] = None,
)
| 176 | return self.encoding_map |
| 177 | |
| 178 | def encode_image( |
| 179 | self, |
| 180 | image_file: Optional[Union[PurePath, str]] = None, |
| 181 | image_array: Optional[np.ndarray] = None, |
| 182 | ) -> np.ndarray: |
| 183 | """ |
| 184 | Generate CNN encoding for a single image. |
| 185 | |
| 186 | Args: |
| 187 | image_file: Path to the image file. |
| 188 | image_array: Optional, used instead of image_file. Image typecast to numpy array. |
| 189 | |
| 190 | Returns: |
| 191 | encoding: Encodings for the image in the form of numpy array. |
| 192 | |
| 193 | Example: |
| 194 | ``` |
| 195 | from imagededup.methods import CNN |
| 196 | myencoder = CNN() |
| 197 | encoding = myencoder.encode_image(image_file='path/to/image.jpg') |
| 198 | OR |
| 199 | encoding = myencoder.encode_image(image_array=<numpy array of image>) |
| 200 | ``` |
| 201 | """ |
| 202 | if isinstance(image_file, str): |
| 203 | image_file = Path(image_file) |
| 204 | |
| 205 | if isinstance(image_file, PurePath): |
| 206 | if not image_file.is_file(): |
| 207 | raise ValueError( |
| 208 | "Please provide either image file path or image array!" |
| 209 | ) |
| 210 | |
| 211 | image_pp = load_image( |
| 212 | image_file=image_file, target_size=None, grayscale=False |
| 213 | ) |
| 214 | |
| 215 | elif isinstance(image_array, np.ndarray): |
| 216 | image_array = expand_image_array_cnn( |
| 217 | image_array |
| 218 | ) # Add 3rd dimension if array is grayscale, do sanity checks |
| 219 | image_pp = preprocess_image( |
| 220 | image=image_array, target_size=None, grayscale=False |
| 221 | ) |
| 222 | else: |
| 223 | raise ValueError("Please provide either image file path or image array!") |
| 224 | |
| 225 | return ( |
| 226 | self._get_cnn_features_single(image_pp) |
| 227 | if isinstance(image_pp, np.ndarray) |
| 228 | else None |
| 229 | ) |
| 230 | |
| 231 | def encode_images( |
| 232 | self, |