infer_single_roi.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/ie.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. const std::string keys =
  17. "{ h help | | Print this help message }"
  18. "{ input | | Path to the input video file }"
  19. "{ facem | face-detection-adas-0001.xml | Path to OpenVINO IE face detection model (.xml) }"
  20. "{ faced | CPU | Target device for face detection model (e.g. CPU, GPU, VPU, ...) }"
  21. "{ r roi | -1,-1,-1,-1 | Region of interest (ROI) to use for inference. Identified automatically when not set }";
  22. namespace {
  23. std::string weights_path(const std::string &model_path) {
  24. const auto EXT_LEN = 4u;
  25. const auto sz = model_path.size();
  26. CV_Assert(sz > EXT_LEN);
  27. auto ext = model_path.substr(sz - EXT_LEN);
  28. std::transform(ext.begin(), ext.end(), ext.begin(), [](unsigned char c){
  29. return static_cast<unsigned char>(std::tolower(c));
  30. });
  31. CV_Assert(ext == ".xml");
  32. return model_path.substr(0u, sz - EXT_LEN) + ".bin";
  33. }
  34. cv::util::optional<cv::Rect> parse_roi(const std::string &rc) {
  35. cv::Rect rv;
  36. char delim[3];
  37. std::stringstream is(rc);
  38. is >> rv.x >> delim[0] >> rv.y >> delim[1] >> rv.width >> delim[2] >> rv.height;
  39. if (is.bad()) {
  40. return cv::util::optional<cv::Rect>(); // empty value
  41. }
  42. const auto is_delim = [](char c) {
  43. return c == ',';
  44. };
  45. if (!std::all_of(std::begin(delim), std::end(delim), is_delim)) {
  46. return cv::util::optional<cv::Rect>(); // empty value
  47. }
  48. if (rv.x < 0 || rv.y < 0 || rv.width <= 0 || rv.height <= 0) {
  49. return cv::util::optional<cv::Rect>(); // empty value
  50. }
  51. return cv::util::make_optional(std::move(rv));
  52. }
  53. } // namespace
  54. namespace custom {
  55. G_API_NET(FaceDetector, <cv::GMat(cv::GMat)>, "face-detector");
  56. using GDetections = cv::GArray<cv::Rect>;
  57. using GRect = cv::GOpaque<cv::Rect>;
  58. using GSize = cv::GOpaque<cv::Size>;
  59. using GPrims = cv::GArray<cv::gapi::wip::draw::Prim>;
  60. G_API_OP(LocateROI, <GRect(cv::GMat)>, "sample.custom.locate-roi") {
  61. static cv::GOpaqueDesc outMeta(const cv::GMatDesc &) {
  62. return cv::empty_gopaque_desc();
  63. }
  64. };
  65. G_API_OP(BBoxes, <GPrims(GDetections, GRect)>, "sample.custom.b-boxes") {
  66. static cv::GArrayDesc outMeta(const cv::GArrayDesc &, const cv::GOpaqueDesc &) {
  67. return cv::empty_array_desc();
  68. }
  69. };
  70. GAPI_OCV_KERNEL(OCVLocateROI, LocateROI) {
  71. // This is the place where we can run extra analytics
  72. // on the input image frame and select the ROI (region
  73. // of interest) where we want to detect our objects (or
  74. // run any other inference).
  75. //
  76. // Currently it doesn't do anything intelligent,
  77. // but only crops the input image to square (this is
  78. // the most convenient aspect ratio for detectors to use)
  79. static void run(const cv::Mat &in_mat, cv::Rect &out_rect) {
  80. // Identify the central point & square size (- some padding)
  81. const auto center = cv::Point{in_mat.cols/2, in_mat.rows/2};
  82. auto sqside = std::min(in_mat.cols, in_mat.rows);
  83. // Now build the central square ROI
  84. out_rect = cv::Rect{ center.x - sqside/2
  85. , center.y - sqside/2
  86. , sqside
  87. , sqside
  88. };
  89. }
  90. };
  91. GAPI_OCV_KERNEL(OCVBBoxes, BBoxes) {
  92. // This kernel converts the rectangles into G-API's
  93. // rendering primitives
  94. static void run(const std::vector<cv::Rect> &in_face_rcs,
  95. const cv::Rect &in_roi,
  96. std::vector<cv::gapi::wip::draw::Prim> &out_prims) {
  97. out_prims.clear();
  98. const auto cvt = [](const cv::Rect &rc, const cv::Scalar &clr) {
  99. return cv::gapi::wip::draw::Rect(rc, clr, 2);
  100. };
  101. out_prims.emplace_back(cvt(in_roi, CV_RGB(0,255,255))); // cyan
  102. for (auto &&rc : in_face_rcs) {
  103. out_prims.emplace_back(cvt(rc, CV_RGB(0,255,0))); // green
  104. }
  105. }
  106. };
  107. } // namespace custom
  108. int main(int argc, char *argv[])
  109. {
  110. cv::CommandLineParser cmd(argc, argv, keys);
  111. if (cmd.has("help")) {
  112. cmd.printMessage();
  113. return 0;
  114. }
  115. // Prepare parameters first
  116. const std::string input = cmd.get<std::string>("input");
  117. const auto opt_roi = parse_roi(cmd.get<std::string>("roi"));
  118. const auto face_model_path = cmd.get<std::string>("facem");
  119. auto face_net = cv::gapi::ie::Params<custom::FaceDetector> {
  120. face_model_path, // path to topology IR
  121. weights_path(face_model_path), // path to weights
  122. cmd.get<std::string>("faced"), // device specifier
  123. };
  124. auto kernels = cv::gapi::kernels
  125. <custom::OCVLocateROI
  126. , custom::OCVBBoxes>();
  127. auto networks = cv::gapi::networks(face_net);
  128. // Now build the graph. The graph structure may vary
  129. // pased on the input parameters
  130. cv::GStreamingCompiled pipeline;
  131. auto inputs = cv::gin(cv::gapi::wip::make_src<cv::gapi::wip::GCaptureSource>(input));
  132. cv::GMat in;
  133. cv::GOpaque<cv::Size> sz = cv::gapi::streaming::size(in);
  134. if (opt_roi.has_value()) {
  135. // Use the value provided by user
  136. std::cout << "Will run inference for static region "
  137. << opt_roi.value()
  138. << " only"
  139. << std::endl;
  140. cv::GOpaque<cv::Rect> in_roi;
  141. auto blob = cv::gapi::infer<custom::FaceDetector>(in_roi, in);
  142. cv::GArray<cv::Rect> rcs = cv::gapi::parseSSD(blob, sz, 0.5f, true, true);
  143. auto out = cv::gapi::wip::draw::render3ch(in, custom::BBoxes::on(rcs, in_roi));
  144. pipeline = cv::GComputation(cv::GIn(in, in_roi), cv::GOut(out))
  145. .compileStreaming(cv::compile_args(kernels, networks));
  146. // Since the ROI to detect is manual, make it part of the input vector
  147. inputs.push_back(cv::gin(opt_roi.value())[0]);
  148. } else {
  149. // Automatically detect ROI to infer. Make it output parameter
  150. std::cout << "ROI is not set or invalid. Locating it automatically"
  151. << std::endl;
  152. cv::GOpaque<cv::Rect> roi = custom::LocateROI::on(in);
  153. auto blob = cv::gapi::infer<custom::FaceDetector>(roi, in);
  154. cv::GArray<cv::Rect> rcs = cv::gapi::parseSSD(blob, sz, 0.5f, true, true);
  155. auto out = cv::gapi::wip::draw::render3ch(in, custom::BBoxes::on(rcs, roi));
  156. pipeline = cv::GComputation(cv::GIn(in), cv::GOut(out))
  157. .compileStreaming(cv::compile_args(kernels, networks));
  158. }
  159. // The execution part
  160. pipeline.setSource(std::move(inputs));
  161. pipeline.start();
  162. cv::Mat out;
  163. size_t frames = 0u;
  164. cv::TickMeter tm;
  165. tm.start();
  166. while (pipeline.pull(cv::gout(out))) {
  167. cv::imshow("Out", out);
  168. cv::waitKey(1);
  169. ++frames;
  170. }
  171. tm.stop();
  172. std::cout << "Processed " << frames << " frames" << " (" << frames / tm.getTimeSec() << " FPS)" << std::endl;
  173. return 0;
  174. }