00001 /*! @file ObjRec/test-cardRec.C test card recognition */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Lior Elazary <elazary@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/ObjRec/test-cardRec.C $ 00035 // $Id: test-cardRec.C 10794 2009-02-08 06:21:09Z itti $ 00036 // 00037 00038 #include "Component/ModelManager.H" 00039 #include "GUI/DebugWin.H" 00040 #include "Image/DrawOps.H" 00041 #include "Image/Image.H" 00042 #include "Image/MathOps.H" 00043 #include "Image/OpenCVUtil.H" 00044 #include "Image/Rectangle.H" 00045 #include "Image/Transforms.H" 00046 #include "Media/FrameSeries.H" 00047 #include "Neuro/EnvSegmenterCannyContour.H" 00048 #include "Raster/GenericFrame.H" 00049 #include "Raster/Raster.H" 00050 #include "SIFT/VisualObject.H" 00051 #include "SIFT/VisualObjectDB.H" 00052 #include "Transport/FrameInfo.H" 00053 #include "Util/Timer.H" 00054 00055 #include <stdio.h> 00056 00057 #define USECOLOR false 00058 00059 VisualObjectDB itsObjectDB; 00060 #ifndef HAVE_OPENCV 00061 LFATAL("OpenCV must be installed in order to use this function"); 00062 #else 00063 00064 std::string recCard(const Image<PixRGB<byte> > &img) 00065 { 00066 std::string cardName; 00067 00068 std::vector< rutz::shared_ptr<VisualObjectMatch> > matches; 00069 rutz::shared_ptr<VisualObject> 00070 vo(new VisualObject("PIC", "PIC", img, 00071 Point2D<int>(-1,-1), 00072 std::vector<double>(), 00073 std::vector< rutz::shared_ptr<Keypoint> >(), 00074 USECOLOR)); 00075 00076 const uint nm = 00077 itsObjectDB.getObjectMatches(vo, matches, VOMA_SIMPLE, 00078 100U, //max objs to return 00079 0.5F, //keypoint distance score default 0.5F 00080 0.5F, //affine distance score default 0.5F 00081 1.0F, //minscore default 1.0F 00082 3U, //min # of keypoint match 00083 100U, //keypoint selection thershold 00084 false //sort by preattentive 00085 ); 00086 00087 LINFO("Found %i", nm); 00088 00089 if (nm > 0) 00090 { 00091 cardName = matches[0]->getVoTest()->getName(); 00092 LINFO("***** %u object recognition match(es) *****", nm); 00093 for (uint i = 0 ; i < nm; i ++) 00094 LINFO(" Match with '%s' [score = %f]", 00095 matches[i]->getVoTest()->getName().c_str(), 00096 matches[i]->getScore()); 00097 } 00098 else 00099 LINFO("***** Could not identify attended object! *****"); 00100 00101 00102 return cardName; 00103 } 00104 00105 void trainCard(const Image<PixRGB<byte> > &img, const std::string &cardName) 00106 { 00107 rutz::shared_ptr<VisualObject> 00108 vo(new VisualObject(cardName, "NULL", img, 00109 Point2D<int>(-1,-1), 00110 std::vector<double>(), 00111 std::vector< rutz::shared_ptr<Keypoint> >(), 00112 USECOLOR)); 00113 00114 itsObjectDB.addObject(vo, false); 00115 00116 itsObjectDB.saveTo("cards.vdb"); 00117 } 00118 00119 00120 int main(const int argc, const char **argv) 00121 { 00122 MYLOGVERB = LOG_INFO; 00123 ModelManager *mgr = new ModelManager("Test ObjRec"); 00124 00125 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(*mgr)); 00126 mgr->addSubComponent(ofs); 00127 00128 nub::ref<InputFrameSeries> ifs(new InputFrameSeries(*mgr)); 00129 mgr->addSubComponent(ifs); 00130 00131 nub::ref<EnvSegmenterCannyContour> seg(new EnvSegmenterCannyContour(*mgr)); 00132 mgr->addSubComponent(seg); 00133 00134 mgr->exportOptions(MC_RECURSE); 00135 00136 if (mgr->parseCommandLine( 00137 (const int)argc, (const char**)argv, "", 0, 0) == false) 00138 return 1; 00139 00140 mgr->start(); 00141 00142 seg->setModelParamVal("CannyMinCos", 1.0); 00143 seg->setModelParamVal("CannyMaxArea", 6000); 00144 seg->setModelParamVal("CannyMaxArea", 12000); 00145 00146 itsObjectDB.loadFrom("cards.vdb"); 00147 while(1) 00148 { 00149 Image< PixRGB<byte> > inputImg; 00150 const FrameState is = ifs->updateNext(); 00151 if (is == FRAME_COMPLETE) 00152 break; 00153 00154 //grab the images 00155 GenericFrame input = ifs->readFrame(); 00156 if (!input.initialized()) 00157 break; 00158 inputImg = input.asRgb(); 00159 00160 Image<PixRGB<byte> > out; 00161 00162 const Rectangle cardbox = seg->getFoa(inputImg, Point2D<int>(), NULL, &out); 00163 00164 ofs->writeRGB(out, "input", FrameInfo("input", SRC_POS)); 00165 00166 if (cardbox.isValid()) 00167 { 00168 Image<PixRGB<byte> > card = 00169 crop(inputImg, cardbox.getOverlap(inputImg.getBounds())); 00170 00171 std::string cardName = recCard(card); 00172 00173 if (cardName.length() == 0) 00174 { 00175 LINFO("Enter name for card:"); 00176 std::getline(std::cin, cardName, '\n'); 00177 00178 if (cardName.length() > 0) 00179 trainCard(card, cardName); 00180 } 00181 00182 writeText(card, Point2D<int>(0,0), cardName.c_str(), 00183 PixRGB<byte>(255), PixRGB<byte>(127)); 00184 00185 ofs->writeRGB(card, "card", FrameInfo("card", SRC_POS)); 00186 } 00187 00188 ofs->updateNext(); 00189 } 00190 mgr->stop(); 00191 00192 return 0; 00193 00194 } 00195 00196 #endif