unify the interface with detector
[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     Detection detections[128];
54     while (reader->read(frame)){
55         LOG_DEBUG(TAG, "Size: " << frame.cols  <<  "x" << frame.rows);
56         // TODO
57         int total = detector->detect(frame, detections);
58         multiTracker->update(total, detections, frame);
59     }
60 }
61
62 void Engine::start()
63 {
64     LOG_DEBUG(TAG, "start");
65     if (!reader){
66         LOG_ERROR(TAG, "reader is null. exit");
67         return;
68     }
69     std::thread t(&Engine::run, this);
70     t.join();
71 }
72
73 void Engine::addObserver(EngineObserver *observer)
74 {
75     observer_list.insert(observer);
76 }