YoloCrowdDetector.cpp 5.1 KB

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