-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
271 lines (217 loc) · 8.07 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# Note: All keys/passwords have been stripped
from flask import request, Flask
import json
import os, uuid
import httplib, urllib, base64, json
import pexpect
from pexpect import pxssh
UPLOAD_FOLDER = '/home/divyansh/codes/ai/whitecane/images/'
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def getCaptions(imagepath):
''' Get captions using TensorFlow Show & Tell Model running locally'''
os.chdir("/home/divyansh/codes/tensorflow/models/im2txt/")
print "Imagepath = ", imagepath
output = os.popen("bazel-bin/im2txt/run_inference --checkpoint_path=pretrained/model.ckpt-2000000 --vocab_file=pretrained/word_counts.txt --input_files=" + imagepath).read()
captions = output.split("\n")[1:-1]
captions = [caption[5:-13] for caption in captions]
print "captions = ", captions
return captions[0]
# def getCaptions(imagepath):
# ''' Use Microsoft Cognitive Services to generate Captions '''
# print "Imagepath = ", imagepath
# image = open(imagepath, "rb").read()
# headers = {
# # Request headers
# 'Content-Type': 'application/octet-stream',
# 'Ocp-Apim-Subscription-Key': '4632ccd8b49b4ec2ad785fad40734f2d',
# }
# params = urllib.urlencode({
# # Request parameters
# 'visualFeatures': 'Description,Faces,Categories',
# 'language': 'en',
# })
# conn = httplib.HTTPConnection('10.3.100.207', 8080)
# conn.request("POST", "http://api.projectoxford.ai/vision/v1.0/analyze?%s" % params, image, headers)
# response = conn.getresponse()
# data = response.read()
# print data
# data = json.loads(data)
# print data
# caption = data['description']['captions'][0]['text']
# conn.close()
# return caption
def runYOLO(imagepath):
os.chdir("/home/divyansh/codes/darknet/")
print "Imagepath = ", imagepath
output = os.popen("./darknet detector test cfg/voc.data cfg/tiny-yolo-voc.cfg tiny-yolo-voc.weights -thresh 0.2" + imagepath).read()
# print output
tags = output.split("\n")[1:]
tags = [tag.split(':')[0] for tag in tags]
print "captions = ", tags
return tags
def findInImage(imagepath, query):
print "Imagepath = ", imagepath
image = open(imagepath, "rb").read()
headers = {
# Request headers
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': 'xxxxxxx',
}
params = urllib.urlencode({
# Request parameters
'visualFeatures': 'Description,Faces,Categories',
'language': 'en',
})
conn = httplib.HTTPConnection('10.3.100.207', 8080)
conn.request("POST", "http://api.projectoxford.ai/vision/v1.0/analyze?%s" % params, image, headers)
response = conn.getresponse()
data = response.read()
print data
data = json.loads(data)
print data
tags = data['description']['tags']
conn.close()
if query in tags or query in data['description']['captions'][0]['text']:
return "Found it"
else:
return "Try again"
def answerQuestion(imagepath, question):
sentinel = open("SENTINEL.txt", "w")
sentinel.write("images/" + os.path.basename(imagepath) + "\n")
sentinel.write(question + "\n")
sentinel.close()
x = "xxxxx"
u = "xxxxx"
child = pexpect.spawn("scp " + imagepath + " xxxx@xxxx:~/whitecane/images/")
child.expect("password:")
child.sendline(x)
child.expect(pexpect.EOF)
child = pexpect.spawn("scp SENTINEL.txt xxxx@xxxxx:~/whitecane/")
child.expect("password:")
child.sendline(x)
child.expect(pexpect.EOF)
session = pxssh.pxssh()
session.login("xxxxxx", u, x)
# session.sendline('ls whitecane')
# session.prompt()
# print session.before
session.sendline('cd whitecane')
session.prompt()
session.sendline('python brahma.py ' + os.path.basename(imagepath))
session.prompt()
print "108's brahma.py started"
print session.before
answered = False
while(not answered):
print "Waiting for 108's reply"
child = pexpect.spawn("scp xxxx@xxxx:~/whitecane/answer.txt ./")
child.expect("password:")
child.sendline(x)
child.expect(pexpect.EOF)
print child.before
if not "No such file" in child.before:
answered = True
# session = pxssh.pxssh()
# session.login(host, u, x)
# session.sendline('rm ~/whitecane/answer.txt')
# session.prompt()
answer = open("answer.txt", 'r')
return answer.readline()
def getFacesDescription(imagepath):
''' Use Microsoft Cognitive Services to generate description of all the faces '''
return "This feature is not yet available"
print "Imagepath = ", imagepath
image = open(imagepath, "rb").read()
headers = {
# Request headers
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': 'xxxxx',
}
params = urllib.urlencode({
# Request parameters
'returnFaceId': 'true',
'returnFaceLandmarks': 'false',
'returnFaceAttributes': 'true',
})
conn = httplib.HTTPConnection('10.3.100.207', 8080)
conn.request("POST", "https://api.projectoxford.ai/face/v1.0/detect?%s" % params, image, headers)
response = conn.getresponse()
data = response.read()
print data
data = json.loads(data)
print data
caption = data[0]['faceAttributes']['gender']
conn.close()
return caption
# def classifyIntent(query):
# ''' Use LUIS to decide which model to use'''
# return "CAPTION"
@app.route('/upload', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
file = request.files['file']
extension = os.path.splitext(file.filename)[1]
f_name = str(uuid.uuid4()) + extension
filepath = os.path.join(app.config['UPLOAD_FOLDER'], f_name)
file.save(filepath)
# query = "lalala"
query = request.form['text']
print query
model = classifyIntent(query)
if model == 'CAPTION':
captions = getCaptions(filepath)
return json.dumps(captions)
elif model == 'QA':
answer = answerQuestion(filepath, query)
return json.dumps(answer)
elif model == 'STORY':
return
if request.method == 'GET':
return json.dumps({'error' : 1})
@app.route('/qa', methods=['POST'])
def qa():
if request.method == 'POST':
file = request.files['file']
query = request.form['text']
extension = os.path.splitext(file.filename)[1]
f_name = str(uuid.uuid4()) + extension
filepath = os.path.join(app.config['UPLOAD_FOLDER'], f_name)
file.save(filepath)
print query
answer = answerQuestion(filepath, query)
return json.dumps(answer)
@app.route('/find', methods=['POST'])
def find():
if request.method == 'POST':
file = request.files['file']
query = request.form['text']
extension = os.path.splitext(file.filename)[1]
f_name = str(uuid.uuid4()) + extension
filepath = os.path.join(app.config['UPLOAD_FOLDER'], f_name)
file.save(filepath)
print query
answer = findInImage(filepath, query)
return json.dumps(answer)
@app.route('/caption', methods=['POST'])
def caption():
if request.method == 'POST':
file = request.files['file']
extension = os.path.splitext(file.filename)[1]
f_name = str(uuid.uuid4()) + extension
filepath = os.path.join(app.config['UPLOAD_FOLDER'], f_name)
file.save(filepath)
captions = getCaptions(filepath)
return json.dumps(captions)
@app.route('/face', methods=['POST'])
def face():
if request.method == 'POST':
file = request.files['file']
extension = os.path.splitext(file.filename)[1]
f_name = str(uuid.uuid4()) + extension
filepath = os.path.join(app.config['UPLOAD_FOLDER'], f_name)
file.save(filepath)
captions = getFacesDescription(filepath)
return json.dumps(captions)
if __name__ == "__main__":
app.run("0.0.0.0", debug=True)