Covert vector to python list
[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 Eigen;
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()
17 {
18     LOG_DEBUG(TAG, "init - load model.pkl");
19     predictor = PredictorWrapper::create("./python/model.pkl");
20     predictor->dump();
21     this->descriptor = {cv::Size(64, 128), cv::Size(16, 16), cv::Size(8, 8), cv::Size(8, 8), 9};
22     std::vector<double> ff (40, 1);
23     predictor->predict(4, ff);
24 }
25
26 MultiTracker::~MultiTracker()
27 {
28     trackers.clear();
29 }
30
31
32 static std::vector<double> similarity(const PatchPtr p1, const PatchPtr p2)
33 {
34     std::vector<double> feature;
35     cv::Mat im1(PREFERRED_SIZE, p1->image_crop.type());
36     cv::Mat im2(PREFERRED_SIZE, p2->image_crop.type());
37     cv::resize(p1->image_crop, im1, im1.size());
38     cv::resize(p2->image_crop, im2, im2.size());
39     cv::Mat result;
40     cv::matchTemplate(im1, im2, result, CV_TM_CCOEFF_NORMED);
41     feature.push_back(result.at<double>(0, 0));
42     cv::matchTemplate(im1, im2, result, CV_TM_CCORR_NORMED);
43     feature.push_back(result.at<double>(0, 0));
44
45     return feature;
46 }
47
48
49 double MultiTracker::distance(TrackerPtr t, const cv::Mat& image, const Detection& d)
50 {
51     PatchPtr patch = createPatch(image);
52     std::vector<double> features;
53     std::vector<double> ss;
54     for (const auto i : t->patches){
55         ss = similarity(i, patch);
56         features.insert(features.end(),  ss.begin(), ss.end());
57     }
58     //predictor->predict();
59     return 0.1; 
60 }
61
62 void MultiTracker::update(unsigned int total, const Detection* detections, const Mat& image)
63 {
64     int row = trackers.size();
65     int col = total;
66     MatrixXi cost_matrix = MatrixXi::Zero(row, col);
67     for (int i = 0; i < row; i++){
68         for (int j = 0; j < col; j++){
69             // TODO
70             cost_matrix(i, j) = distance(trackers[i], image, detections[j]);
71         }
72     }
73
74     // assignment
75     VectorXi tracker_inds, bb_inds;
76     linear_sum_assignment(cost_matrix, tracker_inds, bb_inds);
77
78     // handle unmatched trackers
79     vector<TrackerPtr> unmatched_trackers;
80     for (int i = 0; i < row; i++){
81         if (!(tracker_inds.array() == i).any()){
82             unmatched_trackers.push_back(trackers[i]);
83         }
84     }
85     for (auto t : unmatched_trackers){
86         t->updateState(image);
87     }
88
89
90     // handle unmatched detections
91     vector<int> unmatched_detection;
92     for(int j = 0; j < col; j++){
93         if (!(bb_inds.array() == j).any()){
94             unmatched_detection.push_back(j);
95         }
96     }
97     // create new trackers for new detections
98     for (auto i : unmatched_detection){
99         TrackerPtr t (new Tracker(image));
100         this->trackers.push_back(t);
101     }
102 }
103
104 PatchPtr MultiTracker::createPatch(const cv::Mat& image)
105 {
106     PatchPtr patch(new Patch());
107     std::vector<float> feature_hog;
108     cv::Mat img;
109     cv::resize(image, img, PREFERRED_SIZE);
110     this->descriptor.compute(img, feature_hog);
111
112     cv::Mat hist;
113     //cv::calcHist()
114     patch->image_crop = image;
115     return patch;
116 }