目的:将kitti、Lisa資料集合并,進行訓練
一、資料集準備,将兩種資料集準備成VOC格式
kitti資料集(車輛行人等):http://www.cvlibs.net/datasets/kitti/eval_object.php
lisa資料集(47種交通标志):http://cvrr.ucsd.edu/LISA/lisa-traffic-sign-dataset.html
1、準備kitti資料集
建立VOCdevkit/traffic,并為其建立子目錄:Annotations,ImageSets,JPEGImages
根據VOC格式要求,建立檔案夾,解壓kitti資料集得到:image_2,label_2兩個檔案,分别為圖檔、标注。
标注資訊儲存在txt檔案,内容格式:Car 0.96 0 -0.86 0.00 199.21 302.21 369.00 1.50 1.78 3.69 -3.17 1.66 3.35 -1.57
将image_2内容複制到JPEGImages目錄,
在traffic目錄下建立create_kitti_xml.py腳本,生成xml格式的标注,參考:https://github.com/manutdzou/KITTI_SSD/blob/master/data/KITTI/KITTI_xml.py
from xml.dom.minidom import Document
import cv2
import os
def generate_xml(name,split_lines,img_size,class_ind):
doc = Document() # 建立DOM文檔對象
annotation = doc.createElement('annotation')
doc.appendChild(annotation)
title = doc.createElement('folder')
title_text = doc.createTextNode('KITTI')
title.appendChild(title_text)
annotation.appendChild(title)
img_name=name+'.png'
title = doc.createElement('filename')
title_text = doc.createTextNode(img_name)
title.appendChild(title_text)
annotation.appendChild(title)
source = doc.createElement('source')
annotation.appendChild(source)
title = doc.createElement('database')
title_text = doc.createTextNode('The KITTI Database')
title.appendChild(title_text)
source.appendChild(title)
title = doc.createElement('annotation')
title_text = doc.createTextNode('KITTI')
title.appendChild(title_text)
source.appendChild(title)
size = doc.createElement('size')
annotation.appendChild(size)
title = doc.createElement('width')
title_text = doc.createTextNode(str(img_size[1]))
title.appendChild(title_text)
size.appendChild(title)
title = doc.createElement('height')
title_text = doc.createTextNode(str(img_size[0]))
title.appendChild(title_text)
size.appendChild(title)
title = doc.createElement('depth')
title_text = doc.createTextNode(str(img_size[2]))
title.appendChild(title_text)
size.appendChild(title)
for split_line in split_lines:
line=split_line.strip().split()
if line[0] in class_ind:
object = doc.createElement('object')
annotation.appendChild(object)
title = doc.createElement('name')
title_text = doc.createTextNode(line[0])
title.appendChild(title_text)
object.appendChild(title)
bndbox = doc.createElement('bndbox')
object.appendChild(bndbox)
title = doc.createElement('xmin')
title_text = doc.createTextNode(str(int(float(line[4]))))
title.appendChild(title_text)
bndbox.appendChild(title)
title = doc.createElement('ymin')
title_text = doc.createTextNode(str(int(float(line[5]))))
title.appendChild(title_text)
bndbox.appendChild(title)
title = doc.createElement('xmax')
title_text = doc.createTextNode(str(int(float(line[6]))))
title.appendChild(title_text)
bndbox.appendChild(title)
title = doc.createElement('ymax')
title_text = doc.createTextNode(str(int(float(line[7]))))
title.appendChild(title_text)
bndbox.appendChild(title)
# 将DOM對象doc寫入檔案
f = open('./Annotations/'+name+'.xml','w')
f.write(doc.toprettyxml(indent = ''))
f.close()
if __name__ == '__main__':
class_ind=('Pedestrian', 'Car', 'Cyclist')
cur_dir=os.getcwd()
labels_dir=os.path.join(cur_dir,'label_2')
for parent, dirnames, filenames in os.walk(labels_dir): # 分别得到根目錄,子目錄和根目錄下檔案
for file_name in filenames:
full_path=os.path.join(parent, file_name) # 擷取檔案全路徑
f=open(full_path)
split_lines = f.readlines()
name= file_name[:-4] # 後四位是擴充名.txt,隻取前面的檔案名
img_name=name+'.png'
img_path=os.path.join('/data/wuyan/data/VOCdevkit/traffic/JPEGImages',img_name) # 路徑需要自行修改
img_size=cv2.imread(img_path).shape
generate_xml(name,split_lines,img_size,class_ind)
執行create_kitti_xml.py,會在Annotations目錄下生成xml格式的标注資訊。
2、準備Lisa資料集
建立一個目錄signdatabase,将Lisa資料集解壓到該目錄,解壓後得到 多個目錄(圖檔),其中有一個allAnnotations.csv标注檔案,其内容格式為:
Filename;Annotation tag;Upper left corner X;Upper left corner Y;Lower right corner X;Lower right corner Y;Occluded,On another road;Origin file;Origin frame number;Origin track;Origin track frame number
為解析友善,删除該檔案第一行内容
同樣在traffic目錄下編寫一個腳本create_lisa_xml.py,來生成xml格式的标注檔案
from xml.dom.minidom import Document
import xml.dom.minidom
import cv2
import os
def generate_xml(name,data,img_size):
file = './Annotations/' + name + '.xml'
#如果已經存在,讀出xml内容,插入一個節點
if os.path.exists(file):
old_xml = xml.dom.minidom.parse(file)
new_object = old_xml.createElement('object')
title = old_xml.createElement('name')
title_text = old_xml.createTextNode(data[1])
title.appendChild(title_text)
new_object.appendChild(title)
bndbox = old_xml.createElement('bndbox')
new_object.appendChild(bndbox)
title = old_xml.createElement('xmin')
title_text = old_xml.createTextNode(str(int(float(data[2]))))
title.appendChild(title_text)
bndbox.appendChild(title)
title = old_xml.createElement('ymin')
title_text = old_xml.createTextNode(str(int(float(data[3]))))
title.appendChild(title_text)
bndbox.appendChild(title)
title = old_xml.createElement('xmax')
title_text = old_xml.createTextNode(str(int(float(data[4]))))
title.appendChild(title_text)
bndbox.appendChild(title)
title = old_xml.createElement('ymax')
title_text = old_xml.createTextNode(str(int(float(data[5]))))
title.appendChild(title_text)
bndbox.appendChild(title)
annotation = old_xml.getElementsByTagName('annotation')[0]
annotation.appendChild(new_object)
# 将DOM對象doc寫入檔案
os.remove(file)
f = open(file,'w')
f.write(old_xml.toprettyxml(indent = ''))
f.close()
#不存在
else:
doc = Document() # 建立DOM文檔對象
annotation = doc.createElement('annotation')
doc.appendChild(annotation)
title = doc.createElement('folder')
title_text = doc.createTextNode('KITTI')
title.appendChild(title_text)
annotation.appendChild(title)
img_name = name + '.png'
title = doc.createElement('filename')
title_text = doc.createTextNode(img_name)
title.appendChild(title_text)
annotation.appendChild(title)
source = doc.createElement('source')
annotation.appendChild(source)
title = doc.createElement('database')
title_text = doc.createTextNode('The KITTI Database')
title.appendChild(title_text)
source.appendChild(title)
title = doc.createElement('annotation')
title_text = doc.createTextNode('KITTI')
title.appendChild(title_text)
source.appendChild(title)
size = doc.createElement('size')
annotation.appendChild(size)
title = doc.createElement('width')
title_text = doc.createTextNode(str(img_size[1]))
title.appendChild(title_text)
size.appendChild(title)
title = doc.createElement('height')
title_text = doc.createTextNode(str(img_size[0]))
title.appendChild(title_text)
size.appendChild(title)
title = doc.createElement('depth')
title_text = doc.createTextNode(str(img_size[2]))
title.appendChild(title_text)
size.appendChild(title)
object = doc.createElement('object')
annotation.appendChild(object)
title = doc.createElement('name')
title_text = doc.createTextNode(data[1])
title.appendChild(title_text)
object.appendChild(title)
bndbox = doc.createElement('bndbox')
object.appendChild(bndbox)
title = doc.createElement('xmin')
title_text = doc.createTextNode(str(int(float(data[2]))))
title.appendChild(title_text)
bndbox.appendChild(title)
title = doc.createElement('ymin')
title_text = doc.createTextNode(str(int(float(data[3]))))
title.appendChild(title_text)
bndbox.appendChild(title)
title = doc.createElement('xmax')
title_text = doc.createTextNode(str(int(float(data[4]))))
title.appendChild(title_text)
bndbox.appendChild(title)
title = doc.createElement('ymax')
title_text = doc.createTextNode(str(int(float(data[5]))))
title.appendChild(title_text)
bndbox.appendChild(title)
# 将DOM對象doc寫入檔案
f = open(file,'w')
f.write(doc.toprettyxml(indent = ''))
f.close()
def find_last(string, str):
last_position = -1
while True:
position = string.find(str, last_position + 1)
if position == -1:
return last_position
last_position = position
if __name__ == '__main__':
#隻有這些類别可用
class_ind=('addedLane', 'curveLeft', 'curveRight', 'keepRight', 'laneEnds', 'merge', 'noLeftTurn', 'noRightTurn', 'pedestrianCrossing', 'roundabout', 'signalAhead', 'stopAhead', 'turnLeft', 'turnRight', 'yield')
with open('allAnnotations.csv', 'r') as r_tdf:
for each_line in r_tdf:
data = each_line.strip().split(';')
file_path = data[0] #img file obsolute path
tag = data[1]
img_size = cv2.imread(file_path).shape
name = file_path[find_last(file_path, '/') + 1:-4]
if tag in class_ind:
print('name : ' + name)
generate_xml(name, data, img_size)
執行該腳本後,會在Annotations目錄下生成Lisa資料集的标注檔案。
最後需要将Lisa所有标注的圖檔複制到JPEGImages下。
資料集準備先到這裡。。。下篇再寫如何訓練