himrep
linearClassifierThread.h
1 #include <iostream>
2 #include <string>
3 #include <mutex>
4 
5 #include <yarp/os/all.h>
6 #include <yarp/sig/all.h>
7 #include <yarp/math/Math.h>
8 
9 #include "SVMLinear.h"
10 
11 #ifdef _WIN32
12  #include "win_dirent.h"
13 #else
14  #include "dirent.h"
15 #endif
16 
17 #define STATE_DONOTHING 0
18 #define STATE_SAVING 1
19 #define STATE_RECOGNIZING 2
20 
21 using namespace std;
22 using namespace yarp::os;
23 using namespace yarp::sig;
24 using namespace yarp::math;
25 
26 
27 class linearClassifierThread : public Thread
28 {
29 private:
30  string inputFeatures;
31  string outputPortName;
32  string outputScorePortName;
33 
34  string currPath;
35  string pathObj;
36  Port *commandPort;
37  BufferedPort<Bottle> featuresPort;
38  BufferedPort<Bottle> outputPort;
39  Port scorePort;
40  mutex mtx;
41  fstream objFeatures;
42 
43  vector<pair<string,vector<string> > > knownObjects;
44  vector<SVMLinear> linearClassifiers;
45  vector<int> datasetSizes;
46  vector<vector<vector<double> > > Features;
47  vector<vector<double > > bufferScores;
48  vector<vector<int > > countBuffer;
49  int bufferSize;
50  double CSVM;
51  int useWeightedSVM;
52 
53  int currentState;
54 
55  void checkKnownObjects();
56  int getdir(const string &dir, vector<string> &files);
57  void stopAllHelper();
58  bool trainClassifiersHelper();
59  bool forgetClassHelper(const string &className, const bool retrain);
60 
61 public:
62 
63  linearClassifierThread(yarp::os::ResourceFinder &rf,Port* commPort);
64  void prepareObjPath(const string &objName);
65  bool threadInit();
66  void threadRelease();
67  void run();
68  void onStop();
69  void createFullPath(const char * path);
70  void stopAll();
71  bool loadFeatures();
72  bool trainClassifiers();
73  bool startRecognition();
74  bool forgetClass(const string &className, const bool retrain);
75  bool forgetAll();
76  bool getClassList(Bottle &b);
77  bool changeName(const string &old_name, const string &new_name);
78 };