mat转xml脚本

这不是通用版本,需要改成自己的xml节点,思路是通用的

from pickle import OBJ
# from posix import listdir
from matplotlib.pyplot import text
import scipy.io as sio
import os,glob
import xml.etree.ElementTree as ET
import xml.dom.minidom as DOM
import cv2

pwd = os.getcwd()
print(pwd)
mat_input = r'C:\Users\Lenovo\Desktop\mat_'
xml_output = r'C:\Users\Lenovo\Desktop\xml_'
img_input = r'C:\Users\Lenovo\Desktop\img_'


def read_mat(mat_input):
    file = os.listdir(mat_input)
    for i,file_name in enumerate(file):
        img = cv2.imread(glob.glob(os.path.join(img_input,file_name.replace('mat','jpg')))[0])
        size = img.shape[0]
        point_list = []
        xml_path = mat_input + '\\' + file_name.replace('mat','xml')
        print("xml_path:",xml_path)
        mat_data = sio.loadmat(glob.glob(os.path.join(mat_input + '\\' + file_name))[0])
        # print(mat_data.keys(),type(mat_data))
        for key in mat_data.keys():
            if key == 'marks':
                point_list.append(mat_data['marks'])
        print(point_list)
        save_xml(file_name,point_list,size)
    

def save_xml(file_name,point_list,size):
    #首先创建根节点 
    root = ET.Element('annotation') 
    #添加子节点SubElement(父节点Element对象, Tag字符串格式, Attribute字典格式) 
    folder1 = ET.SubElement(root, 'folder') 
    filename1 = ET.SubElement(root, 'filename')
    path1 = ET.SubElement(root,'path')
    source1 = ET.SubElement(root,'source')
    size1 = ET.SubElement(root,'size')
    segmented1 = ET.SubElement(root,'segmented')
    for i,value in enumerate(point_list[0]):
        object1 = ET.SubElement(root,'object')
        name1 = ET.SubElement(object1,'name')
        pose1 = ET.SubElement(object1,'pose')
        truncated1 = ET.SubElement(object1,'truncated')
        difficult1 = ET.SubElement(object1,'difficult')
        bndbox = ET.SubElement(object1,'bndbox')
        xmin = ET.SubElement(bndbox,'xmin')
        ymin = ET.SubElement(bndbox,'ymin')
        xmax = ET.SubElement(bndbox,'xmax')
        ymax = ET.SubElement(bndbox,'ymax')
        name1.text = 'cross'
        pose1.text = 'Unspecified'
        truncated1.text = '0'
        difficult1.text = '0'
        print('value[0]:',type(value[0]),value[0])
        xmin.text = str(int((round(value[0])*416)/600 - 9))
        ymin.text = str(int((round(value[1])*416)/600 + 9))
        xmax.text = str(int((round(value[0])*416)/600 + 9))
        ymax.text = str(int((round(value[1])*416)/600 - 9))

    #添加子节点 
    database1 = ET.SubElement(source1, 'database') 
    width1 = ET.SubElement(size1,'width')
    height1 = ET.SubElement(size1,'height')
    depth1 = ET.SubElement(size1,'depth')

    #添加text
    folder1.text = img_input.split('\\')[-1]
    filename1.text = file_name.replace('mat','jpg')
    path1.text = img_input + '\\' + file_name.replace('mat','jpg')
    database1.text = 'Unknow'
    width1.text = str(size)
    height1.text = str(size)
    depth1.text = '3'
    segmented1.text = '0'

    #写入保存并格式化
    word = prettify(root)
    with open(xml_output+ '\\' + file_name.replace('mat','xml'),'w') as f:
        f.write(word)  

        #将根目录转化为xml树状结构(即ElementTree对象) 
        # tree = ET.ElementTree(root) 
        # #在终端显示整个xml内容 
        # ET.dump(root) 
        # #写入xml文件 
        # tree.write(xml_output+ '\\' + file_name.replace('mat','xml'), encoding="utf-8", xml_declaration=True,method='xml')


def prettify(elem):
    """将节点转换成字符串,并添加缩进。
    """
    rough_string = ET.tostring(elem, 'utf-8')
    reparsed = DOM.parseString(rough_string)
    return reparsed.toprettyxml(indent="\t")
        

read_mat(mat_input)

#我这里需要resize下图片,不需要的可以不用。
def resize(img_list):
    for i in img_list:
        imgname = glob.glob(os.path.join(img_input,i))
        img = cv2.imread(imgname[0])
        print(img.shape[0])
        img = cv2.resize(img,(416,416))
        cv2.imwrite(imgname[0],img)

img_list = os.listdir(img_input)
# resize(img_list)




    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值