-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwider2pascal.m
138 lines (118 loc) · 4.51 KB
/
wider2pascal.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
function wider2pascal(widerRootDir, widerDevkitDir)
%WIDER2PASCAL converts the WIDER database into Pascal format
% WIDER2PASCAL(widerDir, targetDir) generates a folder
% structure mimicking the Pascal VOC 2007 devkit format
% containing faces from the WIDER database
%
% `widerRootDir` is a path to the WIDER dataset (contains
% subfolders called WIDER_train, WIDER_val, WIDER_test
% and wider_face_split
%
% `widerDevkitDir` is a path to where the new VOCdevkit-style
% folder structure will be generated
%
% The generated folder structure contains only the subset of
% files required for bounding box object detection:
%
% widerDekitDir /
% WIDER /
% Annotations /
% JPEGImages /
% ImageSets /
%
%
% Author: Samuel Albanie
% create the target subdirectories
annotationsDir = fullfile(widerDevkitDir, 'WIDER', 'Annotations');
jpegImagesDir = fullfile(widerDevkitDir, 'WIDER', 'JPEGImages');
imageSetsDir = fullfile(widerDevkitDir, 'WIDER', 'ImageSets');
% the data structures are identical for the training and
% validation sets (the annotations for the test set are
% not publicly available and so are not used here
generateAnnotations(widerRootDir, annotationsDir, 'train');
generateAnnotations(widerRootDir, annotationsDir, 'val');
copyImages(widerRootDir, jpegImagesDir, 'train');
copyImages(widerRootDir, jpegImagesDir, 'val');
generateImageSets(widerRootDir, imageSetsDir, 'train');
generateImageSets(widerRootDir, imageSetsDir, 'val');
%-------------------------------------------------------------------
function generateAnnotations(widerRootDir, annotationsDir, partition)
%------------------------------------------------------------------
% generate the Pascal VOC-style xml annotation files
% for the given partition
% make sure that target directory exists
if ~exist(annotationsDir, 'dir')
mkdir(annotationsDir);
end
% load data for partition
partitionData = load(fullfile(widerRootDir, ...
'wider_face_split', ...
sprintf('wider_face_%s.mat', ...
partition)));
% loop over WIDER events
for i = 1: numel(partitionData.event_list)
% extract images and bounding box per event
files = partitionData.file_list{i};
bboxList = partitionData.face_bbx_list{i};
% loop over the images associated with each event
for j = 1:numel(files)
imgName = strcat(files{j}, '.jpg');
imgDir = fullfile(widerRootDir, sprintf('WIDER_%s', partition), 'images', partitionData.event_list{i}, strcat(files{j}, '.jpg'));
annotationFileName = fullfile(annotationsDir, ...
strcat(files{j}, '.xml'));
generatePascalXML(imgDir, imgName, bboxList{j}, annotationFileName);
end
end
clc
%-------------------------------------------------------------------
function copyImages(widerRootDir, jpegImagesDir, partition)
%------------------------------------------------------------------
% Copy the files from seperate WIDER "events" into a single
% directory, Pascal-style.
% make sure that target directory exists
if ~exist(jpegImagesDir, 'dir')
mkdir(jpegImagesDir);
end
% load data for partition
partitionData = load(fullfile(widerRootDir, ...
'wider_face_split', ...
sprintf('wider_face_%s.mat', ...
partition)));
% loop over WIDER events
for i = 1: numel(partitionData.event_list)
imgPaths = cellfun(@(x) fullfile(widerRootDir, ...
sprintf('WIDER_%s', partition), ...
'images', ...
partitionData.event_list{i}, ...
strcat(x, '.jpg')), ...
partitionData.file_list{i}, ...
'UniformOutput', false);
% copy the images to the target folder
for j = 1:numel(imgPaths)
copyfile(imgPaths{j}, jpegImagesDir);
end
end
clc
%-------------------------------------------------------------------
function generateImageSets(widerRootDir, imageSetsDir, partition)
%------------------------------------------------------------------
% Write the names of the images in the training and validation
% sets to files named 'train.txt' and 'val.txt'
% make sure that target directory exists
if ~exist(imageSetsDir, 'dir')
mkdir(imageSetsDir);
end
% load data for partition
partitionData = load(fullfile(widerRootDir, ...
'wider_face_split', ...
sprintf('wider_face_%s.mat', ...
partition)));
% write to file
targetPath = fullfile(imageSetsDir, sprintf('%s.txt', partition));
files = vertcat(partitionData.file_list{:});
fileID = fopen(targetPath, 'w');
for i = 1:numel(files)
fprintf(fileID, '%s\n', files{i});
end
fclose(fileID);
clc