-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.py
executable file
·243 lines (193 loc) · 7.1 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
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
237
238
239
240
241
242
243
from __future__ import print_function
from flask import Flask, request, redirect, url_for, jsonify, send_from_directory
from time import time
import time
import numpy as np
import hashlib
import os
import sys
sys.path.append("./questionAnswering") # path to question answering module
from QuestionAnswering import QuestionAnswering
from im2txt.imgCaptioning import imgCap
from recognition.recognition import FaceRecognition
from emotion.tf_emotion_class import EmotionPredictor
from detection.Detectface import DetectFaceClass
from LSTM.lstm import SentencePredictor
from menu_recognition.menu_recog import ReadText
import cv2
VIZ_FOLDER = './viz/'
UPLOAD_FOLDER = './uploads/'
ALLOWED_EXTENSIONS = set(['jpg', 'jpeg', 'JPG', 'JPEG', 'png', 'PNG'])
# global variables
app = Flask(__name__, static_url_path='')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
im2txt = None
questionAnswering = None
recognizer = None
faceDetector = None
emotionDetector = None
sentencePredictor = None
textReader = None
feature_cache = {}
def format_image(image, face):
image = image[face[1]:face[3], face[0]:face[2]]
return image
# helpers
def setup():
global im2txt
global questionAnswering
global recognizer
global faceDetector
global emotionDetector
global sentencePredictor
global textReader
# uploads
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
if not os.path.exists(VIZ_FOLDER):
os.makedirs(VIZ_FOLDER)
emotionDetector = EmotionPredictor()
faceDetector = DetectFaceClass(1, '/home/richard/Desktop/emotion-recognition-neural-networks-master/detection/mxnet-face-fr50', 0, 0.3, 0.001, 600, 1000)
im2txt = imgCap()
sentencePredictor = SentencePredictor()
questionAnswering = QuestionAnswering()
textReader = ReadText()
recognizer = FaceRecognition(1.0, faceDetector)
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
# routes
@app.route('/', methods=['GET'])
def index():
return app.send_static_file('demo2.html')
@app.route('/api/upload_all', methods=['POST'])
def upload_all():
tick = time.clock()
file = request.files['image']
if not file:
return jsonify({'error': 'No file was uploaded.'})
if not allowed_file(file.filename):
return jsonify({'error': 'Please upload a JPG or PNG.'})
if request.form['request'] == "":
return jsonify({'answer': "Please, ask a question!"})
file_hash = hashlib.md5(file.read()).hexdigest()
save_path = os.path.join(app.config['UPLOAD_FOLDER'], file_hash + '.jpg')
file.seek(0)
file.save(save_path)
print("file:")
print(save_path)
question = request.form['request']
lst = question.split()
lst[0] = lst[0][0].upper() + lst[0][1:]
question = " ".join(lst)
result = np.squeeze(sentencePredictor.predict(question))
maxval = 0
idx = 0
for i in range(len(result)):
print(result[i])
if result[i] > maxval:
maxval = result[i]
idx = i
# handle image first
print("question:")
print(question)
print( str(idx) + "th NETWORK")
name = question[:question.find(" ")]
print(name)
if (name == 'name' or name == 'Name'):
print("adding person")
name = question[question.find(" ") + 1:]
Image = cv2.imread(save_path)
answer = recognizer.add(Image, name, file_hash + '.jpg')
if answer != "success":
print("could not save person " + name + ", because: " + answer)
return jsonify({'answer': answer})
# img captioning
if idx == 0:
answer = im2txt.feed_image(save_path)
tock = time.clock()
print(str(tock - tick) + "time used for iamge captioning")
return jsonify({'answer': answer})
# face
if (idx == 3):
try:
print("path = " + save_path)
answer = ""
Image = cv2.imread(save_path)
people = recognizer.recognize(Image)
if len(people) == 0:
answer = "I don't see anyone here!"
elif (len(people) == 1):
if people[0] == "I don't know :(":
answer = "I don't know :("
else:
answer = "It is " + people[0]
else:
known_people = ""
unknow_people = 0
for person in people:
if person == "I don't know :(":
unknow_people += 1
else:
if known_people == "":
known_people = person
else:
known_people = known_people + ", " + person
if known_people == "":
answer = "There are " + str(unknow_people) + " people. I don't know anyone here."
else:
answer = "There are " + known_people
if unknow_people > 0:
answer = answer + ". There are " + str(unknow_people) + " people, which I don't know."
except:
tock = time.clock()
print(str(tock - tick) + "time used for face")
return jsonify({'answer': "I don't see anyone here!"})
tock = time.clock()
print(str(tock - tick) + "time used for face")
return jsonify({'answer': answer})
# emotion
if (idx == 2):
img = cv2.imread(save_path)
faces = faceDetector.detect_Face(img)
if len(faces) == 0:
answer = "There are no people here!"
return jsonify({'answer': answer})
else:
answer = []
for face in faces:
answer.append(emotionDetector.predict(img, face))
tock = time.clock()
print(str(tock - tick) + "time used for emotion")
print(answer)
return jsonify({'answer': ",".join(answer)})
# question answering
if idx == 1:
feature = questionAnswering.img_handler(save_path)
if feature is None:
tock = time.clock()
print(str(tock - tick) + "time used for qa")
return jsonify({'error': 'Error reading image.'})
# image + question
img_ques_hash = hashlib.md5(file_hash + question).hexdigest()
json = questionAnswering.get_answers(question, feature, save_path, img_ques_hash, VIZ_FOLDER)
tock = time.clock()
print(str(tock - tick) + "time used for qa")
return jsonify(json)
# text
if idx == 4:
json = textReader.read(save_path)
print(type(json))
if json == '':
tock = time.clock()
print(str(tock - tick) + "time used for text")
return jsonify({'answer': "I don't see the text here!"})
tock = time.clock()
print(str(tock - tick) + "time used for text")
return jsonify({'answer':json})
else:
tock = time.clock()
print(str(tock - tick) + "time used for text")
return jsonify({'answer':"Error text"})
if __name__ == '__main__':
setup()
app.run(host='0.0.0.0', port=5000, debug=False)