70 #include <yarp/os/Network.h>
71 #include <yarp/os/BufferedPort.h>
72 #include <yarp/os/RFModule.h>
73 #include <yarp/os/PeriodicThread.h>
74 #include <yarp/os/Time.h>
75 #include <yarp/os/Stamp.h>
77 #include <yarp/sig/Image.h>
78 #include <yarp/sig/Vector.h>
80 #include <yarp/cv/Cv.h>
82 #include <opencv2/opencv.hpp>
93 using namespace yarp::os;
94 using namespace yarp::sig;
95 using namespace yarp::cv;
103 class PointingThread:
public PeriodicThread,
public BufferedPort<Bottle>
107 BufferedPort<ImageOf<PixelRgb>> inPort;
108 BufferedPort<ImageOf<PixelRgb>> outPort;
109 BufferedPort<Vector> pointPort;
111 double internal_time;
112 double tracking_thresh;
113 double pointing_thresh;
115 list<Vector> positions;
122 PointingThread(ResourceFinder &_rf)
123 : PeriodicThread(0.005),rf(_rf) { }
125 virtual bool threadInit()
127 string name=rf.find(
"name").asString();
128 tracking_thresh=rf.check(
"tracking_threshold",Value(0.5)).asFloat64();
129 pointing_thresh=rf.check(
"pointing_threshold",Value(2.5)).asFloat64();
131 inPort.open(
"/"+name+
"/img:i");
132 outPort.open(
"/"+name+
"/img:o");
133 pointPort.open(
"/"+name+
"/point:o");
136 this->open(
"/"+name+
"/blobs:i");
141 lastPoint.resize(2,0.0);
149 lock_guard<mutex> lg(mtx);
150 if(ImageOf<PixelRgb> *imgIn=inPort.read(
false))
152 ImageOf<PixelRgb> &imgOut=outPort.prepare();
154 cv::Mat imgOutMat=toCvMat(imgOut);
156 if(positions.size()>0)
166 if(Time::now()-internal_time>tracking_thresh)
169 cv::circle(imgOutMat,cv::Point((
int)round(positions.back()[0]),
170 (
int)round(positions.back()[1])),5,cv::Scalar(255),5);
175 cv::circle(imgOutMat,cv::Point((
int)round(positions.back()[0]),
176 (
int)round(positions.back()[1])),5,cv::Scalar(0,0,255),5);
177 cv::circle(imgOutMat,cv::Point((
int)round(positions.back()[0]),
178 (
int)round(positions.back()[1])),10,cv::Scalar(0,255),5);
180 if ((positions.back()[0]!=lastPoint[0]) || (positions.back()[1]!=lastPoint[1]))
182 lastPoint=positions.back();
183 pointPort.prepare()=lastPoint;
184 pointPort.writeStrict();
190 imgOut=fromCvMat<PixelRgb>(imgOutMat);
191 outPort.writeStrict();
195 virtual void onRead(Bottle &bot)
197 lock_guard<mutex> lg(mtx);
198 double curr_time=Time::now();
208 Bottle *blob=bot.get(0).asList();
210 v[0]=blob->get(0).asInt32();
211 v[1]=blob->get(1).asInt32();
214 internal_time=curr_time;
222 Bottle *blob=bot.get(0).asList();
224 v[0]=blob->get(0).asInt32();
225 v[1]=blob->get(1).asInt32();
227 positions.push_back(v);
233 if(curr_time-internal_time>pointing_thresh)
236 internal_time=curr_time;
244 virtual void threadRelease()
252 bool execReq(
const Bottle &command, Bottle &reply)
259 class DetectorModule:
public RFModule
266 virtual bool configure(ResourceFinder &rf)
268 thr=
new PointingThread(rf);
276 string name=rf.find(
"name").asString();
277 rpcPort.open(
"/"+name+
"/rpc");
283 virtual bool interruptModule()
300 virtual double getPeriod()
305 virtual bool updateModule()
310 virtual bool respond(
const Bottle &command, Bottle &reply)
312 if(thr->execReq(command,reply))
315 return RFModule::respond(command,reply);
320 int main(
int argc,
char *argv[])
324 if (!yarp.checkNetwork())
328 rf.setDefault(
"name",
"blobSelector");
329 rf.configure(argc,argv);
333 return mod.runModule(rf);