Add boost python, and predictor wrapper
[trackerpp.git] / src / Engine.cpp
1 #include <mutex>
2 #include <thread>
3 #include "Engine.h"
4 #include "Logger.h"
5 #include "PredictorWrapper.h"
6
7 using namespace suanzi;
8
9 const static std::string TAG = "Engine";
10
11 static std::mutex g_mutex;
12 static EngineWPtr g_instance;
13
14
15 EnginePtr Engine::create()
16 {
17     LOG_DEBUG(TAG, "create");
18     std::lock_guard<std::mutex> lock(g_mutex);
19     if (g_instance.lock()){
20         LOG_ERROR(TAG, "already exists");
21         return EnginePtr(); // nullptr
22     }
23     EnginePtr instance (new Engine());
24     g_instance = instance;
25     return instance;
26 }
27
28 Engine::Engine()
29 {
30     detector = std::make_shared<Detector>();
31     MetricsPtr m (new Metrics("model.pkl"));
32     multiTracker = std::make_shared<MultiTracker>(m);
33 }
34
35 Engine::~Engine()
36 {    
37 }
38
39 void Engine::destroy()
40 {
41 }
42
43
44 void Engine::setVideoSrc(VideoSrcType type, const std::string& url)
45 {
46     PredictorWrapperPtr pp = PredictorWrapper::create("./python/model.pkl");
47
48     pp->dump();
49
50 //    videoSrc = url;
51     //reader = VideoReaderFactory::createVideoReader(type, url);
52
53 }
54
55 void Engine::run()
56 {
57     LOG_DEBUG(TAG, "run");
58     cv::Mat frame;
59     Detection detections[128];
60     while (reader->read(frame)){
61         LOG_DEBUG(TAG, "Size: " << frame.cols  <<  "x" << frame.rows);
62         // TODO
63         int total = detector->detect(frame, detections);
64         multiTracker->update(total, detections, frame);
65     }
66 }
67
68 void Engine::start()
69 {
70     LOG_DEBUG(TAG, "start");
71     if (!reader){
72         LOG_ERROR(TAG, "reader is null. exit");
73         return;
74     }
75     std::thread t(&Engine::run, this);
76     t.join();
77 }
78
79 void Engine::addObserver(EngineObserver *observer)
80 {
81     observer_list.insert(observer);
82 }