-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
283 lines (217 loc) · 9.85 KB
/
app.py
File metadata and controls
283 lines (217 loc) · 9.85 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
#encoding=utf-8
import os
import time
import sys
import base64
import math
import random
import urllib.request
import datetime
import logging
import optparse
import tornado.wsgi
import tornado.httpserver
import numpy as np
import pandas as pd
from flask import Flask, request, redirect, url_for,render_template
from werkzeug import secure_filename
from werkzeug import SharedDataMiddleware
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import tensorflow as tf
import cv2
from nets import ssd_vgg_300, ssd_common, np_methods
from preprocessing import ssd_vgg_preprocessing
import visualization
from datasets import pascalvoc_common
slim = tf.contrib.slim
base_dir = os.getcwd()
UPLOAD_FOLDER = base_dir + '/demo/upload'
DETECTED_FOLDER = base_dir + '/demo/detected'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['DETECTED_FOLDER'] = DETECTED_FOLDER
# Main image processing routine.
def process_image(img, params, select_threshold=0.5, nms_threshold=.45, net_shape=(300, 300)):
# Run SSD network
isess = params['isess']
image_4d = params['image_4d']
predictions = params['predictions']
localisations = params['localisations']
bbox_img = params['bbox_img']
img_input = params['img_input']
ssd_anchors = params['ssd_anchors']
rimg, rpredictions, rlocalisations, rbbox_img = isess.run([image_4d, predictions, localisations, bbox_img],
feed_dict={img_input: img})
# Get classes and bboxes from the net outputs.
rclasses, rscores, rbboxes = np_methods.ssd_bboxes_select(
rpredictions, rlocalisations, ssd_anchors,
select_threshold=select_threshold, img_shape=net_shape, num_classes=21, decode=True)
rbboxes = np_methods.bboxes_clip(rbbox_img, rbboxes)
rclasses, rscores, rbboxes = np_methods.bboxes_sort(rclasses, rscores, rbboxes, top_k=400)
rclasses, rscores, rbboxes = np_methods.bboxes_nms(rclasses, rscores, rbboxes, nms_threshold=nms_threshold)
# Resize bboxes to original image shape. Note: useless for Resize.WARP!
rbboxes = np_methods.bboxes_resize(rbbox_img, rbboxes)
return rclasses, rscores, rbboxes
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/')
def index():
return render_template('index.html', has_result=False)
@app.route('/classify_url', methods=['GET'])
def classify_url():
imageurl = request.args.get('imageurl', '')
try:
# download
user_Agent = 'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
header = {'User-Agent':user_Agent}
req = urllib.request.Request(imageurl,headers=header)
raw_data = urllib.request.urlopen(req).read()
filename = os.path.join(UPLOAD_FOLDER, 'tmp.jpg')
with open(filename,'wb') as f:
f.write(raw_data)
except Exception as err:
# For any exception we encounter in reading the image, we will just
# not continue.
logging.info('URL Image open error: %s', err)
return render_template(
'index.html', has_result=True,
result=(False, 'Cannot open image from URL.')
)
logging.info('Image: %s', imageurl)
results,new_img_base64 = app.clf.classify_image(filename)
return render_template(
'index.html', has_result=True, result=results, imagesrc=new_img_base64)
@app.route('/classify_upload', methods=['POST'])
def classify_upload():
try:
app.logger.addHandler(logging.StreamHandler(sys.stdout))
app.logger.setLevel(logging.DEBUG)
# We will save the file to disk for possible data collection.
imagefile = request.files['imagefile']
app.logger.debug(imagefile)
sys.stdout.write(imagefile.filename+'*****'+'\n')
if imagefile and allowed_file(imagefile.filename):
filename_ = str(datetime.datetime.now()).replace(' ', '_') + secure_filename(imagefile.filename)
sys.stdout.write('fkfkfkfk'+ filename_+ '\n')
filename = os.path.join(UPLOAD_FOLDER, filename_)
imagefile.save(filename)
except Exception as err:
#logging.info('Uploaded image open error: %s', err)
return render_template(
'index.html', has_result=True,
result=(False, 'Cannot open uploaded image.')
)
results,new_img_base64 = app.clf.classify_image(filename)
return render_template(
'index.html', has_result=True, result=results,
imagesrc=new_img_base64
)
def embed_image_html(fileps):
"""Creates an image embedded in HTML base64 format."""
with open(fileps, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
return 'data:image/jpg;base64,' + str(encoded_string)[2:-1]
def init_model(ckpt_path):
l_VOC_CLASS = [name for name, tup in pascalvoc_common.VOC_LABELS.items()]
# TensorFlow session: grow memory when needed. TF, DO NOT USE ALL MY GPU MEMORY!!!
gpu_options = tf.GPUOptions(allow_growth=True)
config = tf.ConfigProto(log_device_placement=False, gpu_options=gpu_options)
isess = tf.InteractiveSession(config=config)
# Input placeholder.
net_shape = (300, 300)
data_format = 'NHWC'
img_input = tf.placeholder(tf.uint8, shape=(None, None, 3))
# Evaluation pre-processing: resize to SSD net shape.
image_pre, labels_pre, bboxes_pre, bbox_img = ssd_vgg_preprocessing.preprocess_for_eval(img_input, None, None, net_shape, data_format, resize=ssd_vgg_preprocessing.Resize.WARP_RESIZE)
image_4d = tf.expand_dims(image_pre, 0)
# Define the SSD model.
reuse = True if 'ssd_net' in locals() else None
ssd_net = ssd_vgg_300.SSDNet()
with slim.arg_scope(ssd_net.arg_scope(data_format=data_format)):
predictions, localisations, _, _ = ssd_net.net(image_4d, is_training=False, reuse=reuse)
# Restore SSD model.
ckpt_filename = ckpt_path
# ckpt_filename = '../checkpoints/VGG_VOC0712_SSD_300x300_ft_iter_120000.ckpt'
isess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
saver.restore(isess, ckpt_filename)
# SSD default anchor boxes.
ssd_anchors = ssd_net.anchors(net_shape)
return {'l_VOC_CLASS':l_VOC_CLASS,'ssd_anchors':ssd_anchors,'img_input':img_input,'isess':isess,'image_4d':image_4d,'predictions':predictions,'localisations':localisations,'bbox_img':bbox_img}
class ImagenetClassifier(object):
# 预先加载模型
def __init__(self, l_VOC_CLASS, ssd_anchors, img_input, isess, image_4d, predictions, localisations, bbox_img):
logging.info('Loading net and associated files...')
self.l_VOC_CLASS = l_VOC_CLASS
self.ssd_anchors = ssd_anchors
self.img_input = img_input
self.isess = isess
self.image_4d = image_4d
self.predictions = predictions
self.localisations = localisations
self.bbox_img = bbox_img
def classify_image(self, filename):
try:
params = {'ssd_anchors':self.ssd_anchors,'img_input':self.img_input,'isess':self.isess,'image_4d':self.image_4d,'predictions':self.predictions,'localisations':self.localisations,'bbox_img':self.bbox_img}
# read image
img = mpimg.imread(filename)
starttime = time.time()
rclasses, rscores, rbboxes = process_image(img,params)
visualization.bboxes_draw_on_img(img, rclasses, rscores, rbboxes, visualization.colors_plasma)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
endtime = time.time()
bet_result = [(str(idx+1)+' : '+ self.l_VOC_CLASS[v], '%.5f' % rscores[idx]) for idx, v in enumerate(rclasses)]
# save image after draw box
fileout = str(datetime.datetime.now()).replace(' ', '_') + 'processed_img' + '.jpg'
fileps = os.path.join(DETECTED_FOLDER, fileout)
cv2.imwrite(fileps,img)
new_img_base64 = embed_image_html(fileps)
rtn = (True, (rclasses, rscores, rbboxes), bet_result, '%.3f' % (endtime - starttime))
return rtn,new_img_base64
except Exception as err:
logging.info('Classification error: %s', err)
return (False, 'Something went wrong when classifying the '
'image. Maybe try another one?')
def start_tornado(app, port=5000):
http_server = tornado.httpserver.HTTPServer(
tornado.wsgi.WSGIContainer(app))
http_server.listen(port)
print("Tornado server starting on port {}".format(port))
tornado.ioloop.IOLoop.instance().start()
def start_from_terminal(app):
"""
Parse command line options and start the server.
"""
parser = optparse.OptionParser()
parser.add_option(
'-d', '--debug',
help="enable debug mode",
action="store_true", default=False)
parser.add_option(
'-p', '--port',
help="which port to serve content on",
type='int', default=5000)
parser.add_option(
'-g', '--gpu',
help="use gpu mode",
action='store_true', default=True)
opts, args = parser.parse_args()
# ImagenetClassifier.default_args.update({'gpu_mode': opts.gpu})
# Initialize classifier + warm start by forward for allocation
ckpt_path = os.getcwd() + '/checkpoints/ssd_300_vgg.ckpt'
init_stateModel = init_model(ckpt_path)
app.clf = ImagenetClassifier(**init_stateModel)
if opts.debug:
app.run(debug=True, host='0.0.0.0', port=opts.port)
else:
start_tornado(app, opts.port)
if __name__ == '__main__':
logging.getLogger().setLevel(logging.INFO)
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
if not os.path.exists(DETECTED_FOLDER):
os.makedirs(DETECTED_FOLDER)
start_from_terminal(app)