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