在完成目标检测任务时,常用的标注数据格式是voc数据集的xml格式和coco数据集的json格式,有时候,我们需要完成标注信息格式的变化,下面主要是对我自己的过程做个记录。
首先需要明白自己的原始标注信息的样子,可能还需要自己修改一下代码,下面我放上自己的 ground truth 信息和代码。
import os,sys
import glob
from PIL import Image
src_img_dir = "E:/Dataset/OTCBVS_dataset/first_walk/all_image"
src_txt_dir = "E:/Dataset/OTCBVS_dataset/first_walk/groud_truth/groundTruth_all.txt"
src_ann_dir = "E:/Dataset/OTCBVS_dataset/first_walk/xml_file"
# 取出图片的路径 如: C:/USer/1.jpg
img_Lists = glob.glob(src_img_dir+'/*.bmp')
img_basenames = []
for item in img_Lists: # 取出最后的文件名 如:1.jpg
img_basenames.append(os.path.basename(item))
img_names = []
for item in img_basenames:
# os.path.splitext :分离文件名与扩展名
# 如 :1
temp1,temp2 = os.path.splitext(item)
img_names.append(temp1)
# 这里是取出我的groud truth,然后进行处理
gt = open(src_txt_dir).read().splitlines()
# 我的ground truth前4行不需要,所以删除
gt = gt[4:]
index = 0
for img in img_names:
im = Image.open(src_img_dir+'/'+img+'.bmp')
width,height = im.size
#print(width,height)
# open the crospronding txt file
#gt = open(src_txt_dir+'/'+img+'.txt').read().splitlines()
#print(gt)
xml_file = open((src_ann_dir + '/' + img + '.xml'), 'w')
xml_file.write('<annotation>\n')
xml_file.write('\t<folder>simple</folder>\n')
xml_file.write('\t<filename>' + str(img) + '.bmp' + '</filename>\n')
xml_file.write('\t<source>\n')
xml_file.write('\t\t<database>' + 'The walk Database' + '</database>\n')
xml_file.write('\t\t<annotation>' + 'walk' + '</annotation>\n')
xml_file.write('\t\t<image>flickr</image>\n')
xml_file.write('\t\t<flickrid>325991873</flickrid>\n')
xml_file.write('\t</source>\n')
# xml_file.write('\t<owner>\n')
# xml_file.write('\t\t<flickrid>archin</flickrid>\n')
# xml_file.write('\t\t<name>?</name>\n')
# xml_file.write('\t</owner>\n')
xml_file.write('\t<size>\n')
xml_file.write('\t\t<width>' + str(width) + '</width>\n')
xml_file.write('\t\t<height>' + str(height) + '</height>\n')
xml_file.write('\t\t<depth>1</depth>\n')
xml_file.write('\t</size>\n')
xml_file.write('\t<segmented>0</segmented>\n')
print(gt[index])
spt_kuohao = gt[index].split('(')
spt_space = gt[index].split(' ')
num = int(spt_space[1])
print(num)
spt_kuohao = spt_kuohao[1:]
for i in range(num):
b = spt_kuohao[i].split(')')[0].split(' ')
print(b)
xml_file.write('\t<object>\n')
xml_file.write('\t\t<name>person</name>\n')
xml_file.write('\t\t<pose>Unspecified</pose>\n')
xml_file.write('\t\t<truncated>0</truncated>\n')
xml_file.write('\t\t<difficult>0</difficult>\n')
xml_file.write('\t\t<bndbox>\n')
xml_file.write('\t\t\t<xmin>' + str(b[0]) + '</xmin>\n')
xml_file.write('\t\t\t<ymin>' + str(b[1]) + '</ymin>\n')
xml_file.write('\t\t\t<xmax>' + str(b[2]) + '</xmax>\n')
xml_file.write('\t\t\t<ymax>' + str(b[3]) + '</ymax>\n')
xml_file.write('\t\t</bndbox>\n')
xml_file.write('\t</object>\n')
index = index+1
xml_file.write('</annotation>')
原始标注信息的样式对过程的处理很重要,很多时候需要修改上面的代码。下面是我的groud truth:
% File Format:
% NumberOfFrames
% [Filename NumberOfBoxesInFrame [ ( UpperLeftXPixel UpperLeftYPixel LowerRightXPixel LowerRightYPixel ) ]+ ]+
284
img_00001.bmp 5 (1 88 22 116) (23 189 41 213) (101 127 122 155) (149 141 171 170) (291 144 312 172)
img_00002.bmp 5 (18 62 38 89) (77 96 98 124) (116 164 136 190) (93 191 111 215) (237 149 259 178)
img_00003.bmp 1 (23 54 46 84)
img_00004.bmp 1 (28 34 50 63)
img_00005.bmp 2 (28 22 49 50) (7 180 30 210)
img_00006.bmp 2 (24 11 46 40) (51 177 74 208)
img_00007.bmp 2 (24 5 45 33) (75 175 98 206)
img_00008.bmp 2 (23 2 43 29) (94 173 117 203)
img_00009.bmp 1 (196 162 217 190)
img_00010.bmp 1 (214 158 237 188)
img_00011.bmp 1 (254 153 274 180)
img_00012.bmp 1 (280 149 300 176)
img_00013.bmp 1 (326 143 347 171)