本文是kaggle Airbus Ship Detection Challenge 数据集转化为coco格式的基础上将其转化为适合yolo训练的voc格式。
kaggle Airbus Ship Detection Challenge 训练集下载:https://blog.csdn.net/weixin_42880443/article/details/82015347
kaggle Airbus Ship Detection Challenge 训练集预处理(转化为coco)格式:https://github.com/pascal1129/kaggle_airbus_ship_detection/tree/master/0_rle_to_coco
coco数据集样式
处理好的coco_label大致长这样,一共是29000余张可用图片,上面是图片id,下面是图片label
运行程序
#-*- coding:utf-8-*-
import xml.dom
import xml.dom.minidom
import os
# from PIL import Image
import cv2
import json
# xml文件规范定义
_IMAGE_PATH = 'E:/coco/COCO/train'
_INDENT = '' * 4
_NEW_LINE = '\n'
_FOLDER_NODE = 'COCO2014'
_ROOT_NODE = 'annotation'
_DATABASE_NAME = 'LOGODection'
_ANNOTATION = 'COCO2014'
_AUTHOR = 'CSDN'
_SEGMENTED = '0'
_DIFFICULT = '0'
_TRUNCATED = '0'
_POSE = 'Unspecified'
# _IMAGE_COPY_PATH= 'JPEGImages'
_ANNOTATION_SAVE_PATH = '/home/casiie/dehaze/airbus/dataset/annon'
# _IMAGE_CHANNEL= 3
# 封装创建节点的过程
def createElementNode(doc, tag, attr): #创建一个元素节点
element_node = doc.createElement(tag)
# 创建一个文本节点
text_node = doc.createTextNode(attr)
# 将文本节点作为元素节点的子节点
element_node.appendChild(text_node)
return element_node
# 封装添加一个子节点
def createChildNode(doc, tag, attr, parent_node):
child_node = createElementNode(doc,tag, attr)
parent_node.appendChild(child_node)
# object节点比较特殊
def createObjectNode(doc, attrs):
object_node =doc.createElement('object')
midname=attrs['id']
midname = midname-1
print(midname)
global i
createChildNode(doc, 'name', ('ship'),
object_node)
#createChildNode(doc, 'name',attrs['name'],
# object_node)
createChildNode(doc, 'pose',
_POSE, object_node)
createChildNode(doc, 'truncated',
_TRUNCATED,object_node)
createChildNode(doc, 'difficult',
_DIFFICULT,object_node)
bndbox_node = doc.createElement('bbox')
createChildNode(doc, 'xmin',str(int(attrs['bbox'][0])),
bndbox_node)
print(attrs['bbox'][0])
createChildNode(doc, 'ymin',str(int(attrs['bbox'][1])),
bndbox_node)
createChildNode(doc, 'xmax',str(int(attrs['bbox'][0] + attrs['bbox'][2])),
bndbox_node)
createChildNode(doc, 'ymax',str(int(attrs['bbox'][1] + attrs['bbox'][3])),
bndbox_node)
object_node.appendChild(bndbox_node)
return object_node
# 将documentElement写入XML文件
def writeXMLFile(doc, filename):
tmpfile = open('tmp.xml', 'w')
doc.writexml(tmpfile, addindent='' *4, newl='\n', encoding='utf-8')
tmpfile.close()
# 删除第一行默认添加的标记
fin = open('tmp.xml')
# print(filename)
fout = open(filename, 'w')
# print(os.path.dirname(fout))
lines = fin.readlines()
for line in lines[1:]:
if line.split():
fout.writelines(line)
# new_lines =''.join(lines[1:])
# fout.write(new_lines)
fin.close()
fout.close()
if __name__ == "__main__":
##读取图片列表
img_path ="/home/casiie/dehaze/airbus/dataset/train/"
fileList = os.listdir(img_path)
if fileList == 0:
os._exit(-1)
with open("instances_ships_train2018.json", encoding='utf-8') as f:
#line = f.read()
ann_data = json.load(f)
current_dirpath =os.path.dirname(os.path.abspath('__file__'))
#if notos.path.exists(_ANNOTATION_SAVE_PATH):
# os.mkdir(_ANNOTATION_SAVE_PATH)
# if notos.path.exists(_IMAGE_COPY_PATH):
# os.mkdir(_IMAGE_COPY_PATH)
for imageName in fileList:
saveName =imageName.strip(".jpg")
print(saveName)
# pos =fileList[xText].rfind(".")
# textName =fileList[xText][:pos]
# ouput_file = open(_TXT_PATH +'/' + fileList[xText])
# ouput_file =open(_TXT_PATH)
# lines = ouput_file.readlines()
xml_file_name =os.path.join(_ANNOTATION_SAVE_PATH, (saveName + '.xml'))
#print(xml_file_name)
# withopen(xml_file_name,"w") as f:
# pass
img =cv2.imread(os.path.join(img_path, imageName))
print(os.path.join(img_path,imageName))
# cv2.imshow(img)
height, width, channel =img.shape
print(height, width, channel)
my_dom = xml.dom.getDOMImplementation()
#print(my_dom)
doc = my_dom.createDocument(None,_ROOT_NODE, None)
#print(doc)
# 获得根节点
root_node = doc.documentElement
# folder节点
createChildNode(doc, 'folder',_FOLDER_NODE, root_node)
# filename节点
createChildNode(doc, 'filename',saveName + '.jpg', root_node)
# source节点
source_node =doc.createElement('source')
# source的子节点
createChildNode(doc, 'database',_DATABASE_NAME, source_node)
createChildNode(doc, 'annotation',_ANNOTATION, source_node)
createChildNode(doc, 'image','flickr', source_node)
createChildNode(doc, 'flickrid','NULL', source_node)
root_node.appendChild(source_node)
# owner节点
owner_node = doc.createElement('owner')
# owner的子节点
createChildNode(doc, 'flickrid','NULL', owner_node)
createChildNode(doc, 'name',_AUTHOR, owner_node)
root_node.appendChild(owner_node)
# size节点
size_node =doc.createElement('size')
createChildNode(doc, 'width',str(width), size_node)
createChildNode(doc, 'height',str(height), size_node)
createChildNode(doc, 'depth',str(channel), size_node)
root_node.appendChild(size_node)
# segmented节点
createChildNode(doc, 'segmented',_SEGMENTED, root_node)
for ann in ann_data["images"]:
#print(ann)
#print(ann["id"])
#检查image id与annotations是否相符
if(saveName+('.jpg') == ann['file_name']):
print(ann['file_name'])
r = ann['id']
print(r)
for bnn in ann_data["annotations"]:
if (r == bnn['id']):
cname=saveName
# object节点
object_node =createObjectNode(doc, bnn)
root_node.appendChild(object_node)
else:
continue
else:
continue
# 构建XML文件名称
print(xml_file_name)
# 创建XML文件
# createXMLFile(attrs, width,height, xml_file_name)
# # 写入文件
#
writeXMLFile(doc, xml_file_name)
这里注意更改路径
我生成的xml没有缩进,不过不影响使用
报错
TypeError: string indices must be integers
json节点名字错误,有些json的节点是双引号,有些为单引号
注意多层的话为[’’][’’]
中间有空层的加[0]
参考
https://blog.csdn.net/ouyangfushu/article/details/79543575
https://blog.csdn.net/yjl9122/article/details/56842098
https://blog.csdn.net/hehangjiang/article/details/79108232
https://blog.csdn.net/qq_34493665/article/details/84988064
https://www.cnblogs.com/laoniubile/p/6036919.html