日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

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

發(fā)布時間:2025/3/19 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 将PascalVOC(XML)格式的标注数据批量转换为YOLO(txt)格式的标注数据 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

    • 20191022
    • 20200523 第一次使用,修改了代碼內(nèi)容
      • 使用方法
      • 修改過的代碼

20191022

引用文章:啊哈~發(fā)表第一篇博客,voc格式的標(biāo)注數(shù)據(jù)轉(zhuǎn)換為yolo格式的標(biāo)注數(shù)據(jù)

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 第一次使用,修改了代碼內(nèi)容

今天用labelImg標(biāo)注數(shù)據(jù)的時候,發(fā)現(xiàn)自己不小心把標(biāo)注格式標(biāo)成xml格式了,這個轉(zhuǎn)換代碼終于有用武之地了。。

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

1、標(biāo)注類默認(rèn)為0
2、轉(zhuǎn)換后標(biāo)注數(shù)字默認(rèn)保存6位小數(shù)

使用方法

在代碼.py文件目錄創(chuàng)建兩個文件夾,一個是labels,還有一個是labelsyolo,將xml格式標(biāo)注放進(jìn)labels文件夾,運行代碼,轉(zhuǎn)換格式后的yolo標(biāo)注就被生成在labelsyolo文件夾里了

如圖:


運行代碼轉(zhuǎn)換后:

修改過的代碼

# -*- 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)

處于邊緣的標(biāo)注框轉(zhuǎn)換后是否會越界,還未檢驗過、、

總結(jié)

以上是生活随笔為你收集整理的python 将PascalVOC(XML)格式的标注数据批量转换为YOLO(txt)格式的标注数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。