https://github.com/ultralytics/JSON2YOLO
https://blog.csdn.net/qq_51831335/article/details/127237772
目标检测数据集标签转换COCO2VOC、YOLO2VOC、JSON2YOLO
<annotation>
<folder>VOC2007</folder>
<filename>000001.jpg</filename>
<source>
<database>The VOC2007 Database</database>
<annotation>PASCAL VOC2007</annotation>
<image>flickr</image>
<flickrid>341012865</flickrid>
</source>
<owner>
<flickrid>Fried Camels</flickrid>
<name>Jinky the Fruit Bat</name>
</owner>
<size>
<width>353</width>
<height>500</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>dog</name>
<pose>Left</pose>
<truncated>1</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>48</xmin>
<ymin>240</ymin>
<xmax>195</xmax>
<ymax>371</ymax>
</bndbox>
</object>
<object>
<name>person</name>
<pose>Left</pose>
<truncated>1</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>8</xmin>
<ymin>12</ymin>
<xmax>352</xmax>
<ymax>498</ymax>
</bndbox>
</object>
</annotation>
#定义一个voc到yolo数据格式的转换代码
#voc的数据标定的时候给定的是:左上角,右下角的坐标(xmin/ymin/xmax/ymax)
#yolo数据标定的时候给定的是:中心点坐标,宽度,高度(x/y/w/h),并且是和原始图像width和height的百分比
import os
from xml.etree import ElementTree as ET
import tqdm
if __name__ == '__main__':
label_2_id = {
'plate':0
}
img_dir= “”
voc_label_dir = ""
yolo_label_dir = ""
#读取所有voc_xml文件名称列表
if not os.path.exists(yolo_label_dir):
os.makedirs(yolo_label_dir):
os.makedirs(yolo_label_dir)
#遍历处理每个xml文件
for voc_label_name in tqdm(voc_label_names):
#1.解析xml文件
tree = ET.parse(os.path.join(voc_label_dir,voc_label_name))
#2.获取根节点的信息
root = tree.getroot()
#3.获取文件名称
filename = root.find('filename').text
#4.判断图像文件是是否存在
img_file = os.path.join(img_dir,filename)
if not os.path.exists(img_file)
continue
#5.将标注的box信息转换成yolo格式,然后保存到txt文件中
#6.提取图像的大小
size_obj = root.find('size')
width = float(size_obj.find('width').text)
height = float(size_obj.find('height').text)
yolo_label_file = os.path.join(yolo_label_dir,f"{os.path.splitext(filename)[0]}.txt")
with open(yolo_label_file,'w',encoding = 'UTF-8') as writer:
for obj in root.findall('object'):
#提取标签名称
label = obj.find('name').text
#提取左上角,右下角坐标
bbox = obj.find('bndbox')
xmin = float(bbox.find('xmin').text)
xmax = float(bbox.find('xmin').text)
ymin = float(bbox.find('xmin').text)
ymax = float(bbox.find('xmin').text)
#转换成中心点坐标,宽度,高度
w = xmax-xmin
h = ymax -ymin
x = xmin + w/2.0
y = ymin + h/2.0
#需要相对宽度和高度
w = w/width
h = h/height
x = x/width
y = y/height
#输出
writer.writelines(f"{label_2_id{label} {x} {y} {w} {h}\n}")
#img_name = voc_label_name[:-3]
#print(voc_label_names)