-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_to_tfrecord.py
110 lines (83 loc) · 3.34 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import tensorflow.compat.v1 as tf
import numpy as np
import base64
import csv
import os
from PIL import Image
import io
def int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def int64_list_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=value))
def bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def bytes_list_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=value))
def float_list_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=value))
class_dict = {
1: b'cup', # List of class map Text with byte
2: b'rutensil'
}
def create_tf_example(img_name, img_path, labels):
with tf.gfile.GFile(img_path, 'rb') as fid:
encoded_jpg = fid.read()
image_format = str.encode(img_name.split('.')[1])
xmins = []
xmaxs = []
ymins = []
ymaxs = []
classes_text = []
classes = []
for label in labels:
xmins.append(float(int(label[0][4]) / int(label[0][1])))
xmaxs.append(float(int(label[0][6]) / int(label[0][1])))
ymins.append(float(int(label[0][5]) / int(label[0][2])))
ymaxs.append(float(int(label[0][7]) / int(label[0][2])))
classes_text.append(label[0][3])
class_ = 0
if (label[0][3] == "cup"):
class_ = 1
elif (label[0][3] == "rutensil"):
class_ = 2
classes.append(class_)
tf_example = tf.train.Example(features=tf.train.Features(feature={
'image/height': int64_feature(int(labels[0][0][2])),
'image/width': int64_feature(int(labels[0][0][1])),
'image/filename': bytes_feature(str.encode(labels[0][0][0])),
'image/source_id': bytes_feature(labels[0][0][0]),
'image/encoded': bytes_feature(encoded_jpg),
'image/format': bytes_feature(image_format),
'image/object/bbox/xmin': float_list_feature(xmins),
'image/object/bbox/xmax': float_list_feature(xmaxs),
'image/object/bbox/ymin': float_list_feature(ymins),
'image/object/bbox/ymax': float_list_feature(ymaxs),
'image/object/class/text': bytes_list_feature(classes_text),
'image/object/class/label': int64_list_feature(classes),
}))
return tf_example
def checkHowManyAhead(csv_reader, i, relative):
try:
if csv_reader[i + relative][0][0] == csv_reader[i + relative + 1][0][0]:
return checkHowManyAhead(csv_reader, i, relative + 1)
else:
return relative
except IndexError:
print ("End of list")
return 0
def main():
writer = tf.python_io.TFRecordWriter('test.tfrecords')
data_path = 'train_valid/train'
images = os.listdir(data_path)
label_csv = 'train_valid/train_labels.csv'
csvee_reader = csv.reader(open(label_csv, 'r'))
csv_reader = zip(csvee_reader)
for i in range(0, len(images)):
labels = []
ahead = checkHowManyAhead(csv_reader, i, 0)
for j in range(0, ahead + 1):
labels.append(csv_reader[i + j])
tf_example = create_tf_example(images[i], data_path + "/" + images[i], labels)
writer.write(tf_example.SerializeToString())
writer.close()
main()