mlpscan.cc

  1 const char *help = "\
  2 progname: mlpscan.cc\n\
  3 code2html: This program detects objects (such as faces) in an image using a MLP.\n\
  4 Updades:\n\
  5    version 0.0: basic mlp with mean-stdv pruning\n\
  6    version 1.0: adding edge pruning\n\
  7    version 2.0: correction of default stepx and stepy values\n\
  8 version: Torch3 vision2.1, 2004-2005-2006\n\
  9 (c) Sebastien Marcel (marcel@idiap.ch) and Yann Rodriguez (rodrig@idiap.ch)\n";
 10 
 11 // image
 12 #include "ImageGray.h"
 13 #include "ImageRgb.h"
 14 #include "Rectangle2D.h"
 15 
 16 // image loader
 17 #include "xtprobeImageDiskXFile.h"
 18 #include "jpegDiskXFile.h"
 19 
 20 // face detector
 21 #include "PatternDetect.h"
 22 #include "PyramidPatternDetect.h"
 23 #include "NonOrientedPatternFusion.h"
 24 #include "PatternMerger.h"
 25 #include "ipSWmlp.h"
 26 
 27 // edge
 28 #include "ipIntegralImage.h" 
 29 #include "ipSobel.h"
 30 
 31 //
 32 #include "MTimer.h"
 33 
 34 //
 35 #include "DiskXFile.h"
 36 #include "CmdLine.h"
 37 
 38 using namespace Torch;
 39 
 40 int main(int argc, char **argv)
 41 {
 42 	char *image_filename;
 43 	bool verbose;
 44 	      
 45         int patternWmin;
 46         int patternHmin;
 47         int minWsize;
 48         int maxWsize;
 49         real scale_factor;
 50         real stepx_factor;
 51         real stepy_factor;
 52 
 53         real MeanMin;
 54         real MeanMax;
 55         real StdvMin;
 56         real StdvMax;
 57 	bool pruning;
 58 	real threshold_edges_min;
 59 	real threshold_edges_max;
 60 
 61 	char *model_filename;
 62 	real model_threshold;
 63  
 64  
 65 	real surfoverlap_fusion;
 66 	real threshold_activation;
 67 
 68 	char *dirname;
 69 
 70 	int nbest;
 71 	bool savepos;
 72 	bool draw;
 73 	bool savejpg;
 74 
 75 
 76 	// Construct the command line.
 77 	// ---------------------------	
 78   	CmdLine cmd;
 79 	cmd.setBOption("write log", false);
 80   	cmd.info(help);
 81   	cmd.addText("\nArguments:");
 82   	cmd.addSCmdArg("image filename", &image_filename, "image filename");
 83   	cmd.addText("\nOptions:");
 84   	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 85         cmd.addText("\nScanning Options:");
 86         cmd.addICmdOption("-patternWmin", &patternWmin, 19, "pattern width min");
 87         cmd.addICmdOption("-patternHmin", &patternHmin, 19, "pattern height min");
 88         cmd.addICmdOption("-minWsize", &minWsize, -1, "width min");
 89         cmd.addICmdOption("-maxWsize", &maxWsize, -1, "width max");
 90         cmd.addRCmdOption("-scalefactor", &scale_factor, 0.125, "scale factor");
 91         cmd.addRCmdOption("-stepxfactor", &stepx_factor, 0.1, "step x factor");
 92         cmd.addRCmdOption("-stepyfactor", &stepy_factor, 0.1, "step y factor");
 93 	cmd.addText("\nPruning Options:");
 94 	cmd.addBCmdOption("-pruning", &pruning, false, "use pruning");
 95 	cmd.addRCmdOption("-mmin", &MeanMin, 0.041, "mean min");
 96 	cmd.addRCmdOption("-mmax", &MeanMax, 0.890, "mean max");
 97 	cmd.addRCmdOption("-vmin", &StdvMin, 0.0204354, "variance min");
 98 	cmd.addRCmdOption("-vmax", &StdvMax, 0.382121, "variance max");
 99 	cmd.addRCmdOption("-emin", &threshold_edges_min, 0.1, "edge threshold min");
100 	cmd.addRCmdOption("-emax", &threshold_edges_max, 0.5, "edge threshold max");
101 	cmd.addText("\nModel Options:");
102   	cmd.addSCmdOption("-model", &model_filename, "models/face361x90.mlp", "MLP model filename");
103 	cmd.addRCmdOption("-threshold", &model_threshold, 0.6, "model threshold");
104 	cmd.addText("\nFusion Options:");
105 	cmd.addRCmdOption("-surfoverlapfusion", &surfoverlap_fusion, 0.6, "threshold for fusion of overlapped patterns");
106 	cmd.addRCmdOption("-threshold_activation", &threshold_activation, 2.0, "threshold after fusion");
107 	cmd.addText("\nMiscellaneous Options:");
108   	cmd.addSCmdOption("-dir", &dirname, ".", "directory to store ouput files");
109         cmd.addBCmdOption("-savepos", &savepos, false, "save pos file");
110         cmd.addBCmdOption("-draw", &draw, false, "draw ppm image with detections");
111         cmd.addBCmdOption("-savejpg", &savejpg, false, "save in jpeg instead of jpeg");
112         cmd.addICmdOption("-nbest", &nbest, -1, "nbest detections (all if -1)");
113 	cmd.read(argc, argv);
114 	
115 	if(strcmp(model_filename, "") == 0)
116 		error("No model specified.");
117 
118 	Allocator *allocator = new Allocator;
119 
120 	
121 	// extract basename from filename
122 	// --------------------------------
123 	char basename[256];
124 	char *extension;
125 	char *separator;
126 	strcpy(basename, image_filename);
127         extension = (char *) strrchr(basename, '.');
128         if(extension != NULL) *extension = '\0';
129 	separator = (char *) rindex(basename, '/');
130 	if(separator != NULL)
131 	{
132 		separator++;
133 		strcpy(basename, separator);
134 	}
135 	
136 	
137 	// load image to scan.
138 	// --------------------
139 	Image *image = NULL;
140 	ImageDiskXFile *image_file = NULL;
141 	image_file = new(allocator) xtprobeImageDiskXFile(image_filename, "r");
142 	image = new(allocator) ImageGray();
143 	image->setBOption("verbose", verbose);
144 	image->loadImageXFile(image_file);
145 	allocator->free(image_file);
146 
147         int width = image->width;
148         int height = image->height;
149 
150 	Sequence *realimage = new(allocator) Sequence(1, width * height);
151 	for(int i = 0 ; i < width * height ; i++) 
152 		realimage->frames[0][i] = image->data[i] / 255.0;
153 
154 	
155 	// pruning.
156 	// ---------
157 	real *pruning_ii = NULL;
158 
159 	if(pruning)
160 	{
161 		ipCore *edges = new(allocator) ipSobel(width, height, "float");
162 		edges->setROption("threshold", 0.4);
163 		Sequence *realedges = new(allocator) Sequence(&edges->seq_out->frames[3], 1, width * height);	
164 		ipCore *integralimagemachine_edges = new(allocator) ipIntegralImage(width, height, "gray");
165 		edges->init();
166 		edges->process(realimage);
167 		integralimagemachine_edges->init();
168 		integralimagemachine_edges->process(realedges);
169 
170 		pruning_ii = integralimagemachine_edges->seq_out->frames[0];
171 	}
172 	
173 	// Detection.
174 	// -----------
175 	PatternDetect *patterndetect;
176 
177 	PatternMerger *merger = new(allocator) meanRectPatternMerger();
178 	PatternFusion *pfusion;
179 	pfusion = new(allocator)NonOrientedPatternFusion(merger, 0, 100);
180 	pfusion->setParams(surfoverlap_fusion, model_threshold, true, threshold_activation);
181 		
182 	ipIntegralImage *ip_ii = new(allocator) ipIntegralImage(width, height, "gray", true);
183 	
184 	ipSubWindow *ip_sw = new(allocator) ipSWmlp(width, height, patternWmin, patternHmin, model_filename, model_threshold, verbose);
185 	
186 	patterndetect = new(allocator) PyramidPatternDetect(
187 			ip_sw, pfusion, ip_ii,
188 	      		width, height, 
189 			patternWmin, patternHmin, 
190 			minWsize, maxWsize, 
191 			stepx_factor, stepy_factor, scale_factor,
192 			MeanMin, MeanMax, StdvMin, StdvMax,
193 			pruning_ii, 
194 			threshold_edges_min, threshold_edges_max,
195 			1000, verbose);
196 	
197 	// Processing.
198 	// ---------------
199 	MTimer *timer = new(allocator) MTimer();
200         timer->reset();
201         patterndetect->init();
202         patterndetect->process(realimage);
203         patterndetect->stat();
204         timer->stop();
205 	print("processing time: %d' %d'' %dms\n\n", timer->minutes, timer->seconds, timer->mseconds);
206 	
207 	//	
208 	DiskXFile *posoutput = NULL;
209 	char str[250];
210 
211 	if(patterndetect->n_patterns > 0)
212 	{
213 	   	ImageRgb *outputimage = NULL;
214 
215 		if(draw)
216 		{
217 	   		outputimage = new(allocator) ImageRgb(width, height);
218 			outputimage->copyFrom(image);
219 		}
220 		
221 		if(savepos)
222 		{
223 			sprintf(str, "%s/%s.pos", dirname, basename);
224 			posoutput = new(allocator) DiskXFile(str, "w");
225 		}
226 		
227 		Color facecolor = green;
228 		Rectangle2D rect;
229 	
230 		int P = patterndetect->n_patterns;
231 		
232 		if(nbest != -1)
233 		{
234 			if(nbest < P) P = nbest;
235 			print("saving %d-bests\n", P);
236 		}
237 		
238 		if(savepos) posoutput->printf("%d\n", P);
239 		
240 		for(int i = 0 ; i < P ; i++)
241 		{
242 			if(verbose)
243 				print("DETECTION [%02d] %d %d %d %d %g %d %g %g\n", 
244                                         i,
245                                         patterndetect->patterns[i]->x, 
246                                         patterndetect->patterns[i]->y, 
247                                         patterndetect->patterns[i]->w, 
248                                         patterndetect->patterns[i]->h, 
249                                         patterndetect->patterns[i]->scale, 
250                                         patterndetect->patterns[i]->view, 
251                                         patterndetect->patterns[i]->confidence,
252                                         patterndetect->patterns[i]->activation);
253 
254 			rect.reset(	patterndetect->patterns[i]->x, patterndetect->patterns[i]->y, 
255 			      		patterndetect->patterns[i]->w, patterndetect->patterns[i]->h);
256 
257 			if(draw) rect.draw(outputimage, facecolor);
258 
259 			if(savepos)
260 				posoutput->printf("%d %d %d %d\n",
261 				      patterndetect->patterns[i]->x,
262 				      patterndetect->patterns[i]->y,
263 				      patterndetect->patterns[i]->w,
264 				      patterndetect->patterns[i]->h);
265 		}
266 		if(verbose) print("\n");
267 	  
268 		if(draw)
269 		{
270 		   	if(savejpg)
271 			{
272 			   	sprintf(str, "%s/%s-detect.jpg", dirname, basename);
273 				jpegDiskXFile *jpeg_file = new(allocator) jpegDiskXFile(str, "w");
274 				jpeg_file->writeHeader(outputimage->width, outputimage->height);
275 				jpeg_file->writePixmap(outputimage->pixmap);
276 				allocator->free(jpeg_file);
277 			}
278 			else
279 			{
280 				sprintf(str, "%s/%s-detect.ppm", dirname, basename);
281 
282 				outputimage->save(str);
283 			}
284 			allocator->free(outputimage);
285 		}
286 	}
287 	else
288 	{
289 		if(savepos) posoutput->printf("0\n");
290 	}
291 
292 	print("done.\n");
293 
294 	delete allocator;
295 
296 	return(0);
297 }