-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsetupDataDetection_vk.m
More file actions
70 lines (66 loc) · 2.29 KB
/
Copy pathsetupDataDetection_vk.m
File metadata and controls
70 lines (66 loc) · 2.29 KB
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
function setupDataDetection_vk
% function that reads the bboxes provided by Viewpoints & Keypoints to
% evaluate my models. Extract images using bboxes provided
clear; clc; close all;
% relevant paths
pascal3d_path = 'data/pascal3d';
db_path = fullfile(pascal3d_path, 'PASCAL/VOCdevkit/VOC2012');
mat_path = 'data/vk_dets';
img_path = fullfile(db_path, 'JPEGImages');
sets_file = fullfile(db_path, 'ImageSets/Main/val.txt');
dest_path = fullfile(mat_path, 'all');
patch_size = [224, 224];
% get list of all test images
fid = fopen(sets_file, 'r');
tmp = textscan(fid, '%s');
image_names = tmp{1};
fclose(fid);
classes = {'aeroplane', 'bicycle', 'boat', 'bottle', 'bus', 'car', ...
'chair', 'diningtable', 'motorbike', 'sofa', 'train', 'tvmonitor'};
num_classes = 12;
% load all detections
tmp = load(fullfile(mat_path, 'VOC2012_val_det'));
classInds = [1 2 4 5 6 7 9 11 14 18 19 20]; %rigid categories gotten from getParams.m in V&K code
chosenboxes = tmp.chosenboxes(classInds);
topscores = tmp.topscores(classInds);
num_classes = length(chosenboxes);
num_images = length(chosenboxes{1});
dets = cell(1, num_classes);
for i = 1:num_classes
cls_dets = cell(1, num_images);
for j = 1:num_images
cls_dets{j} = [chosenboxes{i}{j}, topscores{i}{j}];
end
dets{i} = cls_dets;
end
% get images
for i = 1:length(image_names)
image_name = image_names{i};
bboxes = cell(1, num_classes);
labels = cell(1, num_classes);
for j = 1:num_classes
bboxes{j} = dets{j}{i};
labels{j} = j*ones(size(dets{j}{i}, 1), 1);
end
bboxes = cat(1, bboxes{:});
labels = cat(1, labels{:});
xdata = cell(1, size(bboxes, 1));
% read image
img = imread(fullfile(img_path, sprintf('%s.jpg', image_name)));
for k = 1:size(bboxes, 1)
patch = get_patch(bboxes(k, :), img, patch_size);
xdata{k} = shiftdim(patch, -1);
end
xdata = cat(1, xdata{:});
fprintf('image: %d \t num_boxes: %d \n', i, length(labels));
save(fullfile(dest_path, image_name), 'xdata', 'bboxes', 'labels');
end
function patch = get_patch(bbox, img, patch_size)
% function to get patch inside gt-bbox
[nR, nC, ~] = size(img);
% extract patch inside bounding box
x1 = max(1, round(bbox(1))); x2 = min(nC, round(bbox(3)));
y1 = max(1, round(bbox(2))); y2 = min(nR, round(bbox(4)));
patch = img(y1:y2, x1:x2, :);
% resize patch to canonical size
patch = imresize(patch, patch_size) ;