admin管理员组文章数量:1609966
Yolo_mark工具界面使用不是很方便,常用的精灵标注助手不支持导入PASCAL VOC的标注文件,转换xml文件,一键导入,查看or修改标注信息。
import os, sys
import glob
from PIL import Image
import xml.etree.ElementTree as ET
import numpy as np
def convert_annotation(voc_path,image_id):
print(f"voc_xml_id:{image_id}")
in_file = open(voc_path + '/%s.xml'%(image_id))
tree=ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
bbox = []
for obj in root.iter('object'):
difficult = obj.find('difficult').text
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
bbox.append([xmlbox.find('xmin').text,xmlbox.find('ymin').text,xmlbox.find('xmax').text,xmlbox.find('ymax').text])
return bbox
src_img_dir = "E:\data\group_string\init_data\img"
src_txt_dir = r"E:\data\group_string\init_data\xml"
src_xml_dir = r"E:\data\group_string\init_data\ann"
img_Lists = glob.glob(src_img_dir + '/*.jpg')
img_basenames = []
for item in img_Lists:
img_basenames.append(os.path.basename(item))
img_names = []
for item in img_basenames:
temp1, temp2 = os.path.splitext(item)
img_names.append(temp1)
c = 1588750115840
for img in img_names:
img_path = src_img_dir + '\\' + img + '.jpg'
im = Image.open(img_path)
width, height = im.size
gt = convert_annotation(src_txt_dir,img)
xml_file = open((src_xml_dir + '/' + img + '.xml'), 'w')
xml_file.write('<?xml version="1.0" ?>\n')
xml_file.write('<doc>\n')
xml_file.write(' <path>'+img_path+'</path>\n')
xml_file.write(' <outputs>\n')
xml_file.write(' <object>\n')
for spt in gt:
xml_file.write(' <item>\n')
xml_file.write(' <name>group string</name>\n')
xml_file.write(' <bndbox>\n')
xml_file.write(' <xmin>' + spt[0] + '</xmin>\n')
xml_file.write(' <ymin>' + spt[1] + '</ymin>\n')
xml_file.write(' <xmax>' + spt[2] + '</xmax>\n')
xml_file.write(' <ymax>' + spt[3] + '</ymax>\n')
xml_file.write(' </bndbox>\n')
xml_file.write(' </item>\n')
xml_file.write(' </object>\n')
xml_file.write(' </outputs>\n')
xml_file.write(' <time_labeled>'+str(c)+'</time_labeled>\n')
xml_file.write(' <labeled>true</labeled>\n')
xml_file.write(' <size>\n')
xml_file.write(' <width>' + str(width) + '</width>\n')
xml_file.write(' <height>' + str(height) + '</height>\n')
xml_file.write(' <depth>3</depth>\n')
xml_file.write(' </size>\n')
xml_file.write('</doc>')
c += 5394
版权声明:本文标题:精灵标注助手导入xml数据 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1728585368a1164886.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论