forked from npinto/fddb-evaluation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.cpp
345 lines (293 loc) · 9.32 KB
/
evaluate.cpp
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#ifndef _WIN32
#include <unistd.h>
#endif
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <iterator>
#include <fstream>
#include <limits>
#include "common.hpp"
#ifdef __XCODE__
#include <OpenCV/OpenCV.h>
#endif
#include "RegionsSingleImage.hpp"
#include "EllipsesSingleImage.hpp"
#include "RectanglesSingleImage.hpp"
#include "Matching.hpp"
#include "Results.hpp"
using std::cout;
using std::endl;
using std::cerr;
using std::string;
using std::vector;
using std::stringstream;
using std::ifstream;
using std::ofstream;
enum detFormatTypes {DET_RECTANGLE, DET_ELLIPSE, DET_PIXELS};
vector<string> *getImageList(string inFile){
vector<string> * imNames = new vector<string>;
std::ifstream fin(inFile.c_str());
if(fin.is_open()){
string s;
while(fin >> s){
imNames->push_back(s);
}
fin.close();
return imNames;
}
cerr << "Invalid list file " << inFile << endl;
return NULL;
}
void printUsage(){
cout << "./evaluate [OPTIONS]" << endl;
cout << " -h : print usage" << endl;
cout << " -a fileName : file with face annotations (default: ellipseList.txt)" << endl;
cout << " -d fileName : file with detections (default: faceList.txt)" << endl;
cout << " -f format : representation of faces in the detection file (default: 0)" << endl;
cout << " : [ 0 (rectangle), 1 (ellipse) or 2 (pixels) ]" << endl;
cout << " -i dirName : directory where the original images are stored (default: ~/scratch/Data/facesInTheWild/)" << endl;
cout << " -l fileName : file with list of images to be evaluated (default: temp.txt)" << endl;
cout << " -r fileName : prefix for files to store the ROC curves (default: temp)" << endl;
}
int main(int argc, char *argv[]){
#ifndef _WIN32
int c;
opterr = 0;
#endif
// default values for different command-line arguments
#ifdef _WIN32
string baseDir = "F:/scratch/Data/facesInTheWild/";
string listFile = "F:/scratch/Data/detectionResults/FDDB/imList.txt";
string detFile = "F:/scratch/Data/detectionResults/FDDB/MikolajczykDets.txt";
string annotFile = "F:/scratch/Data/detectionResults/FDDB/ellipseList.txt";
#else
string baseDir = "/Users/vidit/scratch/Data/facesInTheWild/";
string listFile = "/Users/vidit/scratch/Data/detectionResults/FDDB/imList.txt";
string detFile = "/Users/vidit/scratch/Data/detectionResults/FDDB/MikolajczykDets.txt";
string annotFile = "/Users/vidit/scratch/Data/detectionResults/FDDB/ellipseList.txt";
#endif
// directory containing the images
string imDir = baseDir;
// prefix used for writing the ROC-curve files
string rocFilePrefix = "temp";
// format used for specifying the detected regions
int detFormat = DET_RECTANGLE;
// format string to be appended to the image file names in the annotation set
string annotImageFormat = __IMAGE_FORMAT__;
// display the matched pairs
bool showMatchPairs = false;
if(argc == 1)
{
printUsage();
return 0;
}
#ifndef _WIN32
// parse the input
while( (c = getopt(argc, argv, "l:r:d:a:z:i:f:s")) != -1){
switch(c){
case 'l':
listFile = optarg;
break;
case 'r':
rocFilePrefix = optarg;
break;
case 'd':
detFile = optarg;
break;
case 'a':
annotFile = optarg;
break;
case 'z':
annotImageFormat = optarg;
break;
case 'i':
imDir = optarg;
break;
case 'f':
detFormat = atoi(optarg);
break;
case 's':
showMatchPairs = true;
break;
case 'h':
default:
printUsage();
return 0;
}
}
#endif
// read file and compute image-wise scores
vector<string> *imNames = getImageList(listFile);
if(imNames == NULL)
{
cerr << "No images found in the list " << listFile << endl;
cerr << "Set list file using -l option. See usage ./evaluate -h." << endl;
return -1;
}
// file handle for reading annotations
ifstream fAnnot(annotFile.c_str());
if(!fAnnot.is_open()){
cerr << "Can not open annotations from " << annotFile << endl;
cerr << "Set annotation file using -a option. See usage ./evaluate -h." << endl;
return -1;
}
// file handle for reading detections
ifstream fDet(detFile.c_str());
if(!fDet.is_open()){
cerr << "Can not open detections from " << detFile << endl;
cerr << "Set detection file using -d option. See usage ./evaluate -h." << endl;
return -1;
}
cout << "Processing " << imNames->size() << " images" << endl;
// cumRes stores the cumulative results for all the images
vector<Results *> *cumRes = new vector<Results *>;
// dummyRes is used for accessing some method from the Results class
Results *dummyRes = new Results();
// Process each image
for(unsigned int ii = 0; ii < imNames->size(); ii++){
if(ii % 1 == 0)
cout << ii << " images done" << endl;
string imName = imNames->at(ii);
// read image name
string imS1;
getline(fAnnot, imS1);
string imS2;
getline(fDet, imS2);
// make sure that the annotation and detections are read for the same image
if( imName.compare(imS1) || imName.compare(imS2) )
{
cerr << imName << " " << imS1 << " " << imS2 << endl;
cerr << "Incompatible annotation and detection files. See output specifications" << endl;
return -1;
}
// Read the number of annotations/detections in this image
int nAnnot, nDet;
getline(fAnnot, imS1);
getline(fDet, imS2);
stringstream ss1(imS1);
ss1 >> nAnnot;
stringstream ss2(imS2);
ss2 >> nDet;
string imFullName = imDir + imName + annotImageFormat;
// Read the annotations
RegionsSingleImage *annot;
annot = new EllipsesSingleImage(imFullName);
((EllipsesSingleImage *)annot)->read(fAnnot, nAnnot);
// Read the detections
RegionsSingleImage *det;
switch(detFormat)
{
case(DET_ELLIPSE):
det = new EllipsesSingleImage(imFullName);
((EllipsesSingleImage *)det)->read(fDet, nDet);
break;
case(DET_RECTANGLE):
det = new RectanglesSingleImage(imFullName);
((RectanglesSingleImage *)det)->read(fDet, nDet);
break;
case(DET_PIXELS):
cerr << "Not yet implemented " << endl;
assert(false);
default:
;
}
// imageResults holds the results for different thresholds
// applied to a single image
vector<Results *> *imageResults = new vector<Results *>;
if(nDet == 0)
{
// create the image results for zero detections
Results *r = new Results(imName, std::numeric_limits<double>::max(), NULL, annot, det);
imageResults->push_back(r);
}
else
{
// attach annot and det to the Maching object
Matching *M = new Matching(annot, det);
// find the unique values for detection scores
vector<double> *uniqueScores = det->getUniqueScores();
sort(uniqueScores->begin(), uniqueScores->end());
imageResults->reserve(uniqueScores->size());
// For each unique score value st,
// (a) filter the detections with score >= st
// (b) compute the matching annot-det pairs
// (c) compute the result statistics
for(vector<double>::iterator uit=uniqueScores->begin(); uit != uniqueScores->end(); ++uit)
{
// (a) filter the detections with score >= st
double scoreThreshold = *uit;
for(unsigned di =0; di<det->length(); di++)
{
Region *rd = det->get(di);
rd->setValid( rd->detScore >= scoreThreshold );
}
// (b) match annotations to detections
vector<MatchPair *> *mps = M->getMatchPairs();
// if the matched pairs are to be displayed
if(showMatchPairs)
{
if(mps!= NULL)
{
const IplImage *im = annot->getImage();
assert(im != NULL);
IplImage *mask = cvCreateImage(cvGetSize(im), im->depth, im->nChannels);
cvCopy(im, mask, 0);
for(unsigned int i=0; i< mps->size(); i++)
{
stringstream ss("");
ss << i;
const char *textDetInd = ss.str().c_str();
mask = ( (EllipseR *)(mps->at(i)->r1) )->display(mask, CV_RGB(255,0,0), 3, textDetInd);
switch(detFormat)
{
case(DET_RECTANGLE):
mask = ( (RectangleR *)(mps->at(i)->r2) )->display(mask, CV_RGB(0,255,0), 3, textDetInd);
break;
case(DET_ELLIPSE):
mask = ( (EllipseR *)(mps->at(i)->r2) )->display(mask, CV_RGB(0,255,0), 3, textDetInd);
break;
case(DET_PIXELS):
cerr << "Not yet implemented " << endl;
return -1;
break;
default:
;
}
}
showImage(" matches ", mask);
cvReleaseImage(&mask);
}
}
// (c) compute the result statistics and append to the list
Results *r = new Results(imName, scoreThreshold, mps, annot, det);
imageResults->push_back(r);
//r->print(std::cout);
for(unsigned int mpi=0; mpi < mps->size(); mpi++)
delete(mps->at(mpi));
delete(mps);
}
delete(uniqueScores);
delete(M);
}
delete(annot);
delete(det);
// merge the list of results for this image (imageResults) with the global list (cumRes)
vector<Results *> *mergedRes = dummyRes->merge(cumRes, imageResults);
// free memory for the lists used for merging
for(unsigned int mi =0; mi<cumRes->size(); mi++)
delete(cumRes->at(mi));
delete(cumRes);
for(unsigned int mi =0; mi<imageResults->size(); mi++)
delete(imageResults->at(mi));
delete(imageResults);
cumRes = mergedRes;
}
fAnnot.close();
fDet.close();
// save the ROC-curve computed from the cumulative statistics
dummyRes->saveROC(rocFilePrefix, cumRes);
delete(imNames);
return 0;
}