in 1_prepare_data/docker/code/utils/tf_record_util.py [0:0]
def _create_tf_example(self, s3_image_path, annotations):
image_name = os.path.basename(s3_image_path)
image_path = f'{self.image_dir}/{image_name}'
im = Image.open(image_path)
# READ IMAGE FILE
with tf.io.gfile.GFile(image_path, 'rb') as fid:
encoded_jpg = fid.read()
encoded_jpg_io = io.BytesIO(encoded_jpg)
encoded_jpg_io.seek(0)
image = Image.open(encoded_jpg_io)
image_width, image_height = image.size
if image.format != 'JPEG':
image = image.convert('RGB')
xmins = []
ymins = []
xmaxs = []
ymaxs = []
classes = []
classes_text = []
for a in annotations:
x = a['left']
y = a['top']
width = a['width']
height = a['height']
class_id = a['class_id']
xmins.append(float(x) / image_width)
xmaxs.append(float(x + width) / image_width)
ymins.append(float(y) / image_height)
ymaxs.append(float(y + height) / image_height)
class_name = self.label_map[str(class_id)]
classes_text.append(class_name.encode('utf8'))
classes.append(class_id)
feature_dict = {
'image/height': dataset_util.int64_feature(image_height),
'image/width': dataset_util.int64_feature(image_width),
'image/filename': dataset_util.bytes_feature(bytes(image_name, 'utf-8')),
'image/source_id': dataset_util.bytes_feature(bytes(image_name.replace('.jpg', ''), 'utf-8')),
'image/encoded': dataset_util.bytes_feature(encoded_jpg),
'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')),
'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),
}
example = tf.train.Example(features=tf.train.Features(feature=feature_dict))
return example