onevpl_source_to_bgr_conv.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <algorithm>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <cctype>
  5. #include <tuple>
  6. #include <memory>
  7. #include <opencv2/imgproc.hpp>
  8. #include <opencv2/gapi.hpp>
  9. #include <opencv2/gapi/core.hpp>
  10. #include <opencv2/gapi/gpu/ggpukernel.hpp>
  11. #include <opencv2/gapi/streaming/onevpl/source.hpp>
  12. #include <opencv2/gapi/streaming/onevpl/data_provider_interface.hpp>
  13. #include <opencv2/gapi/streaming/onevpl/default.hpp>
  14. #include <opencv2/highgui.hpp> // CommandLineParser
  15. #include <opencv2/gapi/ocl/core.hpp>
  16. const std::string about =
  17. "This is an example presents decoding on GPU using VPL Source and passing it to OpenCL backend";
  18. const std::string keys =
  19. "{ h help | | Print this help message }"
  20. "{ input | | Path to the input video file. Use .avi extension }"
  21. "{ accel_mode | mfxImplDescription.AccelerationMode:MFX_ACCEL_MODE_VIA_D3D11 | Acceleration mode for VPL }";
  22. namespace {
  23. namespace cfg {
  24. // FIXME: Move OneVPL arguments parser to a single place
  25. typename cv::gapi::wip::onevpl::CfgParam create_from_string(const std::string &line);
  26. } // namespace cfg
  27. } // anonymous namespace
  28. int main(int argc, char *argv[]) {
  29. cv::CommandLineParser cmd(argc, argv, keys);
  30. cmd.about(about);
  31. if (cmd.has("help")) {
  32. cmd.printMessage();
  33. return 0;
  34. }
  35. // Get file name
  36. const auto input = cmd.get<std::string>("input");
  37. const auto accel_mode = cmd.get<std::string>("accel_mode");
  38. // Create VPL config
  39. std::vector<cv::gapi::wip::onevpl::CfgParam> source_cfgs;
  40. source_cfgs.push_back(cfg::create_from_string(accel_mode));
  41. // Create VPL-based source
  42. std::shared_ptr<cv::gapi::wip::onevpl::IDeviceSelector> default_device_selector =
  43. cv::gapi::wip::onevpl::getDefaultDeviceSelector(source_cfgs);
  44. cv::gapi::wip::IStreamSource::Ptr source = cv::gapi::wip::make_onevpl_src(input, source_cfgs,
  45. default_device_selector);
  46. // Build the graph
  47. cv::GFrame in; // input frame from VPL source
  48. auto bgr_gmat = cv::gapi::streaming::BGR(in); // conversion from VPL source frame to BGR UMat
  49. auto out = cv::gapi::blur(bgr_gmat, cv::Size(4,4)); // ocl kernel of blur operation
  50. cv::GStreamingCompiled pipeline = cv::GComputation(cv::GIn(in), cv::GOut(out))
  51. .compileStreaming(cv::compile_args(cv::gapi::core::ocl::kernels()));
  52. pipeline.setSource(std::move(source));
  53. // The execution part
  54. size_t frames = 0u;
  55. cv::TickMeter tm;
  56. cv::Mat outMat;
  57. pipeline.start();
  58. tm.start();
  59. while (pipeline.pull(cv::gout(outMat))) {
  60. cv::imshow("OutVideo", outMat);
  61. cv::waitKey(1);
  62. ++frames;
  63. }
  64. tm.stop();
  65. std::cout << "Processed " << frames << " frames" << " (" << frames / tm.getTimeSec() << " FPS)" << std::endl;
  66. return 0;
  67. }
  68. namespace {
  69. namespace cfg {
  70. typename cv::gapi::wip::onevpl::CfgParam create_from_string(const std::string &line) {
  71. using namespace cv::gapi::wip;
  72. if (line.empty()) {
  73. throw std::runtime_error("Cannot parse CfgParam from emply line");
  74. }
  75. std::string::size_type name_endline_pos = line.find(':');
  76. if (name_endline_pos == std::string::npos) {
  77. throw std::runtime_error("Cannot parse CfgParam from: " + line +
  78. "\nExpected separator \":\"");
  79. }
  80. std::string name = line.substr(0, name_endline_pos);
  81. std::string value = line.substr(name_endline_pos + 1);
  82. return cv::gapi::wip::onevpl::CfgParam::create(name, value,
  83. /* vpp params strongly optional */
  84. name.find("vpp.") == std::string::npos);
  85. }
  86. } // namespace cfg
  87. } // anonymous namespace