-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_to_tfrecord.py
88 lines (72 loc) · 3.14 KB
/
csv_to_tfrecord.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 15 16:15:36 2018
@author: GustavZ
"""
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
import os
import io
import pandas as pd
import tensorflow as tf
from PIL import Image
from object_detection.utils import dataset_util,label_map_util
from collections import namedtuple, OrderedDict
def split(df, group):
data = namedtuple('data', ['filename', 'object'])
gb = df.groupby(group)
return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)]
def create_tf_example(group, path, label_map_dict):
with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:
encoded_jpg = fid.read()
encoded_jpg_io = io.BytesIO(encoded_jpg)
image = Image.open(encoded_jpg_io)
width, height = image.size
filename = group.filename.encode('utf8')
image_format = b'jpg'
xmins = []
xmaxs = []
ymins = []
ymaxs = []
classes_text = []
classes = []
for index, obj in group.object.iterrows():
xmins.append(obj['xmin'] / width)
xmaxs.append(obj['xmax'] / width)
ymins.append(obj['ymin'] / height)
ymaxs.append(obj['ymax'] / height)
classes_text.append(obj['class'].encode('utf8'))
classes.append(label_map_dict[obj['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_jpg),
'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():
for directory in ['train','eval']:
image_path = os.path.join(os.getcwd(), 'data/{}/images/'.format(directory))
csv_path = os.path.join(os.getcwd(), 'data/{}_labels.csv'.format(directory,directory))
output_path = os.path.join(os.getcwd(), 'data/{}.record'.format(directory))
label_map_dict = label_map_util.get_label_map_dict(os.path.join(os.getcwd(), 'data/label_map.pbtxt'))
writer = tf.python_io.TFRecordWriter(output_path)
examples = pd.read_csv(csv_path)
grouped = split(examples, 'filename')
for group in grouped:
tf_example = create_tf_example(group, image_path, label_map_dict)
writer.write(tf_example.SerializeToString())
writer.close()
print('Successfully created the {}-TFRecords'.format(directory))
if __name__ == '__main__':
main()