一,介绍
基于上一篇文章“将json文件转换为xml文件”后,继续将表格信息填入xml文件中。
由于我的bmp图片josn文件是由“编号_uuid_图片名称”组成,每一个编号/uuid代表一个人,而一个人有很多图片,表格中是没有文件名的,但是有每个人的编号以及uuid。
方法:
(1)将文件名以特殊符号“_”拆分;
(2)用第一个特殊符号“_”前的编号与表格中的编号进行匹配。
代码:
#按特殊字符'_'拆分文件名,并输出第一个'_'前的字符串,匹配表格列时使用
split_parts = json_file_.split('_')
first_part = split_parts[0]
二,实现
1.表格路径:'F:/code_demo/info.xlsx'
2.代码:
xml.write('\t<info>\n')
workbook = openpyxl.load_workbook(xlsx_file)
sheet = workbook.active
for row in sheet.iter_rows(min_row=2, values_only=True):
if first_part == str(row[16]): # 根据.xml文件名查找对应行
row_found = True
xml.write('\t\t<name>' + str(row[15]) + '</name>\n') # 'name': row[15], # 表格中的第15列
xml.write('\t\t<gender>' + str(row[18]) + '</gender>\n')
xml.write('\t\t<age>' + str(row[19]) + '</age>\n')
xml.write('\t\t<height>' + str(row[20]) + '</height>\n')
xml.write('\t\t<weight>' + str(row[21]) + '</weight>\n')
xml.write('\t\t<disease>' + str(row[63]) + '</disease>\n')
xml.write('\t\t<location>' + str(row[66]) + '</location>\n')
xml.write('\t\t<id>' + str(row[16]) + '</id>\n')
xml.write('\t\t<uuid>' + str(row[76]) + '</uuid>\n')
xml.write('\t</info>\n')
3.总代码
import numpy as np
import codecs
import json
from glob import glob
import cv2
import shutil
from sklearn.model_selection import train_test_split
import os
import openpyxl
# json文件路径
labelme_path = r"F:\code_demo\测试二/"
# bmp图片文件路径
image_path = r"F:\code_demo\测试二/"
# 表格文件路径
xlsx_file = r'F:/code_demo/info.xlsx'
# xml文件路径
saved_path = r"F:\code_demo\xml测试二/"
# 保存路径
isUseTest = True # 是否创建test集
# 2.创建xml中要求的文件夹
if not os.path.exists(saved_path + "Annotations"):
os.makedirs(saved_path + "Annotations")
if not os.path.exists(saved_path + "BMPImages/"):
os.makedirs(saved_path + "BMPImages/")
if not os.path.exists(saved_path + "ImageSets/Main/"):
os.makedirs(saved_path + "ImageSets/Main/")
# 3.获取待处理文件
# glob模块用来查找文件目录和文件
files = glob(labelme_path + "*.json")
# 相当于循环文件名
files = [i.replace("\\", "/").split("/")[-1].split(".json")[0] for i in files]
# 4.读取标注信息并写入 xml
for json_file_ in files:
json_filename = labelme_path + json_file_ + ".json"
json_file = json.load(open(json_filename, "r", encoding="utf-8"))
#按特殊字符'_'拆分文件名,并输出第一个'_'前的字符串,匹配表格列时使用
split_parts = json_file_.split('_')
first_part = split_parts[0]
#文件路径全是英文
# height, width, channels = cv2.imread(image_path + json_file_ + ".bmp").shape)
#文件路径含英文时
height, width, channels = cv2.imdecode(np.fromfile(image_path + json_file_ + ".bmp", dtype=np.uint8), -1).shape
with codecs.open(saved_path + "Annotations/" + json_file_ + ".xml", "w", "utf-8") as xml:
xml.write('<annotation>\n')
xml.write('\t<folder>' + 'GastricCancer' + '</folder>\n')
xml.write('\t<filename>' + json_file_ + ".bmp" + '</filename>\n')
#版本号,判断标注名称
version = json_file["version"]
#写入版本号
xml.write('\t<version>' + version + '</version>\n')
xml.write('\t<info>\n')
workbook = openpyxl.load_workbook(xlsx_file)
sheet = workbook.active
for row in sheet.iter_rows(min_row=2, values_only=True):
if first_part == str(row[77]): # 根据.xml文件名查找对应行
row_found = True
xml.write('\t\t<name>' + str(row[15]) + '</name>\n') # 'name': row[15], # 表格中的第15列
xml.write('\t\t<gender>' + str(row[18]) + '</gender>\n')
xml.write('\t\t<age>' + str(row[19]) + '</age>\n')
xml.write('\t\t<height>' + str(row[20]) + '</height>\n')
xml.write('\t\t<weight>' + str(row[21]) + '</weight>\n')
xml.write('\t\t<disease>' + str(row[63]) + '</disease>\n')
xml.write('\t\t<location>' + str(row[66]) + '</location>\n')
xml.write('\t\t<id>' + str(row[16]) + '</id>\n')
xml.write('\t\t<uuid>' + str(row[76]) + '</uuid>\n')
xml.write('\t</info>\n')
xml.write('\t<source>\n')
xml.write('\t\t<database>The GastricCancer Database</database>\n')
xml.write('\t\t<annotation>The GastricCancer Database</annotation>\n')
xml.write('\t\t<image>flickr</image>\n')
# xml.write('\t\t<flickrid>NULL</flickrid>\n')
xml.write('\t</source>\n')
# xml.write('\t<owner>\n')
# xml.write('\t\t<flickrid>NULL</flickrid>\n')
# xml.write('\t\t<name>WH</name>\n')
# xml.write('\t</owner>\n')
xml.write('\t<size>\n')
xml.write('\t\t<width>' + str(width) + '</width>\n')
xml.write('\t\t<height>' + str(height) + '</height>\n')
xml.write('\t\t<depth>' + str(channels) + '</depth>\n')
xml.write('\t</size>\n')
xml.write('\t\t<segmented>1</segmented>\n')
for multi in json_file["shapes"]:
points = np.array(multi["points"])
labelName = multi["label"]
# if "moreLabel" in multi:
# moreLabelName = multi["moreLabel"]
# else:
# moreLabelName = str(0)
# moreLabelName = multi["moreLabel"]
xmin = min(points[:, 0])
xmax = max(points[:, 0])
ymin = min(points[:, 1])
ymax = max(points[:, 1])
# label = multi["label"]
if xmax <= xmin:
print('wrong1', json_file_)
pass
elif ymax <= ymin:
print('wrong2', json_file_)
pass
else:
xml.write('\t<object>\n')
#判断label标注类型并以二分类形式写入xml文件中
if isinstance(labelName, str) and len(labelName) > 1:
ascii_values = [] #储存ascll码数组
for character in labelName:
ascii_values.append(ord(character)) #将labelName字符串转化为ascll码
#scall = np.frombuffer(labelName, dtype=np.uint8)
for i in range(0, len(ascii_values)):
if ascii_values[i] == 48: #判断字符串中第一个数字是否为0(0的ascll码为48)
xml.write('\t\t<name>' + "0" + '</name>\n')
xml.write('\t\t<moreLabel>' + labelName + '</moreLabel>\n')
break
elif ascii_values[i] == 49: #判断字符串中第一个数字是否为1(0的ascll码为49)
xml.write('\t\t<name>' + "1" + '</name>\n')
xml.write('\t\t<moreLabel>' + labelName + '</moreLabel>\n')
break
else:
i += 1
else: #当labelName为0/1时
xml.write('\t\t<name>' + labelName + '</name>\n')
moreLabelName = "null"
xml.write('\t\t<moreLabel>' + moreLabelName + '</moreLabel>\n')
xml.write('\t\t<pose>Unspecified</pose>\n')
xml.write('\t\t<truncated>1</truncated>\n')
xml.write('\t\t<difficult>0</difficult>\n')
xml.write('\t\t<bndbox>\n')
xml.write('\t\t\t<xmin>' + str(int(xmin)) + '</xmin>\n')
xml.write('\t\t\t<ymin>' + str(int(ymin)) + '</ymin>\n')
xml.write('\t\t\t<xmax>' + str(int(xmax)) + '</xmax>\n')
xml.write('\t\t\t<ymax>' + str(int(ymax)) + '</ymax>\n')
xml.write('\t\t</bndbox>\n')
xml.write('\t</object>\n')
print(json_filename, xmin, ymin, xmax, ymax, labelName)
xml.write('</annotation>')
# 5.复制图片到 data_xml/BMPImages/下
image_files = glob(image_path + "*.bmp")
print("copy image files to data_xml/BMPImages/")
for image in image_files:
shutil.copy(image, saved_path + "BMPImages/")
# 6.拆分训练集、测试集、验证集
txtsavepath = saved_path + "ImageSets/Main/"
ftrainval = open(txtsavepath + '/trainval.txt', 'w')
ftest = open(txtsavepath + '/test.txt', 'w')
ftrain = open(txtsavepath + '/train.txt', 'w')
fval = open(txtsavepath + '/val.txt', 'w')
total_files = glob(saved_path + "Annotations/*.xml")
total_files = [i.replace("\\", "/").split("/")[-1].split(".xml")[0] for i in total_files]
trainval_files = []
test_files = []
if isUseTest:
trainval_files, test_files = train_test_split(total_files, test_size=0.15, random_state=55)
else:
trainval_files = total_files
for file in trainval_files:
ftrainval.write(file + "\n")
# split
train_files, val_files = train_test_split(trainval_files, test_size=0.15, random_state=55)
# train
for file in train_files:
ftrain.write(file + "\n")
# val
for file in val_files:
fval.write(file + "\n")
for file in test_files:
print(file)
ftest.write(file + "\n")
ftrainval.close()
ftrain.close()
fval.close()
ftest.close()