-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtools.py
40 lines (33 loc) · 1022 Bytes
/
hashtools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python
# hashtools.py -- The daemon which sits in the background and monitors for newly added images, then adds the hashes to the database.
from hashlib import sha512
from PIL import Image
import numpy
import pickle
def exact(image):
return sha512(image.tostring()).digest()
#def phash(image, hash_size=32):
# image = image.convert("L").resize((hash_size, hash_size), Image.ANTIALIAS)
# pixels = numpy.array(image.getdata(), dtype=numpy.float).reshape((hash_size, hash_size))
# dct = scipy.fftpack.dct(pixels)
# dctlowfreq = dct[:8, 1:9]
# avg = dctlowfreq.mean()
# diff = dctlowfreq > avg
# return ImageHash(diff)
NN = None
def nn_hash(image):
global NN
if NN is None:
pass # Load NN
#img = Image.open(filename)
img = image.convert('L').resize((128,128))
arr = numpy.asarray(img)
arr = arr.reshape((1,128*128))
_, ha, _ = NN.forward_propagate(numpy.asmatrix(arr))
return ha
HASH_ALGORITHMS = {'exact':exact, 'nn':nn_hash}
def main():
pass
# Sanity test
if __name__=="__main__":
main()