ClipDemo.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. // libyolocrowddetector
  12. #include "DetectorAPI.h"
  13. #include "ClipDetector.h"
  14. #define RUN_TEXT_DRAWING 0
  15. struct CallbackContext
  16. {
  17. cv::Mat* img;
  18. };
  19. void SaveDetectorInfo(const char* info)
  20. {
  21. std::ofstream outFile("detector_info.json", std::ios::out | std::ios::app);
  22. if (outFile.is_open())
  23. {
  24. outFile << "{\n\t\"Detector Information\": \"" << info << "\"\n}" << std::endl;
  25. outFile.close();
  26. TZLogInfo("Detector information saved to detector_info.json~~~");
  27. }
  28. else
  29. {
  30. TZLogError("Failed to open file for writing!!!");
  31. }
  32. }
  33. TZ_INT DetectionCallback(SPtr<masd::StreamInfo>& media, void* ctx)
  34. {
  35. TZLogInfo("Detection callback triggered!~~~");
  36. // Retrieve the context
  37. CallbackContext* callbackContext = reinterpret_cast<CallbackContext*>(ctx);
  38. if ((callbackContext == nullptr) ||
  39. (callbackContext->img == nullptr))
  40. {
  41. TZLogError("Error: Invalid context or image pointer!!!");
  42. return -1;
  43. }
  44. cv::Mat* img = callbackContext->img;
  45. if (img->empty())
  46. {
  47. TZLogError("Error: Invalid image pointer!!!");
  48. return -1;
  49. }
  50. auto allDetRst = media->GetAllDetRst();
  51. int imgWidth = img->cols, imgHeight = img->rows;
  52. for (auto it = allDetRst.begin(); it != allDetRst.end(); ++it)
  53. {
  54. const std::string& detKey = it->first;
  55. const SPtr<masd::DetProducing>& detProducing = it->second;
  56. TZLogInfo("Detection Key: %s~~~", detKey.c_str());
  57. std::cout << "Detection Result: " << detProducing->Result << std::endl;
  58. if (!detProducing->Draw.Rects.empty())
  59. {
  60. TZLogInfo("Processing Draw Info...~~~");
  61. for (const auto& rect : detProducing->Draw.Rects)
  62. {
  63. TZLogInfo("Rect: LTX: %.2f, LTY: %.2f, "
  64. "RBX: %.2f, RBY: %.2f, Color: %s, Thickness: %d~~~",
  65. rect.LTX, rect.LTY, rect.RBX, rect.RBY,
  66. rect.Color.c_str(), rect.Thickness);
  67. if (!rect.Text.Text.empty())
  68. {
  69. TZLogInfo("Text: %s~~~", rect.Text.Text.c_str());
  70. }
  71. cv::Scalar color;
  72. {
  73. std::stringstream colorStream(rect.Color);
  74. int r, g, b;
  75. char comma;
  76. colorStream >> r >> comma >> g >> comma >> b;
  77. color = cv::Scalar(b, g, r);
  78. }
  79. cv::Point topLeft(rect.LTX * imgWidth, rect.LTY * imgHeight);
  80. cv::Point bottomRight(rect.RBX * imgWidth, rect.RBY * imgHeight);
  81. cv::rectangle(*img, topLeft, bottomRight, color, rect.Thickness);
  82. #if RUN_TEXT_DRAWING
  83. if (!rect.Text.Text.empty())
  84. {
  85. cv::putText(*img, rect.Text.Text,
  86. cv::Point(topLeft.x, topLeft.y - 10),
  87. cv::FONT_HERSHEY_SIMPLEX, 0.8, color, 2);
  88. }
  89. #endif
  90. }
  91. }
  92. else
  93. {
  94. TZLogInfo("No Draw Info available.~~~");
  95. }
  96. if (detProducing->DetMedia)
  97. {
  98. const auto& media = detProducing->DetMedia;
  99. TZLogInfo("Media Length: %d~~~", media->Length);
  100. TZLogInfo("Media DataType: %d~~~", media->DataType);
  101. TZLogInfo("Media Height: %d, Width: %d~~~", media->Height, media->Width);
  102. }
  103. }
  104. if (cv::imwrite("DetRst.jpg", *img))
  105. {
  106. TZLogInfo("Image saved as DetRst.jpg~~~");
  107. }
  108. else
  109. {
  110. TZLogError("Failed to save the image!!!");
  111. }
  112. return 0;
  113. }
  114. int main()
  115. {
  116. // Initialize log
  117. INITIALIZE_LOGGER_NORMAL("test", "./test.log", 1, 100, 6, 1, 1);
  118. // Initialize memory pool
  119. masd::MemPool *pool = masd::MEMPOOL;
  120. if (pool->Initialize() != masd::MEC_OK)
  121. {
  122. TZLogError("Memory pool initialization failed!!!");
  123. return -1;
  124. }
  125. /* Calling libyolocrowddetector and libheadcountstrategy */
  126. // Step 1:
  127. // Initialize the SDK
  128. TZ_INT initResult = Initialize();
  129. if (initResult != masd::MEC_OK)
  130. {
  131. TZLogError("Failed to initialize the SDK!!!");
  132. return -1;
  133. }
  134. TZLogInfo("SDK Initialized Successfully~~~");
  135. // Step 2:
  136. // Build Clip detector
  137. masd::Detector* detector = BuildDetector();
  138. if (detector == nullptr)
  139. {
  140. TZLogError("Failed to build Clip detector!!!");
  141. Dispose();
  142. return -1;
  143. }
  144. TZLogInfo("Clip detector built successfully~~~");
  145. // Step 3:
  146. // Initialize the Clip detector with configuration parameters
  147. std::string initParam =
  148. "{\"gpu_id\": 0, \"model_path\": \"/home/flechazo/workspace/abandoned-object-detection-algo/clip/models/visual.onnx\"}";
  149. TZ_INT initDetResult = detector->Initialize(initParam);
  150. if (initDetResult != masd::MEC_OK)
  151. {
  152. TZLogError("Failed to initialize the Clip detector!!!");
  153. DestroyDetector(detector);
  154. Dispose();
  155. return -1;
  156. }
  157. TZLogInfo("Clip detector initialized successfully~~~");
  158. // Step 5:
  159. // Simulate frame processing with using test image
  160. cv::Mat testImage = cv::imread("/home/flechazo/workspace/abandoned-object-detection-algo/proposals/proposal_7.jpg");
  161. if (testImage.empty())
  162. {
  163. TZLogError("Failed to load test image!!!");
  164. DestroyDetector(detector);
  165. Dispose();
  166. return -1;
  167. }
  168. TZ_INT length = testImage.total() * testImage.elemSize();
  169. SPtr<masd::Media> mediaResource = std::make_shared<masd::Media>(length);
  170. mediaResource->Width = testImage.cols;
  171. mediaResource->Height = testImage.rows;
  172. mediaResource->DataType = testImage.type();
  173. mediaResource->Mem = testImage.data;
  174. SPtr<masd::StreamInfo> streamInfo = std::make_shared<masd::StreamInfo>();
  175. streamInfo->SetMediaRsc(mediaResource);
  176. CallbackContext callbackContext{&testImage};
  177. detector->DoDetect(streamInfo, DetectionCallback, &callbackContext);
  178. // Step 6:
  179. // Print DetGetInformation
  180. char detectorInfo[4096];
  181. TZ_INT infoResult = GetInformation(detectorInfo);
  182. if (infoResult != masd::MEC_OK)
  183. {
  184. TZLogError("Failed to get detector information!!!");
  185. Dispose();
  186. return -1;
  187. }
  188. SaveDetectorInfo(detectorInfo);
  189. // Step 7:
  190. // Destroy the detector
  191. DestroyDetector(detector);
  192. TZLogInfo("Detector destroyed successfully~~~");
  193. // Step 8:
  194. // Dispose the SDK
  195. TZ_INT disposeResult = Dispose();
  196. if (disposeResult != masd::MEC_OK)
  197. {
  198. TZLogError("Failed to dispose the SDK!!!");
  199. return -1;
  200. }
  201. TZLogInfo("SDK disposed successfully~~~");
  202. return 0;
  203. }