X-Git-Url: http://47.100.26.94:8080/?a=blobdiff_plain;f=src%2FEngine.cpp;h=16a704dd19eb10a5767272c535e8ac6d5533af70;hb=a80807eb35cc0ff70a9afdd6b7db2f38cb686683;hp=e51dd657bf71248b205185f77bc562e012756299;hpb=642ffd9803b9be0439124d1aebe2780fcbabeb07;p=trackerpp.git diff --git a/src/Engine.cpp b/src/Engine.cpp index e51dd65..16a704d 100644 --- a/src/Engine.cpp +++ b/src/Engine.cpp @@ -1,62 +1,74 @@ #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(); - tracker = new Tracker(); -} +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::setVideoSrc(const std::string& url) +void Engine::destroy() { - videoSrc = url; - reader = VideoReaderFactory::createVideoReader(VideoSrcType::URL,"rtsp://192.168.1.75:554/stream1"); + LOG_DEBUG(TAG, "destroy"); + detector.reset(); + multiTracker.reset(); + reader.reset(); + observer_list.clear(); +} + + +void Engine::setVideoSrc(VideoSrcType type, const std::string& url) +{ + reader = VideoReaderFactory::createVideoReader(type, url); } void Engine::run() { LOG_DEBUG(TAG, "run"); cv::Mat frame; + Detection detections[128]; while (reader->read(frame)){ - detector->detect(frame); + LOG_DEBUG(TAG, "Size: " << frame.cols << "x" << frame.rows); + 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,49 @@ 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)); +}