From 76e8db1ffea469e7178830e7f593c2986815a9a6 Mon Sep 17 00:00:00 2001 From: Joey Date: Fri, 23 Jul 2021 16:42:38 +0200 Subject: [PATCH] Added score and docs --- README.md | 10 ++++++++++ image_comparer/__init__.py | 2 +- image_comparer/compare.py | 9 +++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 793b993..5fbaeac 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,16 @@ image2 = cv2.imread("test/kobe2.jpg") image_comparer.is_similar(image, image2, threshold=0.5) ``` +## API + +### Methods + +`is_similar(image1: Union[Image.Image, np.ndarray], image2: Union[Image.Image, np.ndarray], threshold=0.5)`: Checks if the two images are similar based on the reshold passed + + +`calculate_score(image1: Union[Image.Image, np.ndarray], image2: Union[Image.Image, np.ndarray])`: Calculates the score between the two images. The higher the score, the more closely the two images are related. + + ## Development ### Installation diff --git a/image_comparer/__init__.py b/image_comparer/__init__.py index 47bd6c3..65ecd0c 100644 --- a/image_comparer/__init__.py +++ b/image_comparer/__init__.py @@ -1,3 +1,3 @@ from .compare import is_similar -__version__ = "1.0.1" +__version__ = "1.1.0" diff --git a/image_comparer/compare.py b/image_comparer/compare.py index d3f07a8..cbf0cf1 100644 --- a/image_comparer/compare.py +++ b/image_comparer/compare.py @@ -31,7 +31,12 @@ def _pil_image_(image: ImageType) -> Image: return Image.fromarray(np.array(image)).resize(DEFAULT_IMAGE_SIZE) -def is_similar(image1: ImageType, image2: ImageType, threshold=0.5): + +def is_similar(image1: ImageType, image2: ImageType, threshold=0.5) -> bool: + return calculate_score(image1, image2) > threshold + + +def calculate_score(image1: ImageType, image2: ImageType) -> float: pil_image1 = _pil_image_(image1) pil_image2 = _pil_image_(image2) @@ -39,4 +44,4 @@ def is_similar(image1: ImageType, image2: ImageType, threshold=0.5): image_tensor2 = transformer(pil_image2).unsqueeze(0) results = model(image_tensor1, image_tensor2) - return results.detach().cpu().numpy()[0][0] > threshold + return results.detach().cpu().numpy()[0][0]