From 3ff9a5ad691b8dca9d91f8e9786a8d08d31b70fa Mon Sep 17 00:00:00 2001 From: Peng Li Date: Thu, 19 Jul 2018 13:35:42 +0800 Subject: [PATCH] Fix issue boostpython not stopped by Ctrl+c --- .gitignore | 1 + SConstruct | 1 + include/Metrics.h | 1 + include/MultiTracker.h | 7 ++++-- include/PredictorWrapper.h | 4 +-- include/Tracker.h | 6 ++--- main.cpp | 1 - python/predictor.py | 8 ++++-- src/Engine.cpp | 16 ++++++------ src/MultiTracker.cpp | 62 ++++++++++++++++++++++++++++------------------ src/PredictorWrapper.cpp | 40 ++++++++++++++++++++++++------ src/Tracker.cpp | 18 +++++++------- 12 files changed, 106 insertions(+), 59 deletions(-) diff --git a/.gitignore b/.gitignore index 645f639..c7db8d3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ src/*.o *.o .sconsign.dblite +*.pyc diff --git a/SConstruct b/SConstruct index 2a24ade..b2e7560 100644 --- a/SConstruct +++ b/SConstruct @@ -16,6 +16,7 @@ env.Append(LIBS = ['tracker']) env.ParseConfig("pkg-config --libs opencv log4cpp") env.ParseConfig("python-config --cflags --libs") env.Append(LIBS = ['pthread', 'boost_python']) +env['CCFLAGS'].remove('-Wstrict-prototypes') # invalid in C++ env.Append(LIBPATH=['#.']) diff --git a/include/Metrics.h b/include/Metrics.h index 3da8014..ad1a827 100644 --- a/include/Metrics.h +++ b/include/Metrics.h @@ -18,6 +18,7 @@ namespace suanzi { const static long int MaxCost = 100000; const static int MaxPatch = 5; void similarity(const Patch& p1, const Patch& p2); + //double distance() private: diff --git a/include/MultiTracker.h b/include/MultiTracker.h index 40744b8..0452c00 100644 --- a/include/MultiTracker.h +++ b/include/MultiTracker.h @@ -5,6 +5,7 @@ #include "Detector.h" #include "Metrics.h" #include "SharedPtr.h" +#include "PredictorWrapper.h" #include namespace suanzi { @@ -22,11 +23,13 @@ namespace suanzi { MetricsPtr metrics; std::vector trackers; int max_id = 0; - void addTracker(TrackerPtr t); - TrackerPtr createTracker(int id = 0); + //void addTracker(TrackerPtr t); + //TrackerPtr createTracker(int id = 0); void removeTracker(TrackerPtr t); void correctTrackers(MetricsPtr m, cv::Mat& image); void initNewTrackers(cv::Mat& iamge); + PredictorWrapperPtr predictor; + }; diff --git a/include/PredictorWrapper.h b/include/PredictorWrapper.h index fab9497..ac54f73 100644 --- a/include/PredictorWrapper.h +++ b/include/PredictorWrapper.h @@ -15,8 +15,8 @@ namespace suanzi { public: static PredictorWrapperPtr create(const std::string& fname); ~PredictorWrapper(){} - void dump() { this->dump_func(); } - void predict() { this->predict_func();} + void dump(); + double predict(); private: PredictorWrapper(const std::string& fname); diff --git a/include/Tracker.h b/include/Tracker.h index 490201a..bdf45f0 100644 --- a/include/Tracker.h +++ b/include/Tracker.h @@ -22,10 +22,10 @@ namespace suanzi { class Tracker { public: - Tracker(int id); + Tracker(const cv::Mat& image, int id = 0); virtual ~Tracker(); void updateState(const cv::Mat& image); - void addPatch(Patch* p); +// void addPatch(Patch* p); TrackerStatus status; private: @@ -33,7 +33,7 @@ namespace suanzi { int id; int age; int last_active; - std::vector patches; + std::vector patches; cv::KalmanFilter kf = {4,2}; }; diff --git a/main.cpp b/main.cpp index ddf49d6..f1b466b 100644 --- a/main.cpp +++ b/main.cpp @@ -28,6 +28,5 @@ int main(int argc, char* argv[]) e->setVideoSrc(VideoSrcType::URL, "rtsp://192.168.1.75:554/stream1"); e->start(); e->destroy(); - log4cpp::Category::shutdown(); } diff --git a/python/predictor.py b/python/predictor.py index 9f21518..b58fd41 100644 --- a/python/predictor.py +++ b/python/predictor.py @@ -15,9 +15,13 @@ def init(fname = './model.pkl'): def dump(): global predictors + ss = '\n' for i in predictors: - print i - print i.coef_ + ss += str(i.__dict__) + ss += '\n' +# ss += str(i.coef_) +# ss += '\n' + return ss def predict(index, features): diff --git a/src/Engine.cpp b/src/Engine.cpp index 6d44f3c..78af761 100644 --- a/src/Engine.cpp +++ b/src/Engine.cpp @@ -6,7 +6,7 @@ using namespace suanzi; -const static std::string TAG = "Engine"; +static const std::string TAG = "Engine"; static std::mutex g_mutex; static EngineWPtr g_instance; @@ -34,22 +34,22 @@ Engine::Engine() Engine::~Engine() { + destroy(); } void Engine::destroy() { + LOG_DEBUG(TAG, "destroy"); + detector.reset(); + multiTracker.reset(); + reader.reset(); + observer_list.clear(); } void Engine::setVideoSrc(VideoSrcType type, const std::string& url) { - PredictorWrapperPtr pp = PredictorWrapper::create("./python/model.pkl"); - - pp->dump(); - -// videoSrc = url; - //reader = VideoReaderFactory::createVideoReader(type, url); - + reader = VideoReaderFactory::createVideoReader(type, url); } void Engine::run() diff --git a/src/MultiTracker.cpp b/src/MultiTracker.cpp index d0e7048..01e60c5 100644 --- a/src/MultiTracker.cpp +++ b/src/MultiTracker.cpp @@ -2,13 +2,18 @@ #include "Metrics.h" #include #include "hungarian.h" +#include "Logger.h" using namespace suanzi; using namespace cv; using namespace Eigen; +static const std::string TAG = "MultiTracker"; MultiTracker::MultiTracker(MetricsPtr m) : metrics(m) { + LOG_DEBUG(TAG, "init - load model.pkl"); + predictor = PredictorWrapper::create("./python/model.pkl"); + predictor->dump(); } @@ -16,19 +21,19 @@ MultiTracker::~MultiTracker() { trackers.clear(); } - -TrackerPtr MultiTracker::createTracker(int id) -{ - TrackerPtr t (new Tracker(id)); - addTracker(t); - return t; -} - -void MultiTracker::addTracker(TrackerPtr t) -{ - trackers.push_back(t); -} - +// +//TrackerPtr MultiTracker::createTracker(int id) +//{ +// TrackerPtr t (new Tracker(id)); +// addTracker(t); +// return t; +//} +// +//void MultiTracker::addTracker(TrackerPtr t) +//{ +// trackers.push_back(t); +//} +// void MultiTracker::removeTracker(TrackerPtr t) { // trackers.erase(t); @@ -49,6 +54,11 @@ void calculate_edistance() #define MaxCost 100000 +static double distance(TrackerPtr t, const cv::Mat& image, const Detection& d) +{ + return 0.1; +} + void MultiTracker::update(unsigned int total, const Detection* detections, const Mat& image) { // correct_trackers @@ -58,13 +68,9 @@ void MultiTracker::update(unsigned int total, const Detection* detections, const MatrixXi cost_matrix = MatrixXi::Zero(row, col); for (int i = 0; i < row; i++){ for (int j = 0; j < col; j++){ - TrackerPtr tracker = trackers[i]; - Detection det = detections[j]; - - int cost = MaxCost; - // TODO - cost_matrix(i, j) = cost; + // int cost = MaxCost; + cost_matrix(i, j) = distance(trackers[i], image, detections[j]); } } @@ -72,20 +78,28 @@ void MultiTracker::update(unsigned int total, const Detection* detections, const VectorXi tracker_inds, bb_inds; linear_sum_assignment(cost_matrix, tracker_inds, bb_inds); - // handle the result + // handle unmatched trackers vector unmatched_trackers; - vector unmatched_detection; 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(detections[j]); + 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); + } } diff --git a/src/PredictorWrapper.cpp b/src/PredictorWrapper.cpp index 8c5965f..0943e98 100644 --- a/src/PredictorWrapper.cpp +++ b/src/PredictorWrapper.cpp @@ -1,6 +1,8 @@ #include "PredictorWrapper.h" #include +#include "Logger.h" #include +#include namespace py = boost::python; @@ -11,15 +13,14 @@ using namespace suanzi; PredictorWrapperWPtr PredictorWrapper::instance; const static std::string PREDICTOR_PY_DIR = "./python"; +const static std::string TAG = "PredictorWrapper"; static std::string parse_python_exception(); PredictorWrapperPtr PredictorWrapper::create(const std::string& fname) { - //if (instance == nullptr){ if (instance.lock()){ - //instance = new PredictorWrapper(fname); return PredictorWrapperPtr(); } PredictorWrapperPtr ins (new PredictorWrapper(fname)); @@ -27,6 +28,32 @@ PredictorWrapperPtr PredictorWrapper::create(const std::string& fname) return ins; } +void PredictorWrapper::dump() +{ + LOG_DEBUG(TAG, "dump"); + std::string ss = ""; + try{ + py::object ret = this->dump_func(); + ss = py::extract(ret); + } catch (boost::python::error_already_set const &){ + std::string perror_str = parse_python_exception(); + LOG_ERROR(TAG, "Error in Python: " + perror_str) + } + LOG_DEBUG(TAG, ss); +} + +double PredictorWrapper::predict() +{ + LOG_DEBUG(TAG, "predict"); + try{ + this->predict_func(); + } catch (boost::python::error_already_set const &){ + std::string perror_str = parse_python_exception(); + LOG_ERROR(TAG, "Error in Python: " + perror_str) + } + return 0.1; +} + PredictorWrapper::PredictorWrapper(const std::string& fname) { Py_Initialize(); @@ -36,19 +63,16 @@ PredictorWrapper::PredictorWrapper(const std::string& fname) py::exec("import sys", main_namespace); std::string cmd = "sys.path.insert(0, '" + PREDICTOR_PY_DIR + "')"; py::exec(cmd.c_str(), main_namespace); - //py::exec("sys.path.insert(0, '/home/debian/project/tracker/python')", main_namespace); - //py::exec("sys.path.insert(0, './python')", main_namespace); + py::exec("import signal", main_namespace); + py::exec("signal.signal(signal.SIGINT, signal.SIG_DFL)", main_namespace); py::object predictor_mod = py::import("predictor"); py::object predictor_init = predictor_mod.attr("init"); dump_func = predictor_mod.attr("dump"); predict_func = predictor_mod.attr("predict"); - predictor_init(fname.c_str()); - //predictor_dump(); - //py::exec("import predictor", main_namespace); } catch (boost::python::error_already_set const &){ std::string perror_str = parse_python_exception(); - std::cout << "Error in Python: " << perror_str << std::endl; + LOG_ERROR(TAG, "Error in Python: " + perror_str) } } diff --git a/src/Tracker.cpp b/src/Tracker.cpp index 27dd400..50bc820 100644 --- a/src/Tracker.cpp +++ b/src/Tracker.cpp @@ -5,7 +5,7 @@ using namespace cv; static const int MaxLost = 5; -Tracker::Tracker(int id) : id(id) +Tracker::Tracker(const cv::Mat& image,int id) : id(id) { status = TrackerStatus::Fire; preStatus = TrackerStatus::Fire; @@ -31,14 +31,14 @@ Tracker::~Tracker() { } -void Tracker::addPatch(Patch* p) -{ - patches.push_back(p); - if (patches.size() > Metrics::MaxPatch){ - patches.erase(patches.end()); - } -} - +//void Tracker::addPatch(Patch* p) +//{ +// patches.push_back(p); +// if (patches.size() > Metrics::MaxPatch){ +// patches.erase(patches.end()); +// } +//} +// void Tracker::updateState(const Mat& image) { preStatus = this->status; -- 2.11.0