X-Git-Url: http://47.100.26.94:8080/?a=blobdiff_plain;f=src%2FEngine.cpp;h=4f671039bd60742e898cebf97c1bec7251280bc5;hb=b8f65122758fbbecdb5574acfbca01fe8303c179;hp=82fed4aea881f49e7f30e1b605a310b193e90e62;hpb=5675c1a74ffcb95725eb11463e51cfebbc88a15e;p=trackerpp.git diff --git a/src/Engine.cpp b/src/Engine.cpp index 82fed4a..4f67103 100644 --- a/src/Engine.cpp +++ b/src/Engine.cpp @@ -1,46 +1,55 @@ #include #include +#include #include "Engine.h" #include "Logger.h" +#include "PredictorWrapper.h" using namespace suanzi; +using namespace std; -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(); -} +typedef std::shared_ptr> PersonsInfoPtr; -Engine* Engine::create() +// class Engine +EnginePtr Engine::create() { LOG_DEBUG(TAG, "create"); std::lock_guard 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(); } 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,15 +57,18 @@ 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); } } void Engine::start() { LOG_DEBUG(TAG, "start"); + multiTracker = std::make_shared(g_instance); if (!reader){ LOG_ERROR(TAG, "reader is null. exit"); return; @@ -69,3 +81,58 @@ void Engine::addObserver(EngineObserver *observer) { 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) +{ + PersonsInfoPtr pp = std::make_shared>(p); + eventThread.enqueue(new PersonOutEventWorkItem(this->observer_list, pp)); +} + +void Engine::onPersonsIn(const std::vector& p) +{ + PersonsInfoPtr pp = std::make_shared>(p); + eventThread.enqueue(new PersonInEventWorkItem(this->observer_list, pp)); +} + +void Engine::onStatusChanged() +{ + Person pm, p2; + std::vector ps; + ps.push_back(pm); + ps.push_back(p2); + onPersonsOut(ps); +}