Commit a6878408 authored by Mia Fornes's avatar Mia Fornes
Browse files

Merge branch 'master' of gitlab.stud.idi.ntnu.no:asbjorfk/vehicle-parts-detection

parents c62975c8 9dc95411
item {
id: 1
name: 'car'
}
\ No newline at end of file
item {
id: 1
name: 'car'
}
item {
id: 2
name: 'headlights'
}
item {
id: 3
name: 'carroof'
}
item {
id: 4
name: 'carsign'
}
item {
id: 5
name: 'windshield'
}
item {
id: 6
name: 'carrear'
}
item {
id: 7
name: 'trailer'
}
item {
id: 8
name: 'trailerbody'
}
item {
id: 9
name: 'cartrailer'
}
\ No newline at end of file
"""
Usage:
# From tensorflow/models/
# Create train data:
python generate_tfrecord.py --csv_input=data/train_labels.csv --output_path=train.record
# Create test data:
python generate_tfrecord.py --csv_input=data/test_labels.csv --output_path=test.record
"""
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
import tensorflow.compat.v1 as tf
from PIL import Image
import sys
sys.path.append('../')
from object_detection.utils import dataset_util
from collections import namedtuple, OrderedDict
# TO-DO replace this with label map
def class_text_to_int(row_label):
if row_label == 'car':
return 1
if row_label == 'headlights':
return 2
if row_label == 'carroof':
return 3
if row_label == 'carsign':
return 4
if row_label == 'windshield':
return 5
if row_label == 'carrear':
return 6
if row_label == 'trailer':
return 7
if row_label == 'trailerbody':
return 8
if row_label == 'cartrailer':
return 9
else:
return 0
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):
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, 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_text_to_int(row['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(_):
writer = tf.python_io.TFRecordWriter('data/train.record')
path = os.path.join('video-frames')
examples = pd.read_csv('data/train_labels.csv')
grouped = split(examples, 'filename')
for group in grouped:
tf_example = create_tf_example(group, path)
writer.write(tf_example.SerializeToString())
writer.close()
output_path = os.path.join(os.getcwd(), 'data/train.record')
print('Successfully created the TFRecords: {}'.format(output_path))
if __name__ == '__main__':
tf.app.run()
\ No newline at end of file
......@@ -3,7 +3,6 @@ import glob
import pandas as pd
import xml.etree.ElementTree as ET
def xml_to_csv(path):
xml_list = []
for xml_file in glob.glob(path + '/*.xml'):
......@@ -34,7 +33,7 @@ def main():
xml_df.to_csv('data/{}_labels.csv'.format(directory), index=None)
print('Successfully converted xml to csv.')
'''
image_path = os.path.join(os.getcwd(), 'video-frames')
image_path = os.path.join(os.getcwd())
xml_df = xml_to_csv(image_path)
xml_df.to_csv('data/train_labels.csv', index=None)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment