X-Git-Url: http://47.100.26.94:8080/?a=blobdiff_plain;ds=sidebyside;f=src%2FMultiTracker.cpp;h=b1f380eb73e8b7f769014bfad62c96e06075dd20;hb=209cfd9fe0ea398b794d1d1995629a826125f035;hp=8df378f034123fe3e2746d0c017f16343cfef7ad;hpb=0e3565052ce6db176c34c448a7368b463d318558;p=trackerpp.git diff --git a/src/MultiTracker.cpp b/src/MultiTracker.cpp index 8df378f..b1f380e 100644 --- a/src/MultiTracker.cpp +++ b/src/MultiTracker.cpp @@ -1,75 +1,165 @@ #include "MultiTracker.h" #include "Metrics.h" #include +#include "hungarian.h" +#include "Logger.h" using namespace suanzi; using namespace cv; +using namespace std; -MultiTracker::MultiTracker(MetricsPtr m) : metrics(m) -{ -} +static const std::string TAG = "MultiTracker"; +static const cv::Size PREFERRED_SIZE = Size(64, 128); +#define MaxCost 100000 -MultiTracker::~MultiTracker() +MultiTracker::MultiTracker() { - trackers.clear(); -} + LOG_DEBUG(TAG, "init - loading model.pkl"); + predictor = PredictorWrapper::create("./python/model.pkl"); + predictor->dump(); + this->descriptor = {Size(64, 128), Size(16, 16), Size(8, 8), Size(8, 8), 9}; -TrackerPtr MultiTracker::createTracker(int id) -{ - TrackerPtr t (new Tracker(id)); - addTracker(t); - return t; + std::vector ff (40, 1); + double prob = predictor->predict(4, ff); } -void MultiTracker::addTracker(TrackerPtr t) +MultiTracker::~MultiTracker() { - trackers.insert(t); + predictor.reset(); + trackers.clear(); } -void MultiTracker::removeTracker(TrackerPtr t) -{ - trackers.erase(t); -} -void MultiTracker::initNewTrackers(cv::Mat& iamge) +static std::vector similarity(const PatchPtr p1, const PatchPtr p2) { + std::vector feature; + cv::Mat im1(PREFERRED_SIZE, p1->image_crop.type()); + cv::Mat im2(PREFERRED_SIZE, p2->image_crop.type()); + cv::resize(p1->image_crop, im1, im1.size()); + cv::resize(p2->image_crop, im2, im2.size()); + cv::Mat result; + cv::matchTemplate(im1, im2, result, CV_TM_CCOEFF_NORMED); + feature.push_back(result.at(0, 0)); + cv::matchTemplate(im1, im2, result, CV_TM_CCORR_NORMED); + feature.push_back(result.at(0, 0)); + + + vector& f1_hog = p1->features.first; Mat f1_hue = p1->features.second; + vector& f2_hog = p1->features.first; Mat f2_hue = p1->features.second; + feature.push_back(distance_cosine(Eigen::Map(f1_hog.data(), f1_hog.size()), + Eigen::Map(f2_hog.data(), f2_hog.size()))); + feature.push_back(distance_euclidean(Eigen::Map(f1_hog.data(), f1_hog.size()), + Eigen::Map(f2_hog.data(), f2_hog.size()))); + feature.push_back(compareHist(f1_hue, f2_hue, HISTCMP_CORREL)); + feature.push_back(compareHist(f1_hue, f2_hue, HISTCMP_HELLINGER)); + + Detection& d1 = p1->detection; + Detection& d2 = p2->detection; + + 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; + feature.push_back(iou_ratio); + + return feature; } -void MultiTracker::correctTrackers(MetricsPtr m, Mat& image) +double MultiTracker::distance(TrackerPtr tracker, const cv::Mat& image, const Detection& d) { + PatchPtr patch = createPatch(image, d); + std::vector features; + + std::vector ss; + for (auto i : tracker->patches){ + ss = similarity(i, patch); + features.insert(features.end(), ss.begin(), ss.end()); + } + double prob = predictor->predict(4, features); + return prob; } - -void MultiTracker::update(unsigned int total, const Detection* d, const Mat& image) +void MultiTracker::update(unsigned int total, const Detection* detections, const Mat& image) { + 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]); + } + } + + // assignment + Eigen::VectorXi tracker_inds, bb_inds; + linear_sum_assignment(cost_matrix, tracker_inds, bb_inds); + + // handle unmatched trackers + vector unmatched_trackers; + for (int i = 0; i < row; i++){ + if (!(tracker_inds.array() == i).any()){ + unmatched_trackers.push_back(trackers[i]); + } + } + for (auto t : unmatched_trackers){ + t->updateState(image); + } + + // handle unmatched detections + vector unmatched_detection; + for(int j = 0; j < col; j++){ + if (!(bb_inds.array() == j).any()){ + unmatched_detection.push_back(j); + } + } + // create new trackers for new detections + for (auto i : unmatched_detection){ + TrackerPtr t (new Tracker(image)); + this->trackers.push_back(t); + } - // correct_trackers - - - // - - - - - - + Detection dd; + PatchPtr pp = createPatch(image, dd); +} - // Delete long lost trackers; -// for (auto& t : trackers){ -// if (t->status == TrackerStatus::Delete) -// trackers.erase(t); -// } -// - // Update trackers using kalman filter -// for(auto& t: trackers){ -// //t.bb_ltrb = -// } -// - // associate trackers with detections -// correctTrackers(this->metric, image); +// Get image crop from input image within given bounding box - Detecinon +static cv::Mat image_crop(const cv::Mat& image, const Detection& bb) +{ + // RECT + // TODO; + return image.clone(); +} - // create new trackers for new detections +PatchPtr MultiTracker::createPatch(const Mat& image, const Detection& detect) +{ + PatchPtr patch(new Patch()); + + // calculate hog descriptors, size is 3780 + Mat im, im2; + im = image_crop(image, detect); + resize(im, im2, PREFERRED_SIZE); + vector feature_hog; + this->descriptor.compute(im2, feature_hog); + + // calculate histogram, size is (64 x 45) + Mat hsv, hist; + cvtColor(im, hsv, COLOR_BGR2HSV); + int channels[] = {0, 1}; + int histSize[] = {45, 64}; + float hranges[] = {0, 180}; + 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; + std::vector feature_hog_double (feature_hog.begin(), feature_hog.end()); // convert to double + patch->features = std::make_pair(feature_hog_double, hist); + return patch; }