数据格式定义
Xml转txt
import os
import os.path
import xml.etree.ElementTree as ET
import glob
class_names = ['opium']
xmlpath = 'xml文件路径'
txtpath = '保存txt文件路径'
def xml_to_txt(xmlpath, txtpath):
os.chdir(xmlpath)
annotations = os.listdir('.')
annotations = glob.glob(str(annotations) + '*.xml')
# file_save = 'train' + '.txt'
# file_txt = os.path.join(txtpath, file_save)
# f_w = open(file_txt, 'w')
for i, file in enumerate(annotations):
in_file = open(file)
file = file[:-4]
file_txt = txtpath + file + '.txt'
f_w = open(file_txt, 'w')
print(file_txt)
tree = ET.parse(in_file)
root = tree.getroot()
for obj in root.iter('object'):
current = list()
name = obj.find('name').text
class_num = class_names.index(name)
xmlbox = obj.find('bndbox')
x1 = int(xmlbox.find('xmin').text)
x2 = int(xmlbox.find('xmax').text)
y1 = int(xmlbox.find('ymin').text)
y2 = int(xmlbox.find('ymax').text)
f_w.write(str(x1) + ',' + str(y1) + ',' + str(x2-x1) + ',' + str(y2-y1) + ',' + '1,1,0,0' + '\n')
# f_w.write(x1 + ',' + y1 + ',' + x2 - x1 + ',' + y2 - y1 + ',1,1,-1,-1' + '\n')
f_w.close
xml_to_txt(xmlpath, txtpath)
对于生成的txt文件,以4的倍数txt文件必须有一个object_category项为4
否则无法生成.tfrecord文件
参考:
https://blog.csdn.net/qq_36401512/article/details/85261576
https://blog.csdn.net/Angela_qin/article/details/80944604
源码:https://github.com/yangxue0827/FPN_Tensorflow