博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
widerface数据库转voc2007数据集(python/matlab实现)
阅读量:5324 次
发布时间:2019-06-14

本文共 10229 字,大约阅读时间需要 34 分钟。

python实现基本需求,可以在此基础上修改

# -*- coding: utf-8 -*-"""Created on 17-5-27@author: zly """from skimage import ioimport shutilimport randomimport osimport stringheadstr = """\
VOC2007
%06d.jpg
My Database
PASCAL VOC2007
flickr
NULL
NULL
company
%d
%d
%d
0
"""objstr = """\
%s
Unspecified
0
0
%d
%d
%d
%d
"""tailstr = '''\
'''def all_path(filename): return os.path.join('widerface', filename)def writexml(idx, head, bbxes, tail): filename = all_path("Annotations/%06d.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') f_bbx = open(all_path('wider_face_split/wider_face_' + datatype + '_bbx_gt.txt'), 'r') while True: filename = string.strip(f_bbx.readline(), '\n') if not filename: break im = io.imread(all_path('WIDER_' + datatype + '/images/'+filename)) head = headstr % (idx, im.shape[1], im.shape[0], im.shape[2]) nums = string.strip(f_bbx.readline(), '\n') bbxes = [] for ind in xrange(string.atoi(nums)): bbx_info = string.split(string.strip(f_bbx.readline(), ' \n'), ' ') bbx = [string.atoi(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/%06d.jpg' % (idx))) f.write('%06d\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')

目录结构如下

以下python代码为读取mat,废弃,仅供学习

import h5pyfrom skimage import ioimport shutilimport randomheadstr = """\
VOC2007
%06d.jpg
My Database
PASCAL VOC2007
flickr
NULL
NULL
facevise
%d
%d
%d
0
"""objstr = """\
%s
Unspecified
0
0
%d
%d
%d
%d
"""tailstr ='''\
'''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实现

head.xml

widerface
%06d.jpg
My Database
VOC2007
flickr
NULL
NULL
facevise
%d
%d
%d
0

 object.xml

        
%s
Unspecified
0
0
%d
%d
%d
%d

tail.xml

 

 

function WiderFace2VOC()%% wider face% The corresponding annotations are in the following format:% Here, each face bounding boxe is denoted by:% 
.%% 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);endtrain_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);endfclose(trainID);fclose(trainvalID);fclose(valID);fclose(testID);fclose all;

转载于:https://www.cnblogs.com/linyuanzhou/p/6043436.html

你可能感兴趣的文章
HDU 2063 过山车
查看>>
高精度1--加法
查看>>
String比较
查看>>
Django之Models
查看>>
CSS 透明度级别 及 背景透明
查看>>
Linux 的 date 日期的使用
查看>>
PHP zip压缩文件及解压
查看>>
SOAP web service用AFNetWorking实现请求
查看>>
HTTPS、SPDY和HTTP/2的性能比较
查看>>
Java变量类型,实例变量 与局部变量 静态变量
查看>>
sublime快捷键
查看>>
mysql操作命令梳理(4)-中文乱码问题
查看>>
Python环境搭建(安装、验证与卸载)
查看>>
一个.NET通用JSON解析/构建类的实现(c#)
查看>>
Windows Phone开发(5):室内装修 转:http://blog.csdn.net/tcjiaan/article/details/7269014
查看>>
08.存储Cinder→5.场景学习→08.Backup Volume→1.概述与配置
查看>>
详谈js面向对象 javascript oop,持续更新
查看>>
关于这次软件以及pda终端的培训
查看>>
jQuery上传插件Uploadify 3.2在.NET下的详细例子
查看>>
小程序开发笔记
查看>>