Skip to content

Commit 90fa5e1

Browse files
committed
initial commit
0 parents  commit 90fa5e1

File tree

305 files changed

+4693
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

305 files changed

+4693
-0
lines changed

generate_tfrecord.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
from __future__ import division
2+
from __future__ import print_function
3+
from __future__ import absolute_import
4+
5+
import os
6+
import io
7+
import pandas as pd
8+
import tensorflow as tf
9+
10+
from PIL import Image
11+
from object_detection.utils import dataset_util
12+
from collections import namedtuple, OrderedDict
13+
14+
flags = tf.app.flags
15+
flags.DEFINE_string('csv_input', '', 'Path to the CSV input')
16+
flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
17+
flags.DEFINE_string('image_dir', '', 'Path to images')
18+
FLAGS = flags.FLAGS
19+
20+
21+
def class_text_to_int(row_label):
22+
if row_label == 'Raspberry_Pi_3':
23+
return 1
24+
elif row_label == 'Arduino_Nano':
25+
return 2
26+
elif row_label == 'ESP8266':
27+
return 3
28+
elif row_label == 'Heltec_ESP32_Lora':
29+
return 4
30+
else:
31+
return None
32+
33+
34+
def split(df, group):
35+
data = namedtuple('data', ['filename', 'object'])
36+
gb = df.groupby(group)
37+
return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)]
38+
39+
40+
def create_tf_example(group, path):
41+
with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:
42+
encoded_jpg = fid.read()
43+
encoded_jpg_io = io.BytesIO(encoded_jpg)
44+
image = Image.open(encoded_jpg_io)
45+
width, height = image.size
46+
47+
filename = group.filename.encode('utf8')
48+
image_format = b'jpg'
49+
xmins = []
50+
xmaxs = []
51+
ymins = []
52+
ymaxs = []
53+
classes_text = []
54+
classes = []
55+
56+
for index, row in group.object.iterrows():
57+
xmins.append(row['xmin'] / width)
58+
xmaxs.append(row['xmax'] / width)
59+
ymins.append(row['ymin'] / height)
60+
ymaxs.append(row['ymax'] / height)
61+
classes_text.append(row['class'].encode('utf8'))
62+
classes.append(class_text_to_int(row['class']))
63+
64+
tf_example = tf.train.Example(features=tf.train.Features(feature={
65+
'image/height': dataset_util.int64_feature(height),
66+
'image/width': dataset_util.int64_feature(width),
67+
'image/filename': dataset_util.bytes_feature(filename),
68+
'image/source_id': dataset_util.bytes_feature(filename),
69+
'image/encoded': dataset_util.bytes_feature(encoded_jpg),
70+
'image/format': dataset_util.bytes_feature(image_format),
71+
'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
72+
'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
73+
'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
74+
'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
75+
'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
76+
'image/object/class/label': dataset_util.int64_list_feature(classes),
77+
}))
78+
return tf_example
79+
80+
81+
def main(_):
82+
writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
83+
path = os.path.join(FLAGS.image_dir)
84+
examples = pd.read_csv(FLAGS.csv_input)
85+
grouped = split(examples, 'filename')
86+
for group in grouped:
87+
tf_example = create_tf_example(group, path)
88+
writer.write(tf_example.SerializeToString())
89+
90+
writer.close()
91+
output_path = os.path.join(os.getcwd(), FLAGS.output_path)
92+
print('Successfully created the TFRecords: {}'.format(output_path))
93+
94+
95+
if __name__ == '__main__':
96+
tf.app.run()

images/test/IMG_20181228_102636.jpg

67.3 KB
Loading

