最近看到一个目标检测的比赛,给的训练集中的label是json文件,如下(片段):
{
"info":
{
"image_name":
"5196_1642654682111.jpg"
},
"annotations":
[
{
"bbox":
[88, 522, 9, 19],
"color": "other"
}
{
"bbox":
[228, 562, 8, 16],
"color": "other"}
]
}
在使用yoloV4的模型中
出现了问题,不同于coco数据集中的json文件,在数据集使用的情况下无法直接转换为voc下可用的XML文件,网上找到的代码也都是针对COCO数据集的,在找到COCO数据集中json2xml的代码中做出了一些修改,可以用于此形式的json文件转为xml文件,方法如下:GitHub - bubbliiiing/yolov4-pytorch: 这是一个YoloV4-pytorch的源码,可以用于训练自己的模型。https://link.zhihu.com/?target=https%3A//github.com/bubbliiiing/yolov4-pytorch出现了问题,不同于coco数据集中的json文件,在数据集使用的情况下无法直接转换为voc下可用的XML文件,网上找到的代码也都是针对COCO数据集的,在找到COCO数据集中json2xml的代码中做出了一些修改,可以用于此形式的json文件转为xml文件,方法如下:
一、将json文件合并为一个.json文件
import os
import json
import tqdm
def merge_json(path_results, path_merges):
merges_file = os.path.join(path_merges, "bas_fund_transaction.json")
with open(merges_file, "w", encoding="utf-8") as f0:
for file in os.listdir(path_results):
with open(os.path.join(path_results, file), "r", encoding="utf-8") as f1:
for line in tqdm.tqdm(f1):
line_dict = json.loads(line)
js = json.dumps(line_dict, ensure_ascii=False)
f0.write(js + '\n')
f1.close()
f0.close()
if __name__ == '__main__':
path_results, path_merges = "VOCdevkit/VOC2007/Annotations", "model_data" #path_results为json文件存放路径
#path_merges 为合并后的存放路径
if not os.path.exists(path_merges): # 如果results目录不存在,新建该目录。
os.mkdir(path_merges)
merge_json(path_results, path_merges)
二、在得到合并后的json文件转换为csv文件
找到bas_fund_transaction.json;
使用 在线 JSON 转 CSV 工具 进行csv文件转换
复制
ctrl+A 内容转换为csv文件,以便使用,注意存放路径
csv文件:
三、阅读了众多代码,使用了最容易理解的一个代码,并在此基础上进行修改以针对此类型json数据进行转换。
import csv
import numpy as np
save_xml_dir = "D:\label_xml" #存放转换成功的xml文件的路径
file_path = "model_data/jsoncsv.jsonutil.online.1649310841707.csv" #存放csv文件的路径
width = 1920 #自己数据集的大小
height = 1080
with open(file_path) as csvfile:
csv_reader = csv.reader(csvfile)
csv_header = next(csv_reader)
pre_img = ''
for row in csv_reader:
img = row[0].split("/")[-1].split(".")[0]
if img != pre_img:
if pre_img != '':
xml_file1 = open((save_xml_dir + pre_img + '.xml'), 'a')
xml_file1.write('</annotation>')
xml_file1.close()
xml_file = open((save_xml_dir + img + '.xml'), 'w')
xml_file.write('<annotation>\n')
xml_file.write(' <folder>VOC2007</folder>\n')
xml_file.write(' <filename>' + str(img) + '.jpg' + '</filename>\n')
xml_file.write('<source>\n')
xml_file.write('<database> orgaquant </database>\n')
xml_file.write('<annotation> organoid </annotation>\n')
xml_file.write('</source>\n')
xml_file.write(' <size>\n')
xml_file.write(' <width>' + str(width) + '</width>\n')
xml_file.write(' <height>' + str(height) + '</height>\n')
xml_file.write(' <depth>3</depth>\n')
xml_file.write(' </size>\n')
max_count = len(row[1:])
n = 0
while n < max_count and row[1:][n: n + 5][0] != '':
print(row[1:])
xi = float(row[1:][n:n + 5][0]) - float(row[1:][n:n + 5][2]) / 2.
yi = float(row[1:][n:n + 5][1]) - float(row[1:][n:n + 5][3]) / 2.
xa = float(row[1:][n:n + 5][0]) + float(row[1:][n:n + 5][2]) / 2.
ya = float(row[1:][n:n + 5][1]) + float(row[1:][n:n + 5][3]) / 2.
name = str(row[1:][n:n + 5][4])
print(xi, yi, xa, ya, name)
xml_file.write(' <object>\n')
xml_file.write('<name>' + name + '</name>\n')
xml_file.write(' <bndbox>\n')
xml_file.write(' <xmin>' + str(xi) + '</xmin>\n')
xml_file.write(' <ymin>' + str(yi) + '</ymin>\n')
xml_file.write(' <xmax>' + str(xa) + '</xmax>\n')
xml_file.write(' <ymax>' + str(ya) + '</ymax>\n')
xml_file.write(' </bndbox>\n')
xml_file.write(' </object>\n')
n = n + 5
xml_file.close()
pre_img = img
xml_file1 = open((save_xml_dir + pre_img + '.xml'), 'a')
xml_file1.write('</annotation>')
xml_file1.close()
由于原数据集中"bbox":[88,522,9,19],"color":"other" 中
bbox列表中分别代表[标注框中心坐标x,标注框中心坐标y,标注框宽度,标注框高度]
所以需要额外注意这个转换关系,因为VOC的xml中需要的是xmin,ymin,xmax,ymax
即左上角坐标和右下角坐标
xi = float(row[1:][n:n + 5][0]) - float(row[1:][n:n + 5][2]) / 2.
yi = float(row[1:][n:n + 5][1]) - float(row[1:][n:n + 5][3]) / 2.
xa = float(row[1:][n:n + 5][0]) + float(row[1:][n:n + 5][2]) / 2.
ya = float(row[1:][n:n + 5][1]) + float(row[1:][n:n + 5][3]) / 2.
至此,我们已经得到了一个VOC可用的XML文件:
<annotation>
<folder>VOC2007</folder>
<filename>5776_1642654729537.jpg</filename>
<source>
<database> orgaquant </database>
<annotation> organoid </annotation>
</source>
<size>
<width>1920</width>
<height>1080</height>
<depth>3</depth>
</size>
<object>
<name>other</name>
<bndbox>
<xmin>331.0</xmin>
<ymin>572.5</ymin>
<xmax>341.0</xmax>
<ymax>593.5</ymax>
</bndbox>
</object>
<object>
<name>other</name>
<bndbox>
<xmin>414.0</xmin>
<ymin>515.0</ymin>
<xmax>426.0</xmax>
<ymax>547.0</ymax>
</bndbox>
</object>
………………
</annotation>
处理过后的xml文件可能没有对齐格式,可以批量使用格式化代码。
在pycharm中
即可格式化文件夹下的所有xml文件,得到可用的xml文件。
至此,可以使用voc_annotation.py进行数据集预加载了。