00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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,
00079 0.5F,
00080 0.5F,
00081 1.0F,
00082 3U,
00083 100U,
00084 false
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
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