-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
236 lines (195 loc) · 8.41 KB
/
app.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# -*- coding: utf-8 -*-
from flask import Flask, request, render_template, jsonify, url_for
from elasticsearch import Elasticsearch
from PIL import Image, ExifTags
import face_recognition, base64, io, json
connection_file=open("connection.txt")
connection=connection_file.readlines()
connection=[conn.strip() for conn in connection]
es = Elasticsearch(
connection[0],
basic_auth=(connection[1],connection[2])
)
def register_Image(image, file_name):
description = []
image_base64 = []
score = []
try:
im1 = Image.open(image)
im1 = im1.save(file_name)
image = face_recognition.load_image_file(image)
face_locations = face_recognition.face_locations(image)
face_encodings = face_recognition.face_encodings(image, face_locations)
for face_encoding in face_encodings:
face_encoding = face_encoding.tolist()
#print(face_encoding)
with open(file_name, "rb") as image_file:
img64 = (base64.b64encode(image_file.read()))
img64 = img64.decode('utf-8')
connection_file=open("connection.txt")
connection=connection_file.readlines()
connection=[conn.strip() for conn in connection]
#print(connection)
es = Elasticsearch(
connection[0],
basic_auth=(connection[1],connection[2])
)
es.index(
index="search_project",
document={
"face_vector": face_encoding,
"image_base64": img64,
"description": file_name,
}
)
with open('static/images/awesome.png', "rb") as image_file:
image_b64 = (base64.b64encode(image_file.read()))
image_b64 = image_b64.decode("utf-8")
score.append('-')
image_base64.append("data:image/png;base64,"+ image_b64)
description.append("Image was uploaded with success!!")
return description, image_base64, score
except Exception as e:
print("Upps, error! ", e)
with open('static/images/notFound.png', "rb") as image_file:
image_b64 = (base64.b64encode(image_file.read()))
image_b64 = image_b64.decode("utf-8")
score.append('-')
image_base64.append("data:image/png;base64,"+ image_b64)
description.append("Image upload failed :/!")
return description, image_base64, score
def searchFace(image, search_option):
description = []
image_base64 = []
score = []
try:
image = face_recognition.load_image_file(image)
face_locations = face_recognition.face_locations(image)
face_encodings = face_recognition.face_encodings(image, face_locations)
if len(face_encodings) > 0:
for face_encoding in face_encodings:
face_encoding = face_encoding.tolist()
if search_option != 'cosine':
query_elastic = {
"size": 1,
"_source": False,
"knn": {
"field": "face_vector",
"query_vector": face_encoding,
#"k": 1,
"num_candidates": 10
},
"fields": [ "description","image_base64" ]
}
response = es.search(index='search_project',body=query_elastic)
response = dict(response)
response = json.dumps(response)
response = json.loads(response)
print("Took time: " + str(response['took']))
for hits in response['hits']['hits']:
if hits['_score'] >= 0.8:
score.append(hits['_score'])
image_base64.append("data:image/png;base64,"+ hits['fields']['image_base64'][0])
cut_description = hits['fields']['description'][0]
cut_description = cut_description[-9:]
description.append(cut_description)
return description, image_base64, score
else:
raise ValueError('Erro to extract face')
except Exception as e:
with open('static/images/notFound.png', "rb") as image_file:
image_b64 = (base64.b64encode(image_file.read()))
image_b64 = image_b64.decode("utf-8")
score.append('-')
image_base64.append("data:image/png;base64,"+ image_b64)
description.append("No faces detected")
return description, image_base64, score
def get_orientation(image):
try:
exif = image.getexif()
print("exif fun")
if exif:
for orientation in ExifTags.TAGS.keys():
print("exif for")
if ExifTags.TAGS[orientation] == 'Orientation':
print("exif if tag")
orientation_value = exif.get(orientation, None)
print(orientation_value)
return orientation_value
except AttributeError:
return None
app = Flask(__name__, static_url_path='/static')
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
@app.route('/register', methods=['GET','POST'])
def registerImage():
search_option = 'register'
if request.method == 'POST':
if 'file' not in request.files:
return jsonify(description='File not found', image_base64='')
file = request.files['file']
if file.filename == '':
return jsonify(description='File not selected', image_base64='')
if file:
file_name = file.filename
img = io.BytesIO(file.read())
image = Image.open(img)
try:
orientation = get_orientation(image)
if orientation == 3:
image = image.rotate(180, expand=True)
image.save("temp.jpg")
img = "temp.jpg"
elif orientation == 6:
image = image.rotate(270, expand=True)
image.save("temp.jpg")
img = "temp.jpg"
elif orientation == 8:
image = image.rotate(90, expand=True)
image.save("temp.jpg")
img = "temp.jpg"
except AttributeError:
print("Not to do here!")
img_size = image.size
description, image_base64, score = register_Image(img, file_name)
return jsonify(description=description, image_base64=image_base64, score=score)
else:
return jsonify(description='Extension not allowed!', image_base64='')
return render_template('indexRegister.html')
@app.route('/', methods=['GET','POST'])
def vectorSearch():
search_option = 'vector'
if request.method == 'POST':
if 'file' not in request.files:
return jsonify(description='File not found', image_base64='')
file = request.files['file']
if file.filename == '':
return jsonify(description='File not selected', image_base64='')
if file:
img = io.BytesIO(file.read())
image = Image.open(img)
image.save("temp.jpg")
try:
orientation = get_orientation(image)
if orientation == 3:
image = image.rotate(180, expand=True)
image.save("temp.jpg")
img = "temp.jpg"
elif orientation == 6:
image = image.rotate(270, expand=True)
image.save("temp.jpg")
img = "temp.jpg"
elif orientation == 8:
image = image.rotate(90, expand=True)
image.save("temp.jpg")
img = "temp.jpg"
except AttributeError:
print("Not to do here!")
img_size = image.size
print(orientation)
description, image_base64, score = searchFace(img, search_option)
return jsonify(description=description, image_base64=image_base64, score=score)
else:
return jsonify(description='Extension not allowed!', image_base64='')
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=80)