文章目录
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)
处于边缘的标注框转换后是否会越界,还未检验过、、