Skip to content

Commit

Permalink
Added score and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyism committed Jul 23, 2021
1 parent ce59869 commit 76e8db1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion image_comparer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .compare import is_similar

__version__ = "1.0.1"
__version__ = "1.1.0"
9 changes: 7 additions & 2 deletions image_comparer/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@
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)

image_tensor1 = transformer(pil_image1).unsqueeze(0)
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]

0 comments on commit 76e8db1

Please sign in to comment.