123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- #include "DetUtils.h"
- #include "AbandObjDetector.h"
- #include "Logger.h"
- #include "OSTime.h"
- #include "SysUtils.h"
- NAMESPACE_MAS_BEGIN
- NAMESPACE_ABANDOBJ_BEGIN
- AbandObjDetector::AbandObjDetector(const std::string& key,
- const std::string& name):
- Detector(key, name), m_abandObj(nullptr) {}
- AbandObjDetector::~AbandObjDetector()
- {
- this->Dispose();
- }
- TZ_INT AbandObjDetector::Initialize(const std::string& initParam)
- {
- if (m_inited)
- {
- TZLogInfo("AbandObjDetector has been initialized~~~");
- return MEC_OK;
- }
- m_abandObj = AbandObjApp::Instance();
- m_abandObj->Initialize(initParam);
- this->Start();
- m_inited = TRUE;
- TZLogInfo("AbandObjDetector initialized~~~");
- return MEC_OK;
- }
- TZ_INT AbandObjDetector::Dispose()
- {
- if (!m_inited)
- {
- return MEC_NOT_INITED;
- }
- this->StopAndWait();
- m_inited = FALSE;
- return MEC_OK;
- }
- TZ_INT AbandObjDetector::TurnOnGPU()
- {
- m_useGPU = TRUE;
- return MEC_OK;
- }
- TZ_INT AbandObjDetector::TurnOffGPU()
- {
- m_useGPU = FALSE;
- return MEC_OK;
- }
- TZ_INT AbandObjDetector::DoDetect(
- SPtr<StreamInfo>& media,
- fDetCallback callback, void* ctx)
- {
- if (!m_inited || !callback || !media)
- {
- TZLogWarn("detector state is not active!!!");
- return MEC_FAILED;
- }
- if (m_sema.Count() > 3000)
- {
- TZLogInfo("sema is more than 3000~~~");
- return MEC_FAILED;
- }
- m_cntLock.Lock();
- ++m_detCount;
- if (m_detCount != m_detSample)
- {
- m_cntLock.Unlock();
- this->skipFrame(media, callback, ctx);
- return MEC_OK;
- }
- m_detCount = 0;
- m_cntLock.Unlock();
- m_listLock.Lock();
- m_waitList.push_back({media, callback, ctx});
- if (m_waitList.size() > MAX_WAIT_LIST_LEN)
- {
- TZLogInfo("m_waitList.size = %d", m_waitList.size());
- }
- m_listLock.Unlock();
- m_sema.Signal();
- return MEC_OK;
- }
- TZ_INT AbandObjDetector::SetDetectCfg(const std::string& conf)
- {
- if (!m_inited) return MEC_NOT_INITED;
- AbandObjCfg cfgParam;
- AbandObjCfg::fromJson(conf, m_cfgParam);
- m_detSample = detutils::GetDetSample(cfgParam.freq);
- m_focusArea = cfgParam.focusArea;
- m_ignoreArea = cfgParam.ignoreArea;
- TZ_INT setCfgRes;
- setCfgRes = m_abandObj->SetDetectCfg(conf);
- if(setCfgRes != MEC_OK) return MEC_FAILED;
- return MEC_OK;
- }
- void AbandObjDetector::Entry()
- {
- while (!this->IsStop())
- {
- if (!m_sema.Wait(ABANDOBJ_WAIT_MSECOND)) continue;
- m_listLock.Lock();
- TZ_INT skipCnt = m_waitList.size() - MAX_WAIT_LIST_LEN;
- if (skipCnt > 0)
- {
- TZLogInfo("m_waitList.size=%d, will skip %d frames", m_waitList.size(), skipCnt);
- }
- else
- {
- skipCnt = 0;
- }
-
- m_listLock.Unlock();
- while (skipCnt > 0)
- {
- m_listLock.Lock();
- auto front = m_waitList.front();
- m_waitList.pop_front();
- m_listLock.Unlock();
- SPtr<StreamInfo> media = std::get<0>(front);
- fDetCallback callback = std::get<1>(front);
- void* ctx = std::get<2>(front);
- this->skipFrame(media, callback, ctx);
- --skipCnt;
- }
- m_listLock.Lock();
- if (m_waitList.empty())
- {
- m_listLock.Unlock();
- continue;
- }
- auto front = m_waitList.front();
- m_waitList.pop_front();
- m_listLock.Unlock();
- SPtr<StreamInfo> media = std::get<0>(front);
- fDetCallback callback = std::get<1>(front);
- void* ctx = std::get<2>(front);
- SPtr<Media> data = media->GetMediaRsc();
- if (!data || !data->Mem)
- {
- TZLogWarn("MediaRsc is nullptr!!!");
- this->skipFrame(media, callback, ctx);
- continue;
- }
- cv::Mat image(cv::Mat(data->Height, data->Width, CV_8UC3, data->Mem));
-
- std::vector<Proposal> propRes;
- m_abandObj->DoDetect(image, propRes);
- AbandObjDetectResult detRes;
- for (size_t i = 0; i < propRes.size(); ++i)
- {
- Proposal tmpProp;
- tmpProp.x = propRes[i].x;
- tmpProp.y = propRes[i].y;
- tmpProp.w = propRes[i].w;
- tmpProp.h = propRes[i].h;
- tmpProp.life = propRes[i].life;
- tmpProp.status = propRes[i].status;
- if (detutils::IsInRoi(tmpProp.x / data->Width,
- tmpProp.y / data->Height,
- (tmpProp.x + tmpProp.w) / data->Width,
- (tmpProp.y + tmpProp.h) / data->Height,
- m_focusArea, m_ignoreArea, m_roiThreshold))
- {
- detRes.proposals.push_back(tmpProp);
- }
- }
- m_rstLock.Lock();
- m_cacheJson = detRes.toJson();
- m_cacheDraw.clear();
- this->draw(detRes, m_cacheDraw, data->Height, data->Width);
- media->AddDetRst(m_key, m_cacheJson, data, m_cacheDraw);
- m_rstLock.Unlock();
- callback(media, ctx);
- }
- }
- void AbandObjDetector::skipFrame(
- SPtr<StreamInfo> & media,
- fDetCallback callback,
- void * ctx)
- {
- m_rstLock.Lock();
- media->AddDetRst(m_key, m_cacheJson, media->GetMediaRsc(), m_cacheDraw);
- m_rstLock.Unlock();
- callback(media, ctx);
- }
- void AbandObjDetector::draw(
- AbandObjDetectResult& AbandObjRst, DrawInfo& draw,
- TZ_INT height, TZ_INT width)
- {
- TextInfo text;
- for (auto prop : AbandObjRst.proposals) {
- draw.Rects.emplace_back(
- prop.x / width,
- prop.y / height,
- (prop.x + prop.w) / width,
- (prop.y + prop.h) / height,
- "#00FF00",
- 2,
- text
- );
- }
- }
- NAMESPACE_ABANDOBJ_END
- NAMESPACE_MAS_END
|