upgrade opencv to 3.3.1, complete patch similarity
[trackerpp.git] / src / Engine.cpp
index 82fed4a..74659a0 100644 (file)
@@ -2,45 +2,52 @@
 #include <thread>
 #include "Engine.h"
 #include "Logger.h"
+#include "PredictorWrapper.h"
 
 using namespace suanzi;
 
-const static std::string TAG = "Engine";
+static const std::string TAG = "Engine";
 
 static std::mutex g_mutex;
-static Engine* g_instance = nullptr;
+static EngineWPtr g_instance;
 
-Engine::Engine()
-{
-    detector = new Detector();
-}
 
-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>();
+    multiTracker = std::make_shared<MultiTracker>();
 }
 
 Engine::~Engine()
 {    
-    delete detector;
-//    delete tracker;
+    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)
 {
-    videoSrc = url;
     reader = VideoReaderFactory::createVideoReader(type, url);
 }
 
@@ -48,9 +55,11 @@ void Engine::run()
 {
     LOG_DEBUG(TAG, "run");
     cv::Mat frame;
+    Detection detections[128];
     while (reader->read(frame)){
         LOG_DEBUG(TAG, "Size: " << frame.cols  <<  "x" << frame.rows);
-        detector->detect(frame);
+        int total = detector->detect(frame, detections);
+        multiTracker->update(total, detections, frame);
     }
 }