Skip to content

Commit 7778f95

Browse files
jbittonfacebook-github-bot
authored andcommitted
make image similarity check less sensitive (facebookresearch#258)
Summary: Pull Request resolved: facebookresearch#258 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 c245017 commit 7778f95

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
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/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 < 2
2123

2224

2325
def are_equal_metadata(

0 commit comments

Comments
 (0)