-
Notifications
You must be signed in to change notification settings - Fork 193
/
detector.py
166 lines (131 loc) · 6.78 KB
/
detector.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
'''
Implement and test car detection (localization)
'''
import numpy as np
import tensorflow as tf
from PIL import Image
import os
from matplotlib import pyplot as plt
import time
from glob import glob
cwd = os.path.dirname(os.path.realpath(__file__))
# Uncomment the following two lines if need to use the Tensorflow visualization_unitls
#os.chdir(cwd+'/models')
#from object_detection.utils import visualization_utils as vis_util
class CarDetector(object):
def __init__(self):
self.car_boxes = []
os.chdir(cwd)
#Tensorflow localization/detection model
# Single-shot-dectection with mobile net architecture trained on COCO dataset
detect_model_name = 'ssd_mobilenet_v1_coco_11_06_2017'
PATH_TO_CKPT = detect_model_name + '/frozen_inference_graph.pb'
# setup tensorflow graph
self.detection_graph = tf.Graph()
# configuration for possible GPU use
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
# load frozen tensorflow detection model and initialize
# the tensorflow graph
with self.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='')
self.sess = tf.Session(graph=self.detection_graph, config=config)
self.image_tensor = self.detection_graph.get_tensor_by_name('image_tensor:0')
# Each box represents a part of the image where a particular object was detected.
self.boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0')
# Each score represent how level of confidence for each of the objects.
# Score is shown on the result image, together with the class label.
self.scores =self.detection_graph.get_tensor_by_name('detection_scores:0')
self.classes = self.detection_graph.get_tensor_by_name('detection_classes:0')
self.num_detections =self.detection_graph.get_tensor_by_name('num_detections:0')
# Helper function to convert image into numpy array
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)
# Helper function to convert normalized box coordinates to pixels
def box_normal_to_pixel(self, box, dim):
height, width = dim[0], dim[1]
box_pixel = [int(box[0]*height), int(box[1]*width), int(box[2]*height), int(box[3]*width)]
return np.array(box_pixel)
def get_localization(self, image, visual=False):
"""Determines the locations of the cars in the image
Args:
image: camera image
Returns:
list of bounding boxes: coordinates [y_up, x_left, y_down, x_right]
"""
category_index={1: {'id': 1, 'name': u'person'},
2: {'id': 2, 'name': u'bicycle'},
3: {'id': 3, 'name': u'car'},
4: {'id': 4, 'name': u'motorcycle'},
5: {'id': 5, 'name': u'airplane'},
6: {'id': 6, 'name': u'bus'},
7: {'id': 7, 'name': u'train'},
8: {'id': 8, 'name': u'truck'},
9: {'id': 9, 'name': u'boat'},
10: {'id': 10, 'name': u'traffic light'},
11: {'id': 11, 'name': u'fire hydrant'},
13: {'id': 13, 'name': u'stop sign'},
14: {'id': 14, 'name': u'parking meter'}}
with self.detection_graph.as_default():
image_expanded = np.expand_dims(image, axis=0)
(boxes, scores, classes, num_detections) = self.sess.run(
[self.boxes, self.scores, self.classes, self.num_detections],
feed_dict={self.image_tensor: image_expanded})
if visual == True:
vis_util.visualize_boxes_and_labels_on_image_array(
image,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,min_score_thresh=.4,
line_thickness=3)
plt.figure(figsize=(9,6))
plt.imshow(image)
plt.show()
boxes=np.squeeze(boxes)
classes =np.squeeze(classes)
scores = np.squeeze(scores)
cls = classes.tolist()
# The ID for car in COCO data set is 3
idx_vec = [i for i, v in enumerate(cls) if ((v==3) and (scores[i]>0.3))]
if len(idx_vec) ==0:
print('no detection!')
self.car_boxes = []
else:
tmp_car_boxes=[]
for idx in idx_vec:
dim = image.shape[0:2]
box = self.box_normal_to_pixel(boxes[idx], dim)
box_h = box[2] - box[0]
box_w = box[3] - box[1]
ratio = box_h/(box_w + 0.01)
if ((ratio < 0.8) and (box_h>20) and (box_w>20)):
tmp_car_boxes.append(box)
print(box, ', confidence: ', scores[idx], 'ratio:', ratio)
else:
print('wrong ratio or wrong size, ', box, ', confidence: ', scores[idx], 'ratio:', ratio)
self.car_boxes = tmp_car_boxes
return self.car_boxes
if __name__ == '__main__':
# Test the performance of the detector
det =CarDetector()
os.chdir(cwd)
TEST_IMAGE_PATHS= glob(os.path.join('test_images/', '*.jpg'))
for i, image_path in enumerate(TEST_IMAGE_PATHS[0:2]):
print('')
print('*************************************************')
img_full = Image.open(image_path)
img_full_np = det.load_image_into_numpy_array(img_full)
img_full_np_copy = np.copy(img_full_np)
start = time.time()
b = det.get_localization(img_full_np, visual=False)
end = time.time()
print('Localization time: ', end-start)
#