For making a train set for TensorFlow Object Detection model training. I need to generate a tfrecord
. Here tfrecord
consists of TensorFlow examples where each example is a tf.train.Example
object. Following code shows how a TensorFlow example object is defined:
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),
}))
But the problem is that I have to pass each image as bytes_feature
. Here encoded_jpg
is the image read from file and it is in _io.BytesIO
format. It is not a big deal when I am reading image from disk. But as I am augmenting my image data set hence I am getting each of the augmented image as numpy.ndarray
. So either I have to cast this numpy.ndarray
image to _io.BytesIO
or I have to save the image first such that I can read it in _io.BytesIO
which makes no sense. So how I can easily convert the image to the desired format without requiring to save it to disk.