SaliencyMapService.C
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 "NeovisionII/SaliencyMapService.H"
00039 #include "Image/ColorOps.H"
00040 #include "Image/ShapeOps.H"
00041
00042 SaliencyMapI::SaliencyMapI(OptionManager& mgr,
00043 const std::string& descrName,
00044 const std::string& tagName ) :
00045 ModelComponent(mgr, descrName, tagName)
00046 {
00047 itsSMap = nub::soft_ref<EnvSaliencyMap>(new EnvSaliencyMap(mgr));
00048 addSubComponent(itsSMap);
00049
00050
00051 IceStorm::TopicPrx topicPrx;
00052 TopicInfo tInfo("VisualCortexTopic", topicPrx);
00053 itsTopicsSubscriptions.push_back(tInfo);
00054
00055 itsTopicsSubscriptions.push_back(TopicInfo("HippocampusTopic", topicPrx));
00056
00057
00058 }
00059
00060 SaliencyMapI::~SaliencyMapI()
00061 {
00062 unsubscribeSimEvents();
00063 }
00064
00065
00066 void SaliencyMapI::initSimEvents(Ice::CommunicatorPtr icPtr, Ice::ObjectPrx objectPrx)
00067 {
00068
00069 Ice::ObjectPrx obj = icPtr->stringToProxy("SimEvents/TopicManager:tcp -p 11111");
00070 IceStorm::TopicManagerPrx topicManager =
00071 IceStorm::TopicManagerPrx::checkedCast(obj);
00072
00073
00074 IceStorm::TopicPrx topic;
00075 try {
00076 topic = topicManager->retrieve("SaliencyMapTopic");
00077 } catch (const IceStorm::NoSuchTopic&) {
00078 topic = topicManager->create("SaliencyMapTopic");
00079 }
00080
00081 Ice::ObjectPrx pub = topic->getPublisher()->ice_oneway();
00082 itsEventsPub = SimEvents::EventsPrx::uncheckedCast(pub);
00083
00084
00085 itsObjectPrx = objectPrx;
00086
00087 for(uint i=0; i<itsTopicsSubscriptions.size(); i++)
00088 {
00089 try {
00090 IceStorm::QoS qos;
00091 itsTopicsSubscriptions[i].topicPrx =
00092 topicManager->retrieve(itsTopicsSubscriptions[i].name.c_str());
00093 itsTopicsSubscriptions[i].topicPrx->subscribeAndGetPublisher(qos, itsObjectPrx);
00094 } catch (const IceStorm::NoSuchTopic&) {
00095 LFATAL("Error! No %s topic found!", itsTopicsSubscriptions[i].name.c_str());
00096 } catch (const char* msg) {
00097 LINFO("Error %s", msg);
00098 } catch (const Ice::Exception& e) {
00099 cerr << e << endl;
00100 }
00101
00102 }
00103 }
00104
00105
00106 void SaliencyMapI::unsubscribeSimEvents()
00107 {
00108
00109 for(uint i=0; i<itsTopicsSubscriptions.size(); i++)
00110 {
00111 itsTopicsSubscriptions[i].topicPrx->unsubscribe(itsObjectPrx);
00112 }
00113 }
00114
00115
00116
00117
00118 void SaliencyMapI::evolve(const SimEvents::EventMessagePtr& eMsg,
00119 const Ice::Current&)
00120 {
00121 if(eMsg->ice_isA("::SimEvents::VisualCortexMessage")){
00122 SimEvents::VisualCortexMessagePtr msg = SimEvents::VisualCortexMessagePtr::dynamicCast(eMsg);
00123 Image<byte> vco = Ice2Image<byte>(msg->vco);
00124
00125
00126 Point2D<int> scaled_maxpos(-1,-1);
00127
00128
00129 if (itsBiasImg.initialized())
00130 itsSMap->setBiasImg(itsBiasImg);
00131
00132 const EnvSaliencyMap::State smstate = itsSMap->getSalmap(vco, scaled_maxpos);
00133
00134
00135 SimEvents::SaliencyMapMessagePtr sMapMsg = new SimEvents::SaliencyMapMessage;
00136 sMapMsg->smap = Image2Ice(smstate.salmap);
00137
00138
00139 SimEvents::LocInfoSeq mostSalientLocSeq;
00140 for(uint i=0; i<smstate.nMostSalientLoc.size(); i++)
00141 {
00142 const EnvSaliencyMap::LocInfo envLocInfo = smstate.nMostSalientLoc[i];
00143
00144 SimEvents::LocInfo locInfo;
00145 locInfo.lowresMaxpos.i = envLocInfo.lowres_maxpos.i;
00146 locInfo.lowresMaxpos.j = envLocInfo.lowres_maxpos.j;
00147 locInfo.fullresMaxpos.i = envLocInfo.fullres_maxpos.i;
00148 locInfo.fullresMaxpos.j = envLocInfo.fullres_maxpos.j;
00149 locInfo.maxval = envLocInfo.maxval;
00150 mostSalientLocSeq.push_back(locInfo);
00151 }
00152 sMapMsg->nMostSalientLoc = mostSalientLocSeq;
00153
00154
00155 itsEventsPub->evolve(sMapMsg);
00156 }
00157
00158 if(eMsg->ice_isA("::SimEvents::SaliencyMapBiasMessage")){
00159 SimEvents::SaliencyMapBiasMessagePtr msg = SimEvents::SaliencyMapBiasMessagePtr::dynamicCast(eMsg);
00160 if (msg->biasImg.width > 0)
00161 itsBiasImg = Ice2Image<byte>(msg->biasImg);
00162 else
00163 itsSMap->resetBiasImg();
00164 }
00165
00166
00167 }
00168
00169
00170 class SaliencyMapService : public Ice::Service {
00171 protected:
00172 virtual bool start(int, char* argv[]);
00173 virtual bool stop() {
00174 if (itsMgr)
00175 delete itsMgr;
00176 return true;
00177 }
00178
00179 private:
00180 Ice::ObjectAdapterPtr itsAdapter;
00181 ModelManager *itsMgr;
00182 };
00183
00184 bool SaliencyMapService::start(int argc, char* argv[])
00185 {
00186
00187 itsMgr = new ModelManager("SaliencyMapService");
00188
00189 nub::ref<SaliencyMapI> smap(new SaliencyMapI(*itsMgr));
00190 itsMgr->addSubComponent(smap);
00191
00192 itsMgr->setOptionValString(&OPT_EsmInertiaHalfLife, "60");
00193 itsMgr->setOptionValString(&OPT_EsmIorStrength, "8.0");
00194
00195
00196 itsMgr->parseCommandLine((const int)argc, (const char**)argv, "", 0, 0);
00197
00198 char adapterStr[255];
00199 sprintf(adapterStr, "default -p %i", BrainObjects::SaliencyMapPort);
00200 itsAdapter = communicator()->createObjectAdapterWithEndpoints("SaliencyMapAdapter",
00201 adapterStr);
00202
00203 Ice::ObjectPtr object = smap.get();
00204 Ice::ObjectPrx objectPrx = itsAdapter->add(object, communicator()->stringToIdentity("SaliencyMap"));
00205 smap->initSimEvents(communicator(), objectPrx);
00206 itsAdapter->activate();
00207
00208 itsMgr->start();
00209
00210 return true;
00211 }
00212
00213
00214 int main(int argc, char** argv) {
00215
00216 SaliencyMapService svc;
00217 return svc.main(argc, argv);
00218 }
00219
00220