00001 /*!@file SeaBee/test-SiftRec.C test/train sift recognizer */ 00002 // //////////////////////////////////////////////////////////////////// // 00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00004 // University of Southern California (USC) and the iLab at USC. // 00005 // See http://iLab.usc.edu for information about this project. // 00006 // //////////////////////////////////////////////////////////////////// // 00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00009 // in Visual Environments, and Applications'' by Christof Koch and // 00010 // Laurent Itti, California Institute of Technology, 2001 (patent // 00011 // pending; application number 09/912,225 filed July 23, 2001; see // 00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00013 // //////////////////////////////////////////////////////////////////// // 00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00015 // // 00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00017 // redistribute it and/or modify it under the terms of the GNU General // 00018 // Public License as published by the Free Software Foundation; either // 00019 // version 2 of the License, or (at your option) any later version. // 00020 // // 00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00024 // PURPOSE. See the GNU General Public License for more details. // 00025 // // 00026 // You should have received a copy of the GNU General Public License // 00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00029 // Boston, MA 02111-1307 USA. // 00030 // //////////////////////////////////////////////////////////////////// // 00031 // 00032 // Primary maintainer for this file: Lior Elazary 00033 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/SeaBee/test-SiftRec.C $ 00034 // $Id: test-SiftRec.C 10794 2009-02-08 06:21:09Z itti $ 00035 00036 #include "Component/GlobalOpts.H" 00037 #include "Component/ModelManager.H" 00038 #include "Component/ModelOptionDef.H" 00039 #include "Component/ModelParam.H" 00040 #include "Component/ModelParamBatch.H" 00041 00042 #include "Media/FrameSeries.H" 00043 #include "Transport/FrameIstream.H" 00044 #include "Raster/GenericFrame.H" 00045 #include "Media/MediaOpts.H" 00046 #include "Transport/FrameInfo.H" 00047 #include "Neuro/EnvSegmenterConfigurator.H" 00048 #include "Image/Image.H" 00049 #include "Image/Pixels.H" 00050 #include "Image/DrawOps.H" 00051 #include "Raster/Raster.H" 00052 00053 #include "SeaBee/SiftRec.H" 00054 00055 const ModelOptionCateg MOC_SIFTREC = { 00056 MOC_SORTPRI_2, "SiftRec Options" }; 00057 00058 static const ModelOptionDef OPT_TrainingLabel = 00059 { MODOPT_ARG_STRING, "TrainingLabel", &MOC_SIFTREC, OPTEXP_CORE, 00060 "If this label is set, then the system goes into training mode, " 00061 "Traing all of the image with this label Whether to include an ", 00062 "training-label", '\0', "", "" }; 00063 00064 static const ModelOptionDef OPT_TrainUnknown = 00065 { MODOPT_FLAG, "TrainUnknown", &MOC_SIFTREC, OPTEXP_CORE, 00066 "Wether to train all images with the label or just the unknown." 00067 "Note that with value of false, many more entries in the database will be entered.", 00068 "train-unknown", '\0', "", "true" }; 00069 00070 00071 00072 int main(int argc, char* argv[]) 00073 { 00074 00075 ModelManager mgr("SiftRec Tester"); 00076 00077 OModelParam<bool> trainUnknown(&OPT_TrainUnknown, &mgr); 00078 OModelParam<std::string> trainingLabel(&OPT_TrainingLabel, &mgr); 00079 00080 nub::soft_ref<SiftRec> siftRec(new SiftRec(mgr)); 00081 mgr.addSubComponent(siftRec); 00082 00083 nub::ref<EnvSegmenterConfigurator> esec(new EnvSegmenterConfigurator(mgr)); 00084 mgr.addSubComponent(esec); 00085 00086 00087 00088 nub::soft_ref<InputFrameSeries> ifs(new InputFrameSeries(mgr)); 00089 mgr.addSubComponent(ifs); 00090 00091 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(mgr)); 00092 mgr.addSubComponent(ofs); 00093 00094 mgr.exportOptions(MC_RECURSE); 00095 00096 if (mgr.parseCommandLine(argc, argv, "", 0, 0) == false) return(1); 00097 00098 mgr.start(); 00099 00100 siftRec->initVDB(); //initialize the database 00101 00102 nub::soft_ref<EnvSegmenter> seg = esec->getSeg(); 00103 00104 while(true) 00105 { 00106 const FrameState is = ifs->updateNext(); 00107 if (is == FRAME_COMPLETE) break; 00108 //grab the images 00109 GenericFrame input = ifs->readFrame(); 00110 if (!input.initialized()) break; 00111 Image<PixRGB<byte> > img = input.asRgb(); 00112 00113 Image<byte> foamask; 00114 Image<PixRGB<byte> > segmentdisp; 00115 //Segment the image to get the card 00116 const Rectangle segRect = seg->getFoa(img, Point2D<int>(0,0), 00117 &foamask, &segmentdisp); 00118 00119 std::string objName; 00120 float score = 0; 00121 Rectangle matchRect; 00122 if (trainingLabel.getVal().size() > 0) 00123 { 00124 //Train the dataset 00125 if (trainUnknown.getVal()) //Train only unknown objects with this label 00126 { 00127 //Recognize 00128 objName = siftRec->matchObject(img, score,matchRect); 00129 } 00130 00131 if (objName == "nomatch" || objName.size() == 0) 00132 { 00133 LINFO("Training with object name %s", trainingLabel.getVal().c_str()); 00134 siftRec->trainObject(img, trainingLabel.getVal()); 00135 } 00136 } else { 00137 //Recognize 00138 objName = siftRec->matchObject(img, score,matchRect); 00139 00140 } 00141 00142 //markup the image with the values 00143 const std::string txt = 00144 sformat("%s:%0.2f", objName.c_str(), score); 00145 writeText(img, Point2D<int>(0,0), 00146 txt.c_str(), 00147 PixRGB<byte>(255), PixRGB<byte>(0)); 00148 00149 if (matchRect.isValid() && img.rectangleOk(matchRect)) 00150 { 00151 drawRect(img, matchRect, PixRGB<byte>(255, 255, 0), 1); 00152 drawCircle(img, matchRect.center(), 6, PixRGB<byte>(0,255,0)); 00153 } 00154 00155 if (segRect.isValid() && img.rectangleOk(segRect)) 00156 { 00157 drawRect(img, segRect, PixRGB<byte>(0, 255, 0), 1); 00158 } 00159 00160 ofs->writeRGB(img, "Input", FrameInfo("Input", SRC_POS)); 00161 ofs->writeRGB(segmentdisp, "Seg", FrameInfo("Seg", SRC_POS)); 00162 ofs->updateNext(); 00163 } 00164 00165 00166 // get ready to terminate: 00167 mgr.stop(); 00168 return 0; 00169 } 00170 00171 // ###################################################################### 00172 /* So things look consistent in everyone's emacs... */ 00173 /* Local Variables: */ 00174 /* indent-tabs-mode: nil */ 00175 /* End: */