infer_ssd_onnx.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <opencv2/imgproc.hpp>
  5. #include <opencv2/imgcodecs.hpp>
  6. #include <opencv2/gapi.hpp>
  7. #include <opencv2/gapi/core.hpp>
  8. #include <opencv2/gapi/imgproc.hpp>
  9. #include <opencv2/gapi/infer.hpp>
  10. #include <opencv2/gapi/render.hpp>
  11. #include <opencv2/gapi/infer/onnx.hpp>
  12. #include <opencv2/gapi/cpu/gcpukernel.hpp>
  13. #include <opencv2/gapi/streaming/cap.hpp>
  14. #include <opencv2/highgui.hpp>
  15. #include <opencv2/gapi/infer/parsers.hpp>
  16. namespace custom {
  17. G_API_NET(ObjDetector, <cv::GMat(cv::GMat)>, "object-detector");
  18. using GDetections = cv::GArray<cv::Rect>;
  19. using GSize = cv::GOpaque<cv::Size>;
  20. using GPrims = cv::GArray<cv::gapi::wip::draw::Prim>;
  21. G_API_OP(BBoxes, <GPrims(GDetections)>, "sample.custom.b-boxes") {
  22. static cv::GArrayDesc outMeta(const cv::GArrayDesc &) {
  23. return cv::empty_array_desc();
  24. }
  25. };
  26. GAPI_OCV_KERNEL(OCVBBoxes, BBoxes) {
  27. // This kernel converts the rectangles into G-API's
  28. // rendering primitives
  29. static void run(const std::vector<cv::Rect> &in_obj_rcs,
  30. std::vector<cv::gapi::wip::draw::Prim> &out_prims) {
  31. out_prims.clear();
  32. const auto cvt = [](const cv::Rect &rc, const cv::Scalar &clr) {
  33. return cv::gapi::wip::draw::Rect(rc, clr, 2);
  34. };
  35. for (auto &&rc : in_obj_rcs) {
  36. out_prims.emplace_back(cvt(rc, CV_RGB(0,255,0))); // green
  37. }
  38. std::cout << "Detections:";
  39. for (auto &&rc : in_obj_rcs) std::cout << ' ' << rc;
  40. std::cout << std::endl;
  41. }
  42. };
  43. } // namespace custom
  44. namespace {
  45. void remap_ssd_ports(const std::unordered_map<std::string, cv::Mat> &onnx,
  46. std::unordered_map<std::string, cv::Mat> &gapi) {
  47. // Assemble ONNX-processed outputs back to a single 1x1x200x7 blob
  48. // to preserve compatibility with OpenVINO-based SSD pipeline
  49. const cv::Mat &num_detections = onnx.at("num_detections:0");
  50. const cv::Mat &detection_boxes = onnx.at("detection_boxes:0");
  51. const cv::Mat &detection_scores = onnx.at("detection_scores:0");
  52. const cv::Mat &detection_classes = onnx.at("detection_classes:0");
  53. GAPI_Assert(num_detections.depth() == CV_32F);
  54. GAPI_Assert(detection_boxes.depth() == CV_32F);
  55. GAPI_Assert(detection_scores.depth() == CV_32F);
  56. GAPI_Assert(detection_classes.depth() == CV_32F);
  57. cv::Mat &ssd_output = gapi.at("detection_output");
  58. const int num_objects = static_cast<int>(num_detections.ptr<float>()[0]);
  59. const float *in_boxes = detection_boxes.ptr<float>();
  60. const float *in_scores = detection_scores.ptr<float>();
  61. const float *in_classes = detection_classes.ptr<float>();
  62. float *ptr = ssd_output.ptr<float>();
  63. for (int i = 0; i < num_objects; i++) {
  64. ptr[0] = 0.f; // "image_id"
  65. ptr[1] = in_classes[i]; // "label"
  66. ptr[2] = in_scores[i]; // "confidence"
  67. ptr[3] = in_boxes[4*i + 1]; // left
  68. ptr[4] = in_boxes[4*i + 0]; // top
  69. ptr[5] = in_boxes[4*i + 3]; // right
  70. ptr[6] = in_boxes[4*i + 2]; // bottom
  71. ptr += 7;
  72. in_boxes += 4;
  73. }
  74. if (num_objects < ssd_output.size[2]-1) {
  75. // put a -1 mark at the end of output blob if there is space left
  76. ptr[0] = -1.f;
  77. }
  78. }
  79. } // anonymous namespace
  80. const std::string keys =
  81. "{ h help | | Print this help message }"
  82. "{ input | | Path to the input video file }"
  83. "{ output | | (Optional) path to output video file }"
  84. "{ detm | | Path to an ONNX SSD object detection model (.onnx) }"
  85. ;
  86. int main(int argc, char *argv[])
  87. {
  88. cv::CommandLineParser cmd(argc, argv, keys);
  89. if (cmd.has("help")) {
  90. cmd.printMessage();
  91. return 0;
  92. }
  93. // Prepare parameters first
  94. const std::string input = cmd.get<std::string>("input");
  95. const std::string output = cmd.get<std::string>("output");
  96. const auto obj_model_path = cmd.get<std::string>("detm");
  97. auto obj_net = cv::gapi::onnx::Params<custom::ObjDetector>{obj_model_path}
  98. .cfgOutputLayers({"detection_output"})
  99. .cfgPostProc({cv::GMatDesc{CV_32F, {1,1,200,7}}}, remap_ssd_ports);
  100. auto kernels = cv::gapi::kernels<custom::OCVBBoxes>();
  101. auto networks = cv::gapi::networks(obj_net);
  102. // Now build the graph
  103. cv::GMat in;
  104. auto blob = cv::gapi::infer<custom::ObjDetector>(in);
  105. cv::GArray<cv::Rect> rcs =
  106. cv::gapi::parseSSD(blob, cv::gapi::streaming::size(in), 0.5f, true, true);
  107. auto out = cv::gapi::wip::draw::render3ch(in, custom::BBoxes::on(rcs));
  108. cv::GStreamingCompiled pipeline = cv::GComputation(cv::GIn(in), cv::GOut(out))
  109. .compileStreaming(cv::compile_args(kernels, networks));
  110. auto inputs = cv::gin(cv::gapi::wip::make_src<cv::gapi::wip::GCaptureSource>(input));
  111. // The execution part
  112. pipeline.setSource(std::move(inputs));
  113. cv::TickMeter tm;
  114. cv::VideoWriter writer;
  115. size_t frames = 0u;
  116. cv::Mat outMat;
  117. tm.start();
  118. pipeline.start();
  119. while (pipeline.pull(cv::gout(outMat))) {
  120. ++frames;
  121. cv::imshow("Out", outMat);
  122. cv::waitKey(1);
  123. if (!output.empty()) {
  124. if (!writer.isOpened()) {
  125. const auto sz = cv::Size{outMat.cols, outMat.rows};
  126. writer.open(output, cv::VideoWriter::fourcc('M','J','P','G'), 25.0, sz);
  127. CV_Assert(writer.isOpened());
  128. }
  129. writer << outMat;
  130. }
  131. }
  132. tm.stop();
  133. std::cout << "Processed " << frames << " frames" << " (" << frames / tm.getTimeSec() << " FPS)" << std::endl;
  134. return 0;
  135. }