Advertisement
Guest User

Untitled

a guest
May 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.34 KB | None | 0 0
  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('image_dir', '', 'Path to the image directory')
  17. flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
  18. FLAGS = flags.FLAGS
  19.  
  20. def class_text_to_int(row_label):
  21.     if row_label == 'nine':
  22.         return 1
  23.     elif row_label == 'ten':
  24.         return 2
  25.     elif row_label == 'jack':
  26.         return 3
  27.     elif row_label == 'queen':
  28.         return 4
  29.     elif row_label == 'king':
  30.         return 5
  31.     elif row_label == 'ace':
  32.         return 6
  33.     else:
  34.         None
  35.  
  36. def split(df, group):
  37.     data = namedtuple('data', ['filename', 'object'])
  38.     gb = df.groupby(group)
  39.     return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)]
  40.  
  41.  
  42. def create_tf_example(group, path):
  43.     with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:
  44.         encoded_jpg = fid.read()
  45.     encoded_jpg_io = io.BytesIO(encoded_jpg)
  46.     image = Image.open(encoded_jpg_io)
  47.     width, height = image.size
  48.  
  49.     filename = group.filename.encode('utf8')
  50.     image_format = b'jpg'
  51.     xmins = []
  52.     xmaxs = []
  53.     ymins = []
  54.     ymaxs = []
  55.     classes_text = []
  56.     classes = []
  57.  
  58.     for index, row in group.object.iterrows():
  59.         xmins.append(row['xmin'] / width)
  60.         xmaxs.append(row['xmax'] / width)
  61.         ymins.append(row['ymin'] / height)
  62.         ymaxs.append(row['ymax'] / height)
  63.         classes_text.append(row['class'].encode('utf8'))
  64.         classes.append(class_text_to_int(row['class']))
  65.  
  66.     tf_example = tf.train.Example(features=tf.train.Features(feature={
  67.         'image/height': dataset_util.int64_feature(height),
  68.         'image/width': dataset_util.int64_feature(width),
  69.         'image/filename': dataset_util.bytes_feature(filename),
  70.         'image/source_id': dataset_util.bytes_feature(filename),
  71.         'image/encoded': dataset_util.bytes_feature(encoded_jpg),
  72.         'image/format': dataset_util.bytes_feature(image_format),
  73.         'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
  74.         'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
  75.         'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
  76.         'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
  77.         'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
  78.         'image/object/class/label': dataset_util.int64_list_feature(classes),
  79.     }))
  80.     return tf_example
  81.  
  82.  
  83. def main(_):
  84.     writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
  85.     path = os.path.join(os.getcwd(), FLAGS.image_dir)
  86.     examples =
  87.  
  88. if __name__ == '__main__': pd.read_csv(FLAGS.csv_input)
  89.     grouped = split(examples, 'filename')
  90.     for group in grouped:
  91.         tf_example = create_tf_example(group, path)
  92.         writer.write(tf_example.SerializeToString())
  93.  
  94.     writer.close()
  95.     output_path = os.path.join(os.getcwd(), FLAGS.output_path)
  96.     print('Successfully created the TFRecords: {}'.format(output_path))
  97.  
  98.     tf.app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement