-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
225 lines (194 loc) · 7.03 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
from sqlite3 import dbapi2 as sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, \
render_template, flash,jsonify
from werkzeug import secure_filename
import json,os
from datetime import *
import time
import random
import tensorflow as tf
from inference import *
from utils import vocabulary
from utils import att_nic_vocab
FLAGS = tf.app.flags.FLAGS
tf.flags.DEFINE_string('mode', 'att-nic',
'Can be att-nic or ours')
tf.flags.DEFINE_string('vocab_path', '../output/vocabulary.csv',
'Vocabulary file, be ../data/flickr8k/word_counts.txt for mode=ours')
tf.flags.DEFINE_string("faster_rcnn_path", "../data/frozen_faster_rcnn.pb",
"Faster r-cnn frozen graph")
tf.flags.DEFINE_string("region_lstm_path", "../data/frozen_lstm.pb",
"region attention based lstm forzen graph")
tf.flags.DEFINE_string("att_nic_path", "../data/frozen_att_nic.pb",
"region attention based lstm forzen graph")
if FLAGS.mode == 'ours':
faster_rcnn = FasterRcnnEncoder(FLAGS.faster_rcnn_path)
# build vocabulary file
vocab = vocabulary.Vocabulary(FLAGS.vocab_path)
lstm = LSTMDecoder(FLAGS.region_lstm_path,vocab,max_caption_length=20)
else:
vocab = att_nic_vocab.Vocabulary(5000, FLAGS.vocab_path)
att_nic = ATT_NIC(FLAGS.att_nic_path,vocab,max_caption_length=20)
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
app = Flask(__name__)
# database related operation
# Load default config and override config from an environment variable
app.config.update(dict(
DATABASE='flaskr.db',
DEBUG=True,
SECRET_KEY='development key',
USERNAME='admin',
PASSWORD='0000'
))
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
def init_db():
"""Creates the database tables."""
with app.app_context():
db = get_db()
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()
def get_db():
"""Opens a new database connection if there is none yet for the
current application context.
"""
if not hasattr(g, 'sqlite_db'):
g.sqlite_db = connect_db()
return g.sqlite_db
def connect_db():
"""Connects to the specific database."""
rv = sqlite3.connect(app.config['DATABASE'])
rv.row_factory = sqlite3.Row
return rv
def add_entry(caption, raw_path, att_path):
db = get_db()
db.execute('insert into records (caption, raw_path, att_path) values (?, ?,?)',
[caption, raw_path, att_path])
db.commit()
cur = db.execute('select last_insert_rowid() id')
entries = cur.fetchall()
return entries[0]['id']
def random_filename():
"""自动生成随机文件名"""
nowTime = datetime.now().strftime("%Y%m%d%H%M%S")#生成当前的时间
randomNum = random.randint(0,100)#生成随机数n,其中0<=n<=100
if randomNum<=10:
randomNum = str(0) + str(randomNum)
uniqueNum = str(nowTime) + str(randomNum)
return uniqueNum
@app.teardown_appcontext
def close_db(error):
"""Closes the database again at the end of the request."""
if hasattr(g, 'sqlite_db'):
g.sqlite_db.close()
# im2txt
def generate_caption(data):
"""
return
raw: raw picture in jpeg formate
caption: generated image caption in string formate
attention: attention picture in jpeg formate
"""
# results = classify(data)
# return jsonify({'res': classify(data)})
return data,'helloworld',data
# routes for user
@app.route('/')
def index():
# Reading example picture information
with open('static/images/examples/examples.txt', 'r') as f:
imgs = json.load(f)
return render_template('index.html', imgs=imgs)
@app.route('/upload/', methods=['GET', 'POST'])
def upload():
file = request.files['file']
if file and allowed_file(file.filename):
# caption='hello world hello world hello world hello.'
# raw,caption, attention = generate_caption(file.read())
# get saved folder
folder_name=datetime.now().strftime("%Y%m")
folder_name='static/uploads/' + folder_name + '/'
if not os.path.exists(folder_name):
os.makedirs(folder_name)
# get saved filename
filename = random_filename()
raw_path = folder_name+filename+'0.jpeg'
att_path = folder_name+filename+'1.jpeg'
# save images
file.save(raw_path)
image_np = load_image_into_numpy_array(raw_path)
if image_np is not None:
if FLAGS.mode =='ours':
box, feat = faster_rcnn.encode(image_np)
caption, attention = lstm.decode(feat)
lstm.show_attention(caption, attention,box, image_np, att_path)
else:
caption, attention = att_nic.decode(raw_path)
att_nic.show_attention(caption, attention, image_np, att_path)
# add record to sqlite
id = add_entry(caption['caption'], '/'+raw_path, '/'+att_path)
return jsonify({
"code": 0,
"msg": "succeed",
"url": url_for('result', id=id)
})
else:
return jsonify({
"code": 0,
"msg": "fail",
"url": url_for('index')
})
@app.route('/result/<id>')
def result(id):
db = get_db()
cur = db.execute('select id, caption, raw_path from records where id = '+id)
entries = cur.fetchall()
return render_template('result.html', r=entries[0])
@app.route('/rank/', methods=['GET', 'POST'])
def rank():
db = get_db()
db.execute("update records set rank = ?, updated_at = datetime(CURRENT_TIMESTAMP,'localtime') where id = ?",
[request.args.get('rank'), request.args.get('id')])
db.commit()
return jsonify({
"msg": "succeed",
})
# routes for admin
@app.route('/login/', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] != app.config['USERNAME']:
error = 'Invalid username'
elif request.form['password'] != app.config['PASSWORD']:
error = 'Invalid password'
else:
session['logged_in'] = True
return redirect(url_for('admin'))
return render_template('login.html', error=error)
@app.route('/logout')
def logout():
session.pop('logged_in', None)
return redirect(url_for('login'))
# home page
@app.route('/admin/')
def admin():
if not session.get('logged_in'):
return redirect(url_for('login'))
# return first page by default
db = get_db()
if request.args.get('rank'):
cur = db.execute('select id, caption, raw_path, att_path, rank, created_at, \
updated_at from records where deleted_at is null and rank = ? order by created_at desc',[request.args.get('rank')])
else:
cur = db.execute('select id, caption, raw_path, att_path, rank, created_at, \
updated_at from records where deleted_at is null order by created_at desc')
records = [dict((cur.description[idx][0], value)
for idx, value in enumerate(row)) for row in cur.fetchall()]
return render_template('admin.html', records=json.dumps(records),rank=request.args.get('rank',0))
if __name__ == '__main__':
init_db()
app.run(debug=False)