Add worker thread
[trackerpp.git] / src / MultiTracker.cpp
1 #include "MultiTracker.h"
2 #include "Metrics.h"
3 #include <algorithm>
4 #include "hungarian.h"
5 #include "Logger.h"
6
7 using namespace suanzi;
8 using namespace cv;
9 using namespace std;
10
11 static const std::string TAG = "MultiTracker";
12 static const cv::Size PREFERRED_SIZE = Size(64, 128);
13
14 #define MaxCost  100000
15
16 MultiTracker::MultiTracker(EngineWPtr e)
17 : engine(e)
18 {
19     LOG_DEBUG(TAG, "init - loading model.pkl");
20     predictor = PredictorWrapper::create("./python/model.pkl");
21     predictor->dump();
22     this->descriptor = {Size(64, 128), Size(16, 16), Size(8, 8), Size(8, 8), 9};
23
24     std::vector<double> ff (40, 1);
25     double prob = predictor->predict(4, ff);
26 }
27
28 MultiTracker::~MultiTracker()
29 {
30     predictor.reset();
31     trackers.clear();
32 }
33
34 static double calc_iou_ratio(const Detection& d1, const Detection& d2)
35 {
36     // TODO
37     return 0.1;
38 }
39
40 static std::vector<double> similarity(const PatchPtr p1, const PatchPtr p2)
41 {
42     std::vector<double> feature;
43     cv::Mat im1(PREFERRED_SIZE, p1->image_crop.type());
44     cv::Mat im2(PREFERRED_SIZE, p2->image_crop.type());
45     cv::resize(p1->image_crop, im1, im1.size());
46     cv::resize(p2->image_crop, im2, im2.size());
47     cv::Mat result;
48     cv::matchTemplate(im1, im2, result, CV_TM_CCOEFF_NORMED);
49     feature.push_back(result.at<double>(0, 0));
50     cv::matchTemplate(im1, im2, result, CV_TM_CCORR_NORMED);
51     feature.push_back(result.at<double>(0, 0));
52
53
54     vector<double>& f1_hog = p1->features.first; Mat f1_hue = p1->features.second;
55     vector<double>& f2_hog = p1->features.first; Mat f2_hue = p1->features.second;
56     feature.push_back(distance_cosine(Eigen::Map<Eigen::VectorXd>(f1_hog.data(), f1_hog.size()), 
57                                     Eigen::Map<Eigen::VectorXd>(f2_hog.data(), f2_hog.size())));
58     feature.push_back(distance_euclidean(Eigen::Map<Eigen::VectorXd>(f1_hog.data(), f1_hog.size()), 
59                                     Eigen::Map<Eigen::VectorXd>(f2_hog.data(), f2_hog.size())));
60     feature.push_back(compareHist(f1_hue, f2_hue, HISTCMP_CORREL));
61     feature.push_back(compareHist(f1_hue, f2_hue, HISTCMP_HELLINGER));
62
63     Detection& d1 = p1->detection;
64     Detection& d2 = p2->detection;
65
66     double center_distance = sqrt(pow((d1.center_x - d2.center_x), 2) + pow((d1.center_y - d2.center_y), 2));
67     feature.push_back(center_distance / (d1.width + d1.height + d2.width + d2.height) * 4);
68
69     double iou_ratio = calc_iou_ratio(d1, d2);
70     feature.push_back(iou_ratio);
71
72     return feature;
73 }
74
75
76 double MultiTracker::distance(TrackerPtr tracker, const cv::Mat& image, const Detection& d)
77 {
78     PatchPtr patch = createPatch(image, d);
79     std::vector<double> features;
80
81     std::vector<double> ss;
82     for (auto i : tracker->patches){
83         ss = similarity(i, patch);
84         features.insert(features.end(),  ss.begin(), ss.end());
85     }
86     double prob = predictor->predict(4, features);
87     return prob; 
88 }
89
90 static long cc = 0;
91
92 void MultiTracker::update(unsigned int total, const Detection* detections, const Mat& image)
93 {
94     //////
95     if ((cc % 50) == 0){
96         if (EnginePtr e = engine.lock()){
97             e->onStatusChanged();
98         }
99     }
100     cc++;
101
102     //////
103     int row = trackers.size();
104     int col = total;
105     Eigen::MatrixXi cost_matrix = Eigen::MatrixXi::Zero(row, col);
106     for (int i = 0; i < row; i++){
107         for (int j = 0; j < col; j++){
108             //if (calc_iou_ratio(trackers[i], detections[j]) < -0.1)
109             //    cost_matrix(i, j) = MaxCost;
110             //else
111                 cost_matrix(i, j) = distance(trackers[i], image, detections[j]);
112         }
113     }
114
115     Eigen::VectorXi tracker_inds, bb_inds;
116     linear_sum_assignment(cost_matrix, tracker_inds, bb_inds);
117
118     // handle unmatched trackers
119     //vector<TrackerPtr> unmatched_trackers;
120     for (int i = 0; i < row; i++){
121         if (!(tracker_inds.array() == i).any()){
122             trackers[i]->updateState(image);
123         }
124     }
125
126     // handle unmatched detections
127     vector<int> unmatched_detection;
128     for(int j = 0; j < col; j++){
129         if (!(bb_inds.array() == j).any()){
130             unmatched_detection.push_back(j);
131         }
132     }
133     // create new trackers for new detections
134     for (auto i : unmatched_detection){
135         TrackerPtr t (new Tracker(image));
136         this->trackers.push_back(t);
137     }
138
139     Detection dd;
140
141     PatchPtr pp = createPatch(image, dd);
142 }
143
144 // Get image crop from input image within given bounding box - Detecinon
145 static cv::Mat image_crop(const cv::Mat& image, const Detection& bb)
146 {
147     // RECT
148     // TODO;
149     return image.clone();
150 }
151
152 PatchPtr MultiTracker::createPatch(const Mat& image, const Detection& detect)
153 {
154     PatchPtr patch(new Patch());
155
156     // calculate hog descriptors, size is 3780
157     Mat im, im2;
158     im = image_crop(image, detect);
159     resize(im, im2, PREFERRED_SIZE);
160     vector<float> feature_hog;
161     this->descriptor.compute(im2, feature_hog);
162
163     // calculate histogram, size is (64 x 45)
164     Mat hsv, hist;
165     cvtColor(im, hsv, COLOR_BGR2HSV);
166     int channels[] = {0, 1};
167     int histSize[] = {45, 64};
168     float hranges[] = {0, 180};
169     float sranges[] = {0, 256};
170     const float* ranges[] = {hranges, sranges};
171     calcHist(&hsv, 1, channels, Mat(), hist, 2, histSize, ranges, true, false);
172
173     patch->image_crop = im.clone();
174     patch->detection = detect;
175     std::vector<double> feature_hog_double (feature_hog.begin(), feature_hog.end()); // convert to double
176     patch->features = std::make_pair(feature_hog_double, hist);
177     return patch;
178 }