YoloCrowdApp.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "YoloCrowdApp.h"
  2. #include "ilogger.hpp"
  3. #include "trt_infer.hpp"
  4. NAMESPACE_MAS_BEGIN
  5. NAMESPACE_YOLOCROWD_BEGIN
  6. tzc::Mutex YoloCrowdApp::_mutex;
  7. YoloCrowdApp* YoloCrowdApp::_instance = nullptr;
  8. YoloCrowdApp* YoloCrowdApp::Instance()
  9. {
  10. if (!_instance)
  11. {
  12. _mutex.Lock();
  13. if (!_instance)
  14. {
  15. _instance = new YoloCrowdApp();
  16. TZLogInfo("YoloCrowdApp Instance created~~~");
  17. }
  18. _mutex.Unlock();
  19. }
  20. ++(_instance->m_usecnt);
  21. return _instance;
  22. }
  23. void YoloCrowdApp::DestroyInstance()
  24. {
  25. tzc::ScopedLock lock(_mutex);
  26. if ((_instance->m_usecnt) < 0)
  27. {
  28. TZLogWarn("YoloCrowdApp Instance does not exist!!!");
  29. _instance->m_usecnt = 0;
  30. }
  31. else if ((_instance->m_usecnt) == 0)
  32. {
  33. TZ_delete(_instance);
  34. TZLogInfo("YoloCrowdApp Instance destroyed~~~");
  35. }
  36. else
  37. {
  38. TZLogWarn("YoloCrowdApp still in use by %d objects!", _instance->m_usecnt);
  39. TZ_delete(_instance);
  40. TZLogInfo("YoloCrowdApp Instance destroyed~~~");
  41. }
  42. }
  43. TZ_INT YoloCrowdApp::Initialize(const std::string& initParam)
  44. {
  45. if (m_inited)
  46. {
  47. TZLogInfo("YoloCrowdApp has been initialized~~~");
  48. return MEC_OK;
  49. }
  50. TRT::set_device(m_gpuid);
  51. YoloCrowdBuild param;
  52. YoloCrowdBuild::fromJson(initParam, param);
  53. m_gpuid = param.gpu_id;
  54. m_maxObjects = param.max_objects;
  55. m_confidenceThreshold = param.confidence_threshold;
  56. m_NMSThreshold = param.nms_threshold;
  57. m_modelPath = param.model_path;
  58. if (!iLogger::exists(m_modelPath))
  59. {
  60. TZLogWarn("YoloCrowd model file does not exist!!!");
  61. return MEC_FAILED;
  62. }
  63. m_engine = YoloCrowd::create_infer(
  64. m_modelPath,
  65. YoloCrowd::Type::V5,
  66. m_gpuid,
  67. m_confidenceThreshold,
  68. m_NMSThreshold,
  69. YoloCrowd::NMSMethod::FastGPU,
  70. m_maxObjects,
  71. false
  72. );
  73. if (m_engine == nullptr)
  74. {
  75. TZLogWarn("YoloCrowd Engine is nullptr!!!");
  76. return MEC_NULL_OBJ;
  77. }
  78. // this->warmup();
  79. m_inited = TRUE;
  80. TZLogInfo("YoloCrowdApp Initialized~~~");
  81. return MEC_OK;
  82. }
  83. TZ_INT YoloCrowdApp::Dispose()
  84. {
  85. if (!m_inited)
  86. {
  87. return MEC_NOT_INITED;
  88. }
  89. _mutex.Lock();
  90. --(m_usecnt);
  91. TZLogInfo("YoloCrowdApp usecnt -1, now = %d~~~", m_usecnt);
  92. TZ_BOOL empty = (m_usecnt == 0);
  93. _mutex.Unlock();
  94. if (empty)
  95. {
  96. m_inited = FALSE;
  97. m_engine.reset();
  98. TZLogInfo("YoloCrowdApp Dispose~~~");
  99. this->DestroyInstance();
  100. }
  101. return MEC_OK;
  102. }
  103. TZ_INT YoloCrowdApp::DoDetect(cv::Mat& input,
  104. TargetInfo& targets,
  105. std::vector<int>& objClass,
  106. std::vector<std::vector<float>>& objPos)
  107. {
  108. auto boxes = m_engine->commit(input).get();
  109. for (auto& obj : boxes)
  110. {
  111. if (obj.confidence < targets.target_threshold) continue;
  112. objClass.push_back(obj.class_label);
  113. objPos.push_back({obj.left, obj.top,
  114. obj.right, obj.bottom, obj.confidence});
  115. }
  116. return MEC_OK;
  117. }
  118. YoloCrowdApp::YoloCrowdApp()
  119. : m_inited(FALSE),
  120. m_usecnt(0),
  121. m_gpuid(0),
  122. m_maxObjects(1024),
  123. m_confidenceThreshold(0.10),
  124. m_NMSThreshold(0.45),
  125. m_modelPath(""),
  126. m_engine(nullptr) {}
  127. YoloCrowdApp::~YoloCrowdApp()
  128. {
  129. this->Dispose();
  130. }
  131. void YoloCrowdApp::warmup()
  132. {
  133. auto files = iLogger::find_files(PROGRAM_ROOT_PATH "models/yolo_warmup/", "*.jpg;*.jpeg;*.png");
  134. std::vector<cv::Mat> images;
  135. for (size_t i = 0; i < files.size(); ++i)
  136. {
  137. auto image = cv::imread(files[i]);
  138. images.emplace_back(image);
  139. }
  140. std::vector<std::shared_future<YoloCrowd::BoxArray>> boxes_array;
  141. for (size_t i = 0; i < 10; ++i)
  142. {
  143. boxes_array = m_engine->commits(images);
  144. }
  145. boxes_array.back().get();
  146. boxes_array.clear();
  147. }
  148. NAMESPACE_YOLOCROWD_END
  149. NAMESPACE_MAS_END