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;
18 EnginePtr Engine::create()
20 LOG_DEBUG(TAG, "create");
21 std::lock_guard<std::mutex> lock(g_mutex);
22 if (g_instance.lock()){
23 LOG_ERROR(TAG, "already exists");
24 return EnginePtr(); // nullptr
26 EnginePtr instance (new Engine());
27 g_instance = instance;
33 detector = std::make_shared<Detector>();
41 void Engine::destroy()
43 LOG_DEBUG(TAG, "destroy");
47 observer_list.clear();
51 void Engine::setVideoSrc(VideoSrcType type, const std::string& url)
53 reader = VideoReaderFactory::createVideoReader(type, url);
58 LOG_DEBUG(TAG, "run");
60 Detection detections[128];
61 while (reader->read(frame)){
62 LOG_DEBUG(TAG, "Size: " << frame.cols << "x" << frame.rows);
63 int total = detector->detect(frame, detections);
64 multiTracker->update(total, detections, frame);
70 LOG_DEBUG(TAG, "start");
71 multiTracker = std::make_shared<MultiTracker>(g_instance);
73 LOG_ERROR(TAG, "reader is null. exit");
76 std::thread t(&Engine::run, this);
80 void Engine::addObserver(EngineObserver *observer)
82 observer_list.insert(observer);
86 // WorkItem class for event thread
87 class PersonInEventWorkItem : public WorkItem
90 PersonInEventWorkItem(std::set<EngineObserver*> obs, PersonsInfoPtr info) : obs(obs), info(info){
92 ~PersonInEventWorkItem(){}
95 o->onPersonsIn(*(info.get()));
99 std::set<EngineObserver*> obs;
103 class PersonOutEventWorkItem : public WorkItem
106 PersonOutEventWorkItem(std::set<EngineObserver*> obs, PersonsInfoPtr info) : obs(obs), info(info){
108 ~PersonOutEventWorkItem(){}
111 o->onPersonsIn(*(info.get()));
115 std::set<EngineObserver*> obs;
119 void Engine::onPersonsOut(const std::vector<Person>& p)
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 PersonsInfoPtr pp = std::make_shared<std::vector<Person>>(p);
128 eventThread.enqueue(new PersonInEventWorkItem(this->observer_list, pp));
131 void Engine::onStatusChanged()
134 std::vector<Person> ps;