-
Notifications
You must be signed in to change notification settings - Fork 54
/
data_conversion_udacity_sim.py
104 lines (78 loc) · 3.43 KB
/
data_conversion_udacity_sim.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
'''
Usage: python data_conversion_udacity_sim.py --output_path output_file_name.record
'''
import tensorflow as tf
import yaml
import os
from utils import dataset_util
flags = tf.app.flags
flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
FLAGS = flags.FLAGS
LABEL_DICT = {
"Green" : 1,
"Red" : 2,
"Yellow" : 3,
"off" : 4,
}
def create_tf_example(example):
# Udacity sim data set
height = 600 # Image height
width = 800 # Image width
filename = example['filename'] # Filename of the image. Empty if image is not from file
filename = filename.encode()
with tf.gfile.GFile(example['filename'], 'rb') as fid:
encoded_image = fid.read()
image_format = 'jpg'.encode()
xmins = [] # List of normalized left x coordinates in bounding box (1 per box)
xmaxs = [] # List of normalized right x coordinates in bounding box
# (1 per box)
ymins = [] # List of normalized top y coordinates in bounding box (1 per box)
ymaxs = [] # List of normalized bottom y coordinates in bounding box
# (1 per box)
classes_text = [] # List of string class name of bounding box (1 per box)
classes = [] # List of integer class id of bounding box (1 per box)
for box in example['annotations']:
#if box['occluded'] is False:
#print("adding box")
xmins.append(float(box['xmin'] / width))
xmaxs.append(float((box['xmin'] + box['x_width']) / width))
ymins.append(float(box['ymin'] / height))
ymaxs.append(float((box['ymin']+ box['y_height']) / height))
classes_text.append(box['class'].encode())
classes.append(int(LABEL_DICT[box['class']]))
tf_example = tf.train.Example(features=tf.train.Features(feature={
'image/height': dataset_util.int64_feature(height),
'image/width': dataset_util.int64_feature(width),
'image/filename': dataset_util.bytes_feature(filename),
'image/source_id': dataset_util.bytes_feature(filename),
'image/encoded': dataset_util.bytes_feature(encoded_image),
'image/format': dataset_util.bytes_feature(image_format),
'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
'image/object/class/label': dataset_util.int64_list_feature(classes),
}))
return tf_example
def main(_):
writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
# Udacity
INPUT_YAML = "data/sim_training_data/sim_data_annotations.yaml"
examples = yaml.load(open(INPUT_YAML, 'rb').read())
#examples = examples[:10] # for testing
len_examples = len(examples)
print("Loaded ", len(examples), "examples")
for i in range(len(examples)):
examples[i]['filename'] = os.path.abspath(os.path.join(os.path.dirname(INPUT_YAML), examples[i]['filename']))
counter = 0
for example in examples:
#print example
tf_example = create_tf_example(example)
writer.write(tf_example.SerializeToString())
if counter % 10 == 0:
print("Percent done", (counter/len_examples)*100)
counter += 1
writer.close()
if __name__ == '__main__':
tf.app.run()