forked from dsys/match
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
198 lines (168 loc) · 5.18 KB
/
server.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from elasticsearch import Elasticsearch
from flask import Flask, request
from image_match.elasticsearch_driver import SignatureES
from image_match.goldberg import ImageSignature
import json
import os
import sys
# =============================================================================
# Globals
es_url = os.environ['ELASTICSEARCH_URL']
es_index = os.environ['ELASTICSEARCH_INDEX']
es_doc_type = os.environ['ELASTICSEARCH_DOC_TYPE']
all_orientations = os.environ['ALL_ORIENTATIONS']
app = Flask(__name__)
es = Elasticsearch([es_url], verify_certs=True, timeout=60, max_retries=10, retry_on_timeout=True)
ses = SignatureES(es, index=es_index, doc_type=es_doc_type)
gis = ImageSignature()
# Try to create the index and ignore IndexAlreadyExistsException
# if the index already exists
es.indices.create(index=es_index, ignore=400)
# =============================================================================
# Helpers
def ids_with_path(path):
matches = es.search(index=es_index,
_source='_id',
q='path:' + json.dumps(path))
return [m['_id'] for m in matches['hits']['hits']]
def paths_at_location(offset, limit):
search = es.search(index=es_index,
from_=offset,
size=limit,
_source='path')
return [h['_source']['path'] for h in search['hits']['hits']]
def count_images():
return es.count(index=es_index)['count']
def delete_ids(ids):
for i in ids:
es.delete(index=es_index, doc_type=es_doc_type, id=i, ignore=404)
def dist_to_percent(dist):
return (1 - dist) * 100
def get_image(url_field, file_field):
if url_field in request.form:
return request.form[url_field], False
else:
return request.files[file_field].read(), True
# =============================================================================
# Routes
@app.route('/add', methods=['POST'])
def add_handler():
path = request.form['filepath']
try:
metadata = json.loads(request.form['metadata'])
except KeyError:
metadata = None
img, bs = get_image('url', 'image')
old_ids = ids_with_path(path)
ses.add_image(path, img, bytestream=bs, metadata=metadata)
delete_ids(old_ids)
return json.dumps({
'status': 'ok',
'error': [],
'method': 'add',
'result': []
})
@app.route('/delete', methods=['DELETE'])
def delete_handler():
path = request.form['filepath']
ids = ids_with_path(path)
delete_ids(ids)
return json.dumps({
'status': 'ok',
'error': [],
'method': 'delete',
'result': []
})
@app.route('/search', methods=['POST'])
def search_handler():
img, bs = get_image('url', 'image')
ao = request.form.get('all_orientations', all_orientations) == 'true'
matches = ses.search_image(
path=img,
all_orientations=ao,
bytestream=bs)
return json.dumps({
'status': 'ok',
'error': [],
'method': 'search',
'result': [{
'score': dist_to_percent(m['dist']),
'filepath': m['path'],
'metadata': m['metadata']
} for m in matches]
})
@app.route('/compare', methods=['POST'])
def compare_handler():
img1, bs1 = get_image('url1', 'image1')
img2, bs2 = get_image('url2', 'image2')
img1_sig = gis.generate_signature(img1, bytestream=bs1)
img2_sig = gis.generate_signature(img2, bytestream=bs2)
score = dist_to_percent(gis.normalized_distance(img1_sig, img2_sig))
return json.dumps({
'status': 'ok',
'error': [],
'method': 'compare',
'result': [{ 'score': score }]
})
@app.route('/count', methods=['GET'])
def count_handler():
count = count_images()
return json.dumps({
'status': 'ok',
'error': [],
'method': 'count',
'result': [count]
})
@app.route('/list', methods=['GET'])
def list_handler():
offset = max(int(request.form.get('offset', 0)), 0)
limit = max(int(request.form.get('limit', 20)), 0)
paths = paths_at_location(offset, limit)
return json.dumps({
'status': 'ok',
'error': [],
'method': 'list',
'result': paths
})
@app.route('/ping', methods=['GET'])
def ping_handler():
return json.dumps({
'status': 'ok',
'error': [],
'method': 'ping',
'result': []
})
# =============================================================================
# Error Handling
@app.errorhandler(400)
def bad_request(e):
return json.dumps({
'status': 'fail',
'error': ['bad request'],
'method': '',
'result': []
}), 400
@app.errorhandler(404)
def page_not_found(e):
return json.dumps({
'status': 'fail',
'error': ['not found'],
'method': '',
'result': []
}), 404
@app.errorhandler(405)
def method_not_allowed(e):
return json.dumps({
'status': 'fail',
'error': ['method not allowed'],
'method': '',
'result': []
}), 405
@app.errorhandler(500)
def server_error(e):
return json.dumps({
'status': 'fail',
'error': [str(e)],
'method': '',
'result': []
}), 500