-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathviewdet.m
executable file
·129 lines (111 loc) · 3.51 KB
/
viewdet.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
function viewdet(id,cls,onlytp)
if nargin<2
error(['usage: viewdet(competition,class,onlytp) e.g. viewdet(''comp3'',''car'') or ' ...
'viewdet(''comp3'',''car'',true) to show true positives']);
end
if nargin<3
onlytp=false;
end
% change this path if you install the VOC code elsewhere
addpath([cd '/VOCcode']);
% initialize VOC options
VOCinit;
% load test set
cp=sprintf(VOCopts.annocachepath,VOCopts.testset);
if exist(cp,'file')
fprintf('%s: loading test set\n',cls);
load(cp,'gtids','recs');
else
gtids=textread(sprintf(VOCopts.imgsetpath,VOCopts.testset),'%s');
for i=1:length(gtids)
% display progress
if toc>1
fprintf('%s: load: %d/%d\n',cls,i,length(gtids));
drawnow;
tic;
end
% read annotation
recs(i)=PASreadrecord(sprintf(VOCopts.annopath,gtids{i}));
end
save(cp,'gtids','recs');
end
% extract ground truth objects
npos=0;
gt(length(gtids))=struct('BB',[],'diff',[],'det',[]);
for i=1:length(gtids)
% extract objects of class
clsinds=strmatch(cls,{recs(i).objects(:).class},'exact');
gt(i).BB=cat(1,recs(i).objects(clsinds).bbox)';
gt(i).diff=[recs(i).objects(clsinds).difficult];
gt(i).det=false(length(clsinds),1);
npos=npos+sum(~gt(i).diff);
end
% load results
[ids,confidence,b1,b2,b3,b4]=textread(sprintf(VOCopts.detrespath,id,cls),'%s %f %f %f %f %f');
BB=[b1 b2 b3 b4]';
% sort detections by decreasing confidence
[sc,si]=sort(-confidence);
ids=ids(si);
BB=BB(:,si);
% view detections
clf;
nd=length(confidence);
tic;
for d=1:nd
% display progress
if onlytp&toc>1
fprintf('%s: viewdet: find true pos: %d/%d\n',cls,i,length(gtids));
drawnow;
tic;
end
% find ground truth image
i=strmatch(ids{d},gtids,'exact');
if isempty(i)
error('unrecognized image "%s"',ids{d});
elseif length(i)>1
error('multiple image "%s"',ids{d});
end
% assign detection to ground truth object if any
bb=BB(:,d);
ovmax=-inf;
for j=1:size(gt(i).BB,2)
bbgt=gt(i).BB(:,j);
bi=[max(bb(1),bbgt(1)) ; max(bb(2),bbgt(2)) ; min(bb(3),bbgt(3)) ; min(bb(4),bbgt(4))];
iw=bi(3)-bi(1)+1;
ih=bi(4)-bi(2)+1;
if iw>0 & ih>0
% compute overlap as area of intersection / area of union
ua=(bb(3)-bb(1)+1)*(bb(4)-bb(2)+1)+...
(bbgt(3)-bbgt(1)+1)*(bbgt(4)-bbgt(2)+1)-...
iw*ih;
ov=iw*ih/ua;
if ov>ovmax
ovmax=ov;
jmax=j;
end
end
end
% skip false positives
if onlytp&ovmax<VOCopts.minoverlap
continue
end
% read image
I=imread(sprintf(VOCopts.imgpath,gtids{i}));
% draw detection bounding box and ground truth bounding box (if any)
imagesc(I);
hold on;
if ovmax>=VOCopts.minoverlap
bbgt=gt(i).BB(:,jmax);
plot(bbgt([1 3 3 1 1]),bbgt([2 2 4 4 2]),'y-','linewidth',2);
plot(bb([1 3 3 1 1]),bb([2 2 4 4 2]),'g:','linewidth',2);
else
plot(bb([1 3 3 1 1]),bb([2 2 4 4 2]),'r-','linewidth',2);
end
hold off;
axis image;
axis off;
title(sprintf('det %d/%d: image: "%s" (green=true pos,red=false pos,yellow=ground truth',...
d,nd,gtids{i}),'interpreter','none');
fprintf('press any key to continue with next image\n');
pause;
end