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
|
import os
import re
import time
import dlib
import numpy as np
import operator
from flask import Blueprint, request, jsonify
from PIL import Image # todo: try to remove PIL dependency
from app.processors.face_extractor import ExtractorDLIB
from app.processors import face_detector
from app.processors.faiss import load_faiss_databases
from app.models.sql_factory import load_sql_datasets, list_datasets, get_dataset, get_table
from app.utils.im_utils import pil2np
sanitize_re = re.compile('[\W]+')
valid_exts = ['.gif', '.jpg', '.jpeg', '.png']
LIMIT = 9
THRESHOLD = 0.3
api = Blueprint('api', __name__)
faiss_datasets = load_faiss_databases()
@api.route('/')
def index():
"""List the datasets and their fields"""
return jsonify({ 'datasets': list_datasets() })
@api.route('/dataset/<dataset_name>')
def show(dataset_name):
"""Show the data that a dataset will return"""
dataset = get_dataset(dataset_name)
if dataset:
return jsonify(dataset.describe())
else:
return jsonify({ 'status': 404 })
@api.route('/dataset/<dataset_name>/face', methods=['POST'])
def upload(dataset_name):
"""Query an image against FAISS and return the matching identities"""
start = time.time()
dataset = get_dataset(dataset_name)
if dataset_name not in faiss_datasets:
return jsonify({
'error': 'bad_dataset'
})
faiss_dataset = faiss_datasets[dataset_name]
file = request.files['query_img']
fn = file.filename
if fn.endswith('blob'): # FIX PNG IMAGES?
fn = 'filename.jpg'
basename, ext = os.path.splitext(fn)
# print("got {}, type {}".format(basename, ext))
if ext.lower() not in valid_exts:
return jsonify({
'error': 'not_an_image'
})
im = Image.open(file.stream).convert('RGB')
im_np = pil2np(im)
# Face detection
detector = face_detector.DetectorDLIBHOG()
# get detection as BBox object
bboxes = detector.detect(im_np, largest=True, pyramids=2)
if not bboxes or not len(bboxes):
return jsonify({
'error': 'bbox'
})
bbox = bboxes[0]
if not bbox:
return jsonify({
'error': 'bbox'
})
dim = im_np.shape[:2][::-1]
bbox = bbox.to_dim(dim) # convert back to real dimensions
# print("got bbox")
if not bbox:
return jsonify({
'error': 'bbox'
})
# extract 128-D vector
extractor = face_extractor.ExtractorDLIB()
vec = extractor.extract(im, bbox_norm) # NB use norm, not bbox_dim
# recognition = face_recognition.RecognitionDLIB(gpu=-1)
# vec = recognition.vec(im_np, bbox)
query = np.array([ vec ]).astype('float32')
# query FAISS
distances, indexes = faiss_dataset.search(query, LIMIT)
if len(indexes) == 0 or len(indexes[0]) == 0:
return jsonify({
'error': 'nomatch'
})
# get the results for this single query...
distances = distances[0]
indexes = indexes[0]
dists = []
ids = []
for _d, _i in zip(distances, indexes):
if _d <= THRESHOLD:
dists.append(round(float(_d), 2))
ids.append(_i)
identities = [ dataset.get_identity(int(_i)) for _i in ids ]
identities = list(filter(None, identities))
# print(distances)
# print(ids)
# 'bbox': str(bboxes[0]),
# 'bbox_dim': str(bbox),
# print(bboxes[0])
# print(bbox)
query = {
'timing': round(time.time() - start, 3),
'bbox': str(bbox),
}
# print(results)
return jsonify({
'query': query,
'results': identities,
'distances': dists,
})
@api.route('/dataset/<dataset_name>/name', methods=['GET','POST'])
def name_lookup(dataset_name):
"""Find a name in the dataset"""
start = time.time()
dataset = get_dataset(dataset_name)
q = request.args.get('q')
q = re.sub('[^a-zA-Z. ]+', '*', q)
terms = q.split(' ')
query = {
'q': q,
'timing': time.time() - start,
}
if len(terms) == 0:
return jsonify({ 'query': query, 'results': [] })
lookup = {}
results_lookup = {}
names = dataset.search_name(q + '%')
for name in names:
if name.id in lookup:
print(name.fullname)
lookup[name.id] += 4
else:
print(name.fullname)
lookup[name.id] = 4
results_lookup[name.id] = name
for i, term in enumerate(terms[0:5]):
search_term = '%' + term + '%'
names = dataset.search_name(search_term) if len(term) > 0 else []
descriptions = dataset.search_description(search_term) if len(term) > 0 else []
for name in names:
if name.id in lookup:
print(name.fullname)
lookup[name.id] += 2
else:
print(name.fullname)
lookup[name.id] = 2
results_lookup[name.id] = name
for name in descriptions:
if name.id in lookup:
print(name.fullname)
lookup[name.id] += 1
else:
print(name.fullname)
lookup[name.id] = 1
results_lookup[name.id] = name
sorted_names = sorted(lookup.items(), key=operator.itemgetter(1), reverse=True)[0:10]
top_names = [results_lookup[item[0]] for item in sorted_names]
results = dataset.get_file_records_for_identities(top_names)
print(results)
return jsonify({
'query': query,
'results': results,
})
|