privacy_masking_camera.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cctype>
  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 about =
  17. "This is an OpenCV-based version of Privacy Masking Camera example";
  18. const std::string keys =
  19. "{ h help | | Print this help message }"
  20. "{ input | | Path to the input video file }"
  21. "{ platm | vehicle-license-plate-detection-barrier-0106.xml | Path to OpenVINO IE vehicle/plate detection model (.xml) }"
  22. "{ platd | CPU | Target device for vehicle/plate detection model (e.g. CPU, GPU, VPU, ...) }"
  23. "{ facem | face-detection-retail-0005.xml | Path to OpenVINO IE face detection model (.xml) }"
  24. "{ faced | CPU | Target device for face detection model (e.g. CPU, GPU, VPU, ...) }"
  25. "{ trad | false | Run processing in a traditional (non-pipelined) way }"
  26. "{ noshow | false | Don't display UI (improves performance) }";
  27. namespace {
  28. std::string weights_path(const std::string &model_path) {
  29. const auto EXT_LEN = 4u;
  30. const auto sz = model_path.size();
  31. CV_Assert(sz > EXT_LEN);
  32. auto ext = model_path.substr(sz - EXT_LEN);
  33. std::transform(ext.begin(), ext.end(), ext.begin(), [](unsigned char c){ return static_cast<unsigned char>(std::tolower(c)); });
  34. CV_Assert(ext == ".xml");
  35. return model_path.substr(0u, sz - EXT_LEN) + ".bin";
  36. }
  37. } // namespace
  38. namespace custom {
  39. G_API_NET(VehLicDetector, <cv::GMat(cv::GMat)>, "vehicle-license-plate-detector");
  40. G_API_NET(FaceDetector, <cv::GMat(cv::GMat)>, "face-detector");
  41. using GDetections = cv::GArray<cv::Rect>;
  42. using GPrims = cv::GArray<cv::gapi::wip::draw::Prim>;
  43. G_API_OP(ToMosaic, <GPrims(GDetections, GDetections)>, "custom.privacy_masking.to_mosaic") {
  44. static cv::GArrayDesc outMeta(const cv::GArrayDesc &, const cv::GArrayDesc &) {
  45. return cv::empty_array_desc();
  46. }
  47. };
  48. GAPI_OCV_KERNEL(OCVToMosaic, ToMosaic) {
  49. static void run(const std::vector<cv::Rect> &in_plate_rcs,
  50. const std::vector<cv::Rect> &in_face_rcs,
  51. std::vector<cv::gapi::wip::draw::Prim> &out_prims) {
  52. out_prims.clear();
  53. const auto cvt = [](cv::Rect rc) {
  54. // Align the mosaic region to mosaic block size
  55. const int BLOCK_SIZE = 24;
  56. const int dw = BLOCK_SIZE - (rc.width % BLOCK_SIZE);
  57. const int dh = BLOCK_SIZE - (rc.height % BLOCK_SIZE);
  58. rc.width += dw;
  59. rc.height += dh;
  60. rc.x -= dw / 2;
  61. rc.y -= dh / 2;
  62. return cv::gapi::wip::draw::Mosaic{rc, BLOCK_SIZE, 0};
  63. };
  64. for (auto &&rc : in_plate_rcs) { out_prims.emplace_back(cvt(rc)); }
  65. for (auto &&rc : in_face_rcs) { out_prims.emplace_back(cvt(rc)); }
  66. }
  67. };
  68. } // namespace custom
  69. int main(int argc, char *argv[])
  70. {
  71. cv::CommandLineParser cmd(argc, argv, keys);
  72. cmd.about(about);
  73. if (cmd.has("help")) {
  74. cmd.printMessage();
  75. return 0;
  76. }
  77. const std::string input = cmd.get<std::string>("input");
  78. const bool no_show = cmd.get<bool>("noshow");
  79. const bool run_trad = cmd.get<bool>("trad");
  80. cv::GMat in;
  81. cv::GMat blob_plates = cv::gapi::infer<custom::VehLicDetector>(in);
  82. cv::GMat blob_faces = cv::gapi::infer<custom::FaceDetector>(in);
  83. // VehLicDetector from Open Model Zoo marks vehicles with label "1" and
  84. // license plates with label "2", filter out license plates only.
  85. cv::GOpaque<cv::Size> sz = cv::gapi::streaming::size(in);
  86. cv::GArray<cv::Rect> rc_plates, rc_faces;
  87. cv::GArray<int> labels;
  88. std::tie(rc_plates, labels) = cv::gapi::parseSSD(blob_plates, sz, 0.5f, 2);
  89. // Face detector produces faces only so there's no need to filter by label,
  90. // pass "-1".
  91. std::tie(rc_faces, labels) = cv::gapi::parseSSD(blob_faces, sz, 0.5f, -1);
  92. cv::GMat out = cv::gapi::wip::draw::render3ch(in, custom::ToMosaic::on(rc_plates, rc_faces));
  93. cv::GComputation graph(in, out);
  94. const auto plate_model_path = cmd.get<std::string>("platm");
  95. auto plate_net = cv::gapi::ie::Params<custom::VehLicDetector> {
  96. plate_model_path, // path to topology IR
  97. weights_path(plate_model_path), // path to weights
  98. cmd.get<std::string>("platd"), // device specifier
  99. };
  100. const auto face_model_path = cmd.get<std::string>("facem");
  101. auto face_net = cv::gapi::ie::Params<custom::FaceDetector> {
  102. face_model_path, // path to topology IR
  103. weights_path(face_model_path), // path to weights
  104. cmd.get<std::string>("faced"), // device specifier
  105. };
  106. auto kernels = cv::gapi::kernels<custom::OCVToMosaic>();
  107. auto networks = cv::gapi::networks(plate_net, face_net);
  108. cv::TickMeter tm;
  109. cv::Mat out_frame;
  110. std::size_t frames = 0u;
  111. std::cout << "Reading " << input << std::endl;
  112. if (run_trad) {
  113. cv::Mat in_frame;
  114. cv::VideoCapture cap(input);
  115. cap >> in_frame;
  116. auto exec = graph.compile(cv::descr_of(in_frame), cv::compile_args(kernels, networks));
  117. tm.start();
  118. do {
  119. exec(in_frame, out_frame);
  120. if (!no_show) {
  121. cv::imshow("Out", out_frame);
  122. cv::waitKey(1);
  123. }
  124. frames++;
  125. } while (cap.read(in_frame));
  126. tm.stop();
  127. } else {
  128. auto pipeline = graph.compileStreaming(cv::compile_args(kernels, networks));
  129. pipeline.setSource(cv::gapi::wip::make_src<cv::gapi::wip::GCaptureSource>(input));
  130. pipeline.start();
  131. tm.start();
  132. while (pipeline.pull(cv::gout(out_frame))) {
  133. frames++;
  134. if (!no_show) {
  135. cv::imshow("Out", out_frame);
  136. cv::waitKey(1);
  137. }
  138. }
  139. tm.stop();
  140. }
  141. std::cout << "Processed " << frames << " frames"
  142. << " (" << frames / tm.getTimeSec() << " FPS)" << std::endl;
  143. return 0;
  144. }