19#include <yarp/os/Time.h>
20#include <yarp/cv/Cv.h>
24void VisuoThread::close()
31 outPort[
LEFT].close();
32 outPort[
RIGHT].close();
34 imgPort[
LEFT].close();
35 imgPort[
RIGHT].close();
37 mCUTPort[
LEFT].close();
38 mCUTPort[
RIGHT].close();
70 trackBuffer.push_back(*vec);
72 if(
isTracking() && trackBuffer.size()>minTrackBufSize)
79 for(
unsigned int i=1; i<trackBuffer.size(); i++)
82 speed[
LEFT]=(trackBuffer[i-1][0]-trackBuffer[i][0])*(trackBuffer[i-1][0]-trackBuffer[i][0])+
83 (trackBuffer[i-1][1]-trackBuffer[i][1])*(trackBuffer[i-1][1]-trackBuffer[i][1]);
87 speed[
RIGHT]=(trackBuffer[i-1][0]-trackBuffer[i][0])*(trackBuffer[i-1][0]-trackBuffer[i][0])+
88 (trackBuffer[i-1][1]-trackBuffer[i][1])*(trackBuffer[i-1][1]-trackBuffer[i][1]);
93 double n=1.0/(trackBuffer.size()-1);
102 if(speed_avg[
LEFT]>speedStdThresh || speed_avg[
RIGHT]>speedStdThresh)
107 double dist=(trackBuffer.back()[0]-trackBuffer.back()[6])*(trackBuffer.back()[0]-trackBuffer.back()[6])+
108 (trackBuffer.back()[1]-trackBuffer.back()[7])*(trackBuffer.back()[1]-trackBuffer.back()[7]);
111 if(sqrt(dist)>stereoDistThresh)
118 while(trackBuffer.size()>maxTrackBufSize)
119 trackBuffer.pop_front();
129 if(stereo[2*dominant_eye]==0.0 && stereo[2*dominant_eye+1]==0.0)
131 if(stereo[2*(1-dominant_eye)]==0.0 && stereo[2*(1-dominant_eye)+1]==0.0)
134 eye_in_use=1-dominant_eye;
137 eye_in_use=dominant_eye;
141 if (img[eye_in_use]!=
nullptr)
142 imgMat=yarp::cv::toCvMat(*img[eye_in_use]).clone();
147 tpl.resize(side,side);
148 cv::Mat tplMat=yarp::cv::toCvMat(tpl);
150 int x=stereo[2*eye_in_use]-0.5*side<0?0:cvRound(stereo[2*eye_in_use]-0.5*side);
151 int y=stereo[2*eye_in_use+1]-0.5*side<0?0:cvRound(stereo[2*eye_in_use+1]-0.5*side);
152 int width=stereo[2*eye_in_use]+0.5*side>=imgMat.cols?imgMat.cols-
x:side;
153 int height=stereo[2*eye_in_use+1]+0.5*side>=imgMat.rows?imgMat.rows-
y:side;
155 imgMat(cv::Rect(
x,
y,width,height)).copyTo(tplMat);
156 cv::cvtColor(tplMat,tplMat,CV_BGR2RGB);
158 pftOutPort.write(tpl);
161 lock_guard<mutex> lck(trackMutex);
162 stereoTracker.
side=side;
170 pftOutPort.write(tpl);
172 lock_guard<mutex> lck(trackMutex);
176void VisuoThread::updateImages()
178 ImageOf<PixelRgb> *iL=imgPort[
LEFT].read(
false);
179 ImageOf<PixelRgb> *iR=imgPort[
RIGHT].read(
false);
181 lock_guard<mutex> lck(imgMutex);
187 img[
LEFT]=
new ImageOf<PixelRgb>(*iL);
197 img[
RIGHT]=
new ImageOf<PixelRgb>(*iR);
199 newImage[
RIGHT]=
true;
203void VisuoThread::updateLocationsMIL()
205 Bottle *bLocations=recMILPort.read(
false);
209 lock_guard<mutex> lck(MILMutex);
212 for(
int i=0; i<bLocations->get(0).asList()->size(); i++)
214 Bottle *b=bLocations->get(0).asList()->get(i).asList();
215 locations[b->get(0).asString()]=cvPoint(b->get(1).asInt32(),b->get(2).asInt32());
221void VisuoThread::updateMotionCUT()
225 bMotion[
LEFT]=mCUTPort[
LEFT].read(
false);
228 double tNow=Time::now();
232 lock_guard<mutex> lck(motMutex);
234 Vector stereo(4); stereo=0.0;
235 for(
int cam=0; cam<2; cam++)
239 if(buffer[cam].size()>0 && tNow-buffer[cam].back().t>timeTol)
243 if (bMotion[cam] && bMotion[cam]->size()==1)
247 item.size=bMotion[cam]->get(0).asList()->get(2).asFloat64();
248 item.p=cvPoint(bMotion[cam]->get(0).asList()->get(0).asInt32(),bMotion[cam]->get(0).asList()->get(1).asInt32());
249 buffer[cam].push_back(item);
251 stereo[2*cam]=bMotion[cam]->get(0).asList()->get(0).asFloat64();
252 stereo[2*cam+1]=bMotion[cam]->get(0).asList()->get(1).asFloat64();
256 while (buffer[cam].size() && (tNow-buffer[cam].front().t)>timeTol)
257 buffer[cam].pop_front();
261 stereo_target.
set(stereo);
266void VisuoThread::updatePFTracker()
268 Vector *trackVec=pftInPort.read(
false);
271 if(trackVec!=NULL && trackVec->size()==12)
277 stereoTracker.
vec=*trackVec;
281 stereo[0]=stereoTracker.
vec[0];
282 stereo[1]=stereoTracker.
vec[1];
283 stereo[2]=stereoTracker.
vec[6];
284 stereo[3]=stereoTracker.
vec[7];
287 stereo_target.
set(stereo);
291 lock_guard<mutex> lck(trackMutex);
292 stereoTracker.
vec.clear();
293 stereoTracker.
side=0;
297 lock_guard<mutex> lck(imgMutex);
300 ImageOf<PixelRgb> drawImg[2];
304 if(stereoTracker.
vec.size()==12)
306 cv::Mat tmpL=yarp::cv::toCvMat(drawImg[
LEFT]);
307 cv::circle(tmpL,cv::Point(cvRound(stereoTracker.
vec[0]),cvRound(stereoTracker.
vec[1])),3,cv::Scalar(0,255),3);
308 cv::rectangle(tmpL,cv::Point(cvRound(stereoTracker.
vec[2]),cvRound(stereoTracker.
vec[3])),
309 cv::Point(cvRound(stereoTracker.
vec[4]),cvRound(stereoTracker.
vec[5])),cv::Scalar(0,255),3);
311 cv::Mat tmpR=yarp::cv::toCvMat(drawImg[
RIGHT]);
312 cv::circle(tmpR,cv::Point(cvRound(stereoTracker.
vec[6]),cvRound(stereoTracker.
vec[7])),3,cv::Scalar(0,255),3);
313 cv::rectangle(tmpR,cv::Point(cvRound(stereoTracker.
vec[8]),cvRound(stereoTracker.
vec[9])),
314 cv::Point(cvRound(stereoTracker.
vec[10]),cvRound(stereoTracker.
vec[11])),cv::Scalar(0,255),3);
318 Bottle &vl=v.addList();
319 vl.addInt32(cvRound(stereoTracker.
vec[0]));
320 vl.addInt32(cvRound(stereoTracker.
vec[1]));
321 vl.addInt32(stereoTracker.
side);
322 Bottle &vr=v.addList();
323 vr.addInt32(cvRound(stereoTracker.
vec[6]));
324 vr.addInt32(cvRound(stereoTracker.
vec[7]));
325 vr.addInt32(stereoTracker.
side);
327 boundMILPort.write(v);
337 newImage[
LEFT]=
false;
338 newImage[
RIGHT]=
false;
345 :PeriodicThread(0.02),rf(_rf),stereo_target(initializer->stereo_target),opcPort(initializer->port_opc)
347 buffer[
LEFT].clear();
348 buffer[
RIGHT].clear();
355 stereoTracker.
vec.clear();
356 stereoTracker.
side=0;
364 string name=rf.find(
"name").asString();
366 Bottle bVision=rf.findGroup(
"vision");
368 setPeriod((
double)bVision.check(
"period",Value(20)).asInt32()/1000.0);
370 minMotionBufSize=bVision.check(
"minMotionBufSize",Value(10)).asInt32();
371 minTrackBufSize=bVision.check(
"minTrackBufSize",Value(1)).asInt32();
372 maxTrackBufSize=bVision.check(
"maxTrackBufSize",Value(2)).asInt32();
373 timeTol=bVision.check(
"timeTol",Value(0.5)).asFloat64();
374 motionStdThresh=bVision.check(
"motionStdThresh",Value(5.0)).asFloat64();
375 speedStdThresh=bVision.check(
"speedStdThresh",Value(700.0)).asFloat64();
376 stereoDistThresh=bVision.check(
"stereoDistThresh",Value(300.0)).asFloat64();
377 dominant_eye=bVision.check(
"dominant_eye",Value(
"left")).asString()==
"left"?
LEFT:
RIGHT;
379 rawWaitThresh=bVision.check(
"raw_detection_wait_thresh",Value(15.0)).asFloat64();
380 motionWaitThresh=bVision.check(
"motion_detection_wait_thresh",Value(5.0)).asFloat64();
381 objectWaitThresh=bVision.check(
"object_detection_wait_thresh",Value(5.0)).asFloat64();
384 outPort[
LEFT].open(
"/"+name+
"/left/img:o");
385 outPort[
RIGHT].open(
"/"+name+
"/right/img:o");
387 imgPort[
LEFT].open(
"/"+name+
"/left/img:i");
388 imgPort[
RIGHT].open(
"/"+name+
"/right/img:i");
390 mCUTPort[
LEFT].open(
"/"+name+
"/left/blobs:i");
391 mCUTPort[
RIGHT].open(
"/"+name+
"/right/blobs:i");
393 rawInPort[
LEFT].open(
"/"+name+
"/left/raw:i");
394 rawInPort[
RIGHT].open(
"/"+name+
"/right/raw:i");
396 boundMILPort.open(
"/"+name+
"/MIL/window:o");
397 cmdMILPort.open(
"/"+name+
"/MIL/cmd:o");
398 recMILPort.open(
"/"+name+
"/MIL/rec:i");
400 cmdMSRPort.open(
"/"+name+
"/MSR/cmd:o");
401 recMSRPort.open(
"/"+name+
"/MSR/rec:i");
403 pftInPort.open(
"/"+name+
"/tracker:i");
404 pftOutPort.open(
"/"+name+
"/tracker:o");
406 segPort.open(
"/"+name+
"/seg:o");
408 newImage[
LEFT]=
false;
409 newImage[
RIGHT]=
false;
425 updateLocationsMIL();
442 Bottle &bNewTarget=options.addList();
443 bNewTarget.addString(
"target");
445 Bottle &bTarget=bNewTarget.addList();
446 Bottle &bNewStereo=bTarget.addList();
447 bNewStereo.addString(
"stereo");
449 Bottle &bStereo=bNewStereo.addList();
453 Bottle *list=type.asList();
456 if(list->get(0).asString()==
"right")
458 bStereo.addFloat64(0.0);
459 bStereo.addFloat64(0.0);
460 bStereo.addFloat64(list->get(1).asFloat64());
461 bStereo.addFloat64(list->get(2).asFloat64());
463 else if(list->get(0).asString()==
"left")
465 bStereo.addFloat64(list->get(1).asFloat64());
466 bStereo.addFloat64(list->get(2).asFloat64());
467 bStereo.addFloat64(0.0);
468 bStereo.addFloat64(0.0);
470 else if(list->get(0).asString()==
"cartesian")
472 Bottle &bNewCartesian=bTarget.addList();
473 bNewCartesian.addString(
"cartesian");
475 Bottle &bCartesian=bNewCartesian.addList();
477 for(
int i=1; i<list->size(); i++)
478 bCartesian.addFloat64(list->get(i).asFloat64());
482 Bottle &bNewCartesian=bTarget.addList();
483 bNewCartesian.addString(
"cartesian");
485 Bottle &bCartesian=bNewCartesian.addList();
487 for(
int i=0; i<list->size(); i++)
488 bCartesian.addFloat64(list->get(i).asFloat64());
493 else if(list->size()==2)
495 bStereo.addFloat64(list->get(0).asFloat64());
496 bStereo.addFloat64(list->get(1).asFloat64());
497 bStereo.addFloat64(0.0);
498 bStereo.addFloat64(0.0);
503 else switch(type.asVocab32())
507 ok=getFixation(bStereo);
512 ok=getMotion(bStereo);
517 ok=getTrack(bStereo);
528 Bottle &bName=bTarget.addList();
529 bName.addString(
"name");
530 bName.addString(type.asString());
532 getObject(type.asString(),bStereo);
542bool VisuoThread::getFixation(Bottle &bStereo)
549 stereo[0]=stereo[2]=0.5*img[
LEFT]->width();
550 stereo[1]=stereo[3]=0.5*img[
LEFT]->height();
554 stereo[0]=stereo[2]=160;
555 stereo[1]=stereo[3]=120;
559 for(
size_t i=0; i<stereo.size(); i++)
560 bStereo.addFloat64(stereo[i]);
568bool VisuoThread::getMotion(Bottle &bStereo)
574 double t=Time::now();
576 while(Time::now()-t<motionWaitThresh && !interrupted)
578 lock_guard<mutex> lck(motMutex);
581 if (buffer[
LEFT].size()>minMotionBufSize && buffer[
RIGHT].size()>minMotionBufSize)
584 for (
int cam=0; cam<2; cam++)
586 double size_cam,u,v,
n;
590 n=1.0/buffer[cam].size();
592 for (
unsigned int i=0; i<buffer[cam].size(); i++)
594 size_cam+=buffer[cam][i].size;
595 u+=buffer[cam][i].p.x;
596 v+=buffer[cam][i].p.y;
597 u_std+=buffer[cam][i].p.x*buffer[cam][i].p.x;
598 v_std+=buffer[cam][i].p.y*buffer[cam][i].p.y;
604 u_std=sqrt(
n*u_std-u*u);
605 v_std=sqrt(
n*v_std-v*v);
608 if (u_std<motionStdThresh && v_std<motionStdThresh)
620 int side=cvRound(2*sqrt(size/3.1415)*2);
625 stereo[0]=
p[
LEFT][0];
626 stereo[1]=
p[
LEFT][1];
637 for(
size_t i=0; i<stereo.size(); i++)
638 bStereo.addFloat64(stereo[i]);
644bool VisuoThread::getTrack(Bottle &bStereo)
646 lock_guard<mutex> lck(trackMutex);
651 if(stereoTracker.
vec.size()==12)
654 stereo[0]=stereoTracker.
vec[0];
655 stereo[1]=stereoTracker.
vec[1];
656 stereo[2]=stereoTracker.
vec[6];
657 stereo[3]=stereoTracker.
vec[7];
660 for(
size_t i=0; i<stereo.size(); i++)
661 bStereo.addFloat64(stereo[i]);
671bool VisuoThread::getRaw(Bottle &bStereo)
678 Bottle *bL=rawInPort[
LEFT].read(
false);
679 Bottle *bR=rawInPort[
RIGHT].read(
false);
681 double t=Time::now();
683 while(stereo.size()!=4 && Time::now()-t<rawWaitThresh && !interrupted)
685 Bottle *bL=rawInPort[
LEFT].read(
false);
686 Bottle *bR=rawInPort[
RIGHT].read(
false);
688 if(bL!=NULL || bR!=NULL)
689 stereo.resize(4,0.0);
693 stereo[0]=bL->get(0).asInt32();
694 stereo[1]=bL->get(1).asInt32();
699 stereo[2]=bR->get(0).asInt32();
700 stereo[3]=bR->get(1).asInt32();
711 for(
size_t i=0; i<stereo.size(); i++)
712 bStereo.addFloat64(stereo[i]);
718bool VisuoThread::getObject(
const std::string &object_name, Bottle &bStereo)
726 double t=Time::now();
727 while(Time::now()-t<objectWaitThresh && !interrupted)
729 lock_guard<mutex> lck(MILMutex);
730 if(locations.count(object_name)>0)
733 stereo[0]=locations[object_name].x;
734 stereo[1]=locations[object_name].y;
743 for(
size_t i=0; i<stereo.size(); i++)
744 bStereo.addFloat64(stereo[i]);
753 Bottle *bMSR=recMSRPort.read(
false);
757 obj_name=bDetect.get(0).asString();
769 Bottle command,reply;
770 command.fromString(
"learn " + obj_name +
" template");
772 cmdMILPort.write(command,reply);
774 return (reply.get(0).toString()==
"ack");
780 Bottle command,reply;
781 command.fromString(
"set label 0");
783 cmdMILPort.write(command,reply);
785 return (reply.get(0).toString()==
"ack");
790 Bottle command,reply;
791 command.fromString(
"set label template");
793 cmdMILPort.write(command,reply);
795 return (reply.get(0).toString()==
"ack");
800 Bottle command,reply;
801 command.fromString(
"train");
803 cmdMILPort.write(command,reply);
805 return (reply.get(0).toString()==
"ack");
812 outPort[
LEFT].interrupt();
813 outPort[
RIGHT].interrupt();
815 imgPort[
LEFT].interrupt();
816 imgPort[
RIGHT].interrupt();
818 mCUTPort[
LEFT].interrupt();
819 mCUTPort[
RIGHT].interrupt();
821 rawInPort[
LEFT].interrupt();
822 rawInPort[
RIGHT].interrupt();
824 pftInPort.interrupt();
825 pftOutPort.interrupt();
827 boundMILPort.interrupt();
828 cmdMILPort.interrupt();
829 recMILPort.interrupt();
831 cmdMSRPort.interrupt();
832 recMSRPort.interrupt();
841 outPort[
LEFT].resume();
842 outPort[
RIGHT].resume();
844 imgPort[
LEFT].resume();
845 imgPort[
RIGHT].resume();
847 mCUTPort[
LEFT].resume();
848 mCUTPort[
RIGHT].resume();
850 rawInPort[
LEFT].resume();
851 rawInPort[
RIGHT].resume();
856 boundMILPort.resume();
#define MODE_TRACK_MOTION
#define MODE_TRACK_TEMPLATE
bool getStereoPosition(const string &obj_name, Vector &stereo)
void set(const Vector &stereo)
bool suspendLearningMIL()
Bottle recogMSR(string &obj_name)
void startTracker(const Vector &stereo, const int &side)
virtual void threadRelease()
bool checkTracker(Vector *vec)
bool startLearningMIL(const string &obj_name)
virtual bool threadInit()
VisuoThread(ResourceFinder &_rf, Initializer *initializer)
bool getTarget(Value &type, Bottle &target)