X-Git-Url: http://47.100.26.94:8080/?a=blobdiff_plain;f=src%2FEngine.cpp;h=1b7cca2d3baeae39b20ca7b6167e611fdfbad80c;hb=70532232dd98f31467eff7baaaff6e68f803bb45;hp=78af76113be723e8e33d08d960f466de7f2e4655;hpb=3ff9a5ad691b8dca9d91f8e9786a8d08d31b70fa;p=trackerpp.git diff --git a/src/Engine.cpp b/src/Engine.cpp index 78af761..1b7cca2 100644 --- a/src/Engine.cpp +++ b/src/Engine.cpp @@ -1,16 +1,18 @@ #include #include +#include #include "Engine.h" #include "Logger.h" #include "PredictorWrapper.h" using namespace suanzi; +using namespace std; static const std::string TAG = "Engine"; - static std::mutex g_mutex; static EngineWPtr g_instance; +typedef std::shared_ptr> PersonsInfoPtr; EnginePtr Engine::create() { @@ -18,7 +20,7 @@ EnginePtr Engine::create() std::lock_guard lock(g_mutex); if (g_instance.lock()){ LOG_ERROR(TAG, "already exists"); - return EnginePtr(); // nullptr + return EnginePtr(); } EnginePtr instance (new Engine()); g_instance = instance; @@ -28,8 +30,6 @@ EnginePtr Engine::create() Engine::Engine() { detector = std::make_shared(); - MetricsPtr m (new Metrics("model.pkl")); - multiTracker = std::make_shared(m); } Engine::~Engine() @@ -46,7 +46,6 @@ void Engine::destroy() observer_list.clear(); } - void Engine::setVideoSrc(VideoSrcType type, const std::string& url) { reader = VideoReaderFactory::createVideoReader(type, url); @@ -59,7 +58,6 @@ void Engine::run() 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); } @@ -68,6 +66,7 @@ void Engine::run() void Engine::start() { LOG_DEBUG(TAG, "start"); + multiTracker = std::make_shared(g_instance); if (!reader){ LOG_ERROR(TAG, "reader is null. exit"); return; @@ -78,5 +77,54 @@ void Engine::start() void Engine::addObserver(EngineObserver *observer) { + LOG_DEBUG(TAG, "addObserver"); observer_list.insert(observer); } + + +// WorkItem class for event thread +class PersonInEventWorkItem : public WorkItem +{ +public: + PersonInEventWorkItem(std::set obs, PersonsInfoPtr info) : obs(obs), info(info){ + } + ~PersonInEventWorkItem(){} + void run (){ + for (auto o : obs){ + o->onPersonsIn(*(info.get())); + } + } +private: + std::set obs; + PersonsInfoPtr info; +}; + +class PersonOutEventWorkItem : public WorkItem +{ +public: + PersonOutEventWorkItem(std::set obs, PersonsInfoPtr info) : obs(obs), info(info){ + } + ~PersonOutEventWorkItem(){} + void run (){ + for (auto o : obs){ + o->onPersonsIn(*(info.get())); + } + } +private: + std::set obs; + PersonsInfoPtr info; +}; + +void Engine::onPersonsOut(const std::vector& p) +{ + LOG_DEBUG(TAG, "onPersonOut"); + PersonsInfoPtr pp = std::make_shared>(p); + eventThread.enqueue(new PersonOutEventWorkItem(this->observer_list, pp)); +} + +void Engine::onPersonsIn(const std::vector& p) +{ + LOG_DEBUG(TAG, "onPersonIn"); + PersonsInfoPtr pp = std::make_shared>(p); + eventThread.enqueue(new PersonInEventWorkItem(this->observer_list, pp)); +}