You are on page 1of 1

# Define a function to create the TFRecord file for a single input image and

annotation file
# def create_tf_example(image_path, annotation_path):
# with tf.io.gfile.GFile(image_path, '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 = os.path.basename(image_path)
# image_id = os.path.splitext(filename)[0]

# with tf.io.gfile.GFile(annotation_path, 'rb') as fid:


# xml_str = fid.read()
# xml = ET.fromstring(xml_str)
# data = dataset_util.recursive_parse_xml_to_dict(xml)['annotation']

# xmins = []
# xmaxs = []
# ymins = []
# ymaxs = []
# classes_text = []
# classes = []

# for obj in data['object']:


# xmin = float(obj['bndbox']['xmin']) / width
# xmax = float(obj['bndbox']['xmax']) / width
# ymin = float(obj['bndbox']['ymin']) / height
# ymax = float(obj['bndbox']['ymax']) / height
# class_text = obj['name'].encode('utf8')
# class_id = classes.index(class_text)

# xmins.append(xmin)
# xmaxs.append(xmax)
# ymins.append(ymin)
# ymaxs.append(ymax)
# classes_text.append(class_text)
# classes.append(class_id)

# 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.encode('utf8')),
# 'image/source_id': dataset_util.bytes_feature(image_id.encode('utf8')),
# '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),
# }))
# return tf_examplez

You might also like