00001 /*!@file PrimarySomatosensoryCortex.C drive the actual robot */ 00002 00003 //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // 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 <lelazary@yahoo.com> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/RobotBrain/LateralGeniculateNucleusI.C $ 00035 // $Id: LateralGeniculateNucleusI.C 10794 2009-02-08 06:21:09Z itti $ 00036 // 00037 00038 #include "Component/ModelManager.H" 00039 #include "Component/ModelComponent.H" 00040 #include "Component/ModelParam.H" 00041 #include "Component/ModelOptionDef.H" 00042 #include "Media/FrameSeries.H" 00043 #include "Transport/FrameInfo.H" 00044 #include "Raster/GenericFrame.H" 00045 #include "Image/Image.H" 00046 #include "Image/DrawOps.H" 00047 #include "GUI/ImageDisplayStream.H" 00048 #include "GUI/DebugWin.H" 00049 #include "Robots/RobotBrain/LateralGeniculateNucleusI.H" 00050 #include "Robots/RobotBrain/RobotCommon.H" 00051 00052 #include "Ice/IceImageUtils.H" 00053 00054 // ###################################################################### 00055 LateralGeniculateNucleusI::LateralGeniculateNucleusI(OptionManager& mgr, 00056 const std::string& descrName, const std::string& tagName) : 00057 ModelComponent(mgr, descrName, tagName) 00058 { 00059 itsOfs = nub::ref<OutputFrameSeries>(new OutputFrameSeries(mgr)); 00060 addSubComponent(itsOfs); 00061 00062 itsWindowSize = Dims(150,150); 00063 } 00064 00065 // ###################################################################### 00066 void LateralGeniculateNucleusI::init(Ice::CommunicatorPtr ic, Ice::ObjectAdapterPtr adapter) 00067 { 00068 Ice::ObjectPtr pscPtr = this; 00069 itsObjectPrx = adapter->add(pscPtr, 00070 ic->stringToIdentity("LateralGeniculateNucleus")); 00071 00072 itsPublisher = RobotSimEvents::EventsPrx::uncheckedCast( 00073 SimEventsUtils::getPublisher(ic, "AttendedRegionMessageTopic") 00074 ); 00075 00076 Ice::ObjectPrx base = ic->stringToProxy("IRobotService:default -p 10000 -h " ROBOT_IP); 00077 itsRobot = Robots::IRobotPrx::checkedCast(base); 00078 00079 if(!itsRobot) LFATAL("Invalid Robot Proxy"); 00080 00081 IceUtil::ThreadPtr thread = this; 00082 thread->start(); 00083 00084 usleep(10000); 00085 } 00086 00087 // ###################################################################### 00088 LateralGeniculateNucleusI::~LateralGeniculateNucleusI() 00089 { 00090 //SimEventsUtils::unsubscribeSimEvents(itsTopicsSubscriptions, itsObjectPrx); 00091 } 00092 00093 // ###################################################################### 00094 Point2D<int> LateralGeniculateNucleusI::getMouseClick(nub::soft_ref<OutputFrameSeries> &ofs) 00095 { 00096 const nub::soft_ref<ImageDisplayStream> ids = 00097 ofs->findFrameDestType<ImageDisplayStream>(); 00098 00099 const rutz::shared_ptr<XWinManaged> uiwin = 00100 ids.is_valid() 00101 ? ids->getWindow("RetinaImg") 00102 : rutz::shared_ptr<XWinManaged>(); 00103 00104 if (uiwin.is_valid()) 00105 return uiwin->getLastMouseClick(); 00106 else 00107 return Point2D<int>(-1,-1); 00108 } 00109 00110 // ###################################################################### 00111 int LateralGeniculateNucleusI::getKey(nub::soft_ref<OutputFrameSeries> &ofs) 00112 { 00113 const nub::soft_ref<ImageDisplayStream> ids = 00114 ofs->findFrameDestType<ImageDisplayStream>(); 00115 00116 const rutz::shared_ptr<XWinManaged> uiwin = 00117 ids.is_valid() 00118 ? ids->getWindow("RetinaImg") 00119 : rutz::shared_ptr<XWinManaged>(); 00120 return uiwin->getLastKeyPress(); 00121 } 00122 00123 00124 // ###################################################################### 00125 void LateralGeniculateNucleusI::run() { 00126 sleep(1); //needed to wait for ofs to initalize 00127 00128 while(1) { 00129 ImageIceMod::ImageIce imgIce = itsRobot->getImageSensor(0, false); 00130 Image<PixRGB<byte> > retinaImg; 00131 00132 if (imgIce.pixSize == 1) 00133 retinaImg = toRGB(Ice2Image<byte>(imgIce)); 00134 else 00135 retinaImg = Ice2Image<PixRGB<byte> >(imgIce); 00136 00137 if (!retinaImg.initialized()) 00138 continue; 00139 00140 Image<PixRGB<byte> > tmpImg = retinaImg; 00141 00142 //drawLine(tmpImg, 00143 // Point2D<int>(tmpImg.getWidth()/2, 0), 00144 // Point2D<int>(tmpImg.getWidth()/2, tmpImg.getHeight()), 00145 // PixRGB<byte>(255,0,0)); 00146 00147 itsOfs->writeRGB(tmpImg, "RetinaImg", FrameInfo("RetinaImg", SRC_POS)); 00148 00149 Point2D<int> clickLoc = getMouseClick(itsOfs); 00150 00151 if (clickLoc.isValid()) 00152 { 00153 00154 // the location is at the center, move it to the top left 00155 // Point2D<int> topLeft(clickLoc.i-((itsWindowSize.w()-1)/2), clickLoc.j-((itsWindowSize.h()-1)/2)); 00156 00157 Point2D<int> topLeft = clickLoc; 00158 printf("Please Click The Bottom Right Of The Object\n"); 00159 00160 Point2D<int> bottomRight = getMouseClick(itsOfs); 00161 while(!bottomRight.isValid()) 00162 { 00163 bottomRight = getMouseClick(itsOfs); 00164 usleep(10000); 00165 } 00166 00167 Image<PixRGB<byte> > selectImage = retinaImg; 00168 00169 00170 if(bottomRight.i-topLeft.i <= 0 || bottomRight.j-topLeft.j <= 0) 00171 { 00172 printf("ERROR:Please click top left, then bottom right\n"); 00173 continue; 00174 } 00175 00176 Dims selectDims(bottomRight.i-topLeft.i, bottomRight.j-topLeft.j); 00177 00178 drawRect(selectImage, Rectangle(topLeft, selectDims), PixRGB<byte>(255,0,0)); 00179 itsOfs->writeRGB(selectImage, "RetinaImg", FrameInfo("RetinaImg", SRC_POS)); 00180 00181 Image<PixRGB<byte> > objImg = crop(retinaImg,topLeft, selectDims); 00182 itsOfs->writeRGB(objImg, "Object", FrameInfo("ObjectImg", SRC_POS)); 00183 00184 printf("Store Selection As Landmark? (y/n): "); 00185 std::string keepSelection; 00186 //std::getline(std::cin, keepSelection); 00187 cin >> keepSelection; 00188 00189 if(keepSelection == "y" || keepSelection == "Y") 00190 { 00191 std::string objName; 00192 float objWidth; 00193 float objHeight; 00194 00195 printf("Enter a label for this object: "); 00196 // std::getline(std::cin, objName); 00197 cin >> objName; 00198 printf("Enter the size of this object in millimeters: "); 00199 cin >> objWidth; 00200 cin >> objHeight; 00201 LINFO("Storing Object: '%s' at %fmm x %fmm\n", objName.c_str(), objWidth, objHeight); 00202 00203 if (objName != "") //Send the message of the object 00204 { 00205 RobotSimEvents::AttendedRegionMessagePtr arMsg = 00206 new RobotSimEvents::AttendedRegionMessage; 00207 arMsg->objId = 1; 00208 arMsg->objWidth = objWidth; 00209 arMsg->objHeight = objHeight; 00210 arMsg->name = objName; 00211 // arMsg->img = Image2Ice(objImg); 00212 arMsg->img = Image2Ice(retinaImg); 00213 00214 //Set the attended region 00215 arMsg->attTopLeft.i = topLeft.i; 00216 arMsg->attTopLeft.j = topLeft.j; 00217 arMsg->attWidth = selectDims.w(); 00218 arMsg->attHeight = selectDims.h(); 00219 LINFO("SelectDims: %s :: (%d, %d)", convertToString(selectDims).c_str(), 00220 arMsg->attWidth,arMsg->attHeight); 00221 00222 itsPublisher->updateMessage(arMsg); 00223 LDEBUG("Sent object Img"); 00224 } 00225 } 00226 00227 //Blow through any accumulated buffered mouse clicks 00228 while(getMouseClick(itsOfs).isValid()); 00229 00230 } 00231 else { 00232 00233 RobotSimEvents::AttendedRegionMessagePtr arMsg = 00234 new RobotSimEvents::AttendedRegionMessage; 00235 arMsg->objId = -1; 00236 arMsg->name.clear(); 00237 arMsg->img = Image2Ice(retinaImg); 00238 00239 itsPublisher->updateMessage(arMsg); 00240 } 00241 00242 usleep(10000); 00243 } 00244 } 00245 00246 // ###################################################################### 00247 void LateralGeniculateNucleusI::updateMessage(const RobotSimEvents::EventMessagePtr& eMsg, 00248 const Ice::Current&) 00249 { 00250 }