-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.py
154 lines (125 loc) · 3.71 KB
/
lib.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import json
import numpy as np
import pybktree
import scipy.fftpack as fft
class PHashStore:
tree = pybktree.BKTree(pybktree.hamming_distance, [])
def add(self, phash):
if not self.exists(phash):
self.tree.add(phash)
def find(self, phash, distance=15):
return self.tree.find(phash, distance)
def exists(self, phash):
return len(self.find(phash, 0)) > 0
def load(self, io):
data = json.load(io)
for r in data:
self.add(r)
def dump(self):
return json.dumps(sorted(self.tree))
def phash_for(self, image, algorithm='dhash'):
if algorithm == 'phash':
return self.phash(image)
else:
return self.dhash(image)
def phash(self, image):
r = self.__ndarray_for(image, size="32x32!").astype(np.float64)
h = fft.dctn(r, norm="ortho")[0:8,0:8]
avg = np.average(h.reshape(64,)[1:])
mask = (h <= avg)
h = mask.reshape(64,).dot(2**np.arange(mask.size)[::-1])
return int(h)
def dhash(self, image):
r = self.__ndarray_for(image)
h = 0
try:
for i in range(1, 9):
for j in range(1, 9):
h = h << 1 | (1 if r[i][j] >= r[i][j - 1] else 0)
for i in range(1, 9):
for j in range(1, 9):
h = h << 1 | (1 if r[j][i] >= r[j - 1][i] else 0)
return h
except IndexError as e:
pdb.set_trace()
return -1
except ValueError as e:
pdb.set_trace()
return -2
def __ndarray_for(self, image, size="9x9!"):
image.alpha_channel = False
image.format = 'gray'
image.type = 'grayscale'
image.depth = 8
image.transform(resize=size)
result = np.asarray(bytearray(image.make_blob()), dtype=np.uint8).reshape(image.size)
image.close()
return result
def hamming2(self, s1, s2):
assert len(s1) == len(s2)
return sum(c1 != c2 for c1, c2 in zip(s1, s2))
class PHashMetaStore(PHashStore):
files = {}
def add(self, phash, meta):
if not self.exists(phash):
self.files[phash] = []
super().add(phash)
self.files[phash].append(meta)
def find(self, phash, distance=15):
results = self.tree.find(phash, distance)
return [self.__to_result(x) for x in results]
def load(self, io):
data = json.load(io)
for phash, files in data.items():
phash = int(phash)
if not self.exists(phash):
super().add(phash)
self.files[phash] = []
for f in files:
if not f in self.files[phash]:
self.files[phash].append(f)
def dump(self):
return json.dumps(self.files)
def __to_result(self, x):
return {
'distance': x[0],
'phash': x[1],
'files': self.files[x[1]]
}
class PHashServer:
def __init__(self, host='localhost', port=8001):
self.host = host
self.port = port
def start(self):
from bottle import get, post, request, route, run
self.store = PHashStore()
@post('/load')
def load():
# pdb.set_trace()
io = request.body
self.store.load(io)
return self.__render_json({'message': 'ok'})
@get('/dump')
def dump():
# pdb.set_trace()
return self.__render_json(self.store.dump())
@post('/index')
def index():
image = Image(file=request.files.file.file)
h = self.store.phash_for(image)
self.store.add(h)
return self.__render_json({'phash': h})
@get('/search/<phash>')
def search(phash):
data = []
for r in self.store.find(int(phash)):
data.append({'distance': r[0], 'phash': r[1]})
return self.__render_json(data)
run(host=self.host, port=self.port)
def __render_json(self, data):
from bottle import response
response.content_type = 'application/json'
if type(data) == 'str':
return data
else:
return json.dumps(data)