images/test/IMG_20181228_102636.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<annotation>
2+
<folder>microcontroller object detection smaller</folder>
3+
<filename>IMG_20181228_102636.jpg</filename>
4+
<path>C:\Users\Gilbert\Desktop\Local_Programming\Datasets\Own\microcontroller object detection smaller\IMG_20181228_102636.jpg</path>
5+
<source>
6+
<database>Unknown</database>
7+
</source>
8+
<size>
9+
<width>800</width>
10+
<height>600</height>
11+
<depth>3</depth>
12+
</size>
13+
<segmented>0</segmented>
14+
<object>
15+
<name>ESP8266</name>
16+
<pose>Unspecified</pose>
17+
<truncated>0</truncated>
18+
<difficult>0</difficult>
19+
<bndbox>
20+
<xmin>316</xmin>
21+
<ymin>205</ymin>
22+
<xmax>465</xmax>
23+
<ymax>313</ymax>
24+
</bndbox>
25+
</object>
26+
</annotation>

images/test/IMG_20181228_102641.jpg

65.4 KB
Loading

images/test/IMG_20181228_102641.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<annotation>
2+
<folder>microcontroller object detection smaller</folder>
3+
<filename>IMG_20181228_102641.jpg</filename>
4+
<path>C:\Users\Gilbert\Desktop\Local_Programming\Datasets\Own\microcontroller object detection smaller\IMG_20181228_102641.jpg</path>
5+
<source>
6+
<database>Unknown</database>
7+
</source>
8+
<size>
9+
<width>800</width>
10+
<height>600</height>
11+
<depth>3</depth>
12+
</size>
13+
<segmented>0</segmented>
14+
<object>
15+
<name>Heltec_ESP32_Lora</name>
16+
<pose>Unspecified</pose>
17+
<truncated>0</truncated>
18+
<difficult>0</difficult>
19+
<bndbox>
20+
<xmin>401</xmin>
21+
<ymin>251</ymin>
22+
<xmax>553</xmax>
23+
<ymax>405</ymax>
24+
</bndbox>
25+
</object>
26+
</annotation>

images/test/IMG_20181228_102658.jpg

62.5 KB
Loading

images/test/IMG_20181228_102658.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<annotation>
2+
<folder>microcontroller object detection smaller</folder>
3+
<filename>IMG_20181228_102658.jpg</filename>
4+
<path>C:\Users\Gilbert\Desktop\Local_Programming\Datasets\Own\microcontroller object detection smaller\IMG_20181228_102658.jpg</path>
5+
<source>
6+
<database>Unknown</database>
7+
</source>
8+
<size>
9+
<width>800</width>
10+
<height>600</height>
11+
<depth>3</depth>
12+
</size>
13+
<segmented>0</segmented>
14+
<object>
15+
<name>Arduino_Nano</name>
16+
<pose>Unspecified</pose>
17+
<truncated>0</truncated>
18+
<difficult>0</difficult>
19+
<bndbox>
20+
<xmin>184</xmin>
21+
<ymin>130</ymin>
22+
<xmax>618</xmax>
23+
<ymax>330</ymax>
24+
</bndbox>
25+
</object>
26+
</annotation>

images/test/IMG_20181228_102706.jpg

55.6 KB
Loading

images/test/IMG_20181228_102706.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<annotation>
2+
<folder>microcontroller object detection smaller</folder>
3+
<filename>IMG_20181228_102706.jpg</filename>
4+
<path>C:\Users\Gilbert\Desktop\Local_Programming\Datasets\Own\microcontroller object detection smaller\IMG_20181228_102706.jpg</path>
5+
<source>
6+
<database>Unknown</database>
7+
</source>
8+
<size>
9+
<width>800</width>
10+
<height>600</height>
11+
<depth>3</depth>
12+
</size>
13+
<segmented>0</segmented>
14+
<object>
15+
<name>Raspberry_Pi_3</name>
16+
<pose>Unspecified</pose>
17+
<truncated>0</truncated>
18+
<difficult>0</difficult>
19+
<bndbox>
20+
<xmin>233</xmin>
21+
<ymin>137</ymin>
22+
<xmax>640</xmax>
23+
<ymax>472</ymax>
24+
</bndbox>
25+
</object>
26+
</annotation>

images/test/IMG_20181228_102745.jpg

41 KB
Loading

0 commit comments

Comments
 (0)