質問編集履歴
1
ソースコードを追加しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -1 +1,159 @@
|
|
1
1
|
object detection APIを使って画像で出た物体の名前を画像と一緒に出したいと思っているのですが、どの変数をprint文で出力すればいいのかがわかりません。
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
In [0]:
|
6
|
+
|
7
|
+
# For the sake of simplicity we will use only 2 images:
|
8
|
+
|
9
|
+
# image1.jpg
|
10
|
+
|
11
|
+
# image2.jpg
|
12
|
+
|
13
|
+
# If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS.
|
14
|
+
|
15
|
+
PATH_TO_TEST_IMAGES_DIR = 'test_images'
|
16
|
+
|
17
|
+
TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 3) ]
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
# Size, in inches, of the output images.
|
22
|
+
|
23
|
+
IMAGE_SIZE = (12, 8)
|
24
|
+
|
25
|
+
In [0]:
|
26
|
+
|
27
|
+
def run_inference_for_single_image(image, graph):
|
28
|
+
|
29
|
+
with graph.as_default():
|
30
|
+
|
31
|
+
with tf.Session() as sess:
|
32
|
+
|
33
|
+
# Get handles to input and output tensors
|
34
|
+
|
35
|
+
ops = tf.get_default_graph().get_operations()
|
36
|
+
|
37
|
+
all_tensor_names = {output.name for op in ops for output in op.outputs}
|
38
|
+
|
39
|
+
tensor_dict = {}
|
40
|
+
|
41
|
+
for key in [
|
42
|
+
|
43
|
+
'num_detections', 'detection_boxes', 'detection_scores',
|
44
|
+
|
45
|
+
'detection_classes', 'detection_masks'
|
46
|
+
|
47
|
+
]:
|
48
|
+
|
49
|
+
tensor_name = key + ':0'
|
50
|
+
|
51
|
+
if tensor_name in all_tensor_names:
|
52
|
+
|
53
|
+
tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
|
54
|
+
|
55
|
+
tensor_name)
|
56
|
+
|
57
|
+
if 'detection_masks' in tensor_dict:
|
58
|
+
|
59
|
+
# The following processing is only for single image
|
60
|
+
|
61
|
+
detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
|
62
|
+
|
63
|
+
detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
|
64
|
+
|
65
|
+
# Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
|
66
|
+
|
67
|
+
real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
|
68
|
+
|
69
|
+
detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
|
70
|
+
|
71
|
+
detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
|
72
|
+
|
73
|
+
detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
|
74
|
+
|
75
|
+
detection_masks, detection_boxes, image.shape[0], image.shape[1])
|
76
|
+
|
77
|
+
detection_masks_reframed = tf.cast(
|
78
|
+
|
79
|
+
tf.greater(detection_masks_reframed, 0.5), tf.uint8)
|
80
|
+
|
81
|
+
# Follow the convention by adding back the batch dimension
|
82
|
+
|
83
|
+
tensor_dict['detection_masks'] = tf.expand_dims(
|
84
|
+
|
85
|
+
detection_masks_reframed, 0)
|
86
|
+
|
87
|
+
image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
# Run inference
|
92
|
+
|
93
|
+
output_dict = sess.run(tensor_dict,
|
94
|
+
|
95
|
+
feed_dict={image_tensor: np.expand_dims(image, 0)})
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
# all outputs are float32 numpy arrays, so convert types as appropriate
|
100
|
+
|
101
|
+
output_dict['num_detections'] = int(output_dict['num_detections'][0])
|
102
|
+
|
103
|
+
output_dict['detection_classes'] = output_dict[
|
104
|
+
|
105
|
+
'detection_classes'][0].astype(np.uint8)
|
106
|
+
|
107
|
+
output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
|
108
|
+
|
109
|
+
output_dict['detection_scores'] = output_dict['detection_scores'][0]
|
110
|
+
|
111
|
+
if 'detection_masks' in output_dict:
|
112
|
+
|
113
|
+
output_dict['detection_masks'] = output_dict['detection_masks'][0]
|
114
|
+
|
115
|
+
return output_dict
|
116
|
+
|
117
|
+
In [0]:
|
118
|
+
|
119
|
+
for image_path in TEST_IMAGE_PATHS:
|
120
|
+
|
121
|
+
image = Image.open(image_path)
|
122
|
+
|
123
|
+
# the array based representation of the image will be used later in order to prepare the
|
124
|
+
|
125
|
+
# result image with boxes and labels on it.
|
126
|
+
|
127
|
+
image_np = load_image_into_numpy_array(image)
|
128
|
+
|
129
|
+
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
|
130
|
+
|
131
|
+
image_np_expanded = np.expand_dims(image_np, axis=0)
|
132
|
+
|
133
|
+
# Actual detection.
|
134
|
+
|
135
|
+
output_dict = run_inference_for_single_image(image_np, detection_graph)
|
136
|
+
|
137
|
+
# Visualization of the results of a detection.
|
138
|
+
|
139
|
+
vis_util.visualize_boxes_and_labels_on_image_array(
|
140
|
+
|
141
|
+
image_np,
|
142
|
+
|
143
|
+
output_dict['detection_boxes'],
|
144
|
+
|
145
|
+
output_dict['detection_classes'],
|
146
|
+
|
147
|
+
output_dict['detection_scores'],
|
148
|
+
|
149
|
+
category_index,
|
150
|
+
|
151
|
+
instance_masks=output_dict.get('detection_masks'),
|
152
|
+
|
153
|
+
use_normalized_coordinates=True,
|
154
|
+
|
155
|
+
line_thickness=8)
|
156
|
+
|
157
|
+
plt.figure(figsize=IMAGE_SIZE)
|
158
|
+
|
159
|
+
plt.imshow(image_np)
|