From: Peng Li Date: Tue, 24 Jul 2018 05:22:30 +0000 (+0800) Subject: Fix nan issue in features X-Git-Url: http://47.100.26.94:8080/?a=commitdiff_plain;p=trackerpp.git Fix nan issue in features --- diff --git a/README.md b/README.md index a1112a9..a279d66 100644 --- a/README.md +++ b/README.md @@ -5,14 +5,19 @@ Tracker++ cpp version on Linux (arm) - opencv - 3.4.1 Run `script/install_opencv.sh` to build and install opencv into /usr/local -Refer this page + + Refer this page - log4cpp : logger utils + - eigen : matrix library of C++ + - scons + `apt-get install liblog4cpp5-dev libeigen3-dev scons` - boost-python + - `apt-get install libpython-dev python-dev` - build boost with python - `pip install scipy numpy sklearn` @@ -26,14 +31,17 @@ so need to re-install boost 1.64 by following command. add `--with-python` ## Build with `scons` - Run `scons` + To build folder `src/` and `main.cpp`. `libtracker.a` and `main` will be generated - Run `scons --all` + To build folder `src/` and `test/` and `main.cpp`. `libtracker.a` , `main`, and `test/TestMain` will be generated. Run `TestMain` to run all the unit test - Run `scons -c` or `scons --all -c` + To clean ### Run @@ -41,5 +49,6 @@ so need to re-install boost 1.64 by following command. add `--with-python` `./main` ### Log + Logger config file is located at `config/log4cpp.properties`. By default, log file is `/tmp/trackerpp.log` diff --git a/include/Detector.h b/include/Detector.h index cb52902..0d4c6b1 100644 --- a/include/Detector.h +++ b/include/Detector.h @@ -15,7 +15,7 @@ namespace suanzi { Detector(); virtual ~Detector(); // TODO - unsigned int detect(const cv::Mat& frame, Detection* detections){return 0;} + unsigned int detect(const cv::Mat& frame, Detection* detections){return 2;} }; struct Detection @@ -25,10 +25,10 @@ namespace suanzi { // 检测目标的分数,可以不填 float score; // 检测目标的坐标,包括物体中心的x、y坐标,物体的高和宽 - unsigned int center_x; - unsigned int center_y; - unsigned int height; - unsigned int width; + unsigned int center_x = 100; + unsigned int center_y = 100; + unsigned int height = 200; + unsigned int width = 200; // 检测目标的特征向量 unsigned int feature_size; float * feature; diff --git a/include/Engine.h b/include/Engine.h index ebd5121..8443e19 100644 --- a/include/Engine.h +++ b/include/Engine.h @@ -44,6 +44,7 @@ private: Engine(); void run(); WorkerThread eventThread {"EventThread"}; + WorkerThread writeImgThread {"WriteImgThread"}; DetectorPtr detector; MultiTrackerPtr multiTracker; std::set observer_list; diff --git a/include/Metrics.h b/include/Metrics.h deleted file mode 100644 index 2aa7939..0000000 --- a/include/Metrics.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef _METRICS_H_ -#define _METRICS_H_ - -#include -#include -#include "SharedPtr.h" - -namespace suanzi { - - TK_DECLARE_PTR(Metrics); - //TK_DECLARE_PTR(Patch); -// struct Patch; - class Metrics - { - public: - Metrics(const std::string& cl_path = ""); - ~Metrics(){} - const static long int MaxCost = 100000; - const static int MaxPatch = 5; - //double distance() - private: - cv::HOGDescriptor descriptor = {cv::Size(64, 128), cv::Size(16, 16), cv::Size(8, 8), cv::Size(8, 8), 9}; - }; - -// struct Patch -// { -// // bb_ltrb -// // -// // image_crop -// cv::Mat image_crop; -// // -// // features -// }; -} - -#endif /* _METRICS_H_ */ diff --git a/include/MultiTracker.h b/include/MultiTracker.h index 07bd5db..2a9d0bc 100644 --- a/include/MultiTracker.h +++ b/include/MultiTracker.h @@ -28,6 +28,7 @@ namespace suanzi { int max_id = 0; PatchPtr createPatch(const cv::Mat& image, const Detection& d); double distance(TrackerPtr t, const cv::Mat& image, const Detection& d); + void addTracker(TrackerPtr t); PredictorWrapperPtr predictor; cv::HOGDescriptor descriptor; EngineWPtr engine; diff --git a/include/PyWrapper.cpp b/include/PyWrapper.cpp deleted file mode 100644 index 6319940..0000000 --- a/include/PyWrapper.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#include - diff --git a/include/Tracker.h b/include/Tracker.h index 88d041a..7bfcfb0 100644 --- a/include/Tracker.h +++ b/include/Tracker.h @@ -4,7 +4,6 @@ #include #include #include -#include "Metrics.h" #include "SharedPtr.h" #include "MultiTracker.h" #include "Detector.h" diff --git a/main.cpp b/main.cpp index 577c3fc..9008435 100644 --- a/main.cpp +++ b/main.cpp @@ -30,8 +30,8 @@ int main(int argc, char* argv[]) LOG_DEBUG(TAG, "=================================="); EnginePtr e = Engine::create(); e->addObserver(new Callback()); - //e->setVideoSrc(VideoSrcType::URL, "rtsp://192.168.1.75:554/stream1"); - e->setVideoSrc(VideoSrcType::URL, "rtsp://192.168.56.101:5454/test.mp4"); + e->setVideoSrc(VideoSrcType::URL, "rtsp://192.168.1.75:554/stream1"); + //e->setVideoSrc(VideoSrcType::URL, "rtsp://192.168.56.101:5454/test.mp4"); e->start(); e->destroy(); log4cpp::Category::shutdown(); diff --git a/python/predictor.py b/python/predictor.py index 2b77aac..0e8f026 100644 --- a/python/predictor.py +++ b/python/predictor.py @@ -12,6 +12,7 @@ def init(fname = './model.pkl'): f = open(fname, 'rb') predictors = pickle.load(f) f.close() + return True; def dump(): global predictors diff --git a/src/MultiTracker.cpp b/src/MultiTracker.cpp index bbfc6f3..199d9f7 100644 --- a/src/MultiTracker.cpp +++ b/src/MultiTracker.cpp @@ -1,9 +1,10 @@ #include "MultiTracker.h" -#include "Metrics.h" #include #include "hungarian.h" #include "Logger.h" #include "Utils.h" +#include +#include using namespace suanzi; using namespace cv; @@ -13,6 +14,7 @@ static const std::string TAG = "MultiTracker"; static const cv::Size PREFERRED_SIZE = Size(64, 128); static const double MaxCost = 100000; static const double ProbThreshold = 0.05; +static const int MaxTrackers = 100; MultiTracker::MultiTracker(EngineWPtr e) : engine(e) @@ -37,7 +39,9 @@ static std::vector similarity(const PatchPtr p1, const PatchPtr p2) 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::Mat result = Mat::zeros(1,1, CV_64F); + 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); @@ -61,6 +65,14 @@ static std::vector similarity(const PatchPtr p1, const PatchPtr p2) feature.push_back(calc_iou_ratio(getRectInDetection(d1), getRectInDetection(d2))); +// for (auto i : feature){ +// cout << i << "\t"; +// if (isnan(i)){ +// throw overflow_error("Nan in feature"); +// } +// } +// cout << endl; +// return feature; } @@ -74,7 +86,7 @@ double MultiTracker::distance(TrackerPtr tracker, const cv::Mat& image, const De ss = similarity(i, patch); features.insert(features.end(), ss.begin(), ss.end()); } - double prob = predictor->predict(Tracker::MaxPatch - 1, features); // TODO why is MaxPatch-1 + double prob = predictor->predict(tracker->patches.size() - 1, features); // TODO why is MaxPatch-1 if (prob > ProbThreshold) return -log(prob); else @@ -150,7 +162,7 @@ void MultiTracker::update(unsigned int total, const Detection* detections, const for (auto i : unmatch_bbs_indices){ TrackerPtr new_tracker (new Tracker(image, detections[i])); new_tracker->addPatch(createPatch(image, detections[i])); - this->trackers.push_back(new_tracker); + addTracker(new_tracker); Person test; // TODO inPersons.push_back(test); } @@ -212,3 +224,12 @@ PatchPtr MultiTracker::createPatch(const Mat& image, const Detection& detect) patch->features = std::make_pair(feature_hog_double, hist); return patch; } + +void MultiTracker::addTracker(TrackerPtr t) +{ + trackers.insert(trackers.begin(), t); + if(trackers.size() > MaxTrackers){ + LOG_ERROR(TAG, "trackers reaches the maximum " + to_string(MaxTrackers)); + trackers.pop_back(); + } +} diff --git a/src/PredictorWrapper.cpp b/src/PredictorWrapper.cpp index 5559ddc..77d99af 100644 --- a/src/PredictorWrapper.cpp +++ b/src/PredictorWrapper.cpp @@ -29,6 +29,7 @@ boost::python::list toPythonList(const std::vector& v) { ret_val = py::extract(py_ret); \ }catch(boost::python::error_already_set const &){ \ LOG_ERROR(TAG, PyWrapper::parse_python_exception()); \ + throw runtime_error("Python error"); \ } \ @@ -46,9 +47,9 @@ bool PredictorWrapper::load(const string& fname) { LOG_DEBUG(TAG, "load " + fname); py::object func = m_module.attr("init"); - string ret; + bool ret = true; CALL_WITH_EXCEPTION(func(fname.c_str()), ret); - return true; + return ret; } void PredictorWrapper::dump() diff --git a/src/Tracker.cpp b/src/Tracker.cpp index 37595b4..2d4f2b0 100644 --- a/src/Tracker.cpp +++ b/src/Tracker.cpp @@ -50,7 +50,7 @@ void Tracker::addPatch(PatchPtr p) { patches.insert(patches.begin(), p); if (patches.size() > MaxPatch){ - patches.erase(patches.end()); + patches.pop_back(); } } diff --git a/src/Utils.cpp b/src/Utils.cpp index c174e28..df6802b 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -1,10 +1,16 @@ #include "Utils.h" +#include +#include using namespace suanzi; using namespace cv; +using namespace std; double distance_cosine(const Eigen::VectorXd& u, const Eigen::VectorXd& v) { + if (u.dot(u) * v.dot(v) == 0){ + throw overflow_error("zero vector for cosine distance"); + } return (1 - u.dot(v) / std::sqrt(u.dot(u) * v.dot(v))); } diff --git a/src/VideoReader.cpp b/src/VideoReader.cpp index cfba733..3d0e22d 100644 --- a/src/VideoReader.cpp +++ b/src/VideoReader.cpp @@ -49,11 +49,6 @@ UrlReader::~UrlReader() static long count = 0; -static void printImg(const cv::Mat& mat) -{ - std::cout << mat.at(0,0) << std::endl; -} - bool UrlReader::read(cv::Mat& mat) { bool ret = vcap.read(mat); diff --git a/test/TestPredictor.cpp b/test/TestPredictor.cpp index e3add17..0b2a6b6 100644 --- a/test/TestPredictor.cpp +++ b/test/TestPredictor.cpp @@ -1,7 +1,9 @@ #include "gtest/gtest.h" #include "PredictorWrapper.h" +#include using namespace suanzi; +using namespace std; TEST(Predictor, load) { @@ -10,4 +12,21 @@ TEST(Predictor, load) predictor->dump(); std::vector ff (40, 1); double prob = predictor->predict(4, ff); + cout << prob << endl; } + + +// +//TEST(Predictor, predict) +//{ +// PredictorWrapperPtr predictor (new PredictorWrapper("predictor", "../python")); +// predictor->load("../resources/model.pkl"); +// predictor->dump(); +// //std::vector ff (40, 1); +// +// std::vector ff = { 6.9154247185294e-310, 6.91542471861144e-310, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 6.9154247185294e-310, 6.91542471861144e-310, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, -3.0949108904043924e+217, -3.0949108904066356e+217, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 6.91542471837167e-310, 6.9154247185979e-310, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 6.9154247183334e-310, 6.9154247185946e-310, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0 }; +// int index = 4; +// double prob = predictor->predict(index, ff); +// cout << prob << endl; +//} +//