Repository navigation
Expand file tree
/
Copy pathapi_object_detection_script_same_dir.py
More file actions
334 lines (266 loc) · 13.1 KB
/
Copy pathapi_object_detection_script_same_dir.py
File metadata and controls
334 lines (266 loc) · 13.1 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
from flask import request, Flask
from flask_cors import CORS
app = Flask(__name__)
app.config['DEBUG'] = True
CORS(app)
# Setup configuration file
import configparser
config = configparser.ConfigParser()
config.read('api_config.ini')
# Setup logs
import logging
logging.basicConfig(filename=config['PATH']['LOGS'], level=logging.INFO,
format='%(asctime)s: %(levelname)s: %(message)s ')
logging.info('----------Started----------')
from elastic import update_doc, search_id
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
import time
from distutils.version import StrictVersion
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image
import cv2
from json2xml import readfromstring, json2xml
import json
import re
import xml.etree.ElementTree as xml
# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")
from object_detection.utils import ops as utils_ops
if StrictVersion(tf.__version__) < StrictVersion('1.12.0'):
raise ImportError('Please upgrade your TensorFlow installation to v1.12.*.')
# ## Object detection imports
# Here are the imports from the object detection module.
# In[3]:
from utils import label_map_util
from utils import visualization_utils as vis_util
# What model to download.
# MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17'
# MODEL_NAME = 'ssd_inception_v2_coco_2018_01_28'
# MODEL_NAME = 'faster_rcnn_nas_lowproposals_coco_2018_01_28'
MODEL_NAME = 'faster_rcnn_nas_coco_2018_01_28'
# MODEL_NAME = 'faster_rcnn_inception_resnet_v2_atrous_lowproposals_coco_2018_01_28'
# MODEL_NAME = 'faster_rcnn_resnet101_lowproposals_coco_2018_01_28'
MODEL_FILE = MODEL_NAME + '.tar.gz'
DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'
# Path to frozen detection graph. This is the actual model that is used for the object detection.
PATH_TO_FROZEN_GRAPH = MODEL_NAME + '/frozen_inference_graph.pb'
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')
# ## Download Model
# In[5]:
'''
logging.info('Downloading'+ MODEL_FILE +'Model...')
opener = urllib.request.URLopener()
opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
tar_file = tarfile.open(MODEL_FILE)
for file in tar_file.getmembers():
file_name = os.path.basename(file.name)
if 'frozen_inference_graph.pb' in file_name:
tar_file.extract(file, os.getcwd())
logging.info('Downloaded')
'''
# ## Helper code
# In[9]:
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)
# TEST_IMAGE_FOLDER = config['PATH']['TEST_IMAGE_FOLDER']
# IMAGES_FOLDER_NAME = os.path.basename(TEST_IMAGE_FOLDER)
# TEST_IMAGE_PATHS = next(os.walk(TEST_IMAGE_FOLDER))[2]
# TEST_IMAGE_PATHS.sort()
# TEST_IMAGE_PATHS
OUTPUT_DIR_XML = config['PATH']['OUTPUT_DIR_XML']
OUTPUT_DIR_JSON = config['PATH']['OUTPUT_DIR_JSON']
ANNOTATED_IMAGE_DIR = config['PATH']['ANNOTATED_IMAGES']
def run_inference_for_single_image(image, graph):
with graph.as_default():
with tf.Session() as sess:
# Get handles to input and output tensors
ops = tf.get_default_graph().get_operations()
all_tensor_names = {output.name for op in ops for output in op.outputs}
tensor_dict = {}
for key in [
'num_detections', 'detection_boxes', 'detection_scores',
'detection_classes', 'detection_masks'
]:
tensor_name = key + ':0'
if tensor_name in all_tensor_names:
tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
tensor_name)
if 'detection_masks' in tensor_dict:
# The following processing is only for single image
detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
# Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
detection_masks, detection_boxes, image.shape[1], image.shape[2])
detection_masks_reframed = tf.cast(
tf.greater(detection_masks_reframed, 0.5), tf.uint8)
# Follow the convention by adding back the batch dimension
tensor_dict['detection_masks'] = tf.expand_dims(
detection_masks_reframed, 0)
image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')
# Run inference
output_dict = sess.run(tensor_dict,
feed_dict={image_tensor: image})
# all outputs are float32 numpy arrays, so convert types as appropriate
output_dict['num_detections'] = int(output_dict['num_detections'][0])
output_dict['detection_classes'] = output_dict[
'detection_classes'][0].astype(np.int64)
output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
output_dict['detection_scores'] = output_dict['detection_scores'][0]
if 'detection_masks' in output_dict:
output_dict['detection_masks'] = output_dict['detection_masks'][0]
return output_dict
def save_json_xml(TEST_IMAGE_FOLDER, IMAGES_FOLDER_NAME, folder, category_index, image_path, output_dict):
img = cv2.imread(os.path.join(TEST_IMAGE_FOLDER, image_path))
image_height, image_width, depth = img.shape
out_dict = {'folder': IMAGES_FOLDER_NAME,
'filename': image_path,
'path': os.path.join(TEST_IMAGE_FOLDER, image_path),
'size': {'width': image_width, 'height': image_height, 'depth': depth}}
for index, score in enumerate(output_dict['detection_scores']):
if score < 0.3:
continue
label = category_index[output_dict['detection_classes'][index]]['name']
ymin, xmin, ymax, xmax = output_dict['detection_boxes'][index]
out_dict.update({'object' + str(index + 1): {'name': label,
'bndbox': {'xmin': int(xmin * image_width),
'ymin': int(ymin * image_height),
'xmax': int(xmax * image_width),
'ymax': int(ymax * image_height)}}
})
data = readfromstring(json.dumps(out_dict))
s = json2xml.Json2xml(data, wrapper="annotation").to_xml()
s = re.sub("object\d+", 'object', s)
fname = os.path.splitext(image_path)[0]
OUTPUT_FOLDER_XML = os.path.join(folder, OUTPUT_DIR_XML)
OUTPUT_FOLDER_JSON = os.path.join(folder, OUTPUT_DIR_JSON)
# create dir for storing xml
if not os.path.exists(OUTPUT_FOLDER_XML):
os.makedirs(OUTPUT_FOLDER_XML)
# create dir for storing json
if not os.path.exists(OUTPUT_FOLDER_JSON):
os.makedirs(OUTPUT_FOLDER_JSON)
with open(os.path.join(OUTPUT_FOLDER_XML, fname + '.xml'), 'w') as output:
output.write(s)
output.close()
with open(os.path.join(OUTPUT_FOLDER_JSON, fname + '.json'), 'w') as out:
out.write(str(output_dict))
out.close()
@app.route("/rosbag/image_annotate", endpoint="image_annotate", methods=['POST'])
def image_annotate():
req_data = request.data
req_data = req_data.decode("utf-8")
req_data = json.loads(req_data)
doc_id = req_data['doc_id']
# search for existing data
info_data = search_id(doc_id)
info_dict = info_data['hits'][0]
# remove doc_id from dictionary
info_dict.pop('doc_id')
TEST_IMAGE_FOLDER = req_data["image_dir"]
IMAGES_FOLDER_NAME = os.path.basename(TEST_IMAGE_FOLDER)
TEST_IMAGE_PATHS = next(os.walk(TEST_IMAGE_FOLDER))[2]
TEST_IMAGE_PATHS.sort()
# fetch bag_file_name
folder = os.path.splitext(info_dict['path'])[0]
# ## Load a (frozen) Tensorflow model into memory.
# In[6]:
logging.info('Load a (frozen) Tensorflow model into memory')
model_load_start = time.time()
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
model_load_end = time.time()
logging.info('Done')
# ## Loading label map
# Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`. Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine
# In[7]:
logging.info('Loading label map')
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
logging.info('Done')
# # Detection
object_detect_time = {}
for image_path in TEST_IMAGE_PATHS:
try:
logging.info('Loading image into memory')
image = Image.open(os.path.join(TEST_IMAGE_FOLDER, image_path))
logging.info('Loaded')
# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
image_np = load_image_into_numpy_array(image)
image.close()
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
logging.info('Expand dimensions since the model expects images to have shape: [1, None, None, 3]')
image_np_expanded = np.expand_dims(image_np, axis=0)
logging.info('Done')
# Actual detection.
logging.info('**********Actual Detection**********')
object_detect_start = time.time()
output_dict = run_inference_for_single_image(image_np_expanded, detection_graph)
object_detect_end = time.time()
object_detect_time[image_path] = str(round(object_detect_end - object_detect_start, 2)) + 's'
logging.info('**********Done**********')
# Create json and xml files
logging.info('Create json and xml files')
save_json_xml(TEST_IMAGE_FOLDER, IMAGES_FOLDER_NAME, folder, category_index, image_path, output_dict)
logging.info('Done')
# Visualization of the results of a detection.
logging.info('Visualization of the results of a detection')
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
output_dict['detection_boxes'],
output_dict['detection_classes'],
output_dict['detection_scores'],
category_index,
instance_masks=output_dict.get('detection_masks'),
use_normalized_coordinates=True,
line_thickness=5)
plt.figure()
ANNOTATED_IMAGE_FOLDER = os.path.join(folder, ANNOTATED_IMAGE_DIR)
# create dir for storing annotated images
if not os.path.exists(ANNOTATED_IMAGE_FOLDER):
os.makedirs(ANNOTATED_IMAGE_FOLDER)
plt.imsave(os.path.join(ANNOTATED_IMAGE_FOLDER, image_path), image_np)
logging.info('**********Saved Image**********')
except Exception as e:
logging.log(level=logging.ERROR, msg=str(e))
continue
logging.info('--------------------SUMMARY--------------------')
logging.info('Time taken to load model = ' + str(round(model_load_end - model_load_start, 2)) + 's')
logging.info('Total number of images = ' + str(len(TEST_IMAGE_PATHS)))
logging.info('Number of images successfully processed = ' + str(len(object_detect_time)))
logging.info('Number of images left unprocessed = ' + str(len(TEST_IMAGE_PATHS) - len(object_detect_time)))
logging.info('Time taken per image for object detection = ' + json.dumps(object_detect_time))
# logging.info(f'Time taken to load model = {(model_load_end - model_load_start):.2f}')
logging.info('----------Stopped----------')
result = {
"annotation": {
"annotated_images_dir": os.path.join(folder, ANNOTATED_IMAGE_DIR),
"logs_dir": config['PATH']['LOGS'],
"xml_dir": os.path.join(folder, OUTPUT_DIR_XML),
"json_dir": os.path.join(folder, OUTPUT_DIR_JSON)
}
}
info_dict.update(result)
response = update_doc(doc_id, info_dict)
return response
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8081)