OnlyDetImgDemo.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include <fstream>
  2. #include <opencv2/opencv.hpp>
  3. // libcommon
  4. #include "Logger.h"
  5. #include "OSTime.h"
  6. #include "SysUtils.h"
  7. // libmascommon
  8. #include "MemPool.h"
  9. // deps
  10. #include "DetUtils.h"
  11. #include "ilogger.hpp"
  12. #include "trt_infer.hpp"
  13. // libyolocrowddetector
  14. #include "DetectorAPI.h"
  15. #include "YoloCrowdDetector.h"
  16. TZ_INT DetectionCallback(SPtr<masd::StreamInfo>& media, void* ctx)
  17. {
  18. TZLogInfo("Detection callback triggered!~~~");
  19. if (ctx == nullptr)
  20. {
  21. TZLogError("Error: Invalid image pointer!!!");
  22. return -1;
  23. }
  24. cv::Mat* img = reinterpret_cast<cv::Mat*>(ctx);
  25. if (img->empty())
  26. {
  27. TZLogError("Error: Invalid image pointer!!!");
  28. return -1;
  29. }
  30. auto allDetRst = media->GetAllDetRst();
  31. int imgWidth = img->cols, imgHeight = img->rows;
  32. for (auto it = allDetRst.begin(); it != allDetRst.end(); ++it)
  33. {
  34. const std::string& detKey = it->first;
  35. const SPtr<masd::DetProducing>& detProducing = it->second;
  36. TZLogInfo("Detection Key: %s~~~", detKey.c_str());
  37. std::cout << "Detection Result: " << detProducing->Result << std::endl;
  38. if (!detProducing->Draw.Rects.empty())
  39. {
  40. TZLogInfo("Processing Draw Info...~~~");
  41. for (const auto& rect : detProducing->Draw.Rects)
  42. {
  43. TZLogInfo("Rect: LTX: %.2f, LTY: %.2f, "
  44. "RBX: %.2f, RBY: %.2f, Color: %s, Thickness: %d~~~",
  45. rect.LTX, rect.LTY, rect.RBX, rect.RBY,
  46. rect.Color.c_str(), rect.Thickness);
  47. if (!rect.Text.Text.empty())
  48. {
  49. TZLogInfo("Text: %s~~~", rect.Text.Text.c_str());
  50. }
  51. cv::Scalar color;
  52. {
  53. std::stringstream colorStream(rect.Color);
  54. int r, g, b;
  55. char comma;
  56. colorStream >> r >> comma >> g >> comma >> b;
  57. color = cv::Scalar(b, g, r);
  58. }
  59. cv::Point topLeft(rect.LTX * imgWidth, rect.LTY * imgHeight);
  60. cv::Point bottomRight(rect.RBX * imgWidth, rect.RBY * imgHeight);
  61. cv::rectangle(*img, topLeft, bottomRight, color, rect.Thickness);
  62. }
  63. }
  64. else
  65. {
  66. TZLogInfo("No Draw Info available.~~~");
  67. }
  68. if (detProducing->DetMedia)
  69. {
  70. const auto& media = detProducing->DetMedia;
  71. TZLogInfo("Media Length: %d~~~", media->Length);
  72. TZLogInfo("Media DataType: %d~~~", media->DataType);
  73. TZLogInfo("Media Height: %d, Width: %d~~~", media->Height, media->Width);
  74. }
  75. }
  76. if (cv::imwrite("DetRst.jpg", *img))
  77. {
  78. TZLogInfo("Image saved as DetRst.jpg~~~");
  79. }
  80. else
  81. {
  82. TZLogError("Failed to save the image!!!");
  83. }
  84. return 0;
  85. }
  86. int main()
  87. {
  88. // Initialize log
  89. INITIALIZE_LOGGER_NORMAL("test", "./test.log", 1, 100, 6, 1, 1);
  90. // Initialize memory pool
  91. masd::MemPool *pool = masd::MEMPOOL;
  92. if (pool->Initialize() != masd::MEC_OK)
  93. {
  94. TZLogError("Memory pool initialization failed!!!");
  95. return -1;
  96. }
  97. /* Calling libyolocrowddetector and libheadcountstrategy */
  98. // Step 1:
  99. // Initialize the SDK
  100. TZ_INT initResult = Initialize();
  101. if (initResult != masd::MEC_OK)
  102. {
  103. TZLogError("Failed to initialize the SDK!!!");
  104. return -1;
  105. }
  106. TZLogInfo("SDK Initialized Successfully~~~");
  107. // Step 2:
  108. // Build yolo-crowd detector
  109. masd::Detector* detector = BuildDetector();
  110. if (detector == nullptr)
  111. {
  112. TZLogError("Failed to build yolo-crowd detector!!!");
  113. Dispose();
  114. return -1;
  115. }
  116. TZLogInfo("Yolo-crowd detector built successfully~~~");
  117. // Step 3:
  118. // Initialize the yolo-crowd detector with configuration parameters
  119. std::string initParam =
  120. "{\"gpu_id\": 0, \"max_objects\": 1024, "
  121. "\"confidence_threshold\": 0.2, \"nms_threshold\": 0.5, "
  122. "\"model_path\": \"../../models/yolo-crowd-output0.trt\"}";
  123. TZ_INT initDetResult = detector->Initialize(initParam);
  124. if (initDetResult != masd::MEC_OK)
  125. {
  126. TZLogError("Failed to initialize the yolo-crowd detector!!!");
  127. DestroyDetector(detector);
  128. Dispose();
  129. return -1;
  130. }
  131. TZLogInfo("Yolo-crowd detector initialized successfully~~~");
  132. // Step 4:
  133. // Simulate frame processing with using test image
  134. cv::Mat testImage = cv::imread("../../media/image.jpg");
  135. if (testImage.empty())
  136. {
  137. TZLogError("Failed to load test image!!!");
  138. DestroyDetector(detector);
  139. Dispose();
  140. return -1;
  141. }
  142. TZ_INT length = testImage.total() * testImage.elemSize();
  143. SPtr<masd::Media> mediaResource = std::make_shared<masd::Media>(length);
  144. mediaResource->Width = testImage.cols;
  145. mediaResource->Height = testImage.rows;
  146. mediaResource->DataType = testImage.type();
  147. mediaResource->Mem = testImage.data;
  148. SPtr<masd::StreamInfo> streamInfo = std::make_shared<masd::StreamInfo>();
  149. streamInfo->SetMediaRsc(mediaResource);
  150. detector->DoDetect(streamInfo, DetectionCallback, &testImage);
  151. // Step 5:
  152. // Destroy the detector
  153. DestroyDetector(detector);
  154. TZLogInfo("Detector destroyed successfully~~~");
  155. // Step 6:
  156. // Dispose the SDK
  157. TZ_INT disposeResult = Dispose();
  158. if (disposeResult != masd::MEC_OK)
  159. {
  160. TZLogError("Failed to dispose the SDK!!!");
  161. return -1;
  162. }
  163. TZLogInfo("SDK disposed successfully~~~");
  164. return 0;
  165. }