WIDERFACE转VOC格式(Python3代码)及相关问题解决办法

    xiaoxiao2024-11-03  75

    WIDERFACE文件放置目录如下

    python3 代码

    # -*- coding: utf-8 -*- """ Created on 18-5-13 @author: CHR """ from skimage import io import shutil import random import os import string headstr = """\ <annotation> <folder>VOC2007</folder> <filename>d.jpg</filename> <source> <database>My Database</database> <annotation>PASCAL VOC2007</annotation> <image>flickr</image> <flickrid>NULL</flickrid> </source> <owner> <flickrid>NULL</flickrid> <name>company</name> </owner> <size> <width>%d</width> <height>%d</height> <depth>%d</depth> </size> <segmented>0</segmented> """ objstr = """\ <object> <name>%s</name> <pose>Unspecified</pose> <truncated>0</truncated> <difficult>0</difficult> <bndbox> <xmin>%d</xmin> <ymin>%d</ymin> <xmax>%d</xmax> <ymax>%d</ymax> </bndbox> </object> """ tailstr = '''\ </annotation> ''' def all_path(filename): return os.path.join('widerface', filename) def writexml(idx, head, bbxes, tail): filename = all_path("Annotations/d.xml" % (idx)) f = open(filename, "w") f.write(head) for bbx in bbxes: f.write(objstr % ('face', bbx[0], bbx[1], bbx[0] + bbx[2], bbx[1] + bbx[3])) f.write(tail) f.close() def clear_dir(): if shutil.os.path.exists(all_path('Annotations')): shutil.rmtree(all_path('Annotations')) if shutil.os.path.exists(all_path('ImageSets')): shutil.rmtree(all_path('ImageSets')) if shutil.os.path.exists(all_path('JPEGImages')): shutil.rmtree(all_path('JPEGImages')) shutil.os.mkdir(all_path('Annotations')) shutil.os.makedirs(all_path('ImageSets/Main')) shutil.os.mkdir(all_path('JPEGImages')) def excute_datasets(idx, datatype): f = open(all_path('ImageSets/Main/' + datatype + '.txt'), 'a') print('wider_face_split/wider_face_' + datatype + '_bbx_gt.txt') f_bbx = open(all_path('wider_face_split/wider_face_' + datatype + '_bbx_gt.txt'), 'r') while True: filename = f_bbx.readline().strip('\n') if not filename: break #print(all_path('WIDER_' + datatype + '/images/' + filename)) im = io.imread(all_path('WIDER_' + datatype + '/images/' + filename)) head = headstr % (idx, im.shape[1], im.shape[0], im.shape[2]) nums = f_bbx.readline().strip('\n') bbxes = [] for ind in range(int(nums)): bbx_info = f_bbx.readline().strip(' \n').split(' ') bbx = [int(bbx_info[i]) for i in range(len(bbx_info))] # x1, y1, w, h, blur, expression, illumination, invalid, occlusion, pose if bbx[7] == 0: bbxes.append(bbx) writexml(idx, head, bbxes, tailstr) shutil.copyfile(all_path('WIDER_' + datatype + '/images/' + filename), all_path('JPEGImages/d.jpg' % (idx))) f.write('d\n' % (idx)) idx += 1 f.close() f_bbx.close() return idx # 打乱样本 def shuffle_file(filename): f = open(filename, 'r+') lines = f.readlines() random.shuffle(lines) f.seek(0) f.truncate() f.writelines(lines) f.close() if __name__ == '__main__': clear_dir() idx = 1 idx = excute_datasets(idx, 'train') idx = excute_datasets(idx, 'val')

     

    在运行过程中会出现下面这种情况,我个人认为是原始数据标注出了问题,总共有五六张照片的标注信息是空的,删除掉就好了。

     

    你需要做的是将这张照片的名字print出来,然后在txt文件中crtl+F找到他  删除掉标注信息就好了,把89行的注释去掉就可以显示出图片名字了

     

    可以看到是0_Parade_Parade_0_452.jpg出了问题

     

    打开wider_face_train_bbx_gt.txt  ,crtl+F寻找0_Parade_Parade_0_452.jpg

    可以看到注释是空的,删除掉整个图片的标注信息,如此反复删除没用的标注信息就可以了,总共就五六张照片是有问题的。

    最新回复(0)