00001 /*!@file Neuro/VisualCortexService.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/VisualCortexService.C $ 00035 // $Id: VisualCortexService.C 12962 2010-03-06 02:13:53Z irock $ 00036 // 00037 00038 #include "NeovisionII/VisualCortexService.H" 00039 #include "Image/ColorOps.H" 00040 #include "GUI/DebugWin.H" 00041 00042 VisualCortexI::VisualCortexI(OptionManager& mgr, 00043 const std::string& descrName, 00044 const std::string& tagName ) : 00045 ModelComponent(mgr, descrName, tagName) 00046 { 00047 itsEvc = nub::soft_ref<EnvVisualCortex>(new EnvVisualCortex(mgr)); 00048 addSubComponent(itsEvc); 00049 00050 //Subscribe to the various topics 00051 IceStorm::TopicPrx topicPrx; 00052 TopicInfo tInfo("RetinaTopic", topicPrx); 00053 itsTopicsSubscriptions.push_back(tInfo); 00054 00055 } 00056 00057 VisualCortexI::~VisualCortexI() 00058 { 00059 unsubscribeSimEvents(); 00060 } 00061 00062 void VisualCortexI::initSimEvents(Ice::CommunicatorPtr icPtr, Ice::ObjectPrx objectPrx) 00063 { 00064 //Get the IceStorm object 00065 Ice::ObjectPrx obj = icPtr->stringToProxy("SimEvents/TopicManager:tcp -p 11111"); 00066 IceStorm::TopicManagerPrx topicManager = 00067 IceStorm::TopicManagerPrx::checkedCast(obj); 00068 00069 //Create a VisualCortex Topic 00070 IceStorm::TopicPrx topic; 00071 try { 00072 topic = topicManager->retrieve("VisualCortexTopic"); //check if the Retina topic exists 00073 } catch (const IceStorm::NoSuchTopic&) { 00074 topic = topicManager->create("VisualCortexTopic"); //The retina topic does not exists, create 00075 } 00076 //Make a one way visualCortex message publisher for efficency 00077 Ice::ObjectPrx pub = topic->getPublisher()->ice_oneway(); 00078 itsEventsPub = SimEvents::EventsPrx::uncheckedCast(pub); 00079 00080 //Subscribe the SimulationViewer to theTopics 00081 itsObjectPrx = objectPrx; 00082 //subscribe to the topics 00083 for(uint i=0; i<itsTopicsSubscriptions.size(); i++) 00084 { 00085 try { 00086 IceStorm::QoS qos; 00087 itsTopicsSubscriptions[i].topicPrx = 00088 topicManager->retrieve(itsTopicsSubscriptions[i].name.c_str()); //Get the 00089 itsTopicsSubscriptions[i].topicPrx->subscribeAndGetPublisher(qos, itsObjectPrx); //Subscribe to the retina topic 00090 } catch (const IceStorm::NoSuchTopic&) { 00091 LFATAL("Error! No %s topic found!", itsTopicsSubscriptions[i].name.c_str()); 00092 } catch (const char* msg) { 00093 LINFO("Error %s", msg); 00094 } catch (const Ice::Exception& e) { 00095 cerr << e << endl; 00096 } 00097 00098 } 00099 } 00100 00101 void VisualCortexI::unsubscribeSimEvents() 00102 { 00103 //Unsubscribe from all the topics we are registerd to 00104 for(uint i=0; i<itsTopicsSubscriptions.size(); i++) 00105 { 00106 LINFO("Unsubscribe from %s", itsTopicsSubscriptions[i].name.c_str()); 00107 itsTopicsSubscriptions[i].topicPrx->unsubscribe(itsObjectPrx); 00108 } 00109 } 00110 00111 00112 //The VC excepts a retina image and posts a saliency map 00113 void VisualCortexI::evolve(const SimEvents::EventMessagePtr& eMsg, 00114 const Ice::Current&) 00115 { 00116 if(eMsg->ice_isA("::SimEvents::RetinaMessage")){ 00117 SimEvents::RetinaMessagePtr msg = SimEvents::RetinaMessagePtr::dynamicCast(eMsg); 00118 Image<PixRGB<byte> > retinaImg = Ice2Image<PixRGB<byte> > (msg->img); 00119 00120 itsEvc->input(retinaImg); 00121 Image<byte> smap = itsEvc->getVCXmap(); 00122 00123 //send a visual cortex message 00124 SimEvents::VisualCortexMessagePtr vcMsg = new SimEvents::VisualCortexMessage; 00125 vcMsg->vco = Image2Ice(smap); 00126 try { 00127 itsEventsPub->evolve(vcMsg); 00128 } catch(const IceStorm::NoSuchTopic&) { 00129 } 00130 } 00131 } 00132 00133 /////////////////////////// The VC Service to init the retina and start as a deamon /////////////// 00134 class VisualCortexService : public Ice::Service { 00135 protected: 00136 virtual bool start(int, char* argv[]); 00137 virtual bool stop() { 00138 if (itsMgr) 00139 delete itsMgr; 00140 return true; 00141 } 00142 00143 private: 00144 Ice::ObjectAdapterPtr itsAdapter; 00145 ModelManager *itsMgr; 00146 }; 00147 00148 bool VisualCortexService::start(int argc, char* argv[]) 00149 { 00150 00151 itsMgr = new ModelManager("VisualCortexService"); 00152 00153 nub::ref<VisualCortexI> vc(new VisualCortexI(*itsMgr)); 00154 itsMgr->addSubComponent(vc); 00155 00156 itsMgr->parseCommandLine((const int)argc, (const char**)argv, "", 0, 0); 00157 00158 char adapterStr[255]; 00159 sprintf(adapterStr, "default -p %i", BrainObjects::VisualCortexPort); 00160 itsAdapter = communicator()->createObjectAdapterWithEndpoints("VisualCortexAdapter", 00161 adapterStr); 00162 00163 Ice::ObjectPtr object = vc.get(); 00164 Ice::ObjectPrx objectPrx = itsAdapter->add(object, communicator()->stringToIdentity("VisualCortex")); 00165 vc->initSimEvents(communicator(), objectPrx); 00166 itsAdapter->activate(); 00167 00168 itsMgr->start(); 00169 00170 return true; 00171 } 00172 00173 // ###################################################################### 00174 int main(int argc, char** argv) { 00175 00176 VisualCortexService svc; 00177 return svc.main(argc, argv); 00178 } 00179 00180