-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
executable file
·143 lines (104 loc) · 4.31 KB
/
main.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
import json
import logging
import cStringIO
import urllib
import os
import sys
import numpy as np
import tensorflow as tf
from PIL import Image
from flask import Flask, render_template
from object_detection.utils import label_map_util
app = Flask(__name__)
sys.path.append("..")
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
MODEL_NAME = 'faster_rcnn_inception_v2_coco_2017_11_08'
MODEL_FILE = MODEL_NAME + '.tar.gz'
DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'
PATH_TO_LABELS = os.path.join('object_detection/data', 'mscoco_label_map.pbtxt')
NUM_CLASSES = 90
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES,
use_display_name=True)
category_index = label_map_util.create_category_index(categories)
def detect_alert(boxes, classes, scores, category_index, max_boxes_to_draw=20,
min_score_thresh=.5,
):
r = []
for i in range(min(max_boxes_to_draw, boxes.shape[0])):
if scores is None or scores[i] > min_score_thresh:
test1 = None
test2 = None
if category_index[classes[i]]['name']:
test1 = category_index[classes[i]]['name']
test2 = int(100 * scores[i])
line = {}
line[test1] = test2
r.append(line)
return r
def detect_objects(image_np, sess, detection_graph):
image_np_expanded = np.expand_dims(image_np, axis=0)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
# Actual detection.
(boxes, scores, classes, num_detections) = sess.run(
[boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
alert_array = detect_alert(np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores),
category_index)
return alert_array
def load_image_into_numpy_array(image):
(im_width, im_height) = image.size
return np.array(image.getdata()).reshape(
(im_height, im_width, 3)).astype(np.uint8)
def process_image(image):
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='')
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
alert_array = detect_objects(image, sess, detection_graph)
return alert_array
@app.route('/', methods=['GET'])
def tensorxraywelcome():
return render_template('welcome.html', **locals())
@app.route('/photo/<path:photo_url>', methods=['GET'])
def tensor_photo(photo_url):
url = photo_url
file = cStringIO.StringIO(urllib.urlopen(photo_url).read())
img = Image.open(file)
if img:
list_elements = process_image(img)
list = str(list_elements)
return render_template('index.html', **locals())
@app.route('/photobot/<path:photo_url>', methods=['GET'])
def tensor_photobot(photo_url):
file = cStringIO.StringIO(urllib.urlopen(photo_url).read())
img = Image.open(file)
if img:
list_elements = process_image(img)
return json.dumps(list_elements)
@app.route('/arloimage/<path:photo_url>', methods=['GET'])
def tensor_arloimage(photo_url):
file = cStringIO.StringIO(urllib.urlopen(photo_url).read())
img = Image.open(file)
if img:
list_elements = process_image(img)
return json.dumps(list_elements)
@app.errorhandler(500)
def server_error(e):
logging.exception('An error occurred during a request.')
return """
An internal error occurred: <pre>{}</pre>
See logs for full stacktrace.
""".format(e), 500
if __name__ == '__main__':
app.run(host='127.0.0.1', port=5000, debug=True)