Foggy_driving数据集官方下载地址
百度网盘下载链接:https://pan.baidu.com/s/1q4dhnlX-doxlt13Mi-uFZQ
提取码:2ap3
VOC格式的Foggy_driving数据集百度网盘下载链接:https://pan.baidu.com/s/14bIND62U0wyhXLvUy5nJFQ
提取码:ekrn
将Foggy_driving数据集转换成VOC数据格式的python代码如下:
###Foggy_driving中的gtbox标注为txt格式,现工作需要将目标检测标注转为pascal voc格式的标注格式
###Foggy_driving数据集的图片在leftImg8bit中(且分为了test:pedestrian、public和test_extra:pedestrain、web)
###目标检测的标注在bboxGt中(且分为了test:pedestrian、public和test_extra:pedestrain、web)
####所以需要将bounding_box标注放到同一个文件夹中,方便读取
import os, sys
import glob
from PIL import Image
# 图像存储位置
src_img_dir = "D:/XUEXI/CODES/Detections/DATASET/Foggy_Driving/Foggy_Driving/VOC2007/JPEGImages"
# 图像的 ground truth 的 txt 文件存放位置
src_txt_dir = "D:/XUEXI/CODES/Detections/DATASET/Foggy_Driving/bboxGt"
###生成的xml文件想要保存的位置
src_xml_dir = "D:/XUEXI/CODES/Detections/DATASET/Foggy_Driving/Foggy_Driving/VOC2007/Annotations"
### - `bboxGt`
###the bounding box annotations induced from the above semantic annotations, available for all 101 images of the dataset.
# Annotations are encoded as `txt` files, in which each line corresponds to a single object and is formatted as
# ```
# {class} {xmin} {ymin} {xmax} {ymax}
# ```
# `class` stands for the ID of the class this object belongs to, and the rest four elements encode the extent of
# its bounding box in 1-based integer pixel coordinates. The 8 relevant classes are encoded with the following IDs:
# - *car*: 0
# - *person*: 1
# - *bicycle*: 2
# - *bus*: 3
# - *truck*: 4
# - *train*: 5
# - *motorcycle*: 6
# - *rider*: 7
classes = {'0': 'car','1': 'person','2': 'bicycle', '3': 'bus', '4': 'truck', '5': 'train', '6': 'motorcycle',
'7': 'rider',}
img_Lists = glob.glob(src_img_dir + '/*.png')
img_basenames = [] # e.g. 100.jpg
for item in img_Lists:
img_basenames.append(os.path.basename(item))
img_names = [] # e.g. 100
for item in img_basenames:
temp1, temp2 = os.path.splitext(item)
img_names.append(temp1)
image_ids = open(r'D:\XUEXI\CODES\Detections\DATASET\Foggy_Driving\Foggy_Driving\VOC2007\ImageSets\Main\val.txt', 'a')###val.txt需要提前新建
image_ids.write('% s\n'% (temp1))
image_ids.close()
for img in img_names:
im = Image.open((src_img_dir + '/' + img + '.png'))
width, height = im.size
# open the crospronding txt file
###txt的文件名:pedestrian_20161201_101324.txt 图像的文件名:pedestrian_20161201_101324_leftImg8bit.png 所以img[:-12]
gt = open(src_txt_dir + '/' + img[:-12] + '.txt').read().splitlines()###{class} {xmin} {ymin} {xmax} {ymax} 此时的gt为List 但是length为1
# write in xml file
# os.mknod(src_xml_dir + '/' + img + '.xml')
xml_file = open((src_xml_dir + '/' + img + '.xml'), 'w')
xml_file.write('<annotation>\n')
xml_file.write(' <folder>VOC2007</folder>\n')
xml_file.write(' <filename>' + str(img) + '.png' + '</filename>\n')
xml_file.write(' <size>\n')
xml_file.write(' <width>' + str(width) + '</width>\n')
xml_file.write(' <height>' + str(height) + '</height>\n')
xml_file.write(' <depth>3</depth>\n')
xml_file.write(' </size>\n')
# write the region of image on xml file
for img_each_label in gt:
spt = img_each_label.split(' ') # 这里如果txt里面是以逗号‘,’隔开的,那么就改为spt = img_each_label.split(',')。
xml_file.write(' <object>\n')
xml_file.write(' <name>' + str(classes[spt[0]]) + '</name>\n')
xml_file.write(' <pose>Unspecified</pose>\n')
xml_file.write(' <truncated>0</truncated>\n')
xml_file.write(' <difficult>0</difficult>\n')
xml_file.write(' <bndbox>\n')
xml_file.write(' <xmin>' + str(spt[1]) + '</xmin>\n')
xml_file.write(' <ymin>' + str(spt[2]) + '</ymin>\n')
xml_file.write(' <xmax>' + str(spt[3]) + '</xmax>\n')
xml_file.write(' <ymax>' + str(spt[4]) + '</ymax>\n')
xml_file.write(' </bndbox>\n')
xml_file.write(' </object>\n')
xml_file.write('</annotation>')