mlpcascadescan.cc

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