使⽤YOLOv3训练BDD100K数据集之标签格式转换
BDD100K数据集介绍:
BDD100K的道路⽬标检测部分总共有10类:bus,traffic light,traffic sign,person,bike,truck,moter,car,train,rider。BDD100K数据集的下载(百度云)
链接:pan.baidu/s/1fFSzGJt6Op4k7Gyo9QjtYA
提取码:kuld
⼀般只需要下载解压bdd100k_labels.zip和bdd100k_images.zip,会出现两个bdd100k⽂件夹,这两个⽂件夹内分别存储了images和labels两个⼦⽂件夹,其中images⽂件夹内存放了1280x720的图⽚,labels存放了json格式的标签⽂件,我们把两个⼦⽂件夹合并到⼀个bdd100k⽂件夹内,⽅便查看和处理。⽂件⽬录树如下:
├── bdd100k
│├── images
││├── 100k
│││├── test # 20k测试集图⽚
│││├── train # 70k训练集图⽚
│││└── val # 10k验证集图⽚
││└── 10k
getsavefilename││├── test # 2k测试集图⽚
││├── train # 7k测试集图⽚
││└── val # 1k验证集图⽚
│├── labels
││└── 100k
││├── train # 70k训练集标签
││├── val # 10k验证集标签,这⾥没有提供20k测试集标签
⾸先,我们需要两个辅助脚本:parseJson.py(⽤来解析json⾥⾯的对象检测部分的数据)和pascal_voc_io.py(创建VOC格式的xml,并把json⽂件的数据填充到xml)。
parseJson.py
#!/usr/bin/env python
# -*- coding: utf8 -*-
#parse json,input json filename,output info needed by voc
import json
#这⾥是我需要的10个类别
categorys = ['car', 'bus', 'person', 'bike', 'truck', 'motor', 'train', 'rider', 'traffic sign', 'traffic light']
def parseJson(jsonFile):
'''
params:
jsonFile -- BDD00K数据集的⼀个json标签⽂件
return:
返回⼀个列表的列表,存储了⼀个json⽂件⾥⾯的⽅框坐标及其所属的类,
形如:[[325, 342, 376, 384, 'car'], [245, 333, 336, 389, 'car']]
'''
objs = []
obj = []
f = open(jsonFile)
info = json.load(f)
objects = info['frames'][0]['objects']
for i in objects:
if(i['category'] in categorys):
obj.append(int(i['box2d']['x1']))
obj.append(int(i['box2d']['y1']))
obj.append(int(i['box2d']['x2']))
obj.append(int(i['box2d']['y2']))
obj.append(i['category'])
objs.append(obj)
obj = []
#print("objs",objs)
return objs
#test
#result = parseJson("/media/xavier/SSD256/global_datasets/BDD00K/bdd100k/labels/100k/val/b1c9c847-3bda4659.json") #print(len(result))
#print(result)
pascal_voc_io.py`
#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys
import os
import ElementTree
ElementTree import Element, SubElement
from lxml import etree
from xml.dom.minidom import parseString
class PascalVocWriter:
def __init__(self, foldername, filename, imgSize, databaseSrc='Unknown', localImgPath=None):
'''
params:
foldername -- 要存储的xml⽂件的⽗⽬录
filename -- xml⽂件的⽂件名
imgSize -- 图⽚的尺⼨
databaseSrc -- 数据库名,这⾥不需要,默认为Unknown
localImaPath -- xml⽂件⾥⾯的<path></path>标签的内容
'''
self.foldername = foldername
self.filename = filename
self.databaseSrc = databaseSrc
self.imgSize = imgSize
self.boxlist = []
self.localImgPath = localImgPath
def prettify(self, elem):
"""
params:
elem -- xml的根标签,以<annotation>开始
return:
返回⼀个美观输出的xml(⽤到minidom),本质是⼀个str
"""
xml = string(elem)
dom = parseString(xml)
# prettyxml(' '))
prettifyResult = prettyxml(' ')
return prettifyResult
def genXML(self):
"""
return:
⽣成⼀个VOC格式的xml,返回⼀个xml的根标签,以<annotation>开始
"""
# Check conditions
if self.filename is None or \
self.foldername is None or \
self.imgSize is None or \
len(self.boxlist) <= 0:
return None
top = Element('annotation') # 创建⼀个根标签<annotation>
folder = SubElement(top, 'folder') # 在根标签<annotation>下创建⼀个⼦标签<folder>
< = self.foldername # ⽤self.foldername的数据填充⼦标签<folder>
filename = SubElement(top, 'filename') # 在根标签<annotation>下创建⼀个⼦标签<filename>
< = self.filename # ⽤self.filename的数据填充⼦标签<filename>
localImgPath = SubElement(top, 'path') # 在根标签<annotation>下创建⼀个⼦标签<path>
< = self.localImgPath # ⽤self.localImgPath的数据填充⼦标签<path>
source = SubElement(top, 'source') # 在根标签<annotation>下创建⼀个⼦标签<source>
database = SubElement(source, 'database') # 在根标签<source>下创建⼀个⼦标签<database>
< = self.databaseSrc # ⽤self.databaseSrc的数据填充⼦标签<database>
size_part = SubElement(top, 'size') # 在根标签<annotation>下创建⼀个⼦标签<size>
width = SubElement(size_part, 'width') # 在根标签<size>下创建⼀个⼦标签<width>
height = SubElement(size_part, 'height') # 在根标签<size>下创建⼀个⼦标签<height>
depth = SubElement(size_part, 'depth') # 在根标签<size>下创建⼀个⼦标签<depth>
< = str(self.imgSize[1]) # ⽤self.imgSize[1]的数据填充⼦标签<width>
< = str(self.imgSize[0]) # ⽤self.imgSize[0]的数据填充⼦标签<height>
if len(self.imgSize) == 3: # 如果图⽚深度为3,则⽤self.imgSize[2]的数据填充⼦标签<height>,否则⽤1填充 = str(self.imgSize[2])
else:
< = '1'
segmented = SubElement(top, 'segmented')
< = '0'
return top
def addBndBox(self, xmin, ymin, xmax, ymax, name):
'''
将检测对象框坐标及其对象类别作为⼀个字典加⼊到self.boxlist中
params:
xmin -- 检测框的左上⾓的x坐标
ymin -- 检测框的左上⾓的y坐标
xmax -- 检测框的右下⾓的x坐标
ymax -- 检测框的右下⾓的y坐标
name -- 检测框内的对象类别名
'''
bndbox = {'xmin': xmin, 'ymin': ymin, 'xmax': xmax, 'ymax': ymax}
bndbox['name'] = name
self.boxlist.append(bndbox)
def appendObjects(self, top):
'''
在xml⽂件中加⼊检测框的坐标及其对象类别名
params:
top -- xml的根标签,以<annotation>开始
'''
for each_object in self.boxlist:
object_item = SubElement(top, 'object')
name = SubElement(object_item, 'name')
< = str(each_object['name'])
pose = SubElement(object_item, 'pose')
< = "Unspecified"
truncated = SubElement(object_item, 'truncated')
< = "0"
difficult = SubElement(object_item, 'Difficult')
< = "0"
bndbox = SubElement(object_item, 'bndbox')
xmin = SubElement(bndbox, 'xmin')
< = str(each_object['xmin'])
ymin = SubElement(bndbox, 'ymin')
< = str(each_object['ymin'])
xmax = SubElement(bndbox, 'xmax')
< = str(each_object['xmax'])
ymax = SubElement(bndbox, 'ymax')
< = str(each_object['ymax'])
def save(self, targetFile=None):
'''
以美观输出的xml格式来保存xml⽂件
params:
targetFile -- 存储的xml⽂件名,不包括.xml部分
'''
root = XML()
self.appendObjects(root)
out_file = None
subdir = self.foldername.split('/')[-1]
if not os.path.isdir(subdir):
os.mkdir(subdir)
if targetFile is None:
with open(self.foldername+'/'+self.filename + '.xml', 'w') as out_file: prettifyResult = self.prettify(root)
out_file.write(prettifyResult)
out_file.close()
else:
with open(targetFile, 'w') as out_file:
prettifyResult = self.prettify(root)
out_file.write(prettifyResult)
out_file.close()
class PascalVocReader:
def __init__(self, filepath):
# shapes type:
# [labbel, [(x1,y1), (x2,y2), (x3,y3), (x4,y4)], color, color]
# [labbel, [(x1,y1), (x2,y2), (x3,y3), (x4,y4)], color, color]
self.shapes = []
self.filepath = filepath
self.parseXML()
def getShapes(self):
return self.shapes
def addShape(self, label, bndbox):
xmin = int(bndbox.find('xmin').text)
ymin = int(bndbox.find('ymin').text)
xmax = int(bndbox.find('xmax').text)
ymax = int(bndbox.find('ymax').text)
points = [(xmin, ymin), (xmax, ymin), (xmax, ymax), (xmin, ymax)] self.shapes.append((label, points, None, None))
def parseXML(self):
assert dswith('.xml'), "Unsupport file format"
parser = etree.XMLParser(encoding='utf-8')
xmltree = ElementTree.parse(self.filepath, parser=parser).getroot() filename = xmltree.find('filename').text
for object_iter in xmltree.findall('object'):
bndbox = object_iter.find("bndbox")
label = object_iter.find('name').text
self.addShape(label, bndbox)
return True
# tempParseReader = PascalVocReader('l')
# Shapes()
#"""
# Test
#tmp = PascalVocWriter('temp','test', (10,20,3))
#tmp.addBndBox(10,10,20,30,'chair')
#tmp.addBndBox(1,1,600,600,'car')
#tmp.save()
#"""
其次,我们创建⼀个脚本bdd2voc.py将两个辅助脚本整合起来。
bdd2voc.py
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论