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