X-Git-Url: http://47.100.26.94:8080/?a=blobdiff_plain;f=src%2FMultiTracker.cpp;h=c87395ef3415c1da78da878eaa40aa2b9fcc568d;hb=b5f0328f4054d19fcf8a6b870d5448be8087d29c;hp=267ba0f7e86a3f23545ae0f001089a4d03497091;hpb=3aa517d206c44156fe86697aeadc5f75ea212329;p=trackerpp.git diff --git a/src/MultiTracker.cpp b/src/MultiTracker.cpp index 267ba0f..c87395e 100644 --- a/src/MultiTracker.cpp +++ b/src/MultiTracker.cpp @@ -6,28 +6,49 @@ using namespace suanzi; using namespace cv; -using namespace Eigen; +using namespace std; static const std::string TAG = "MultiTracker"; static const cv::Size PREFERRED_SIZE = Size(64, 128); #define MaxCost 100000 -MultiTracker::MultiTracker() +MultiTracker::MultiTracker(EngineWPtr e) +: engine(e) { - LOG_DEBUG(TAG, "init - load model.pkl"); + LOG_DEBUG(TAG, "init - loading model.pkl"); predictor = PredictorWrapper::create("./python/model.pkl"); predictor->dump(); - this->descriptor = {cv::Size(64, 128), cv::Size(16, 16), cv::Size(8, 8), cv::Size(8, 8), 9}; + this->descriptor = {Size(64, 128), Size(16, 16), Size(8, 8), Size(8, 8), 9}; + std::vector ff (40, 1); - predictor->predict(4, ff); + double prob = predictor->predict(4, ff); } MultiTracker::~MultiTracker() { + predictor.reset(); trackers.clear(); } +static Rect getRectInDetection(const Detection& d) +{ + Rect r; + r.x = d.center_x - d.width / 2; + r.y = d.center_y - d.height / 2; + r.width = d.width; + r.height = d.height; + return r; +} + +static double calc_iou_ratio(const Detection& d1, const Detection& d2) +{ + Rect r1 = getRectInDetection (d1); + Rect r2 = getRectInDetection (d2); + Rect r_inner = r1 & r1; + Rect r_union = r1 | r2; + return 1.0 * r_inner.area() / r_union.area(); +} static std::vector similarity(const PatchPtr p1, const PatchPtr p2) { @@ -42,50 +63,77 @@ static std::vector similarity(const PatchPtr p1, const PatchPtr p2) 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); + + feature.push_back(calc_iou_ratio(d1, d2)); + return feature; } -double MultiTracker::distance(TrackerPtr t, const cv::Mat& image, const Detection& d) +double MultiTracker::distance(TrackerPtr tracker, const cv::Mat& image, const Detection& d) { - PatchPtr patch = createPatch(image); + PatchPtr patch = createPatch(image, d); std::vector features; + std::vector ss; - for (const auto i : t->patches){ + for (auto i : tracker->patches){ ss = similarity(i, patch); features.insert(features.end(), ss.begin(), ss.end()); } - //predictor->predict(); - return 0.1; + double prob = predictor->predict(4, features); + 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; - MatrixXi cost_matrix = MatrixXi::Zero(row, col); + 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 - VectorXi tracker_inds, bb_inds; + Eigen::VectorXi tracker_inds, bb_inds; linear_sum_assignment(cost_matrix, tracker_inds, bb_inds); // handle unmatched trackers - vector unmatched_trackers; + //vector 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 unmatched_detection; @@ -99,18 +147,41 @@ void MultiTracker::update(unsigned int total, const Detection* detections, const TrackerPtr t (new Tracker(image)); this->trackers.push_back(t); } + + Detection dd; + + PatchPtr pp = createPatch(image, dd); +} + +static cv::Mat image_crop(const cv::Mat& image, const Detection& bb) +{ + return image(getRectInDetection(bb)); } -PatchPtr MultiTracker::createPatch(const cv::Mat& image) +PatchPtr MultiTracker::createPatch(const Mat& image, const Detection& detect) { PatchPtr patch(new Patch()); - std::vector feature_hog; - cv::Mat img; - cv::resize(image, img, PREFERRED_SIZE); - this->descriptor.compute(img, feature_hog); - - cv::Mat hist; - //cv::calcHist() - patch->image_crop = image; + + // 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); + + 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; }