1 #include "MultiTracker.h"
8 using namespace suanzi;
12 static const std::string TAG = "MultiTracker";
13 static const cv::Size PREFERRED_SIZE = Size(64, 128);
15 static const double MaxCost = 100000;
16 static const int MaxPath = 5;
18 MultiTracker::MultiTracker(EngineWPtr e)
21 LOG_DEBUG(TAG, "init - loading model.pkl");
22 predictor = PredictorWrapper::create("./python", "./python/model.pkl");
24 this->descriptor = {Size(64, 128), Size(16, 16), Size(8, 8), Size(8, 8), 9};
27 MultiTracker::~MultiTracker()
33 static std::vector<double> similarity(const PatchPtr p1, const PatchPtr p2)
35 std::vector<double> feature;
36 cv::Mat im1(PREFERRED_SIZE, p1->image_crop.type());
37 cv::Mat im2(PREFERRED_SIZE, p2->image_crop.type());
38 cv::resize(p1->image_crop, im1, im1.size());
39 cv::resize(p2->image_crop, im2, im2.size());
41 cv::matchTemplate(im1, im2, result, CV_TM_CCOEFF_NORMED);
42 feature.push_back(result.at<double>(0, 0));
43 cv::matchTemplate(im1, im2, result, CV_TM_CCORR_NORMED);
44 feature.push_back(result.at<double>(0, 0));
47 vector<double>& f1_hog = p1->features.first; Mat f1_hue = p1->features.second;
48 vector<double>& f2_hog = p1->features.first; Mat f2_hue = p1->features.second;
49 feature.push_back(distance_cosine(Eigen::Map<Eigen::VectorXd>(f1_hog.data(), f1_hog.size()),
50 Eigen::Map<Eigen::VectorXd>(f2_hog.data(), f2_hog.size())));
51 feature.push_back(distance_euclidean(Eigen::Map<Eigen::VectorXd>(f1_hog.data(), f1_hog.size()),
52 Eigen::Map<Eigen::VectorXd>(f2_hog.data(), f2_hog.size())));
53 feature.push_back(compareHist(f1_hue, f2_hue, HISTCMP_CORREL));
54 feature.push_back(compareHist(f1_hue, f2_hue, HISTCMP_HELLINGER));
56 Detection& d1 = p1->detection;
57 Detection& d2 = p2->detection;
59 double center_distance = sqrt(pow((d1.center_x - d2.center_x), 2) + pow((d1.center_y - d2.center_y), 2));
60 feature.push_back(center_distance / (d1.width + d1.height + d2.width + d2.height) * 4);
62 feature.push_back(calc_iou_ratio(getRectInDetection(d1), getRectInDetection(d2)));
67 double MultiTracker::distance(TrackerPtr tracker, const cv::Mat& image, const Detection& d)
69 PatchPtr patch = createPatch(image, d);
70 std::vector<double> features;
72 std::vector<double> ss;
73 for (auto i : tracker->patches){
74 ss = similarity(i, patch);
75 features.insert(features.end(), ss.begin(), ss.end());
77 double prob = predictor->predict(4, features);
81 static float calc_iou_ratio(const Detection& d1, const Detection& d2)
83 return calc_iou_ratio(getRectInDetection(d1), getRectInDetection(d2));
86 void MultiTracker::update(unsigned int total, const Detection* detections, const Mat& image)
88 int row = trackers.size();
90 Eigen::MatrixXi cost_matrix = Eigen::MatrixXi::Zero(row, col);
91 for (int i = 0; i < row; i++){
92 for (int j = 0; j < col; j++){
93 if (calc_iou_ratio(trackers[i]->detection, detections[j]) < -0.1)
94 cost_matrix(i, j) = MaxCost;
96 cost_matrix(i, j) = distance(trackers[i], image, detections[j]);
100 Eigen::VectorXi tracker_inds, bb_inds;
101 linear_sum_assignment(cost_matrix, tracker_inds, bb_inds);
103 // handle unmatched trackers
104 //vector<TrackerPtr> unmatched_trackers;
105 for (int i = 0; i < row; i++){
106 if (!(tracker_inds.array() == i).any()){
107 trackers[i]->updateState(image);
111 // handle unmatched detections
112 vector<int> unmatched_detection;
113 for(int j = 0; j < col; j++){
114 if (!(bb_inds.array() == j).any()){
115 unmatched_detection.push_back(j);
118 // create new trackers for new detections
119 for (auto i : unmatched_detection){
120 TrackerPtr t (new Tracker(image));
121 this->trackers.push_back(t);
126 PatchPtr pp = createPatch(image, dd);
129 static cv::Mat image_crop(const cv::Mat& image, const Detection& bb)
131 return image(getRectInDetection(bb));
134 PatchPtr MultiTracker::createPatch(const Mat& image, const Detection& detect)
136 PatchPtr patch(new Patch());
138 // calculate hog descriptors, size is 3780
140 im = image_crop(image, detect);
141 resize(im, im2, PREFERRED_SIZE);
142 vector<float> feature_hog;
143 this->descriptor.compute(im2, feature_hog);
145 // calculate histogram, size is (64 x 45)
147 cvtColor(im, hsv, COLOR_BGR2HSV);
148 int channels[] = {0, 1};
149 int histSize[] = {45, 64};
150 float hranges[] = {0, 180};
151 float sranges[] = {0, 256};
152 const float* ranges[] = {hranges, sranges};
153 calcHist(&hsv, 1, channels, Mat(), hist, 2, histSize, ranges, true, false);
155 patch->image_crop = im.clone();
156 patch->detection = detect;
157 std::vector<double> feature_hog_double (feature_hog.begin(), feature_hog.end()); // convert to double
158 patch->features = std::make_pair(feature_hog_double, hist);