-
Notifications
You must be signed in to change notification settings - Fork 10
/
generate_tfrecord.py
107 lines (83 loc) · 3.38 KB
/
generate_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
import os
import io
import pandas as pd
import tensorflow as tf
from PIL import Image
import dataset_util
from collections import namedtuple , OrderedDict
def class_img_dict(path):
# Getting the class as a list
class_list = os.listdir(path)
class_dict ={}
for i in range(0,len(class_list)):
class_dict[class_list[i]] = i+1
return class_dict
def split(df,group):
# Spliting the object from the files
data = namedtuple('data',['filename','label','object'])
gb = df.groupby(group)
li=[]
for key, x in zip(gb.groups.keys(), gb.groups):
d = data(key[0],key[1],gb.get_group(x))
li.append(d)
return li
def create_tf_example(group, path):
# Class numeric labels as dict
class_dict=class_img_dict(path)
#Opening and readinf the files
with tf.gfile.GFile(os.path.join(path,'{}/{}'.format(group.label,group.filename)),'rb') as fid:
encoded_jpg = fid.read()
# Encode the image in jpeg format to array values
encoded_jpg_io= io.BytesIO(encoded_jpg)
image = Image.open(encoded_jpg_io)
# Setting up the image size
width , height = image.size
#Creating the boundary box coordinate instances such as xmin,ymin,xmax,ymax
filename = group.filename.encode('utf8')
image_format = b'jpg'
xmins = []
xmaxs = []
ymins = []
ymaxs = []
classes_text = []
classes = []
for index, row in group.object.iterrows():
xmins.append(row['xmin'] / width)
xmaxs.append(row['xmax'] /width)
ymins.append(row['ymin'] / height)
ymaxs.append(row['ymax'] / height)
classes_text.append(row['class'].encode('utf8'))
classes.append(class_dict[row['class']])
# This is already exisiting code to convert csv to tfrecord
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 generate_tf(csv_name,tf_name,img_dir):
#Creating a TFRecordWriter
writer = tf.python_io.TFRecordWriter(tf_name)
# selecting the path to the image folder
path = os.path.join(os.getcwd(),'images')
# Reading the csv from the data folder
examples = pd.read_csv(csv_name)
grouped = split(examples, ['filename','class'])
for group in grouped:
tf_example = create_tf_example(group,path)
writer.write(tf_example.SerializeToString())
writer.close()
# After the conversion display the message
output_path = os.path.join(os.getcwd(),tf_name)
print('Successfully created the tfrecord for the images: {}'.format(output_path))
if __name__ == '__main__':
tf.app.run()