Skip to content

Commit

Permalink
Use bitarray for efficient ahash calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
desbma committed Jul 12, 2018
1 parent ec63fad commit 7cb3967
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
aiohttp>=3.0.0
appdirs>=1.4.0
bitarray>=0.8.3
cssselect>=0.9.1
lxml>=4.0.0
mutagen>=1.31
Expand Down
12 changes: 5 additions & 7 deletions sacad/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import urllib.parse

import appdirs
import bitarray
import PIL.Image
import PIL.ImageFile
import PIL.ImageFilter
Expand Down Expand Up @@ -645,22 +646,19 @@ def computeImgSignature(image_data):
pixels = img.getdata()
pixel_count = target_size[0] * target_size[1]
color_count = 3
r = 0
r = bitarray.bitarray(pixel_count * color_count)
r.setall(False)
for ic in range(color_count):
mean = sum(p[ic] for p in pixels) // pixel_count
for ip, p in enumerate(pixels):
if p[ic] > mean:
r |= 1 << (pixel_count * ic + ip)
r[pixel_count * ic + ip] = True
return r

@staticmethod
def areImageSigsSimilar(sig1, sig2):
""" Compare 2 image "signatures" and return True if they seem to come from a similar image, False otherwise. """
# get the hamming distance between 2 ahashes
s1 = bin(sig1)[2:]
s2 = bin(sig2)[2:]
hamming = sum(int(b1 != b2) for b1, b2 in zip(s1, s2))
return hamming < 100
return bitarray.bitdiff(sig1, sig2) < 100


# silence third party module loggers
Expand Down
21 changes: 19 additions & 2 deletions win/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ PYTHON_VERSION_MAJOR := $(word 1,$(subst ., ,${PYTHON_VERSION})).$(word 2,$(subs
PYTHON_VERSION_SHORT := $(subst .,,${PYTHON_VERSION_MAJOR})
CXFREEZE_VERSION := 5.1.1
LXML_VERSION := 4.2.0
BITARRAY_VERSION := 0.8.3

# installers
DOWNLOAD_DIR ?= dl
Expand All @@ -19,6 +20,9 @@ CXFREEZE_WHEEL := ${CXFREEZE_WHEEL-${WINEARCH}}
LXML_WHEEL-win32 := ${DOWNLOAD_DIR}/lxml-${LXML_VERSION}-cp${PYTHON_VERSION_SHORT}-none-${WINEARCH}.whl
LXML_WHEEL-win64 := ${DOWNLOAD_DIR}/lxml-${LXML_VERSION}-cp${PYTHON_VERSION_SHORT}-none-win_amd64.whl
LXML_WHEEL := ${LXML_WHEEL-${WINEARCH}}
BITARRAY_WHEEL-win32 := ${DOWNLOAD_DIR}/bitarray-${BITARRAY_VERSION}-cp${PYTHON_VERSION_SHORT}-cp${PYTHON_VERSION_SHORT}m-${WINEARCH}.whl
BITARRAY_WHEEL-win64 := ${DOWNLOAD_DIR}/bitarray-${BITARRAY_VERSION}-cp${PYTHON_VERSION_SHORT}-cp${PYTHON_VERSION_SHORT}m-win_amd64.whl
BITARRAY_WHEEL := ${BITARRAY_WHEEL-${WINEARCH}}
7ZIP_INSTALLER := ${DOWNLOAD_DIR}/7z938.msi

# env
Expand All @@ -31,6 +35,7 @@ PYTHON_INSTALLED := ${WINE_DIR}/drive_c/Python${PYTHON_VERSION_SHORT}/python.e
CXFREEZE_INSTALLED := ${WINE_DIR}/drive_c/Python${PYTHON_VERSION_SHORT}/Scripts/cxfreeze
PIP_INSTALLED := ${WINE_DIR}/drive_c/Python${PYTHON_VERSION_SHORT}/Scripts/pip.exe
LXML_INSTALLED := ${WINE_DIR}/drive_c/Python${PYTHON_VERSION_SHORT}/Lib/site-packages/lxml/__init__.py
BITARRAY_INSTALLED := ${WINE_DIR}/drive_c/Python${PYTHON_VERSION_SHORT}/Lib/site-packages/bitarray/__init__.py
7ZIP_INSTALLED := ${WINE_DIR}/drive_c/7-Zip/7z.sfx

# temp dirs
Expand Down Expand Up @@ -67,7 +72,7 @@ sacad_${WINEARCH}.exe: ${7ZIP_INSTALLED} sacad_${WINEARCH}.7z
# Intermediate targets
#

${OUTPUT_DIR}/sacad.exe: ${CXFREEZE_INSTALLED} ${LXML_INSTALLED} .requirements
${OUTPUT_DIR}/sacad.exe: ${CXFREEZE_INSTALLED} ${LXML_INSTALLED} ${BITARRAY_INSTALLED} .requirements
mkdir -p $(dir $@)
cd .. && ${WINE} ${PYTHON_INSTALLED} freeze.py build_exe -b win/${OUTPUT_DIR} 2> /dev/null > /dev/null
touch $@
Expand Down Expand Up @@ -95,7 +100,7 @@ ${OUTPUT_DIR}/readme.txt:
# Tests
#

test_ut: ${LXML_INSTALLED} .requirements
test_ut: ${LXML_INSTALLED} ${BITARRAY_INSTALLED} .requirements
cd .. && ${WINE} ${PYTHON_INSTALLED} setup.py test

test_freeze:: ${TEST_LIB_DIR}/test.jpg ${TEST_LIB_DIR}/test.png
Expand Down Expand Up @@ -136,6 +141,10 @@ ${LXML_INSTALLED}: ${LXML_WHEEL} ${PIP_INSTALLED} ${PYTHON_INSTALLED}
${WINE} ${PYTHON_INSTALLED} -m pip -qqq install $<
touch $@

${BITARRAY_INSTALLED}: ${BITARRAY_WHEEL} ${PIP_INSTALLED} ${PYTHON_INSTALLED}
${WINE} ${PYTHON_INSTALLED} -m pip -qqq install $<
touch $@

${7ZIP_INSTALLED}: ${7ZIP_INSTALLER}
${WINE} msiexec /i $< /qb INSTALLDIR='c:\7-Zip'
touch $@
Expand Down Expand Up @@ -165,6 +174,14 @@ ${LXML_WHEEL-win64}:
mkdir -p $(dir $@)
${CURL} https://www.dropbox.com/s/1p0nt3z49enp7x0/$(notdir $@) > $@

${BITARRAY_WHEEL-win32}:
mkdir -p $(dir $@)
${CURL} https://www.dropbox.com/s/wiwhdyu4p2fjdl7/$(notdir $@) > $@

${BITARRAY_WHEEL-win64}:
mkdir -p $(dir $@)
${CURL} https://www.dropbox.com/s/64nl2ug32z4yf8v/$(notdir $@) > $@

${7ZIP_INSTALLER}:
mkdir -p $(dir $@)
${CURL} http://www.7-zip.org/a/$(notdir $@) > $@.unckecked
Expand Down

0 comments on commit 7cb3967

Please sign in to comment.