ClipDetector.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "DetUtils.h"
  2. #include "ClipDetector.h"
  3. #include "Logger.h"
  4. #include "OSTime.h"
  5. #include "SysUtils.h"
  6. NAMESPACE_MAS_BEGIN
  7. NAMESPACE_CLIP_BEGIN
  8. ClipDetector::ClipDetector(const std::string& key,
  9. const std::string& name):
  10. Detector(key, name), m_clip(nullptr) {}
  11. ClipDetector::~ClipDetector()
  12. {
  13. this->Dispose();
  14. }
  15. TZ_INT ClipDetector::Initialize(const std::string& initParam)
  16. {
  17. if (m_inited)
  18. {
  19. TZLogInfo("ClipDetector has been initialized~~~");
  20. return MEC_OK;
  21. }
  22. m_clip = ClipApp::Instance();
  23. m_clip->Initialize(initParam);
  24. this->Start();
  25. m_inited = TRUE;
  26. TZLogInfo("ClipDetector initialized~~~");
  27. return MEC_OK;
  28. }
  29. TZ_INT ClipDetector::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 ClipDetector::TurnOnGPU()
  40. {
  41. m_useGPU = TRUE;
  42. return MEC_OK;
  43. }
  44. TZ_INT ClipDetector::TurnOffGPU()
  45. {
  46. m_useGPU = FALSE;
  47. return MEC_OK;
  48. }
  49. TZ_INT ClipDetector::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 ClipDetector::SetDetectCfg(const std::string& conf)
  84. {
  85. if (!m_inited) return MEC_NOT_INITED;
  86. ClipCfg cfgParam;
  87. ClipCfg::fromJson(conf, cfgParam);
  88. m_detSample = detutils::GetDetSample(cfgParam.freq);
  89. return MEC_OK;
  90. }
  91. void ClipDetector::Entry()
  92. {
  93. while (!this->IsStop())
  94. {
  95. if (!m_sema.Wait(CLIP_WAIT_MSECOND)) continue;
  96. m_listLock.Lock();
  97. TZ_INT skipCnt = m_waitList.size() - MAX_WAIT_LIST_LEN;
  98. if (skipCnt > 0)
  99. {
  100. TZLogInfo("m_waitList.size=%d, will skip %d frames", m_waitList.size(), skipCnt);
  101. }
  102. else
  103. {
  104. skipCnt = 0;
  105. }
  106. m_listLock.Unlock();
  107. while (skipCnt > 0)
  108. {
  109. m_listLock.Lock();
  110. auto front = m_waitList.front();
  111. m_waitList.pop_front();
  112. m_listLock.Unlock();
  113. SPtr<StreamInfo> media = std::get<0>(front);
  114. fDetCallback callback = std::get<1>(front);
  115. void* ctx = std::get<2>(front);
  116. this->skipFrame(media, callback, ctx);
  117. --skipCnt;
  118. }
  119. m_listLock.Lock();
  120. if (m_waitList.empty())
  121. {
  122. m_listLock.Unlock();
  123. continue;
  124. }
  125. auto front = m_waitList.front();
  126. m_waitList.pop_front();
  127. m_listLock.Unlock();
  128. SPtr<StreamInfo> media = std::get<0>(front);
  129. fDetCallback callback = std::get<1>(front);
  130. void* ctx = std::get<2>(front);
  131. SPtr<Media> data = media->GetMediaRsc();
  132. if (!data || !data->Mem)
  133. {
  134. TZLogWarn("MediaRsc is nullptr!!!");
  135. this->skipFrame(media, callback, ctx);
  136. continue;
  137. }
  138. cv::Mat image(cv::Mat(data->Height, data->Width, CV_8UC3, data->Mem));
  139. auto & detMap = media->GetAllDetRst();
  140. if (detMap.count("AbandObj"))
  141. {
  142. m_abandObjLock.Lock();
  143. m_qAbandObjQueue.push(detMap["AbandObj"]->Result);
  144. m_abandObjLock.Unlock();
  145. }
  146. ClipDetectResult detRes;
  147. if (!m_qAbandObjQueue.empty())
  148. {
  149. m_abandObjLock.Lock();
  150. std::string jAbandObjRst = m_qAbandObjQueue.front();
  151. m_qAbandObjQueue.pop();
  152. m_abandObjLock.Unlock();
  153. abandobj::AbandObjDetectResult abandObjRst;
  154. abandobj::AbandObjDetectResult::fromJson(jAbandObjRst, abandObjRst);
  155. std::vector<abandobj::Proposal> abandObjProposals = abandObjRst.proposals;
  156. if (!abandObjProposals.empty())
  157. {
  158. for (const auto& proposal : abandObjProposals)
  159. {
  160. AbandObjBox box;
  161. box.x = proposal.x;
  162. box.y = proposal.y;
  163. box.w = proposal.w;
  164. box.h = proposal.h;
  165. detRes.boxes.push_back(box);
  166. }
  167. cropImageByProposals(image, abandObjProposals);
  168. m_clip->DoDetect(image, detRes);
  169. }
  170. }
  171. m_rstLock.Lock();
  172. m_cacheJson = detRes.toJson();
  173. m_cacheDraw.clear();
  174. media->AddDetRst(m_key, m_cacheJson, data, m_cacheDraw);
  175. m_rstLock.Unlock();
  176. callback(media, ctx);
  177. }
  178. }
  179. void ClipDetector::skipFrame(
  180. SPtr<StreamInfo> & media,
  181. fDetCallback callback,
  182. void * ctx)
  183. {
  184. m_rstLock.Lock();
  185. media->AddDetRst(m_key, m_cacheJson, media->GetMediaRsc(), m_cacheDraw);
  186. m_rstLock.Unlock();
  187. callback(media, ctx);
  188. }
  189. void ClipDetector::cropImageByProposals(const cv::Mat& image, const std::vector<abandobj::Proposal>& proposals)
  190. {
  191. for (const auto& proposal : proposals)
  192. {
  193. // 根据 proposal 获取裁切的矩形区域
  194. int x = static_cast<int>(proposal.x);
  195. int y = static_cast<int>(proposal.y);
  196. int w = static_cast<int>(proposal.w);
  197. int h = static_cast<int>(proposal.h);
  198. // 确保裁切区域在图像范围内
  199. x = std::max(0, std::min(x, image.cols - 1));
  200. y = std::max(0, std::min(y, image.rows - 1));
  201. w = std::max(0, std::min(w, image.cols - x));
  202. h = std::max(0, std::min(h, image.rows - y));
  203. // 裁切图像
  204. cv::Rect roi(x, y, w, h);
  205. }
  206. }
  207. NAMESPACE_CLIP_END
  208. NAMESPACE_MAS_END