VOC格式标签
:图片的实际宽和高,标注框的左上角和右下角坐标YOLO格式标签
:标注框的中心坐标(归一化的),标注框的宽和高(归一化的)
将xml文件从pic_path移到xml_path
def copy_xml_to_other_folder(pic_path,xml_path):
"""move xml file from pic_path to xml_path"""
pic_xml_list = os.listdir(pic_path)
for sample in tqdm(pic_xml_list):
if 'xml' in sample:
ori_xml_path = osp.join(pic_path,sample)
end_xml_path = osp.join(xml_path,sample)
shutil.copy(ori_xml_path,end_xml_path)
读取xml文件,并转为voc形式的txt。
def xml_to_txt(xml_path, outdir):
"""读取xml文件,获得想要的内容存到txt文件中"""
"""XML设计的核心是包含和传输数据"""
xml_list = os.listdir(xml_path)
for xml_file in tqdm(xml_list):
xml_file_name = osp.basename(xml_file)[:-4]
xml_file_path = osp.join(xml_path,xml_file)
if '.DS_Store' in xml_file_path:
pass
else:
DOMTree = ET.parse(xml_file_path)
root = DOMTree.getroot()
object_list = []
for object in root.iter('object'):
# bndbox这部分看自己的标签是什么
object_bndbox = object.iter('bndbox')
# 读取xml文件中的object_name
object_name = object.iter('name')
for bndbox in object_bndbox:
node = []
for child in bndbox:
node.append(int(child.text))
x, y = node[0], node[1]
w, h = node[2], node[3]
for name in object_name:
name = name.text
object_list.append("{} {} {} {} {}\n".format(x, y, w, h,name))
txt_file_path = osp.join(outdir,xml_file_name+'.txt')
with open(txt_file_path,'w') as fp:
fp.writelines(object_list)
版权声明:本文为CSDN博主「这个利弗莫尔不太冷」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_48262500/article/details/122114048
暂无评论