首页 > 代码库 > widerface数据库转voc2007数据集(python/matlab实现)
widerface数据库转voc2007数据集(python/matlab实现)
python实现基本需求,可以在此基础上修改
import h5py from skimage import io import shutil import random headstr = """<annotation> <folder>VOC2007</folder> <filename>%06d.jpg</filename> <source> <database>My Database</database> <annotation>PASCAL VOC2007</annotation> <image>flickr</image> <flickrid>NULL</flickrid> </source> <owner> <flickrid>NULL</flickrid> <name>facevise</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 writexml(idx, head, objs, tail): filename = "Annotations/%06d.xml" % (idx) f = open(filename, "w") f.write(head) f.write(objs) f.write(tail) f.close() def clear_dir(): if shutil.os.path.exists(‘Annotations‘): shutil.rmtree(‘Annotations‘) if shutil.os.path.exists(‘ImageSets‘): shutil.rmtree(‘ImageSets‘) if shutil.os.path.exists(‘JPEGImages‘): shutil.rmtree(‘JPEGImages‘) shutil.os.mkdir(‘Annotations‘) shutil.os.makedirs(‘ImageSets/Main‘) shutil.os.mkdir(‘JPEGImages‘) def excute_datasets(idx, datatype): f = open(‘ImageSets/Main/‘+datatype+‘.txt‘, ‘a‘) mat = h5py.File(‘wider_face_split/wider_face_‘+datatype+‘.mat‘, ‘r‘) file_list = mat[‘file_list‘][:] event_list = mat[‘event_list‘][:] bbx_list = mat[‘face_bbx_list‘][:] for i in range(file_list.size): file_list_sub = mat[file_list[0,i]][:] bbx_list_sub = mat[bbx_list[0, i]][:] event_value = ‘‘.join(chr(x) for x in mat[event_list[0,i]][:]) for j in range(file_list_sub.size): root = ‘WIDER_‘+datatype+‘/images/‘+event_value+‘/‘ filename = root + ‘‘.join([chr(x) for x in mat[file_list_sub[0, j]][:]])+‘.jpg‘ im = io.imread(filename) head = headstr % (idx, im.shape[1], im.shape[0], im.shape[2]) bboxes = mat[bbx_list_sub[0, j]][:] objs = ‘‘.join([objstr % (‘face‘, bboxes[0,k],bboxes[1,k], bboxes[0,k]+bboxes[2,k]-1,bboxes[1,k]+bboxes[3,k]-1) for k in range(bboxes.shape[1])]) writexml(idx, head, objs, tailstr) shutil.copyfile(filename, ‘JPEGImages/%06d.jpg‘ % (idx)) f.write(‘%06d\n‘ % (idx)) idx +=1 f.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‘)
matlab实现
function WiderFace2VOC() %% wider face % The corresponding annotations are in the following format: % Here, each face bounding boxe is denoted by: % <x_left y_top width height>. %% voc % 000001.jpg car 44 28 132 121 %前面是图片名,中间是目标类别,最后是目标的包围框坐标(左上角和右下角坐标)。 %% clc; clear; fclose all; [~, ~, ~] = rmdir(‘Annotations‘, ‘s‘); [~, ~, ~] = rmdir(‘ImageSets‘, ‘s‘); [~, ~, ~] = rmdir(‘JPEGImages‘, ‘s‘); [~, ~, ~] = mkdir(‘Annotations‘); [~, ~, ~] = mkdir(‘ImageSets/Main‘); [~, ~, ~] = mkdir(‘JPEGImages‘); train_root = ‘WIDER_train/images‘; split_file = ‘wider_face_split/wider_face_train‘; data = load(split_file); headXml = fopen(‘head.xml‘, ‘r‘); headXmlFormat = fread(headXml, Inf, ‘*char‘); fclose(headXml); objectXml = fopen(‘object.xml‘, ‘r‘); objectXmlFormat = fread(objectXml, Inf, ‘*char‘); fclose(objectXml); tailXml = fopen(‘tail.xml‘, ‘r‘); tailXmlFormat = fread(tailXml, Inf, ‘*char‘); fclose(tailXml); trainID = fopen(‘ImageSets/Main/train.txt‘, ‘w‘); trainvalID = fopen(‘ImageSets/Main/trainval.txt‘, ‘w‘); valID = fopen(‘ImageSets/Main/val.txt‘, ‘w‘); testID = fopen(‘ImageSets/Main/test.txt‘, ‘w‘); idx = 1; for i=1:numel(data.event_list) for j=1:numel(data.file_list{i}) imagename = fullfile(train_root, data.event_list{i}, strcat(data.file_list{i}{j}, ‘.jpg‘)); sz = size(imread(imagename)); AnnotationsXml = fopen(sprintf(‘Annotations/%06d.xml‘, idx), ‘w‘); fprintf(AnnotationsXml, headXmlFormat, idx, sz(2), sz(1),sz(3)); for k = 1:size(data.face_bbx_list{i}{j}, 1) rc = data.face_bbx_list{i}{j}(k, :); rc = round([rc(1), rc(2), rc(1)+rc(3)-1, rc(2)+rc(4)-1]); fprintf(AnnotationsXml, objectXmlFormat, ‘face‘, rc(1), rc(2), rc(3), rc(4)); end fprintf(AnnotationsXml, tailXmlFormat); fprintf(trainID, ‘%06d\n‘, idx); fprintf(trainvalID, ‘%06d\n‘, idx); fclose(AnnotationsXml); copyfile(imagename, sprintf(‘JPEGImages/%06d.jpg‘, idx)); idx = idx + 1; end disp(i); end train_root = ‘WIDER_val/images‘; split_file = ‘wider_face_split/wider_face_val‘; data = load(split_file); for i=1:numel(data.event_list) for j=1:numel(data.file_list{i}) imagename = fullfile(train_root, data.event_list{i}, strcat(data.file_list{i}{j}, ‘.jpg‘)); sz = size(imread(imagename)); AnnotationsXml = fopen(sprintf(‘Annotations/%06d.xml‘, idx), ‘w‘); fprintf(AnnotationsXml, headXmlFormat, idx, sz(2), sz(1),sz(3)); for k = 1:size(data.face_bbx_list{i}{j}, 1) rc = data.face_bbx_list{i}{j}(k, :); rc = round([rc(1), rc(2), rc(1)+rc(3)-1, rc(2)+rc(4)-1]); fprintf(AnnotationsXml, objectXmlFormat, ‘face‘, rc(1), rc(2), rc(3), rc(4)); end fprintf(AnnotationsXml, tailXmlFormat); if mod(idx, 2) fprintf(valID, ‘%06d\n‘, idx); fprintf(trainvalID, ‘%06d\n‘, idx); else fprintf(testID, ‘%06d\n‘, idx); end fclose(AnnotationsXml); copyfile(imagename, sprintf(‘JPEGImages/%06d.jpg‘, idx)); idx = idx+1; end disp(i); end fclose(trainID); fclose(trainvalID); fclose(valID); fclose(testID); fclose all;
widerface数据库转voc2007数据集(python/matlab实现)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。