3 #include <condition_variable>
6 #include "PredictorWrapper.h"
8 using namespace suanzi;
11 static const std::string TAG = "Engine";
12 static std::mutex g_mutex;
13 static EngineWPtr g_instance;
15 typedef std::shared_ptr<std::vector<Person>> PersonsInfoPtr;
17 EnginePtr Engine::create()
19 LOG_DEBUG(TAG, "create");
20 std::lock_guard<std::mutex> lock(g_mutex);
21 if (g_instance.lock()){
22 LOG_ERROR(TAG, "already exists");
25 EnginePtr instance (new Engine());
26 g_instance = instance;
32 detector = DetectorPtr(new Detector());
40 void Engine::destroy()
42 LOG_DEBUG(TAG, "destroy");
46 observer_list.clear();
49 void Engine::setVideoSrc(VideoSrcType type, const std::string& url)
51 reader = VideoReaderFactory::createVideoReader(type, url);
56 LOG_DEBUG(TAG, "run");
58 Detection detections[128];
59 while (reader->read(frame)){
60 LOG_DEBUG(TAG, "Size: " << frame.cols << "x" << frame.rows);
61 int total = detector->detect(frame, detections);
62 multiTracker->update(total, detections, frame);
68 LOG_DEBUG(TAG, "start");
69 multiTracker = std::make_shared<MultiTracker>(g_instance);
71 LOG_ERROR(TAG, "reader is null. exit");
74 std::thread t(&Engine::run, this);
78 void Engine::addObserver(EngineObserver *observer)
80 LOG_DEBUG(TAG, "addObserver");
81 observer_list.insert(observer);
85 // WorkItem class for event thread
86 class PersonInEventWorkItem : public WorkItem
89 PersonInEventWorkItem(std::set<EngineObserver*> obs, PersonsInfoPtr info) : obs(obs), info(info){
91 ~PersonInEventWorkItem(){}
94 o->onPersonsIn(*(info.get()));
98 std::set<EngineObserver*> obs;
102 class PersonOutEventWorkItem : public WorkItem
105 PersonOutEventWorkItem(std::set<EngineObserver*> obs, PersonsInfoPtr info) : obs(obs), info(info){
107 ~PersonOutEventWorkItem(){}
110 o->onPersonsIn(*(info.get()));
114 std::set<EngineObserver*> obs;
118 void Engine::onPersonsOut(const std::vector<Person>& p)
120 LOG_DEBUG(TAG, "onPersonOut");
121 PersonsInfoPtr pp = std::make_shared<std::vector<Person>>(p);
122 eventThread.enqueue(new PersonOutEventWorkItem(this->observer_list, pp));
125 void Engine::onPersonsIn(const std::vector<Person>& p)
127 LOG_DEBUG(TAG, "onPersonIn");
128 PersonsInfoPtr pp = std::make_shared<std::vector<Person>>(p);
129 eventThread.enqueue(new PersonInEventWorkItem(this->observer_list, pp));