The Machine Perception Toolbox

[Introduction]- [News]- [Download]- [Screenshots]- [Manual (pdf)]- [Forums]- [API Reference]- [Repository ]

 

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Apps/unix/mpisearch/main.cc

Go to the documentation of this file.
00001 /*
00002  *  main.cc
00003  *
00004  *  Authors: Ryan Dahl, Ian Fasel, Javier Movellan
00005  *  Fixes:
00006  *
00007  *  Copyright (c) 2003 Machine Perception Laboratory
00008  *  University of California San Diego.
00009  *
00010  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00011  *
00012  *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00013  *    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
00014  *    3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00017  *
00018  */
00019 
00020 #include "mpisearchFaceDetector.h"
00021 
00022 #include <list>
00023 #include <algorithm>
00024 #include <Magick++.h>
00025 #include <iostream>
00026 
00027 extern "C" {
00028 #include <math.h>
00029 #include <string.h>
00030 #include <sys/time.h>
00031 #include <stdlib.h>
00032 #ifndef __APPLE_CC__  
00033 #include <argp.h> // argp is a linux specific library.  Doh!
00034 #else
00035 #include <sys/sysctl.h>
00036 #endif
00037 }
00038 
00039 #define FALSE 0
00040 #define TRUE 1 
00041 #define MAX_NUM_IMAGES 10
00042 
00043 #define BINARY
00044 
00045 using namespace std;
00046 
00048 #ifndef __APPLE_CC__
00049 const char *argp_program_version = "2.0";
00050 const char *argp_program_bug_address = "ianfasel@cogsci.ucsd.edu";
00051 static char doc[] = "An example program demonstrating the use of libmpisearch."
00052 "To run the face detector on an image file, accepting all defaults, "
00053 "type:\n\t mpisearch <image>\nwhere <image> is the name of the image file. "
00054 "You can also specify the following options:\vEnjoy!"
00055 ;
00056 static struct argp_option options[] = {
00057   {0}
00058 };
00059 #endif //__APPLE_CC__
00060 struct arguments {
00061   char *args[MAX_NUM_IMAGES];   // up to xx images can be processed at once
00062   int matlab;
00063   char matfile[50], cfile[50], emofile[50];
00064   int emoFlag;
00065 };
00066 #ifndef __APPLE_CC__
00067 static error_t parse_opt (int key, char *arg, struct argp_state *state){
00068   // Get input argument from argp_parse, which is our
00069   // arguments pointer.
00070   struct arguments *arguments = static_cast<struct arguments *>(state->input);
00071   
00072   switch(key)
00073     {
00074     case ARGP_KEY_ARG:
00075       if(state->arg_num >= 10)   // too many arguments
00076         argp_usage (state);
00077       arguments->args[state->arg_num] = arg;
00078       break;
00079     case ARGP_KEY_NO_ARGS:
00080       argp_usage(state);
00081       break;
00082     case ARGP_KEY_END:
00083       if (state->arg_num <= 0) // not enough arguments
00084         argp_usage(state);
00085       break;
00086 
00087     default:
00088       return ARGP_ERR_UNKNOWN;
00089     }
00090   return 0;
00091 }
00092 
00093 static char args_doc[] = "image1 image2 ... imageN (N <= 10)";
00094 static struct argp argp = {options, parse_opt, args_doc, doc};
00095 #endif //__APPLE_CC__
00096 
00097 
00098 static inline double difftv(const struct timeval &t1, const struct timeval &t0)
00099 { return( (t1.tv_sec -t0.tv_sec )*1.0 +(t1.tv_usec-t0.tv_usec)*1e-6); }
00100 
00101 using namespace Magick;
00102 
00103 int main (int argc, char **argv)
00104 {  
00105   struct arguments arguments;
00106   // default values
00107   strcpy(arguments.matfile,"out.mat");
00108   strcpy(arguments.cfile,"ci.xml");
00109   strcpy(arguments.emofile,"emo.xml");
00110   arguments.emoFlag = 0;
00111   for(int i = 0; i<MAX_NUM_IMAGES; i++)
00112     arguments.args[i] = 0;
00113     
00114   // get values from command line
00115 #ifndef __APPLE_CC__
00116   argp_parse(&argp, argc, argv, 0, 0, &arguments);
00117 #else
00118   int opt_count = 0;
00119   arguments.args[0] = (char *)malloc(256 * sizeof(char));
00120   if(argc==2)
00121     strcpy(arguments.args[0],argv[1]);
00122   else{
00123     arguments.emoFlag = 1;
00124     strcpy(arguments.args[0],argv[2]);
00125   }
00126   
00127 #endif
00128 
00129   //
00130   // Initialize the MPISearchObjectDetector object
00131   //
00132   MPISearchFaceDetector mpi;
00133 
00134   // 
00135   // Loop through all images
00136   //
00137   int last_width = 0, last_height = 0;
00138   int arg_count = -1;
00139   while(arguments.args[++arg_count]!=0){
00140     // Try and open the file.  
00141     cout << "opening file " <<  arguments.args[arg_count] << endl; 
00142     try { 
00143       Image image( arguments.args[arg_count] );
00144 
00145       // 
00146       // allocate our buffer and write the pixels to it. 
00147       //
00148       RImage<float> pixels(image.columns(), image.rows());
00149 
00150       image.quantizeColors ( 256 ); // convert to 256 colors
00151       image.quantize( ); // convert to grayscale
00152 
00153       image.write(0,0, image.columns(), image.rows(), "I", FloatPixel, pixels.array);
00154       image.display();
00155 
00156       //pixels.print(); // why are colors between 0 and 100 ?
00157 
00158       cout << "Image: " << arguments.args[arg_count]
00159            << " - " << pixels.width 
00160            << "x" << pixels.height 
00161            << endl;
00162       if((pixels.width != last_width) || (pixels.height != last_height))
00163             mpi.resetStream(pixels.width, pixels.height, 0.75);
00164       last_width = pixels.width ; last_height = pixels.height;
00165 
00166       FaceBoxList faces;
00167 
00168       struct timeval tv_now, last, tv_first, final;
00169 
00170       int numwindows, nframes = 50, num_successes = 0;
00171       Square trackedFace;
00172       // Search nframes times
00173       for(int fc = 0; fc < nframes; ++fc){
00174         faces.clear();
00175         gettimeofday(&last,0);
00176         if(fc==0)
00177           numwindows = mpi.search(pixels, faces, 0, 1.25);
00178         else
00179           numwindows = mpi.search(pixels, faces, 1, .75);
00180         gettimeofday(&tv_now,0);
00181         if(fc == 0){
00182           cout << "numwindows: " << numwindows << endl;
00183           printf("\tThe first search took %g seconds. \n", difftv(tv_now, last));
00184           printf("Initially found %d faces in %s.\n", faces.size(), arguments.args[arg_count]);
00185         }
00186         faces.simplify( 0.2 ); //0.24);
00187         if(fc == 0)
00188           printf("Now found %d.\n", faces.size());
00189         if(!faces.empty()){
00190           faces.objects.sort(decreasing<int>);
00191           trackedFace = faces.front();
00192           //cout << "Front face size: "<< trackedFace.size << endl;
00193           num_successes++;
00194         }
00195         mpi.AdjSearchWindow(trackedFace);
00196         if(fc == 0)
00197           gettimeofday(&tv_first,0);
00198       }
00199       gettimeofday(&final,0);
00200       printf("\tTheoretical frame rate for one-face tracking: %g fps. \n", (nframes-1) / difftv(final, tv_first));
00201       printf("\tFound face in %2.0f%% of frames\n",((float)num_successes/(float)nframes) * 100.0);       
00202 
00203       if(faces.size() != 0) {
00204         image.strokeColor("white");
00205         image.strokeWidth(1);
00206         image.fillColor("none" );
00207 
00208         while(!faces.empty( ))
00209         {
00210           Square face = faces.front();  
00211           faces.pop_front();
00212 
00213           image.draw( DrawableRectangle(face.x, face.y, 
00214                 face.x + face.size, face.y + face.size));
00215 
00216           //cout << "square_size: " << face->size << " x: " << face->x << " y: " << face->y << endl;
00217 
00218           //delete face;
00219         }
00220         
00221         image.display();
00222         //image.write("result.jpg");
00223       }
00224     }
00225     catch ( ErrorFileOpen &error ) {
00226       cerr << "Error opening file: " 
00227            <<  arguments.args[arg_count]
00228            << ". ImageMagick said: " 
00229            << error.what() 
00230            << endl;
00231     }
00232   } 
00233   return 0;
00234 }

Generated on Mon Nov 8 17:07:41 2004 for MPT by  doxygen 1.3.9.1