<!--001.xml-->
<?xml version='1.0' encoding='utf-8'?>
<annotation>
<folder>COMMON FIRECREST</folder>
<filename>001.jpg</filename>
<path>XXXX FIRECREST/001.jpg</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>224</width>
<height>224</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>bird</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>14</xmin>
<ymin>5</ymin>
<xmax>222</xmax>
<ymax>224</ymax>
</bndbox>
</object>
</annotation>
上面是模板xml,下面代码的功能是将mat文件转换为xml文件,仅仅是获取了一个目标框的坐标。
# decodemat.py
import scipy.io
import cv2
import os
from xml.dom.minidom import parse
# .mat文件路径
annotations_mat_path = 'XXXXXX/Caltech-UCSD-Birds-200-2010/annotations/annotations-mat'
# .mat文件对应图片路径
images_read_path = 'XXXXXX/Caltech-UCSD-Birds-200-2010/images/images'
# 要保存的图片路径
image_saved_path = 'XXXXXX/Caltech-UCSD-Birds-200-2010/img'
# 要保存的xml路径
xml_saved_path = 'XXXXX/Caltech-UCSD-Birds-200-2010/xml'
# 读取模板xml
domTree = parse("001.xml")
rootNode = domTree.documentElement
def decode_mat():
# 统计图片数量
cntsum = 0
for root, dirs, files in os.walk(annotations_mat_path):
# root 表示当前正在访问的文件夹路径
# dirs 表示该文件夹下的子目录名list
# files 表示该文件夹下的文件list
# 遍历所有的文件夹
for d in dirs:
imgpath = os.path.join(images_read_path, d)
for chroot, chdir, chfiles in os.walk(os.path.join(root, d)):
for file in chfiles:
image = cv2.imread(os.path.join(imgpath, file.split('/')[-1].split('.')[0]) + '.jpg')
data = scipy.io.loadmat(os.path.join(chroot, file))
filename = rootNode.getElementsByTagName("filename")[0]
path = rootNode.getElementsByTagName("path")[0]
height = rootNode.getElementsByTagName("height")[0]
width = rootNode.getElementsByTagName("width")[0]
xmin = rootNode.getElementsByTagName("xmin")[0]
xmax = rootNode.getElementsByTagName("xmax")[0]
ymin = rootNode.getElementsByTagName("ymin")[0]
ymax = rootNode.getElementsByTagName("ymax")[0]
filename.childNodes[0].data = file.split('/')[-1].split('.')[0] + '.jpg'
path.childNodes[0].data = os.path.join(imgpath, file.split('/')[-1].split('.')[0]) + '.jpg'
height.childNodes[0].data = image.shape[0]
width.childNodes[0].data = image.shape[1]
xmin.childNodes[0].data = data['bbox'][0][0][0][0][0]
ymin.childNodes[0].data = data['bbox'][0][0][1][0][0]
xmax.childNodes[0].data = data['bbox'][0][0][2][0][0]
ymax.childNodes[0].data = data['bbox'][0][0][3][0][0]
cv2.imwrite(os.path.join(image_saved_path, str(cntsum) + '.jpg'), image)
with open(os.path.join(xml_saved_path, str(cntsum) + '.xml'), 'w') as f:
domTree.writexml(f, addindent=' ', encoding='utf-8')
cntsum += 1
print(os.path.join(imgpath, file.split('/')[-1].split('.')[0]) + '.xml', ' 完成')
if __name__ == '__main__':
decode_mat()