Tensorflow 读取XML文件内容并对图片等比例缩放
Posted ʚVVcatɞ
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tensorflow 读取XML文件内容并对图片等比例缩放相关的知识,希望对你有一定的参考价值。
根据XML文件中对图片标记的信息读取,并显示在图片中。
xml 文件内容:
<annotation>
<folder>OXIIIT</folder>
<filename>samoyed_100.jpg</filename>
<source>
<database>OXFORD-IIIT Pet Dataset</database>
<annotation>OXIIIT</annotation>
<image>flickr</image>
</source>
<size>
<width>500</width>
<height>334</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>dog</name>
<pose>Frontal</pose>
<truncated>0</truncated>
<occluded>0</occluded>
<bndbox>
<xmin>136</xmin>
<ymin>1</ymin>
<xmax>344</xmax>
<ymax>304</ymax>
</bndbox>
<difficult>0</difficult>
</object>
</annotation>
目标原图片:
实现代码:
import tensorflow as tf
import matplotlib.pyplot as plt
from lxml import etree
from matplotlib.patches import Rectangle
img = tf.io.read_file(r'./images/samoyed_100.jpg') # 读取图片
img = tf.image.decode_jpeg(img) # 对图像进行解码
plt.imshow(img)
plt.show()
xml = open(r'./xmls/samoyed_100.xml').read() # 读取 xml文件
sel = etree.html(xml) # 对 xml 文件进行解析
width = sel.xpath('//size/width/text()')[0]
height = sel.xpath('//size/height/text()')[0]
xmin = sel.xpath('//bndbox/xmin/text()')[0]
ymin = sel.xpath('//bndbox/ymin/text()')[0]
xmax = sel.xpath('//bndbox/xmax/text()')[0]
ymax = sel.xpath('//bndbox/ymax/text()')[0]
width = int(width)
height = int(height)
xmin = int(xmin)
ymin = int(ymin)
xmax = int(xmax)
ymax = int(ymax)
plt.imshow(img.numpy())
rect = Rectangle((xmin, ymin), (xmax - xmin), (ymax - ymin), fill=False, color='red') # fill=False 不需要填充
ax = plt.gca() # 获取当前图像
ax.axes.add_patch(rect) # 添加矩形框
plt.show()
得到
对图片进行比例缩放:
img = tf.image.resize(img, [224, 224])
img = img / 255 # 标准化
xmin = (xmin / width) * 224
ymin = (ymin / height) * 224
xmax = (xmax / width) * 224
ymax = (ymax / height) * 224
plt.imshow(img.numpy())
rect = Rectangle((xmin, ymin), (xmax - xmin), (ymax - ymin), fill=False, color='red')
ax = plt.gca()
ax.axes.add_patch(rect)
plt.show()
缩放后:
以上是关于Tensorflow 读取XML文件内容并对图片等比例缩放的主要内容,如果未能解决你的问题,请参考以下文章