re-locate config file
[trackerpp.git] / src / Engine.cpp
index 7b00ce6..49a17da 100644 (file)
@@ -1,51 +1,73 @@
+#include <mutex>
+#include <thread>
 #include "Engine.h"
 #include "Logger.h"
-#include <mutex>
 
 using namespace suanzi;
 
 const static std::string TAG = "Engine";
 
 static std::mutex g_mutex;
-static Engine* g_instance = nullptr;
+static EngineWPtr g_instance;
 
-Engine::Engine()
-{
-}
 
-Engine* Engine::create()
+EnginePtr Engine::create()
 {
     LOG_DEBUG(TAG, "create");
     std::lock_guard<std::mutex> lock(g_mutex);
-    if (g_instance)
-        return g_instance;
-    
-    Engine* instance (new Engine());
+    if (g_instance.lock()){
+        LOG_ERROR(TAG, "already exists");
+        return EnginePtr(); // nullptr
+    }
+    EnginePtr instance (new Engine());
     g_instance = instance;
-    return g_instance;
+    return instance;
 }
 
-void Engine::destroy()
+Engine::Engine()
 {
-    delete g_instance;
+    detector = std::make_shared<Detector>();
+    MetricsPtr m (new Metrics("model.pkl"));
+    multiTracker = std::make_shared<MultiTracker>(m);
 }
 
 Engine::~Engine()
+{    
+}
+
+void Engine::destroy()
+{
+}
+
+
+void Engine::setVideoSrc(VideoSrcType type, const std::string& url)
 {
+//    videoSrc = url;
+    reader = VideoReaderFactory::createVideoReader(type, url);
 }
 
-void Engine::setVideoSrc(const std::string& url)
+void Engine::run()
 {
-    videoSrc = url;
+    LOG_DEBUG(TAG, "run");
+    cv::Mat frame;
+    Detection detections[128];
+    while (reader->read(frame)){
+        LOG_DEBUG(TAG, "Size: " << frame.cols  <<  "x" << frame.rows);
+        // TODO
+        int total = detector->detect(frame, detections);
+        multiTracker->update(total, detections, frame);
+    }
 }
 
 void Engine::start()
 {
     LOG_DEBUG(TAG, "start");
-    Person p;
-    for(auto& o: observer_list){
-        o->onPersonIn(p);
+    if (!reader){
+        LOG_ERROR(TAG, "reader is null. exit");
+        return;
     }
+    std::thread t(&Engine::run, this);
+    t.join();
 }
 
 void Engine::addObserver(EngineObserver *observer)