AbandObjDetector.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include "DetUtils.h"
  2. #include "AbandObjDetector.h"
  3. #include "Logger.h"
  4. #include "OSTime.h"
  5. #include "SysUtils.h"
  6. NAMESPACE_MAS_BEGIN
  7. NAMESPACE_ABANDOBJ_BEGIN
  8. AbandObjDetector::AbandObjDetector(const std::string& key,
  9. const std::string& name):
  10. Detector(key, name), m_abandObj(nullptr) {}
  11. AbandObjDetector::~AbandObjDetector()
  12. {
  13. this->Dispose();
  14. }
  15. TZ_INT AbandObjDetector::Initialize(const std::string& initParam)
  16. {
  17. if (m_inited)
  18. {
  19. TZLogInfo("AbandObjDetector has been initialized~~~");
  20. return MEC_OK;
  21. }
  22. m_abandObj = AbandObjApp::Instance();
  23. m_abandObj->Initialize(initParam);
  24. this->Start();
  25. m_inited = TRUE;
  26. TZLogInfo("AbandObjDetector initialized~~~");
  27. return MEC_OK;
  28. }
  29. TZ_INT AbandObjDetector::Dispose()
  30. {
  31. if (!m_inited)
  32. {
  33. return MEC_NOT_INITED;
  34. }
  35. this->StopAndWait();
  36. m_inited = FALSE;
  37. return MEC_OK;
  38. }
  39. TZ_INT AbandObjDetector::TurnOnGPU()
  40. {
  41. m_useGPU = TRUE;
  42. return MEC_OK;
  43. }
  44. TZ_INT AbandObjDetector::TurnOffGPU()
  45. {
  46. m_useGPU = FALSE;
  47. return MEC_OK;
  48. }
  49. TZ_INT AbandObjDetector::DoDetect(
  50. SPtr<StreamInfo>& media,
  51. fDetCallback callback, void* ctx)
  52. {
  53. if (!m_inited || !callback || !media)
  54. {
  55. TZLogWarn("detector state is not active!!!");
  56. return MEC_FAILED;
  57. }
  58. if (m_sema.Count() > 3000)
  59. {
  60. TZLogInfo("sema is more than 3000~~~");
  61. return MEC_FAILED;
  62. }
  63. m_cntLock.Lock();
  64. ++m_detCount;
  65. if (m_detCount != m_detSample)
  66. {
  67. m_cntLock.Unlock();
  68. this->skipFrame(media, callback, ctx);
  69. return MEC_OK;
  70. }
  71. m_detCount = 0;
  72. m_cntLock.Unlock();
  73. m_listLock.Lock();
  74. m_waitList.push_back({media, callback, ctx});
  75. if (m_waitList.size() > MAX_WAIT_LIST_LEN)
  76. {
  77. TZLogInfo("m_waitList.size = %d", m_waitList.size());
  78. }
  79. m_listLock.Unlock();
  80. m_sema.Signal();
  81. return MEC_OK;
  82. }
  83. TZ_INT AbandObjDetector::SetDetectCfg(const std::string& conf)
  84. {
  85. if (!m_inited) return MEC_NOT_INITED;
  86. AbandObjCfg cfgParam;
  87. AbandObjCfg::fromJson(conf, m_cfgParam);
  88. m_detSample = detutils::GetDetSample(cfgParam.freq);
  89. m_focusArea = cfgParam.focusArea;
  90. m_ignoreArea = cfgParam.ignoreArea;
  91. TZ_INT setCfgRes;
  92. setCfgRes = m_abandObj->SetDetectCfg(conf);
  93. if(setCfgRes != MEC_OK) return MEC_FAILED;
  94. return MEC_OK;
  95. }
  96. void AbandObjDetector::Entry()
  97. {
  98. while (!this->IsStop())
  99. {
  100. if (!m_sema.Wait(ABANDOBJ_WAIT_MSECOND)) continue;
  101. m_listLock.Lock();
  102. TZ_INT skipCnt = m_waitList.size() - MAX_WAIT_LIST_LEN;
  103. if (skipCnt > 0)
  104. {
  105. TZLogInfo("m_waitList.size=%d, will skip %d frames", m_waitList.size(), skipCnt);
  106. }
  107. else
  108. {
  109. skipCnt = 0;
  110. }
  111. m_listLock.Unlock();
  112. while (skipCnt > 0)
  113. {
  114. m_listLock.Lock();
  115. auto front = m_waitList.front();
  116. m_waitList.pop_front();
  117. m_listLock.Unlock();
  118. SPtr<StreamInfo> media = std::get<0>(front);
  119. fDetCallback callback = std::get<1>(front);
  120. void* ctx = std::get<2>(front);
  121. this->skipFrame(media, callback, ctx);
  122. --skipCnt;
  123. }
  124. m_listLock.Lock();
  125. if (m_waitList.empty())
  126. {
  127. m_listLock.Unlock();
  128. continue;
  129. }
  130. auto front = m_waitList.front();
  131. m_waitList.pop_front();
  132. m_listLock.Unlock();
  133. SPtr<StreamInfo> media = std::get<0>(front);
  134. fDetCallback callback = std::get<1>(front);
  135. void* ctx = std::get<2>(front);
  136. SPtr<Media> data = media->GetMediaRsc();
  137. if (!data || !data->Mem)
  138. {
  139. TZLogWarn("MediaRsc is nullptr!!!");
  140. this->skipFrame(media, callback, ctx);
  141. continue;
  142. }
  143. cv::Mat image(cv::Mat(data->Height, data->Width, CV_8UC3, data->Mem));
  144. std::vector<Proposal> propRes;
  145. m_abandObj->DoDetect(image, propRes);
  146. AbandObjDetectResult detRes;
  147. for (size_t i = 0; i < propRes.size(); ++i)
  148. {
  149. Proposal tmpProp;
  150. tmpProp.x = propRes[i].x;
  151. tmpProp.y = propRes[i].y;
  152. tmpProp.w = propRes[i].w;
  153. tmpProp.h = propRes[i].h;
  154. tmpProp.life = propRes[i].life;
  155. tmpProp.status = propRes[i].status;
  156. if (detutils::IsInRoi(tmpProp.x / data->Width,
  157. tmpProp.y / data->Height,
  158. (tmpProp.x + tmpProp.w) / data->Width,
  159. (tmpProp.y + tmpProp.h) / data->Height,
  160. m_focusArea, m_ignoreArea, m_roiThreshold))
  161. {
  162. detRes.proposals.push_back(tmpProp);
  163. }
  164. }
  165. m_rstLock.Lock();
  166. m_cacheJson = detRes.toJson();
  167. m_cacheDraw.clear();
  168. this->draw(detRes, m_cacheDraw, data->Height, data->Width);
  169. media->AddDetRst(m_key, m_cacheJson, data, m_cacheDraw);
  170. m_rstLock.Unlock();
  171. callback(media, ctx);
  172. }
  173. }
  174. void AbandObjDetector::skipFrame(
  175. SPtr<StreamInfo> & media,
  176. fDetCallback callback,
  177. void * ctx)
  178. {
  179. m_rstLock.Lock();
  180. media->AddDetRst(m_key, m_cacheJson, media->GetMediaRsc(), m_cacheDraw);
  181. m_rstLock.Unlock();
  182. callback(media, ctx);
  183. }
  184. void AbandObjDetector::draw(
  185. AbandObjDetectResult& AbandObjRst, DrawInfo& draw,
  186. TZ_INT height, TZ_INT width)
  187. {
  188. TextInfo text;
  189. for (auto prop : AbandObjRst.proposals) {
  190. draw.Rects.emplace_back(
  191. prop.x / width,
  192. prop.y / height,
  193. (prop.x + prop.w) / width,
  194. (prop.y + prop.h) / height,
  195. "#00FF00",
  196. 2,
  197. text
  198. );
  199. }
  200. }
  201. NAMESPACE_ABANDOBJ_END
  202. NAMESPACE_MAS_END