Fawkes API Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * qa_camargp.h - QA for camera argument parser 00004 * 00005 * Generated: Wed Apr 11 16:02:33 2007 00006 * Copyright 2005-2007 Tim Niemueller [www.niemueller.de] 00007 * 2009 Daniel Beck 00008 * 00009 ****************************************************************************/ 00010 00011 /* This program is free software; you can redistribute it and/or modify 00012 * it under the terms of the GNU General Public License as published by 00013 * the Free Software Foundation; either version 2 of the License, or 00014 * (at your option) any later version. A runtime exception applies to 00015 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00016 * 00017 * This program is distributed in the hope that it will be useful, 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 * GNU Library General Public License for more details. 00021 * 00022 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00023 */ 00024 00025 /// @cond QA 00026 00027 #include <fvutils/adapters/iplimage.h> 00028 #include <fvutils/color/colorspaces.h> 00029 #include <fvutils/readers/jpeg.h> 00030 #include <fvutils/draw/drawer.h> 00031 #include <classifiers/faces.h> 00032 #include <filters/roidraw.h> 00033 #include <fvwidgets/image_display.h> 00034 #include <utils/system/argparser.h> 00035 #include <utils/time/tracker.h> 00036 #include <cams/factory.h> 00037 00038 #include <SDL.h> 00039 #include <opencv/cv.h> 00040 00041 #include <cstdlib> 00042 #include <cstdio> 00043 00044 using namespace fawkes; 00045 00046 int 00047 main(int argc, char **argv) 00048 { 00049 ArgumentParser* argp = new ArgumentParser( argc, argv, "h:f:c:" ); 00050 00051 if ( argp->has_arg( "h" ) && argp->has_arg( "f" ) ) 00052 // read image from file 00053 { 00054 const char *cascade_file = argp->arg( "h" ); 00055 const char *image_file = argp->arg( "f" ); 00056 00057 JpegReader *reader = new JpegReader(image_file); 00058 unsigned char *buffer = malloc_buffer(YUV422_PLANAR, 00059 reader->pixel_width(), reader->pixel_height()); 00060 00061 reader->set_buffer(buffer); 00062 reader->read(); 00063 00064 FacesClassifier *classifier = new FacesClassifier(cascade_file, reader->pixel_width(), 00065 reader->pixel_height()); 00066 00067 00068 classifier->set_src_buffer(buffer, reader->pixel_width(), reader->pixel_height()); 00069 std::list< ROI > *rois = classifier->classify(); 00070 00071 FilterROIDraw *roi_draw = new FilterROIDraw(); 00072 for (std::list< ROI >::iterator i = rois->begin(); i != rois->end(); ++i) { 00073 printf("ROI: start (%u, %u) extent %u x %u\n", (*i).start.x, (*i).start.y, 00074 (*i).width, (*i).height); 00075 00076 roi_draw->set_dst_buffer(buffer, &(*i)); 00077 roi_draw->apply(); 00078 } 00079 00080 ImageDisplay *display = new ImageDisplay(reader->pixel_width(), reader->pixel_height()); 00081 display->show(buffer); 00082 00083 display->loop_until_quit(); 00084 00085 delete display; 00086 00087 delete rois; 00088 free(buffer); 00089 delete reader; 00090 delete classifier; 00091 } 00092 00093 else if ( argp->has_arg( "h" ) && argp->has_arg( "c" ) ) 00094 // get images from camera 00095 { 00096 const char *cascade_file = argp->arg( "h" ); 00097 00098 Camera* camera = NULL; 00099 try 00100 { 00101 camera = CameraFactory::instance( argp->arg( "c" ) ); 00102 camera->open(); 00103 camera->start(); 00104 } 00105 catch ( Exception& e ) 00106 { 00107 printf( "Failed to open camera.\n" ); 00108 delete camera; 00109 return( -1 ); 00110 } 00111 00112 printf( "successfully opened camera: w=%d h=%d\n", 00113 camera->pixel_width(), camera->pixel_height() ); 00114 00115 TimeTracker* tt = new TimeTracker(); 00116 unsigned int ttc_recognition = tt->add_class( "Face recognition" ); 00117 unsigned int loop_count = 0; 00118 00119 00120 IplImage* image = cvCreateImage( cvSize( camera->pixel_width(), 00121 camera->pixel_height() ), 00122 IPL_DEPTH_8U, 3 ); 00123 00124 IplImage* scaled_image = cvCreateImage( cvSize( camera->pixel_width() / 2, 00125 camera->pixel_height() / 2 ), 00126 IPL_DEPTH_8U, 3 ); 00127 00128 FacesClassifier *classifier = new FacesClassifier( cascade_file, 00129 camera->pixel_width(), 00130 camera->pixel_height(), 00131 scaled_image, 00132 1.2 /* scale factor */, 00133 2 /* min neighbours */, 00134 CV_HAAR_DO_CANNY_PRUNING ); 00135 00136 unsigned char* display_buffer = (unsigned char*) malloc( camera->buffer_size() ); 00137 00138 ImageDisplay* display = new ImageDisplay( camera->pixel_width(), 00139 camera->pixel_height(), 00140 "QA Faces Classifier" ); 00141 00142 Drawer* drawer = new Drawer(); 00143 drawer->set_buffer( display_buffer, 00144 camera->pixel_width(), 00145 camera->pixel_height() ); 00146 00147 SDL_Event redraw_event; 00148 redraw_event.type = SDL_KEYUP; 00149 redraw_event.key.keysym.sym = SDLK_SPACE; 00150 00151 SDL_PushEvent( &redraw_event ); 00152 00153 bool quit = false; 00154 while ( !quit ) 00155 { 00156 SDL_Event event; 00157 if ( SDL_WaitEvent( &event ) ) 00158 { 00159 switch ( event.type ) 00160 { 00161 case SDL_QUIT: 00162 quit = true; 00163 break; 00164 00165 case SDL_KEYUP: 00166 if ( event.key.keysym.sym == SDLK_SPACE ) 00167 { 00168 camera->capture(); 00169 00170 if ( camera->buffer() != NULL ) 00171 { 00172 IplImageAdapter::convert_image_bgr( camera->buffer(), image ); 00173 cvResize( image, scaled_image, CV_INTER_LINEAR ); 00174 memcpy( display_buffer, camera->buffer(), camera->buffer_size() ); 00175 00176 tt->ping_start( ttc_recognition ); 00177 std::list< ROI > *rois = classifier->classify(); 00178 tt->ping_end( ttc_recognition ); 00179 00180 camera->dispose_buffer(); 00181 00182 bool first = true; 00183 for ( std::list< ROI >::reverse_iterator i = rois->rbegin(); 00184 i != rois->rend(); 00185 ++i ) 00186 { 00187 if ( first ) { drawer->set_color( 127, 70, 200 ); } 00188 drawer->draw_rectangle( 2 * i->start.x, 2 * i->start.y, 2 * i->width, 2 * i->height ); 00189 if ( first ) { drawer->set_color( 30, 30, 30 ); first = false; } 00190 } 00191 00192 if ( ++loop_count % 15 == 0 ) { tt->print_to_stdout(); } 00193 00194 display->show( display_buffer ); 00195 } 00196 00197 SDL_PushEvent( &redraw_event ); 00198 } 00199 00200 else if ( event.key.keysym.sym == SDLK_ESCAPE ) 00201 { quit = true; } 00202 00203 break; 00204 00205 default: 00206 break; 00207 } 00208 } 00209 } 00210 00211 camera->stop(); 00212 camera->close(); 00213 delete camera; 00214 delete display; 00215 delete drawer; 00216 free( display_buffer ); 00217 cvReleaseImage( &image ); 00218 cvReleaseImage( &scaled_image ); 00219 delete tt; 00220 } 00221 00222 else 00223 { 00224 printf("Usage: %s -h <Haar cascade file> -f <Image file as JPEG>\n", argv[0]); 00225 printf(" or %s -h <Haar cascade file> -c <Camera argument string>\n", argv[0]); 00226 exit(-1); 00227 } 00228 00229 delete argp; 00230 } 00231 00232 /// @endcond