-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathapp.py
executable file
·196 lines (148 loc) · 5.85 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import base64
import cStringIO
import sys
import tempfile
MODEL_BASE = '/opt/models/research'
sys.path.append(MODEL_BASE)
sys.path.append(MODEL_BASE + '/object_detection')
sys.path.append(MODEL_BASE + '/slim')
from decorator import requires_auth
from flask import Flask
from flask import redirect
from flask import render_template
from flask import request
from flask import url_for
from flask_wtf.file import FileField
import numpy as np
from PIL import Image
from PIL import ImageDraw
import tensorflow as tf
from utils import label_map_util
from werkzeug.datastructures import CombinedMultiDict
from wtforms import Form
from wtforms import ValidationError
app = Flask(__name__)
@app.before_request
@requires_auth
def before_request():
pass
PATH_TO_CKPT = '/opt/graph_def/frozen_inference_graph.pb'
PATH_TO_LABELS = MODEL_BASE + '/object_detection/data/mscoco_label_map.pbtxt'
content_types = {'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
'png': 'image/png'}
extensions = sorted(content_types.keys())
def is_image():
def _is_image(form, field):
if not field.data:
raise ValidationError()
elif field.data.filename.split('.')[-1].lower() not in extensions:
raise ValidationError()
return _is_image
class PhotoForm(Form):
input_photo = FileField(
'File extension should be: %s (case-insensitive)' % ', '.join(extensions),
validators=[is_image()])
class ObjectDetector(object):
def __init__(self):
self.detection_graph = self._build_graph()
self.sess = tf.Session(graph=self.detection_graph)
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(
label_map, max_num_classes=90, use_display_name=True)
self.category_index = label_map_util.create_category_index(categories)
def _build_graph(self):
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
return detection_graph
def _load_image_into_numpy_array(self, image):
(im_width, im_height) = image.size
return np.array(image.getdata()).reshape(
(im_height, im_width, 3)).astype(np.uint8)
def detect(self, image):
image_np = self._load_image_into_numpy_array(image)
image_np_expanded = np.expand_dims(image_np, axis=0)
graph = self.detection_graph
image_tensor = graph.get_tensor_by_name('image_tensor:0')
boxes = graph.get_tensor_by_name('detection_boxes:0')
scores = graph.get_tensor_by_name('detection_scores:0')
classes = graph.get_tensor_by_name('detection_classes:0')
num_detections = graph.get_tensor_by_name('num_detections:0')
(boxes, scores, classes, num_detections) = self.sess.run(
[boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
boxes, scores, classes, num_detections = map(
np.squeeze, [boxes, scores, classes, num_detections])
return boxes, scores, classes.astype(int), num_detections
def draw_bounding_box_on_image(image, box, color='red', thickness=4):
draw = ImageDraw.Draw(image)
im_width, im_height = image.size
ymin, xmin, ymax, xmax = box
(left, right, top, bottom) = (xmin * im_width, xmax * im_width,
ymin * im_height, ymax * im_height)
draw.line([(left, top), (left, bottom), (right, bottom),
(right, top), (left, top)], width=thickness, fill=color)
def encode_image(image):
image_buffer = cStringIO.StringIO()
image.save(image_buffer, format='PNG')
imgstr = 'data:image/png;base64,{:s}'.format(
base64.b64encode(image_buffer.getvalue()))
return imgstr
def detect_objects(image_path):
image = Image.open(image_path).convert('RGB')
boxes, scores, classes, num_detections = client.detect(image)
image.thumbnail((480, 480), Image.ANTIALIAS)
new_images = {}
for i in range(num_detections):
if scores[i] < 0.7: continue
cls = classes[i]
if cls not in new_images.keys():
new_images[cls] = image.copy()
draw_bounding_box_on_image(new_images[cls], boxes[i],
thickness=int(scores[i]*10)-4)
result = {}
result['original'] = encode_image(image.copy())
for cls, new_image in new_images.iteritems():
category = client.category_index[cls]['name']
result[category] = encode_image(new_image)
return result
@app.route('/')
def upload():
photo_form = PhotoForm(request.form)
return render_template('upload.html', photo_form=photo_form, result={})
@app.route('/post', methods=['GET', 'POST'])
def post():
form = PhotoForm(CombinedMultiDict((request.files, request.form)))
if request.method == 'POST' and form.validate():
with tempfile.NamedTemporaryFile() as temp:
form.input_photo.data.save(temp)
temp.flush()
result = detect_objects(temp.name)
photo_form = PhotoForm(request.form)
return render_template('upload.html',
photo_form=photo_form, result=result)
else:
return redirect(url_for('upload'))
client = ObjectDetector()
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80, debug=False)