1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python 将PascalVOC(XML)格式的标注数据批量转换为YOLO(txt)格式的标注数据

python 将PascalVOC(XML)格式的标注数据批量转换为YOLO(txt)格式的标注数据

时间:2019-11-18 22:48:45

相关推荐

python 将PascalVOC(XML)格式的标注数据批量转换为YOLO(txt)格式的标注数据

文章目录

1022 第一次使用,修改了代码内容使用方法修改过的代码

1022

引用文章:啊哈~发表第一篇博客,voc格式的标注数据转换为yolo格式的标注数据

import xml.etree.ElementTree as ETimport pickleimport osfrom os import listdir, getcwdfrom os.path import join#classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"] #为了获得cls iddef convert(size, box):dw = 1./(size[0])dh = 1./(size[1])x = (box[0] + box[1])/2.0 - 1y = (box[2] + box[3])/2.0 - 1w = box[1] - box[0]h = box[3] - box[2]x = x*dww = w*dwy = y*dhh = h*dhreturn (x,y,w,h)def convert_annotation(image_id):in_file = open('labels/%s.xml'%(image_id))out_file = open('labelsyolo/%s.txt'%(image_id), 'w')tree=ET.parse(in_file)root = tree.getroot()size = root.find('size')w = int(size.find('width').text)h = int(size.find('height').text)for obj in root.iter('object'):difficult = obj.find('difficult').textcls = obj.find('name').textif cls=='you':cls=0else:cls=1xmlbox = obj.find('bndbox')b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))bb = convert((w,h), b)out_file.write(str(cls) + " " + " ".join([str(a) for a in bb]) + '\n')wd = getcwd()b=0list_file = os.listdir('labels')for file in list_file:f=file.replace('.xml','')convert_annotation(f)b=b+1print(b)

代码还未检验过、、

第一次使用,修改了代码内容

今天用labelImg标注数据的时候,发现自己不小心把标注格式标成xml格式了,这个转换代码终于有用武之地了。。

自己把代码修改了一下,,主要涉及两方面,

1、标注类默认为0

2、转换后标注数字默认保存6位小数

使用方法

在代码.py文件目录创建两个文件夹,一个是labels,还有一个是labelsyolo,将xml格式标注放进labels文件夹,运行代码,转换格式后的yolo标注就被生成在labelsyolo文件夹里了

如图:

运行代码转换后:

修改过的代码

# -*- coding: utf-8 -*-"""@File : xml2yolo.py@Time : /5/23 17:11@Author : Dontla@Email : sxana@@Software: PyCharm"""import xml.etree.ElementTree as ETimport pickleimport osfrom os import listdir, getcwdfrom os.path import join# classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"] #为了获得cls iddef convert(size, box):dw = 1. / (size[0])dh = 1. / (size[1])x = (box[0] + box[1]) / 2.0 - 1y = (box[2] + box[3]) / 2.0 - 1w = box[1] - box[0]h = box[3] - box[2]x = x * dww = w * dwy = y * dhh = h * dhreturn (x, y, w, h)def convert_annotation(image_id):in_file = open('labels/%s.xml' % (image_id))out_file = open('labelsyolo/%s.txt' % (image_id), 'w')tree = ET.parse(in_file)root = tree.getroot()size = root.find('size')w = int(size.find('width').text)h = int(size.find('height').text)for obj in root.iter('object'):difficult = obj.find('difficult').textcls = obj.find('name').textif cls == 'you':cls = 0else:# Dontla # origin:cls = 1cls = 0xmlbox = obj.find('bndbox')b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),float(xmlbox.find('ymax').text))bb = convert((w, h), b)# Dontla # origin:out_file.write(str(cls) + " " + " ".join([str(a) for a in bb]) + '\n')out_file.write(str(cls) + " " + " ".join([str('{:6f}'.format(a)) for a in bb]) + '\n')wd = getcwd()b = 0list_file = os.listdir('labels')for file in list_file:f = file.replace('.xml', '')convert_annotation(f)b = b + 1print(b)

处于边缘的标注框转换后是否会越界,还未检验过、、

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。