Add worker thread
[trackerpp.git] / src / MultiTracker.cpp
index b1f380e..0b4dab5 100644 (file)
@@ -13,7 +13,8 @@ static const cv::Size PREFERRED_SIZE = Size(64, 128);
 
 #define MaxCost  100000
 
-MultiTracker::MultiTracker()
+MultiTracker::MultiTracker(EngineWPtr e)
+: engine(e)
 {
     LOG_DEBUG(TAG, "init - loading model.pkl");
     predictor = PredictorWrapper::create("./python/model.pkl");
@@ -30,6 +31,11 @@ MultiTracker::~MultiTracker()
     trackers.clear();
 }
 
+static double calc_iou_ratio(const Detection& d1, const Detection& d2)
+{
+    // TODO
+    return 0.1;
+}
 
 static std::vector<double> similarity(const PatchPtr p1, const PatchPtr p2)
 {
@@ -60,8 +66,7 @@ static std::vector<double> similarity(const PatchPtr p1, const PatchPtr p2)
     double center_distance = sqrt(pow((d1.center_x - d2.center_x), 2) + pow((d1.center_y - d2.center_y), 2));
     feature.push_back(center_distance / (d1.width + d1.height + d2.width + d2.height) * 4);
 
-    //TODO
-    double iou_ratio = 0.03;
+    double iou_ratio = calc_iou_ratio(d1, d2);
     feature.push_back(iou_ratio);
 
     return feature;
@@ -82,32 +87,41 @@ double MultiTracker::distance(TrackerPtr tracker, const cv::Mat& image, const De
     return prob; 
 }
 
+static long cc = 0;
+
 void MultiTracker::update(unsigned int total, const Detection* detections, const Mat& image)
 {
+    //////
+    if ((cc % 50) == 0){
+        if (EnginePtr e = engine.lock()){
+            e->onStatusChanged();
+        }
+    }
+    cc++;
+
+    //////
     int row = trackers.size();
     int col = total;
     Eigen::MatrixXi cost_matrix = Eigen::MatrixXi::Zero(row, col);
     for (int i = 0; i < row; i++){
         for (int j = 0; j < col; j++){
-            // TODO
-            cost_matrix(i, j) = distance(trackers[i], image, detections[j]);
+            //if (calc_iou_ratio(trackers[i], detections[j]) < -0.1)
+            //    cost_matrix(i, j) = MaxCost;
+            //else
+                cost_matrix(i, j) = distance(trackers[i], image, detections[j]);
         }
     }
 
-    // assignment
     Eigen::VectorXi tracker_inds, bb_inds;
     linear_sum_assignment(cost_matrix, tracker_inds, bb_inds);
 
     // handle unmatched trackers
-    vector<TrackerPtr> unmatched_trackers;
+    //vector<TrackerPtr> unmatched_trackers;
     for (int i = 0; i < row; i++){
         if (!(tracker_inds.array() == i).any()){
-            unmatched_trackers.push_back(trackers[i]);
+            trackers[i]->updateState(image);
         }
     }
-    for (auto t : unmatched_trackers){
-        t->updateState(image);
-    }
 
     // handle unmatched detections
     vector<int> unmatched_detection;
@@ -155,7 +169,6 @@ PatchPtr MultiTracker::createPatch(const Mat& image, const Detection& detect)
     float sranges[] = {0, 256};
     const float* ranges[] = {hranges, sranges};
     calcHist(&hsv, 1, channels, Mat(), hist, 2, histSize, ranges, true, false);
-    Size sm = hist.size();
 
     patch->image_crop = im.clone();
     patch->detection = detect;