解析lxml文件和图片并且将其数据信息存进tfrecord类型的文件中
最近学习了如何解析lxml文件和图片信息,最后将其数据存进tfrecoed类型文件中去。
代码如下:要说的全部都在注释之中
#库的导入
import tensorflow as tf
from lxml import etree
import os
def lxml_analyse(filepath):
#获取解析器
parser = etree.XMLParser()
#获取根节点
root = etree.parse(filepath, parser).getroot()
names = []
xmins = []
xmaxs = []
ymins = []
ymaxs = []
#遍历根节点中的object
for i in root.findall("object"):
#类型转换
name = bytes(i.xpath('.//name')[0].text, encoding='utf8')
xmin = float(i.xpath('.//xmin')[0].text)
xmax = float(i.xpath('.//xmax')[0].text)
ymin = float(i.xpath('.//ymin')[0].text)
ymax = float(i.xpath('.//ymax')[0].text)
#将name,xmin,xmax.ymin,ymax保存到对应列表中去
names.append(name)
xmaxs.append(xmax)
xmins.append(xmin)
ymaxs.append(ymax)
ymins.append(ymin)
return names, xmins, xmaxs, ymins, ymaxs
#解析图片,返回图片二进制编码,长,宽
def image_analyse(image_path):
with open(image_path, 'rb') as fp:
#获取图片二进制信息
output = fp.read()
#tf.io.read_file(path)读取指定路径的数据,相当于python的open()函数,读取完毕之后,要配合tf.image.decode_image()函数对图片数据解码
img = tf.io.read_file(image_path)
img = tf.image.decode_jpeg(img)
return output, img.shape[0], img.shape[1]
#将其全部转换成列表方便
#example=tf.train.Example(features=tf.train.Features(feature={"image":tf.train.Feature(int64_list=Int64List(value=[img]),})
# 中的value传入的是列表格式
def int_64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def int_64_list_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=value))
def float_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
def float_list_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=value))
def bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def bytes_list_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=value))
#使得传入的列表中的数据归一化
def regularization(lenth, data):
for i in range(len(data)):
if(data[i]/lenth > 1.0):
data[i] = 1.0
else:
data[i] = data[i]/lenth
return data
if __name__ == '__main__':
all_xlml_location = "./lxml"
all_img_location = "./img"
all_xlml_name = os.listdir(all_xlml_location)
all_img_name = os.listdir(all_img_location)
num = 1
for xlml_location,img_location in zip(all_xlml_name, all_img_name):
# print(xlml_location,img_location)
# 得到正确的路径
xlml_location = './'+all_xlml_location+'/'+xlml_location
img_location = './'+all_img_location+'/'+img_location
#一一对应函数的return
names, xmins, xmaxs, ymins, ymaxs = lxml_analyse(xlml_location)
output,hight,width = image_analyse(img_location)
#归一化
xmaxs = regularization(width, xmaxs)
xmins = regularization(width, xmins)
ymins = regularization(hight, xmins)
ymaxs = regularization(hight, ymaxs)
# print(names,xmins,ymaxs,xmaxs,ymins)
# print(output,hight,width)
#用tf.train.Example(features=tf.train.Features(feature=
# {
# "image":tf.train.Feature(int64_list=Int64List(value=[img]),可以传多个}))生成example
example = tf.train.Example(features = tf.train.Features(
feature={
'image/object/bbox/xmins':float_list_feature(xmins),
'image/object/bbox/xmaxs':float_list_feature(xmaxs),
'image/object/bbox/ymins':float_list_feature(ymins),
'image/object/bbox/ymaxs':float_list_feature(ymaxs),
'image/class/text':bytes_list_feature(names),
'image/encoded_image_data':bytes_feature(output)
}))
#tfrecord文件保存路径
tfrecord_path = './'+str(num)+'tf_data.record'
print(tfrecord_path)
# 用writer = tf.io.TFRecordWriter(tf文件地址)
# writer.write(example.SerializeToString())保存tfrecord类型文件
writer = tf.io.TFRecordWriter(tfrecord_path)
writer.write(example.SerializeToString())
num +=1
如果有错误希望各位指正。