LateralGeniculateNucleusI.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 "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
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);
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
00143
00144
00145
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
00155
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
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
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 != "")
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
00212 arMsg->img = Image2Ice(retinaImg);
00213
00214
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
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 }