app-TestSIFT_Worker.C

00001 #include "Robots/Beobot2/Testing/TestSIFT.ice.H"
00002 #include "SIFT/VisualObjectDB.H"
00003 #include <Ice/Ice.h>
00004 
00005 class SIFTMatcherI : public TestSIFT::SIFTMatcher
00006 {
00007   public:
00008     SIFTMatcherI(char const* dbfile)
00009       : TestSIFT::SIFTMatcher()
00010     {
00011       std::cout << "Loading Database " << dbfile << "...";
00012       itsSIFTdb.loadFrom(dbfile, false);
00013       std::cout << "Done, Loaded " << itsSIFTdb.numObjects() << " objects" << std::endl;
00014 
00015       std::cout << "Building KDTree...";
00016       itsSIFTdb.buildKDTree();
00017       std::cout << "Done" << std::endl;
00018 
00019       std::cout << "Waiting for server..." << std::endl;
00020     }
00021 
00022     TestSIFT::idSequence matchKeypoints(TestSIFT::keypointSequence const& keypoints, Ice::Current const&)
00023     {
00024       LINFO("Got %Zu Keypoints", keypoints.size());
00025       std::vector<rutz::shared_ptr<Keypoint> > VOkeypoints;
00026       for(size_t keyIdx=0; keyIdx<keypoints.size(); keyIdx++)
00027       {
00028         //Create a new keypoint
00029         TestSIFT::keypoint const* const key = &keypoints.at(keyIdx);
00030         VOkeypoints.push_back(
00031             rutz::shared_ptr<Keypoint>(new Keypoint(
00032                 key->oriFV, key->x, key->y, key->s, key->o, key->m
00033                 )
00034               )
00035             );
00036       }
00037 
00038       rutz::shared_ptr<VisualObject> vo(
00039           new VisualObject(
00040           "MyVisualObject",            //Name
00041           "NULL",                      //Image File Name
00042           Image<PixRGB<byte> >(),      //Image
00043           Point2D<int>(-1,-1),         //Attention point
00044           std::vector<float>(),        //Pre-attentive features
00045           VOkeypoints,                 //Our list of keypoints
00046           false,                       //useColor
00047           false));                     //computeKP
00048 
00049       //Find a match for the keypoints
00050       std::vector<rutz::shared_ptr<VisualObjectMatch> > matches;
00051       uint n_matches = itsSIFTdb.getObjectMatches(vo, matches);
00052       LINFO("Got %d matches: Best: %s", n_matches, 
00053           (matches.size() > 0 ? matches.at(0)->getVoTest()->getName().c_str() : "NONE" ));
00054 
00055       return TestSIFT::idSequence();
00056     }
00057 
00058   private:
00059     VisualObjectDB itsSIFTdb;
00060 };
00061 
00062 char* progName; 
00063 void printHelp()
00064 {
00065   std::cout << "Usage: " << progName << " [--port PORT] [--siftdb SIFTDBFILEPATH]" << std::endl;
00066 }
00067 
00068 int main(int argc, char const* argv[])
00069 {
00070   progName = (char*)argv[0];
00071   int port = TestSIFT::DEFAULTWORKERPORT;
00072   char const* siftdb = "sift.db";
00073 
00074 
00075   ////////////////////////////////////////////////////////////////////////
00076   // Hacky command line option grabber (don't judge!)
00077   ////////////////////////////////////////////////////////////////////////
00078   if((argc-1)%2) { 
00079     std::cout << "Bad Command Line Arguments!" << std::endl;
00080     printHelp();
00081     exit(0); 
00082   }
00083   for(int i=1; i< argc; ++i)
00084   {
00085     if(strncmp(argv[i], "--", 2) != 0)
00086     {
00087       std::cout << "Bad Command Line Arguments! (" << argv[i] << ")" << std::endl;
00088       printHelp();
00089       exit(0);
00090     }
00091 
00092     if(strcmp(argv[i], "--port") == 0)
00093       port = atoi(argv[++i]);
00094     else if(strcmp(argv[i], "--siftdb") == 0)
00095       siftdb = argv[++i];
00096     else if(strcmp(argv[i], "--help") == 0)
00097       printHelp();
00098     else
00099     {
00100       std::cout << "Unknown Command Line Arguments! (" << argv[i] << ")" << std::endl;
00101       printHelp();
00102       exit(0);
00103     }
00104   }
00105 
00106   ////////////////////////////////////////////////////////////////////////
00107   // Load up the Ice runtime, and create a new SIFTMatcher on the adapter
00108   // The details here are unimportannt - check out the implementation of
00109   // SIFTMatcher above for the interesting bits
00110   ////////////////////////////////////////////////////////////////////////
00111   int status = 0;
00112   Ice::CommunicatorPtr ic;
00113   try
00114   {
00115     std::cout << "Starting SIFT Matching Worker" << std::endl;
00116     std::cout << "Port: " << port << std::endl;
00117     std::cout << "DB File: " << siftdb << std::endl;
00118 
00119                 Ice::PropertiesPtr props = Ice::createProperties(argc,(char**)argv);
00120                 props->setProperty("Ice.MessageSizeMax", "1048576");
00121                 props->setProperty("Ice.Warn.Connections", "1");
00122 
00123                 Ice::InitializationData id;
00124                 id.properties = props;
00125 
00126     ic = Ice::initialize(id);
00127 
00128     char connectionBuffer[256];
00129     if(argc == 2)
00130       port = atoi(argv[1]);
00131     sprintf(connectionBuffer, "default -p %d", port);
00132     Ice::ObjectAdapterPtr adapter = ic->createObjectAdapterWithEndpoints(
00133         "SIFTMatcher", connectionBuffer);
00134     std::cout << "Connection opened on port " << port << std::endl;
00135 
00136     Ice::ObjectPtr object = new SIFTMatcherI(siftdb);
00137     adapter->add(object, ic->stringToIdentity("SIFTMatcher"));
00138     adapter->activate();
00139     ic->waitForShutdown();
00140   }
00141   catch(Ice::Exception const& e)
00142   {
00143     std::cerr << e << std::endl;
00144     status=1;
00145   }
00146   catch(char const* msg)
00147   {
00148     std::cerr << msg << std::endl;
00149     status=1;
00150   }
00151 
00152   if(ic)
00153   {
00154     try
00155     { 
00156       std::cout << "Shutting Down...";
00157       ic->destroy();
00158       std::cout << "Success" << std::endl;
00159     }
00160     catch(Ice::Exception const& e)
00161     {
00162       std::cerr << "Failed: " << e << std::endl;
00163       status = 1;
00164     }
00165   }
00166     return status;
00167 }
Generated on Sun May 8 08:41:19 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3