Skip to content

Commit e746ff9

Browse files
jbittonfacebook-github-bot
authored andcommitted
make image similarity check less sensitive (facebookresearch#258)
Summary: as part of my personal side quest to make augly's tests pass again, i am making a change to our tests. currently, to assess image similarity, we use the `np.allclose` function. while that's better / less sensitive than an MD5 hash it's not much better because imperceptible changes can actually have large differences in values between numpy image arrays. thus, to make augly's tests less affected by slight version updates by PIL or whatever else, we are switching to using imagehash. we're specifically using the phash - you can read about it here: https://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html phash isn't a perfect fit though, long term. it's not sensitive to color, scaling, or aspect ratio changes. to deal with the latter two, im keeping in the size equality check. for color, i want to do some more research on what is an efficient way to do this. nonetheless, this is still better than what we currently have right now. Differential Revision: D70137163
1 parent 5e00000 commit e746ff9

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

.github/workflows/test_python.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
python-version: '3.9'
1515
- run: sudo apt-get update
1616
- run: sudo apt-get install --fix-missing ffmpeg python3-soundfile
17-
- run: pip install pyre-check pytest
17+
- run: pip install pyre-check pytest imagehash
1818
- run: pip install -e .[all]
1919
- run: pyre --source-directory "." --noninteractive check || true
2020
- run: pytest --durations=10 .

augly/image/functional.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,9 @@ def overlay_wrap_text(
17121712
color = (rand.randrange(255), rand.randrange(255), rand.randrange(255))
17131713

17141714
red, green, blue = color
1715-
draw = ImageDraw.Draw(image)
1715+
aug_image = image.copy()
1716+
1717+
draw = ImageDraw.Draw(aug_image)
17161718
for line in lines:
17171719
# draw text on the image
17181720
draw.text(
@@ -1726,11 +1728,11 @@ def overlay_wrap_text(
17261728
imutils.get_metadata(
17271729
metadata=metadata,
17281730
function_name="overlay_wrap_text",
1729-
aug_image=image,
1731+
aug_image=aug_image,
17301732
**func_kwargs,
17311733
)
17321734

1733-
return imutils.ret_and_save_image(image, output_path, src_mode)
1735+
return imutils.ret_and_save_image(aug_image, output_path, src_mode)
17341736

17351737

17361738
def pad(

augly/tests/image_tests/base_unit_test.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
import unittest
1111
from typing import Any, Callable, Dict, List, Optional
1212

13-
import numpy as np
13+
import imagehash
1414
from augly.tests import ImageAugConfig
1515
from augly.utils import pathmgr, TEST_URI
1616
from PIL import Image
1717

1818

1919
def are_equal_images(a: Image.Image, b: Image.Image) -> bool:
20-
return a.size == b.size and np.allclose(np.array(a), np.array(b))
20+
a_hash = imagehash.phash(a)
21+
b_hash = imagehash.phash(b)
22+
return a.size == b.size and a_hash - b_hash == 0
2123

2224

2325
def are_equal_metadata(

0 commit comments

Comments
 (0)