之前大爷写了alfw的标注转换,我就大概看了一下,绝知此事要躬行啊,今天写了一下车牌的转换。
车牌用的是何大神的标注工具,大概长相:
# PASCAL Annotation Version 1.00 Image filename : "E:\lprset\train\images\_藏AU8666.jpg" Image size (X x Y x C) : 1198 x 1600 x 3 Database : "UESTC-IMAGE-PROCESSING-LAB" Objects with ground truth : 1 { "" } # Note that there might be other objects in the image # for which ground truth data has not been provided. # Top left pixel co-ordinates : (1, 1) # Details for object 1 ("") Original label for object 1 "" : "" Bounding box for object 1 "" (Xmin, Ymin) - (Xmax, Ymax) : (828, 555) - (955, 590)
问题1. 文件名中有中文,python写的时候出现了乱码,且以防后续问题,解决方案统一修改文件名。
问题2. 原文件夹文件目录层太多。
test/train下又各分为annotations和images。太复杂了。因为bing下的标注是annotations和images分开放在两个大文件夹下,所以写的时候图片直接写在上层目录test/train下。
bing的标注实际分为三部分。1. Main下的三个文件: text.txt, train.txt, class.txt:分别是测试、训练文件标注路径以及类别信息。2. Annotations下.yml的标注信息。3. ImageJPEG下.jpg的图片。通过.txt读取标注信息,再在标注信息里找到图片所在地。
import os import cv2 import re root = './lprset__OLD/' test_train = ['test', 'train'] anno_image = ['annotations', 'images'] count = 1 for te_tra in test_train: print te_tra path_file = open(te_tra+'.txt', 'w') #新旧读写上层路径 an_write_base = 'Annotations/' + te_tra an_read_base = root + te_tra + '/' + anno_image[0] im_write_base = root + te_tra im_read_base = root + te_tra + '/' + anno_image[1] #创建写目录 if not os.path.exists(an_write_base): os.makedirs(an_wirte_base) if not os.path.exists(im_write_base): os.makedirs(im_wirte_base) for r, dirs, file_list in os.walk(an_read_base): for files in file_list: #file_list标注文件列表 #提取旧标注中的数据 plate_file = open(an_read_base+'/'+files).read().split('\n') if len(plate_file) == 16: #只有一个标注 filename = files.split('.')[0] height, width = plate_file[3].split()[8], plate_file[3].split()[10] xy = re.findall('\d+', plate_file[14]) xmin, ymin = int(xy[1])-20, int(xy[2])-20 #原来框太小了,扩大一点 xmax, ymax = int(xy[3])+20, int(xy[4])+20 if xmin <= 0: xmin = 1 if ymin <= 0: ymin = 1 if ymax >= int(height): ymax = int(height)-1 if xmax >= int(width): xmax = int(width)-1 #读写图片 path = te_tra + '/' + str(count) + '.jpg' img = cv2.imread(im_read_base + '/' + filename + '.jpg') #cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0,0,255)) cv2.imwrite(im_write_base + '/' + str(count) + '.jpg', img) #写路径文件 path_file.write(te_tra + '/' + str(count) + '\n') #写新标注 file = open(an_write_base+'/'+str(count)+'.yml', 'w') print >> file, '%YAML:1.0\n' print >> file, 'annotation:' print >> file, ' folder: plate' print >> file, ' filename: \"%s\"'%path print >> file, ' source: UESTC-IMAGE-PROCESSING-LAB' print >> file, ' owner: {name: zhuqian}' print >> file, ' size: {width: \'%s\', height: \'%s\', depth: \'3\'}'%(width, height) print >> file, ' segmented: \'0\'' print >> file, ' object:' print >> file, ' - bndbox: {xmin: \'%s\', ymin: \'%s\', xmax: \'%s\', ymax: \'%s\'}'%(xmin, ymin, xmax, ymax) print >> file, ' name: plate' print >> file, ' pose: Left' print >> file, ' truncated: \'1\'' print >> file, ' difficult: \'0\'' file.close() count += 1 path_file.close()