python 将PascalVOC(XML)格式的标注数据批量转换为YOLO(txt)格式的标注数据
生活随笔
收集整理的這篇文章主要介紹了
python 将PascalVOC(XML)格式的标注数据批量转换为YOLO(txt)格式的标注数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 20191022
- 20200523 第一次使用,修改了代碼內容
- 使用方法
- 修改過的代碼
20191022
引用文章:啊哈~發表第一篇博客,voc格式的標注數據轉換為yolo格式的標注數據
import xml.etree.ElementTree as ET import pickle import os from os import listdir, getcwd from 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=0 list_file = os.listdir('labels') for file in list_file:f=file.replace('.xml','')convert_annotation(f)b=b+1 print(b)代碼還未檢驗過、、
20200523 第一次使用,修改了代碼內容
今天用labelImg標注數據的時候,發現自己不小心把標注格式標成xml格式了,這個轉換代碼終于有用武之地了。。
自己把代碼修改了一下,,主要涉及兩方面,
1、標注類默認為0
2、轉換后標注數字默認保存6位小數
使用方法
在代碼.py文件目錄創建兩個文件夾,一個是labels,還有一個是labelsyolo,將xml格式標注放進labels文件夾,運行代碼,轉換格式后的yolo標注就被生成在labelsyolo文件夾里了
如圖:
運行代碼轉換后:
修改過的代碼
# -*- coding: utf-8 -*- """ @File : xml2yolo.py @Time : 2020/5/23 17:11 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ import xml.etree.ElementTree as ET import pickle import os from os import listdir, getcwd from 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 20200523# 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 20200523# 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 = 0 list_file = os.listdir('labels') for file in list_file:f = file.replace('.xml', '')convert_annotation(f)b = b + 1 print(b)處于邊緣的標注框轉換后是否會越界,還未檢驗過、、
總結
以上是生活随笔為你收集整理的python 将PascalVOC(XML)格式的标注数据批量转换为YOLO(txt)格式的标注数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何利用LabelImg将标注文件在YO
- 下一篇: python 将YOLO(txt)格式的