RetinaService.C

Go to the documentation of this file.
00001 /*!@file NeovisionII/RetinaService.C */
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
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/NeovisionII/RetinaService.C $
00035 // $Id: RetinaService.C 10794 2009-02-08 06:21:09Z itti $
00036 //
00037 
00038 #include "NeovisionII/RetinaService.H"
00039 
00040 RetinaI::RetinaI(ModelManager& mgr,
00041     nub::ref<InputFrameSeries> ifs,
00042     const std::string& descrName,
00043     const std::string& tagName
00044     ) :
00045   ModelComponent(mgr, descrName, tagName),
00046   itsIfs(ifs)
00047 {
00048 }
00049 
00050 void RetinaI::initSimEvents(const Ice::CommunicatorPtr icPtr, const Ice::ObjectPrx objectPrx)
00051 {
00052   //Get the IceStorm object and create a new Retina topic
00053   Ice::ObjectPrx obj = icPtr->stringToProxy("SimEvents/TopicManager:tcp -p 11111");
00054   IceStorm::TopicManagerPrx topicManager =
00055     IceStorm::TopicManagerPrx::checkedCast(obj);
00056   IceStorm::TopicPrx topic;
00057   try {
00058     topic = topicManager->retrieve("RetinaTopic"); //check if the Retina topic exists
00059   } catch (const IceStorm::NoSuchTopic&) {
00060     topic = topicManager->create("RetinaTopic"); //The retina topic does not exists, create
00061   }
00062 
00063   //Make a one way retina message publisher for efficency
00064   Ice::ObjectPrx pub = topic->getPublisher()->ice_oneway();
00065   itsEventsPub = SimEvents::EventsPrx::uncheckedCast(pub);
00066 }
00067 
00068 //We dont get any messages for now
00069 void RetinaI::evolve(const SimEvents::EventMessagePtr& eMsg,
00070     const Ice::Current&)
00071 {
00072 }
00073 
00074 ///The run function which runes in a sperate thread
00075 void RetinaI::run()
00076 {
00077   while(1)
00078   {
00079     getFrame();
00080     if (itsCurrentImg.initialized())
00081     {
00082       SimEvents::RetinaMessagePtr retinaMsg = new SimEvents::RetinaMessage;
00083       retinaMsg->img = Image2Ice(itsCurrentImg);
00084       itsEventsPub->evolve(retinaMsg);
00085     }
00086     usleep(10000);
00087   }
00088 }
00089 
00090 //Read a frame into currentImg
00091 void RetinaI::getFrame()
00092 {
00093   itsCurrentImg.clear();
00094   const FrameState is = itsIfs->updateNext();
00095   if (is == FRAME_COMPLETE) return;
00096   //grab the images
00097   GenericFrame input = itsIfs->readFrame();
00098   if (!input.initialized()) return;
00099   itsCurrentImg = input.asRgb();
00100 }
00101 
00102 
00103 ImageIceMod::ImageIce RetinaI::getOutput(const Ice::Current&)
00104 {
00105   return Image2Ice(itsCurrentImg);
00106 }
00107 
00108 /////////////////////////// The Retina Service to init the retina and start as a deamon ///////////////
00109 class RetinaService : public Ice::Service {
00110   protected:
00111     virtual bool start(int, char* argv[]);
00112     virtual bool stop();
00113   private:
00114     Ice::ObjectAdapterPtr itsAdapter;
00115     ModelManager *itsMgr;
00116 };
00117 
00118 bool RetinaService::stop()
00119 {
00120   if (itsMgr)
00121     delete itsMgr;
00122   return true;
00123 }
00124 
00125 bool RetinaService::start(int argc, char* argv[])
00126 {
00127 
00128         itsMgr = new ModelManager("RetinaService");
00129 
00130         nub::ref<InputFrameSeries> ifs(new InputFrameSeries(*itsMgr));
00131         itsMgr->addSubComponent(ifs);
00132 
00133   nub::ref<RetinaI> retina(new RetinaI(*itsMgr, ifs));
00134         itsMgr->addSubComponent(retina);
00135 
00136         itsMgr->parseCommandLine((const int)argc, (const char**)argv, "", 0, 0);
00137 
00138   char adapterStr[255];
00139   sprintf(adapterStr, "default -p %i", BrainObjects::RetinaPort);
00140         itsAdapter = communicator()->createObjectAdapterWithEndpoints("RetinaAdapter", adapterStr);
00141 
00142         Ice::ObjectPtr object = retina.get();
00143   Ice::ObjectPrx objectPrx = itsAdapter->add(object, communicator()->stringToIdentity("Retina"));
00144   retina->initSimEvents(communicator(), objectPrx);
00145         itsAdapter->activate();
00146 
00147   itsMgr->start();
00148 
00149   //Start the retina evolve thread
00150   IceUtil::ThreadPtr retinaThread = retina.get();
00151   retinaThread->start();
00152 
00153         return true;
00154 }
00155 
00156 // ######################################################################
00157 int main(int argc, char** argv) {
00158 
00159   RetinaService svc;
00160   return svc.main(argc, argv);
00161 }
00162 
Generated on Sun May 8 08:41:02 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3