VideoDemo.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #include <fstream>
  2. #include <opencv2/opencv.hpp>
  3. // libcommon
  4. #include "Logger.h"
  5. #include "OSTime.h"
  6. #include "SysUtils.h"
  7. #include "Semaphore.h"
  8. // libmascommon
  9. #include "MemPool.h"
  10. // deps
  11. #include "DetUtils.h"
  12. #include "ilogger.hpp"
  13. #include "trt_infer.hpp"
  14. // libyolocrowddetector
  15. #include "DetectorAPI.h"
  16. #include "YoloCrowdDetector.h"
  17. // libheadcountstrategy
  18. #include "StrategyAPI.h"
  19. #include "HeadCountStrategy.h"
  20. #define RUN_TEXT_DRAWING 0
  21. struct CallbackContext
  22. {
  23. masd::Strategy* strategy;
  24. cv::Mat* img;
  25. cv::VideoWriter* videoWriter;
  26. };
  27. tzc::Semaphore SEMA;
  28. void SaveDetectorInfo(const char* info)
  29. {
  30. std::ofstream outFile("detector_info.json", std::ios::out | std::ios::app);
  31. if (outFile.is_open())
  32. {
  33. outFile << "{\n\t\"Detector Information\": \"" << info << "\"\n}" << std::endl;
  34. outFile.close();
  35. TZLogInfo("Detector information saved to detector_info.json~~~");
  36. }
  37. else
  38. {
  39. TZLogError("Failed to open file for writing!!!");
  40. }
  41. }
  42. TZ_INT DetectionCallback(SPtr<masd::StreamInfo>& media, void* ctx)
  43. {
  44. TZLogInfo("Detection callback triggered!~~~");
  45. // Retrieve the context
  46. CallbackContext* callbackContext = reinterpret_cast<CallbackContext*>(ctx);
  47. if (!callbackContext || !callbackContext->img || callbackContext->strategy == nullptr)
  48. {
  49. TZLogError("Error: Invalid context or image pointer!!!");
  50. return -1;
  51. }
  52. cv::Mat* img = callbackContext->img;
  53. masd::Strategy* strategy = callbackContext->strategy;
  54. if (img->empty())
  55. {
  56. TZLogError("Error: Invalid image pointer!!!");
  57. return -1;
  58. }
  59. auto allDetRst = media->GetAllDetRst();
  60. int imgWidth = img->cols, imgHeight = img->rows;
  61. for (auto it = allDetRst.begin(); it != allDetRst.end(); ++it)
  62. {
  63. const std::string& detKey = it->first;
  64. const SPtr<masd::DetProducing>& detProducing = it->second;
  65. TZLogInfo("Detection Key: %s~~~", detKey.c_str());
  66. std::cout << "Detection Result: " << detProducing->Result << std::endl;
  67. if (!detProducing->Draw.Rects.empty())
  68. {
  69. TZLogInfo("Processing Draw Info...~~~");
  70. for (const auto& rect : detProducing->Draw.Rects)
  71. {
  72. TZLogInfo("Rect: LTX: %.2f, LTY: %.2f, "
  73. "RBX: %.2f, RBY: %.2f, Color: %s, Thickness: %d~~~",
  74. rect.LTX, rect.LTY, rect.RBX, rect.RBY,
  75. rect.Color.c_str(), rect.Thickness);
  76. if (!rect.Text.Text.empty())
  77. {
  78. TZLogInfo("Text: %s~~~", rect.Text.Text.c_str());
  79. }
  80. cv::Scalar color;
  81. {
  82. std::stringstream colorStream(rect.Color);
  83. int r, g, b;
  84. char comma;
  85. colorStream >> r >> comma >> g >> comma >> b;
  86. color = cv::Scalar(b, g, r);
  87. }
  88. cv::Point topLeft(rect.LTX * imgWidth, rect.LTY * imgHeight);
  89. cv::Point bottomRight(rect.RBX * imgWidth, rect.RBY * imgHeight);
  90. cv::rectangle(*img, topLeft, bottomRight, color, rect.Thickness);
  91. #if RUN_TEXT_DRAWING
  92. if (!rect.Text.Text.empty())
  93. {
  94. cv::putText(*img, rect.Text.Text,
  95. cv::Point(topLeft.x, topLeft.y - 10),
  96. cv::FONT_HERSHEY_SIMPLEX, 0.8, color, 2);
  97. }
  98. #endif
  99. }
  100. }
  101. else
  102. {
  103. TZLogInfo("No Draw Info available.~~~");
  104. }
  105. if (detProducing->DetMedia)
  106. {
  107. const auto& media = detProducing->DetMedia;
  108. TZLogInfo("Media Length: %d~~~", media->Length);
  109. TZLogInfo("Media DataType: %d~~~", media->DataType);
  110. TZLogInfo("Media Height: %d, Width: %d~~~", media->Height, media->Width);
  111. }
  112. }
  113. // Process the strategy inside the callback
  114. TZ_INT strategyResult = strategy->DoStrategy(media);
  115. if (strategyResult != masd::MEC_OK)
  116. {
  117. TZLogError("Headcount strategy failed to process the stream info!!!");
  118. return -1;
  119. }
  120. TZLogInfo("Headcount strategy processed the stream info successfully~~~");
  121. SPtr<masd::StraProducing> straProducing = media->GetStraProducing();
  122. if (straProducing)
  123. {
  124. std::string headcountResult = straProducing->Result.RstName;
  125. TZLogInfo("Detected headcount: %s", headcountResult.c_str());
  126. int fontFace = cv::FONT_HERSHEY_SIMPLEX;
  127. double fontScale = 0.8;
  128. int thickness = 2;
  129. cv::Scalar textColor(0, 255, 0);
  130. cv::Point textPosition(10, 30);
  131. cv::putText(*img, headcountResult, textPosition, fontFace, fontScale, textColor, thickness);
  132. }
  133. else
  134. {
  135. TZLogWarn("No headcount result found in the stream info.");
  136. }
  137. callbackContext->videoWriter->write(*img);
  138. SEMA.Signal();
  139. return 0;
  140. }
  141. int main()
  142. {
  143. // Initialize log
  144. INITIALIZE_LOGGER_NORMAL("test", "./test.log", 1, 100, 6, 1, 1);
  145. // Initialize memory pool
  146. masd::MemPool *pool = masd::MEMPOOL;
  147. if (pool->Initialize() != masd::MEC_OK)
  148. {
  149. TZLogError("Memory pool initialization failed!!!");
  150. return -1;
  151. }
  152. /* Calling libyolocrowddetector and libheadcountstrategy */
  153. // Step 1:
  154. // Initialize the SDK
  155. TZ_INT initResult = Initialize();
  156. if (initResult != masd::MEC_OK)
  157. {
  158. TZLogError("Failed to initialize the SDK!!!");
  159. return -1;
  160. }
  161. TZLogInfo("SDK Initialized Successfully~~~");
  162. // Step 2:
  163. // Build yolo-crowd detector
  164. masd::Detector* detector = BuildDetector();
  165. if (detector == nullptr)
  166. {
  167. TZLogError("Failed to build yolo-crowd detector!!!");
  168. Dispose();
  169. return -1;
  170. }
  171. TZLogInfo("Yolo-crowd detector built successfully~~~");
  172. // Build headcount strategy
  173. masd::Strategy* strategy = BuildStrategy();
  174. if(strategy == nullptr)
  175. {
  176. TZLogError("Failed to build headcount strategy!!!");
  177. Dispose();
  178. return -1;
  179. }
  180. TZLogInfo("Headcount strategy built successfully~~~");
  181. // Step 3:
  182. // Initialize the yolo-crowd detector with configuration parameters
  183. std::string initParam =
  184. "{\"gpu_id\": 0, \"max_objects\": 1024, "
  185. "\"confidence_threshold\": 0.2, \"nms_threshold\": 0.5, "
  186. "\"model_path\": \"../../models/yolo-crowd-output0.trt\"}";
  187. TZ_INT initDetResult = detector->Initialize(initParam);
  188. if (initDetResult != masd::MEC_OK)
  189. {
  190. TZLogError("Failed to initialize the yolo-crowd detector!!!");
  191. DestroyDetector(detector);
  192. DestroyStrategy(strategy);
  193. Dispose();
  194. return -1;
  195. }
  196. TZLogInfo("Yolo-crowd detector initialized successfully~~~");
  197. // Initialize the headcount strategy
  198. TZ_INT initStraResult = strategy->Initialize();
  199. if (initStraResult != masd::MEC_OK)
  200. {
  201. TZLogError("Failed to initialize the headcount detector!!!");
  202. DestroyDetector(detector);
  203. DestroyStrategy(strategy);
  204. Dispose();
  205. return -1;
  206. }
  207. TZLogInfo("Headcount strategy initialized successfully~~~");
  208. // Step 4:
  209. // Set yolo-crowd detection configuration (optional)
  210. std::string detectConfig = R"({
  211. "freq": 0,
  212. "target": {
  213. "target_class": 0,
  214. "target_threshold": 0.7
  215. },
  216. "focusArea": [
  217. { "LTX": 0.0, "LTY": 0.0, "RBX": 1.0, "RBY": 1.0 }
  218. ],
  219. "ignoreArea": [
  220. { "LTX": 0.0, "LTY": 0.0, "RBX": 0.0, "RBY": 0.0 }
  221. ]
  222. })";
  223. TZ_INT setDetCfgResult = detector->SetDetectCfg(detectConfig);
  224. if (setDetCfgResult != masd::MEC_OK)
  225. {
  226. TZLogError("Failed to set yolo-crowd detection configuration!!!");
  227. DestroyDetector(detector);
  228. DestroyStrategy(strategy);
  229. Dispose();
  230. return -1;
  231. }
  232. TZLogInfo("Yolo-crowd detection configuration set successfully~~~");
  233. // Set headcount strategy configuration (optional)
  234. std::string headcountConfig = R"({
  235. "TimeThreshold": 5
  236. })";
  237. TZ_INT setStraCfgResult = strategy->SetStrategyCfg(headcountConfig);
  238. if (setStraCfgResult != masd::MEC_OK)
  239. {
  240. TZLogError("Failed to set headcount strategy configuration!!!");
  241. DestroyDetector(detector);
  242. DestroyStrategy(strategy);
  243. Dispose();
  244. return -1;
  245. }
  246. TZLogInfo("Headcount strategy configuration set successfully~~~");
  247. // Step 5:
  248. // Simulate frame processing with using test MP4
  249. cv::VideoCapture videoCapture("../../media/video.mp4");
  250. if(!videoCapture.isOpened())
  251. {
  252. TZLogError("Failed to load test video!!!");
  253. DestroyDetector(detector);
  254. DestroyStrategy(strategy);
  255. Dispose();
  256. return -1;
  257. }
  258. TZ_INT videoWidth = static_cast<TZ_INT>(videoCapture.get(cv::CAP_PROP_FRAME_WIDTH));
  259. TZ_INT videoHeight = static_cast<TZ_INT>(videoCapture.get(cv::CAP_PROP_FRAME_HEIGHT));
  260. TZ_INT videoFPS = static_cast<TZ_INT>(videoCapture.get(cv::CAP_PROP_FPS));
  261. cv::VideoWriter videoWriter("output_video.mp4",
  262. cv::VideoWriter::fourcc('X', '2', '6', '4'),
  263. videoFPS, cv::Size(videoWidth, videoHeight));
  264. if (!videoWriter.isOpened())
  265. {
  266. TZLogError("Failed to open video writer!!!");
  267. DestroyDetector(detector);
  268. DestroyStrategy(strategy);
  269. Dispose();
  270. return -1;
  271. }
  272. cv::Mat frame;
  273. while(videoCapture.read(frame))
  274. {
  275. if(frame.empty())
  276. {
  277. TZLogError("Failed to read frame from video!!!");
  278. break;
  279. }
  280. TZ_INT length = frame.total() * frame.elemSize();
  281. SPtr<masd::Media> mediaResource = std::make_shared<masd::Media>(length);
  282. mediaResource->Width = frame.cols;
  283. mediaResource->Height = frame.rows;
  284. mediaResource->DataType = frame.type();
  285. mediaResource->Mem = frame.data;
  286. SPtr<masd::StreamInfo> streamInfo = std::make_shared<masd::StreamInfo>();
  287. streamInfo->SetMediaRsc(mediaResource);
  288. CallbackContext callbackContext{strategy, &frame, &videoWriter};
  289. detector->DoDetect(streamInfo, DetectionCallback, &callbackContext);
  290. SEMA.Wait();
  291. }
  292. // Step 6:
  293. // Print DetGetInformation
  294. char detectorInfo[4096];
  295. TZ_INT infoResult = GetInformation(detectorInfo);
  296. if (infoResult != masd::MEC_OK)
  297. {
  298. TZLogError("Failed to get detector information!!!");
  299. Dispose();
  300. return -1;
  301. }
  302. SaveDetectorInfo(detectorInfo);
  303. // Step 7:
  304. // Destroy the detector
  305. DestroyDetector(detector);
  306. TZLogInfo("Detector destroyed successfully~~~");
  307. // Destroy the strategy
  308. DestroyStrategy(strategy);
  309. TZLogInfo("Detector strategy successfully~~~");
  310. // Step 8:
  311. // Dispose the SDK
  312. TZ_INT disposeResult = Dispose();
  313. if (disposeResult != masd::MEC_OK)
  314. {
  315. TZLogError("Failed to dispose the SDK!!!");
  316. return -1;
  317. }
  318. TZLogInfo("SDK disposed successfully~~~");
  319. return 0;
  320. }