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);
MultiTracker::MultiTracker()
{
- 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<double> ff (40, 1);
- predictor->predict(4, ff);
+ double prob = predictor->predict(4, ff);
}
MultiTracker::~MultiTracker()
{
+ predictor.reset();
trackers.clear();
}
cv::matchTemplate(im1, im2, result, CV_TM_CCORR_NORMED);
feature.push_back(result.at<double>(0, 0));
+
+ vector<double>& f1_hog = p1->features.first; Mat f1_hue = p1->features.second;
+ vector<double>& f2_hog = p1->features.first; Mat f2_hue = p1->features.second;
+ feature.push_back(distance_cosine(Eigen::Map<Eigen::VectorXd>(f1_hog.data(), f1_hog.size()),
+ Eigen::Map<Eigen::VectorXd>(f2_hog.data(), f2_hog.size())));
+ feature.push_back(distance_euclidean(Eigen::Map<Eigen::VectorXd>(f1_hog.data(), f1_hog.size()),
+ Eigen::Map<Eigen::VectorXd>(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;
}
-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<double> features;
+
std::vector<double> 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;
}
void MultiTracker::update(unsigned int total, const Detection* detections, const Mat& image)
{
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
}
// assignment
- VectorXi tracker_inds, bb_inds;
+ Eigen::VectorXi tracker_inds, bb_inds;
linear_sum_assignment(cost_matrix, tracker_inds, bb_inds);
// handle unmatched trackers
t->updateState(image);
}
-
// handle unmatched detections
vector<int> unmatched_detection;
for(int j = 0; j < col; j++){
TrackerPtr t (new Tracker(image));
this->trackers.push_back(t);
}
+
+ Detection dd;
+
+ PatchPtr pp = createPatch(image, dd);
}
-PatchPtr MultiTracker::createPatch(const cv::Mat& 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();
+}
+
+PatchPtr MultiTracker::createPatch(const Mat& image, const Detection& detect)
{
PatchPtr patch(new Patch());
- std::vector<float> 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<float> 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<double> feature_hog_double (feature_hog.begin(), feature_hog.end()); // convert to double
+ patch->features = std::make_pair(feature_hog_double, hist);
return patch;
}
#include "hungarian.h"
#include "gtest/gtest.h"
#include <cmath>
+#include <vector>
using namespace std;
using namespace Eigen;
EXPECT_TRUE(expect_col_ind == col_ind);
}
+TEST(Hungarian, 0x0)
+{
+ MatrixXi C = MatrixXi::Zero(0, 0);
+ VectorXi row_ind, col_ind;
+ int ret = linear_sum_assignment(C, row_ind, col_ind);
+ EXPECT_EQ(ret, 0);
+}
+
+
TEST(Distance, consine)
{
Vector3d u, v;
d = distance_euclidean(u, v);
EXPECT_DOUBLE_EQ(d, 1.0);
}
+
+TEST(Distance, vector)
+{
+ std::vector<int> sv = {1, 2, 3, 4, 5, 6};
+ VectorXi v1;
+ VectorXi b = Eigen::Map<VectorXi>(sv.data(), sv.size());
+ std::cout << b << std::endl;
+ std::vector<float> f1_hog = { 0.1, 0.2, 0,3};
+// Eigen::Map<Eigen::VectorXd>(f2_hog.data(), f2_hog.size())
+
+ //VectorXd mf = Map<VectorXd, 0, InnerStride<2> >(sv.data(), sv.size());
+ std::vector<double> sd = {1, 2, 3, 4, 5, 6};
+ VectorXd mm = Map<VectorXd>(sd.data(), sd.size());
+ VectorXd xd = Map<VectorXd, 0, InnerStride<2> >(sd.data(), sd.size());
+ cout << Map<VectorXd, 0, InnerStride<2> >(sd.data(), sd.size()) << endl;
+
+ int array[12];
+ for(int i = 0; i < 12; ++i) array[i] = i;
+ cout << Map<VectorXi, 0, InnerStride<2> >(sv.data(), sv.size()) // the inner stride has already been passed as template parameter
+ << endl;
+
+
+ //Vector3d v = Vector3d::Random();
+ //std::cout << v << std::endl;
+
+}
+
+