icub-client
module.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 WYSIWYD Consortium, European Commission FP7 Project ICT-612139
3  * Authors: Ugo Pattacini, Tobias Fischer
4  * email: ugo.pattacini@iit.it, t.fischer@imperial.ac.uk
5  * Permission is granted to copy, distribute, and/or modify this program
6  * under the terms of the GNU General Public License, version 2 or any
7  * later version published by the Free Software Foundation.
8  *
9  * A copy of the license can be found at
10  * icub-client/license/gpl.txt
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15  * Public License for more details
16 */
17 
18 #ifndef __MODULE_H__
19 #define __MODULE_H__
20 
21 #include <string>
22 #include <deque>
23 #include <map>
24 
25 #include <yarp/os/all.h>
26 #include <yarp/sig/all.h>
27 #include <yarp/math/Math.h>
28 #include <yarp/math/Rand.h>
29 #include <iCub/ctrl/filters.h>
31 
32 #include <opencv2/opencv.hpp>
33 #include <opencv2/tracking.hpp>
34 
35 #include "utils.h"
36 #include "iol2opc_IDL.h"
37 
38 #define RET_INVALID -1
39 #define OBJECT_UNKNOWN "?"
40 
41 using namespace std;
42 using namespace yarp::os;
43 using namespace yarp::sig;
44 using namespace yarp::math;
45 using namespace iCub::ctrl;
46 using namespace icubclient;
47 
48 
52 /**********************************************************/
53 namespace Bridge {
54  typedef enum { idle, load_database, localization } State;
55 }
56 
57 
58 /**********************************************************/
62 class IOLObject
63 {
64 protected:
65  MedianFilter filterPos;
66  MedianFilter filterDim;
68  double presenceTmo;
69  double presenceTimer;
70 
71  enum { idle, init, no_need, tracking };
72 
73  string trackerType;
75  double trackerTmo;
76  double trackerTimer;
77 
78  cv::Rect2d trackerResult;
79  cv::Ptr<cv::Tracker> tracker;
80 
81 public:
82  /**********************************************************/
83  IOLObject(const int filter_order=1, const double presenceTmo_=0.0,
84  const string &trackerType_="BOOSTING", const double trackerTmo_=0.0) :
85  filterPos(filter_order), filterDim(10*filter_order),
86  init_filters(true), presenceTmo(presenceTmo_),
87  trackerType(trackerType_), trackerState(idle),
88  trackerTmo(trackerTmo_), trackerTimer(0.0)
89  {
90  trackerResult.x=trackerResult.y=0;
91  trackerResult.width=trackerResult.height=0;
92  heartBeat();
93  }
94 
95  /**********************************************************/
99  void heartBeat()
100  {
101  presenceTimer=Time::now();
102  }
103 
104  /**********************************************************/
109  bool isDead()
110  {
111  bool dead=(Time::now()-presenceTimer>=presenceTmo);
112  if (dead)
113  trackerState=idle;
114  return dead;
115  }
116 
117  /**********************************************************/
125  void filt(const Vector &x, Vector &xFilt,
126  const Vector &d, Vector &dFilt)
127  {
128  if (init_filters)
129  {
130  filterPos.init(x); xFilt=x;
131  filterDim.init(d); dFilt=d;
132  init_filters=false;
133  }
134  else
135  {
136  xFilt=filterPos.filt(x);
137  dFilt=filterDim.filt(d);
138  }
139  }
140 
141  /**********************************************************/
145  void prepare()
146  {
147  if (trackerState==no_need)
148  trackerState=init;
149  }
150 
151  /**********************************************************/
156  void latchBBox(const CvRect& bbox)
157  {
158  trackerResult.x=bbox.x;
159  trackerResult.y=bbox.y;
160  trackerResult.width=bbox.width;
161  trackerResult.height=bbox.height;
162  trackerState=no_need;
163  }
164 
165  /**********************************************************/
170  void track(const Image& img)
171  {
172  cv::Mat frame=cv::cvarrToMat((IplImage*)img.getIplImage());
173  if (trackerState==init)
174  {
175 #if CV_MINOR_VERSION <= 2
176  tracker=cv::Tracker::create(trackerType);
177 #else
178  if(trackerType=="BOOSTING")
179  tracker=cv::TrackerBoosting::create();
180  else if(trackerType=="MIL")
181  tracker=cv::TrackerMIL::create();
182  else if (trackerType=="TLD")
183  tracker=cv::TrackerTLD::create();
184  else if (trackerType=="KCF")
185  tracker=cv::TrackerKCF::create();
186  else
187  throw std::runtime_error("This tracker is not supported. Supported trackers: BOOSTING, MIL, TLD, KCF");
188 #endif
189  tracker->init(frame,trackerResult);
190  trackerTimer=Time::now();
191  trackerState=tracking;
192  }
193  else if (trackerState==tracking)
194  {
195  if (Time::now()-trackerTimer<trackerTmo)
196  {
197  tracker->update(frame,trackerResult);
198  CvPoint tl=cvPoint((int)trackerResult.x,(int)trackerResult.y);
199  CvPoint br=cvPoint(tl.x+(int)trackerResult.width,
200  tl.y+(int)trackerResult.height);
201 
202  if ((tl.x<5) || (br.x>frame.cols-5) ||
203  (tl.y<5) || (br.y>frame.rows-5))
204  trackerState=idle;
205  else
206  heartBeat();
207  }
208  else
209  trackerState=idle;
210  }
211  }
212 
213  /**********************************************************/
219  bool is_tracking(CvRect& bbox) const
220  {
221  bbox=cvRect((int)trackerResult.x,(int)trackerResult.y,
222  (int)trackerResult.width,(int)trackerResult.height);
223  return (trackerState!=idle);
224  }
225 };
226 
227 
228 /**********************************************************/
232 class IOL2OPCBridge : public RFModule, public iol2opc_IDL
233 {
234 protected:
235  RpcServer rpcPort;
236  RpcClient rpcClassifier;
237  RpcClient rpcGet3D;
239  RpcClient rpcGetSPQ;
240  RpcClient rpcGetBlobPoints;
241 
242  BufferedPort<Bottle> blobExtractor;
243  BufferedPort<Bottle> histObjLocPort;
244  BufferedPort<Bottle> getClickPort;
245  BufferedPort<Bottle> objLocOut;
246  BufferedPort<ImageOf<PixelBgr> > imgIn;
247  BufferedPort<ImageOf<PixelBgr> > imgRtLocOut;
248  BufferedPort<ImageOf<PixelBgr> > imgTrackOut;
249  BufferedPort<ImageOf<PixelBgr> > imgSelBlobOut;
250  BufferedPort<ImageOf<PixelBgr> > imgHistogram;
252 
257 
258  ImageOf<PixelBgr> imgRtLoc;
263 
264  double period;
265  bool verbose;
266  bool empty;
268  bool useSPQ;
270 
272  string tracker_type;
274  VectorOf<int> tracker_min_blob_size;
275  map<string,IOLObject> db;
278 
279  map<string,Filter*> histFiltersPool;
281  deque<CvScalar> histColorsCode;
282 
285  Bottle lastBlobs;
286  Bottle opcBlobs;
287  Bottle opcScores;
288 
292 
299 
300  CvPoint clickLocation;
301 
302  friend class RtLocalization;
303  friend class OpcUpdater;
304  friend class ClassifierReporter;
305 
306  void yInfoGated(const char *msg, ...) const;
307 
314  string findName(const Bottle &scores, const string &tag);
315 
321  Bottle skimBlobs(const Bottle &blobs);
322 
329  bool thresBBox(CvRect &bbox, const Image &img);
330 
335  Bottle getBlobs();
336 
343  CvPoint getBlobCOG(const Bottle &blobs, const unsigned int i);
344 
351  bool getBlobPoints(const CvPoint &cog, deque<CvPoint> &blobPoints);
352 
360  bool getSuperQuadric(const CvPoint &point, Vector &pos, Vector &dim);
361 
368  bool get3DPosition(const CvPoint &point, Vector &x);
369 
377  bool get3DPositionAndDimensions(const CvRect &bbox, Vector &x, Vector &dim);
378  Vector calibPosition(const Vector &x);
379 
385  bool getClickPosition(CvPoint &pos);
386 
390  void acquireImage();
391 
398  void drawBlobs(const Bottle &blobs, const unsigned int i, const Bottle &scores);
399 
406  void rotate(cv::Mat &src, const double angle, cv::Mat &dst);
407 
414  void drawScoresHistogram(const Bottle &blobs, const Bottle &scores, const int i);
415 
422  int findClosestBlob(const Bottle &blobs, const CvPoint &loc);
423 
430  int findClosestBlob(const Bottle &blobs, const Vector &loc);
431 
437  Bottle classify(const Bottle &blobs);
438 
445  void train(const string &object, const Bottle &blobs, const int i);
446 
450  void doLocalization();
451 
455  void updateOPC();
456 
462  ObjectArea getReachableArea(const yarp::sig::VectorOf<double> &objpos);
463 
464  bool configure(ResourceFinder &rf);
465 
474  void setBounds(ResourceFinder &rf, Vector &bounds, string configName, double std_lower, double std_upper);
475  bool interruptModule();
476  bool close();
477  bool attach(RpcServer &source);
478  double getPeriod();
479  bool updateModule();
480 
481 public:
482  bool train_object(const string &name);
483  bool remove_object(const string &name);
484  bool remove_all();
485  bool change_name(const string &old_name, const string &new_name);
486  bool set_object_persistence(const string &sw);
487  string get_object_persistence();
488  void pause();
489  void resume();
490 };
491 
492 #endif
493 
Vector robot_area_y_bounds
Yarp Vector of min, max bounding of robot region in y-axis.
Definition: module.h:296
int opcMedianFilterOrder
Definition: module.h:255
Bottle lastBlobs
Bottle contains last blob information.
Definition: module.h:285
double lastBlobsArrivalTime
time stamp of last received blob
Definition: module.h:284
Bottle opcBlobs
Bottle contains received blobs of objects from OPC.
Definition: module.h:286
Vector skim_blobs_y_bounds
Yarp Vector of min, max bounding in y-axis to reduce the blob detection.
Definition: module.h:290
map< string, Filter * > histFiltersPool
Definition: module.h:279
RpcClient rpcGetSPQ
rpc client port to send requests to superquadric-model and receive superquadric parameters ...
Definition: module.h:239
MedianFilter filterPos
median filter for position of object
Definition: module.h:65
ObjectArea
Definition: object.h:28
ClassifierReporter classifierReporter
Definition: module.h:256
Vector shared_area_y_bounds
Yarp Vector of min, max bounding of shared region in y-axis.
Definition: module.h:298
void heartBeat()
heartBeat current time value
Definition: module.h:99
iol2opc_IDL IDL Interface to iol2opc service.
Definition: iol2opc_IDL.h:25
cv::Ptr< cv::Tracker > tracker
Definition: module.h:79
STL namespace.
Vector human_area_y_bounds
Yarp Vector of min, max bounding of human region in y-axis.
Definition: module.h:294
Mutex mutexResourcesOpc
Definition: module.h:260
double trackerTimer
current value of tracker timer
Definition: module.h:76
BufferedPort< ImageOf< PixelBgr > > imgHistogram
buffered port of output image of histogram of classification scores.
Definition: module.h:250
RpcClient rpcClassifier
rpc client port to send requests to himrepClassifier
Definition: module.h:236
int trackerState
tracker state: idle, init, no_need or tracking
Definition: module.h:74
Vector robot_area_x_bounds
Yarp Vector of min, max bounding of robot region in x-axis.
Definition: module.h:295
void track(const Image &img)
track procdure to start tracking an image
Definition: module.h:170
Port imgClassifier
port of output image to himrefClassifier
Definition: module.h:251
BufferedPort< ImageOf< PixelBgr > > imgIn
buffered port of input calibrated image from left camera of iCub
Definition: module.h:246
Mutex mutexResourcesSFM
Definition: module.h:261
BufferedPort< ImageOf< PixelBgr > > imgRtLocOut
buffered port of output image for real-time objects localization
Definition: module.h:247
double tracker_timeout
Definition: module.h:273
BufferedPort< ImageOf< PixelBgr > > imgTrackOut
buffered port of output image of tracked object
Definition: module.h:248
double trackerTmo
set value of tracker timout
Definition: module.h:75
double blobs_detection_timeout
Definition: module.h:283
cv::Rect2d trackerResult
Definition: module.h:78
double presence_timeout
Definition: module.h:271
Vector human_area_x_bounds
Yarp Vector of min, max bounding of human region in x-axis.
Definition: module.h:293
IOLObject onlyKnownObjects
Definition: module.h:277
Bottle opcScores
Bottle contains received (class) score of objects from OPC.
Definition: module.h:287
bool empty
Definition: module.h:266
An OPC client using the datastructures defined within the icub-client library.
Definition: opcClient.h:35
map< string, IOLObject > db
Definition: module.h:275
Mutex mutexResourcesSPQ
Definition: module.h:262
MedianFilter filterDim
median filter for object dimension
Definition: module.h:66
BufferedPort< Bottle > blobExtractor
buffered port of input of received blobs from lbpExtract
Definition: module.h:242
void latchBBox(const CvRect &bbox)
latchBBox assign tracker with bounding box value
Definition: module.h:156
BufferedPort< ImageOf< PixelBgr > > imgSelBlobOut
buffered port of output image inside the selected blob (by clicking on)
Definition: module.h:249
Vector shared_area_x_bounds
Yarp Vector of min, max bounding of shared region in x-axis.
Definition: module.h:297
BufferedPort< Bottle > getClickPort
buffered port of input of clicked position
Definition: module.h:244
RpcServer rpcPort
rpc server to receive user request
Definition: module.h:235
bool init_filters
Definition: module.h:67
BufferedPort< Bottle > histObjLocPort
buffered port of input of localized objects from iol localizer
Definition: module.h:243
bool verbose
Definition: module.h:265
string tracker_type
Definition: module.h:272
void prepare()
prepare initialize the tracker
Definition: module.h:145
double period
Definition: module.h:264
RpcClient rpcGetBlobPoints
rpc client port to send requests to lbpExtract and receive all points of a blob
Definition: module.h:240
OPCClient * opc
OPC client object.
Definition: module.h:238
Bridge::State state
Definition: module.h:276
bool is_tracking(CvRect &bbox) const
is_tracking procedure to check the tracking state
Definition: module.h:219
Vector skim_blobs_x_bounds
Yarp Vector of min, max bounding in x-axis to reduce the blob detection.
Definition: module.h:289
Definition: module.h:53
bool object_persistence
Definition: module.h:267
deque< CvScalar > histColorsCode
Definition: module.h:281
CvPoint clickLocation
Definition: module.h:300
VectorOf< int > tracker_min_blob_size
minimum size of tracker blob
Definition: module.h:274
bool isDead()
isDead duration comparison procedure with presenceTmo
Definition: module.h:109
bool useSPQ
boolean flag to enable/disable using Superquadric-model for object pose and size estimation ...
Definition: module.h:268
RpcClient rpcGet3D
rpc client port to send requests to SFM
Definition: module.h:237
Vector histObjLocation
Definition: module.h:291
BufferedPort< Bottle > objLocOut
buffered port of output of localized objects
Definition: module.h:245
Mutex mutexResources
Definition: module.h:259
IOLObject(const int filter_order=1, const double presenceTmo_=0.0, const string &trackerType_="BOOSTING", const double trackerTmo_=0.0)
Definition: module.h:83
double presenceTimer
current timer
Definition: module.h:69
void filt(const Vector &x, Vector &xFilt, const Vector &d, Vector &dFilt)
filt apply the median filter
Definition: module.h:125
OpcUpdater opcUpdater
Definition: module.h:254
int histFilterLength
Definition: module.h:280
RtLocalization rtLocalization
Definition: module.h:253
State
Definition: module.h:54
double presenceTmo
current set timeout
Definition: module.h:68
ImageOf< PixelBgr > imgRtLoc
Image for real-time objects localization.
Definition: module.h:258
bool connectedSPQ
boolean flag to check internal connection to Superquadric-model
Definition: module.h:269
string trackerType
Definition: module.h:73