Fix issue boostpython not stopped by Ctrl+c
authorPeng Li <seudut@gmail.com>
Thu, 19 Jul 2018 05:35:42 +0000 (13:35 +0800)
committerPeng Li <seudut@gmail.com>
Thu, 19 Jul 2018 05:35:42 +0000 (13:35 +0800)
12 files changed:
.gitignore
SConstruct
include/Metrics.h
include/MultiTracker.h
include/PredictorWrapper.h
include/Tracker.h
main.cpp
python/predictor.py
src/Engine.cpp
src/MultiTracker.cpp
src/PredictorWrapper.cpp
src/Tracker.cpp

index 645f639..c7db8d3 100644 (file)
@@ -1,3 +1,4 @@
 src/*.o
 *.o
 .sconsign.dblite
+*.pyc
index 2a24ade..b2e7560 100644 (file)
@@ -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=['#.'])
 
index 3da8014..ad1a827 100644 (file)
@@ -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:
index 40744b8..0452c00 100644 (file)
@@ -5,6 +5,7 @@
 #include "Detector.h"
 #include "Metrics.h"
 #include "SharedPtr.h"
+#include "PredictorWrapper.h"
 #include <opencv2/opencv.hpp>
 
 namespace suanzi {
@@ -22,11 +23,13 @@ namespace suanzi {
         MetricsPtr metrics;
         std::vector<TrackerPtr> 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;
+
     };
 
 
index fab9497..ac54f73 100644 (file)
@@ -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);
index 490201a..bdf45f0 100644 (file)
@@ -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<Patch *> patches;
+        std::vector<Patch> patches;
         cv::KalmanFilter kf = {4,2};
     };
 
index ddf49d6..f1b466b 100644 (file)
--- 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();
 }
index 9f21518..b58fd41 100644 (file)
@@ -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):
index 6d44f3c..78af761 100644 (file)
@@ -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()
index d0e7048..01e60c5 100644 (file)
@@ -2,13 +2,18 @@
 #include "Metrics.h"
 #include <algorithm>
 #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<TrackerPtr> unmatched_trackers;
-    vector<Detection> 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<int> 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);
+    }
 }
index 8c5965f..0943e98 100644 (file)
@@ -1,6 +1,8 @@
 #include "PredictorWrapper.h"
 #include <string>
+#include "Logger.h"
 #include <iostream>
+#include <thread>
 
 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<std::string>(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)
     }
 }
 
index 27dd400..50bc820 100644 (file)
@@ -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;