onevpl_infer_single_roi.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. #include <algorithm>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <cctype>
  5. #include <tuple>
  6. #include <opencv2/imgproc.hpp>
  7. #include <opencv2/gapi.hpp>
  8. #include <opencv2/gapi/core.hpp>
  9. #include <opencv2/gapi/cpu/gcpukernel.hpp>
  10. #include <opencv2/gapi/infer/ie.hpp>
  11. #include <opencv2/gapi/render.hpp>
  12. #include <opencv2/gapi/streaming/onevpl/source.hpp>
  13. #include <opencv2/gapi/streaming/onevpl/data_provider_interface.hpp>
  14. #include <opencv2/highgui.hpp> // CommandLineParser
  15. #include <opencv2/gapi/infer/parsers.hpp>
  16. #ifdef HAVE_INF_ENGINE
  17. #include <inference_engine.hpp> // ParamMap
  18. #ifdef HAVE_DIRECTX
  19. #ifdef HAVE_D3D11
  20. #pragma comment(lib,"d3d11.lib")
  21. // get rid of generate macro max/min/etc from DX side
  22. #define D3D11_NO_HELPERS
  23. #define NOMINMAX
  24. #include <cldnn/cldnn_config.hpp>
  25. #include <d3d11.h>
  26. #pragma comment(lib, "dxgi")
  27. #undef NOMINMAX
  28. #undef D3D11_NO_HELPERS
  29. #endif // HAVE_D3D11
  30. #endif // HAVE_DIRECTX
  31. #endif // HAVE_INF_ENGINE
  32. const std::string about =
  33. "This is an OpenCV-based version of oneVPLSource decoder example";
  34. const std::string keys =
  35. "{ h help | | Print this help message }"
  36. "{ input | | Path to the input demultiplexed video file }"
  37. "{ output | | Path to the output RAW video file. Use .avi extension }"
  38. "{ facem | face-detection-adas-0001.xml | Path to OpenVINO IE face detection model (.xml) }"
  39. "{ faced | AUTO | Target device for face detection model (e.g. AUTO, GPU, VPU, ...) }"
  40. "{ cfg_params | <prop name>:<value>;<prop name>:<value> | Semicolon separated list of oneVPL mfxVariants which is used for configuring source (see `MFXSetConfigFilterProperty` by https://spec.oneapi.io/versions/latest/elements/oneVPL/source/index.html) }"
  41. "{ streaming_queue_capacity | 1 | Streaming executor queue capacity. Calculated automaticaly if 0 }"
  42. "{ frames_pool_size | 0 | OneVPL source applies this parameter as preallocated frames pool size}"
  43. "{ vpp_frames_pool_size | 0 | OneVPL source applies this parameter as preallocated frames pool size for VPP preprocessing results}"
  44. "{ source_preproc_enable | 0 | Turn on OneVPL source frame preprocessing using network input description instead of IE plugin preprocessing}";
  45. namespace {
  46. bool is_gpu(const std::string &device_name) {
  47. return device_name.find("GPU") != std::string::npos;
  48. }
  49. std::string get_weights_path(const std::string &model_path) {
  50. const auto EXT_LEN = 4u;
  51. const auto sz = model_path.size();
  52. CV_Assert(sz > EXT_LEN);
  53. auto ext = model_path.substr(sz - EXT_LEN);
  54. std::transform(ext.begin(), ext.end(), ext.begin(), [](unsigned char c){
  55. return static_cast<unsigned char>(std::tolower(c));
  56. });
  57. CV_Assert(ext == ".xml");
  58. return model_path.substr(0u, sz - EXT_LEN) + ".bin";
  59. }
  60. #ifdef HAVE_INF_ENGINE
  61. #ifdef HAVE_DIRECTX
  62. #ifdef HAVE_D3D11
  63. // Since ATL headers might not be available on specific MSVS Build Tools
  64. // we use simple `CComPtr` implementation like as `ComPtrGuard`
  65. // which is not supposed to be the full functional replacement of `CComPtr`
  66. // and it uses as RAII to make sure utilization is correct
  67. template <typename COMNonManageableType>
  68. void release(COMNonManageableType *ptr) {
  69. if (ptr) {
  70. ptr->Release();
  71. }
  72. }
  73. template <typename COMNonManageableType>
  74. using ComPtrGuard = std::unique_ptr<COMNonManageableType, decltype(&release<COMNonManageableType>)>;
  75. template <typename COMNonManageableType>
  76. ComPtrGuard<COMNonManageableType> createCOMPtrGuard(COMNonManageableType *ptr = nullptr) {
  77. return ComPtrGuard<COMNonManageableType> {ptr, &release<COMNonManageableType>};
  78. }
  79. using AccelParamsType = std::tuple<ComPtrGuard<ID3D11Device>, ComPtrGuard<ID3D11DeviceContext>>;
  80. AccelParamsType create_device_with_ctx(IDXGIAdapter* adapter) {
  81. UINT flags = 0;
  82. D3D_FEATURE_LEVEL feature_levels[] = { D3D_FEATURE_LEVEL_11_1,
  83. D3D_FEATURE_LEVEL_11_0,
  84. };
  85. D3D_FEATURE_LEVEL featureLevel;
  86. ID3D11Device* ret_device_ptr = nullptr;
  87. ID3D11DeviceContext* ret_ctx_ptr = nullptr;
  88. HRESULT err = D3D11CreateDevice(adapter, D3D_DRIVER_TYPE_UNKNOWN,
  89. nullptr, flags,
  90. feature_levels,
  91. ARRAYSIZE(feature_levels),
  92. D3D11_SDK_VERSION, &ret_device_ptr,
  93. &featureLevel, &ret_ctx_ptr);
  94. if (FAILED(err)) {
  95. throw std::runtime_error("Cannot create D3D11CreateDevice, error: " +
  96. std::to_string(HRESULT_CODE(err)));
  97. }
  98. return std::make_tuple(createCOMPtrGuard(ret_device_ptr),
  99. createCOMPtrGuard(ret_ctx_ptr));
  100. }
  101. #endif // HAVE_D3D11
  102. #endif // HAVE_DIRECTX
  103. #endif // HAVE_INF_ENGINE
  104. } // anonymous namespace
  105. namespace custom {
  106. G_API_NET(FaceDetector, <cv::GMat(cv::GMat)>, "face-detector");
  107. using GDetections = cv::GArray<cv::Rect>;
  108. using GRect = cv::GOpaque<cv::Rect>;
  109. using GSize = cv::GOpaque<cv::Size>;
  110. using GPrims = cv::GArray<cv::gapi::wip::draw::Prim>;
  111. G_API_OP(LocateROI, <GRect(GSize, std::reference_wrapper<const std::string>)>, "sample.custom.locate-roi") {
  112. static cv::GOpaqueDesc outMeta(const cv::GOpaqueDesc &,
  113. std::reference_wrapper<const std::string>) {
  114. return cv::empty_gopaque_desc();
  115. }
  116. };
  117. G_API_OP(BBoxes, <GPrims(GDetections, GRect)>, "sample.custom.b-boxes") {
  118. static cv::GArrayDesc outMeta(const cv::GArrayDesc &, const cv::GOpaqueDesc &) {
  119. return cv::empty_array_desc();
  120. }
  121. };
  122. GAPI_OCV_KERNEL(OCVLocateROI, LocateROI) {
  123. // This is the place where we can run extra analytics
  124. // on the input image frame and select the ROI (region
  125. // of interest) where we want to detect our objects (or
  126. // run any other inference).
  127. //
  128. // Currently it doesn't do anything intelligent,
  129. // but only crops the input image to square (this is
  130. // the most convenient aspect ratio for detectors to use)
  131. static void run(const cv::Size& in_size,
  132. std::reference_wrapper<const std::string> device_id_ref,
  133. cv::Rect &out_rect) {
  134. // Identify the central point & square size (- some padding)
  135. // NB: GPU plugin in InferenceEngine doesn't support ROI at now
  136. if (!is_gpu(device_id_ref.get())) {
  137. const auto center = cv::Point{in_size.width/2, in_size.height/2};
  138. auto sqside = std::min(in_size.width, in_size.height);
  139. // Now build the central square ROI
  140. out_rect = cv::Rect{ center.x - sqside/2
  141. , center.y - sqside/2
  142. , sqside
  143. , sqside
  144. };
  145. } else {
  146. // use whole frame for GPU device
  147. out_rect = cv::Rect{ 0
  148. , 0
  149. , in_size.width
  150. , in_size.height
  151. };
  152. }
  153. }
  154. };
  155. GAPI_OCV_KERNEL(OCVBBoxes, BBoxes) {
  156. // This kernel converts the rectangles into G-API's
  157. // rendering primitives
  158. static void run(const std::vector<cv::Rect> &in_face_rcs,
  159. const cv::Rect &in_roi,
  160. std::vector<cv::gapi::wip::draw::Prim> &out_prims) {
  161. out_prims.clear();
  162. const auto cvt = [](const cv::Rect &rc, const cv::Scalar &clr) {
  163. return cv::gapi::wip::draw::Rect(rc, clr, 2);
  164. };
  165. out_prims.emplace_back(cvt(in_roi, CV_RGB(0,255,255))); // cyan
  166. for (auto &&rc : in_face_rcs) {
  167. out_prims.emplace_back(cvt(rc, CV_RGB(0,255,0))); // green
  168. }
  169. }
  170. };
  171. } // namespace custom
  172. namespace cfg {
  173. typename cv::gapi::wip::onevpl::CfgParam create_from_string(const std::string &line);
  174. }
  175. int main(int argc, char *argv[]) {
  176. cv::CommandLineParser cmd(argc, argv, keys);
  177. cmd.about(about);
  178. if (cmd.has("help")) {
  179. cmd.printMessage();
  180. return 0;
  181. }
  182. // get file name
  183. const auto file_path = cmd.get<std::string>("input");
  184. const auto output = cmd.get<std::string>("output");
  185. const auto face_model_path = cmd.get<std::string>("facem");
  186. const auto streaming_queue_capacity = cmd.get<uint32_t>("streaming_queue_capacity");
  187. const auto source_decode_queue_capacity = cmd.get<uint32_t>("frames_pool_size");
  188. const auto source_vpp_queue_capacity = cmd.get<uint32_t>("vpp_frames_pool_size");
  189. const auto vpl_source_preproc_enable = cmd.get<uint32_t>("source_preproc_enable");
  190. const auto device_id = cmd.get<std::string>("faced");
  191. // check ouput file extension
  192. if (!output.empty()) {
  193. auto ext = output.find_last_of(".");
  194. if (ext == std::string::npos || (output.substr(ext + 1) != "avi")) {
  195. std::cerr << "Output file should have *.avi extension for output video" << std::endl;
  196. return -1;
  197. }
  198. }
  199. // get oneVPL cfg params from cmd
  200. std::stringstream params_list(cmd.get<std::string>("cfg_params"));
  201. std::vector<cv::gapi::wip::onevpl::CfgParam> source_cfgs;
  202. try {
  203. std::string line;
  204. while (std::getline(params_list, line, ';')) {
  205. if (vpl_source_preproc_enable == 0) {
  206. if (line.find("vpp.") != std::string::npos) {
  207. // skip VPP preprocessing primitives if not requested
  208. continue;
  209. }
  210. }
  211. source_cfgs.push_back(cfg::create_from_string(line));
  212. }
  213. } catch (const std::exception& ex) {
  214. std::cerr << "Invalid cfg parameter: " << ex.what() << std::endl;
  215. return -1;
  216. }
  217. if (source_decode_queue_capacity != 0) {
  218. source_cfgs.push_back(cv::gapi::wip::onevpl::CfgParam::create_frames_pool_size(source_decode_queue_capacity));
  219. }
  220. if (source_vpp_queue_capacity != 0) {
  221. source_cfgs.push_back(cv::gapi::wip::onevpl::CfgParam::create_vpp_frames_pool_size(source_vpp_queue_capacity));
  222. }
  223. auto face_net = cv::gapi::ie::Params<custom::FaceDetector> {
  224. face_model_path, // path to topology IR
  225. get_weights_path(face_model_path), // path to weights
  226. device_id
  227. };
  228. // Create device_ptr & context_ptr using graphic API
  229. // InferenceEngine requires such device & context to create its own
  230. // remote shared context through InferenceEngine::ParamMap in
  231. // GAPI InferenceEngine backend to provide interoperability with onevpl::GSource
  232. // So GAPI InferenceEngine backend and onevpl::GSource MUST share the same
  233. // device and context
  234. void* accel_device_ptr = nullptr;
  235. void* accel_ctx_ptr = nullptr;
  236. #ifdef HAVE_INF_ENGINE
  237. #ifdef HAVE_DIRECTX
  238. #ifdef HAVE_D3D11
  239. auto dx11_dev = createCOMPtrGuard<ID3D11Device>();
  240. auto dx11_ctx = createCOMPtrGuard<ID3D11DeviceContext>();
  241. if (is_gpu(device_id)) {
  242. auto adapter_factory = createCOMPtrGuard<IDXGIFactory>();
  243. {
  244. IDXGIFactory* out_factory = nullptr;
  245. HRESULT err = CreateDXGIFactory(__uuidof(IDXGIFactory),
  246. reinterpret_cast<void**>(&out_factory));
  247. if (FAILED(err)) {
  248. std::cerr << "Cannot create CreateDXGIFactory, error: " << HRESULT_CODE(err) << std::endl;
  249. return -1;
  250. }
  251. adapter_factory = createCOMPtrGuard(out_factory);
  252. }
  253. auto intel_adapter = createCOMPtrGuard<IDXGIAdapter>();
  254. UINT adapter_index = 0;
  255. const unsigned int refIntelVendorID = 0x8086;
  256. IDXGIAdapter* out_adapter = nullptr;
  257. while (adapter_factory->EnumAdapters(adapter_index, &out_adapter) != DXGI_ERROR_NOT_FOUND) {
  258. DXGI_ADAPTER_DESC desc{};
  259. out_adapter->GetDesc(&desc);
  260. if (desc.VendorId == refIntelVendorID) {
  261. intel_adapter = createCOMPtrGuard(out_adapter);
  262. break;
  263. }
  264. ++adapter_index;
  265. }
  266. if (!intel_adapter) {
  267. std::cerr << "No Intel GPU adapter on aboard. Exit" << std::endl;
  268. return -1;
  269. }
  270. std::tie(dx11_dev, dx11_ctx) = create_device_with_ctx(intel_adapter.get());
  271. accel_device_ptr = reinterpret_cast<void*>(dx11_dev.get());
  272. accel_ctx_ptr = reinterpret_cast<void*>(dx11_ctx.get());
  273. // put accel type description for VPL source
  274. source_cfgs.push_back(cfg::create_from_string(
  275. "mfxImplDescription.AccelerationMode"
  276. ":"
  277. "MFX_ACCEL_MODE_VIA_D3D11"));
  278. }
  279. #endif // HAVE_D3D11
  280. #endif // HAVE_DIRECTX
  281. // set ctx_config for GPU device only - no need in case of CPU device type
  282. if (is_gpu(device_id)) {
  283. InferenceEngine::ParamMap ctx_config({{"CONTEXT_TYPE", "VA_SHARED"},
  284. {"VA_DEVICE", accel_device_ptr} });
  285. face_net.cfgContextParams(ctx_config);
  286. face_net.pluginConfig({{"GPU_NV12_TWO_INPUTS", "YES" }});
  287. std::cout <<"/*******************************************************/\n"
  288. "ATTENTION: GPU Inference Engine preprocessing is not vital as expected!"
  289. " Please consider param \"source_preproc_enable=1\" and specify "
  290. " appropriated media frame transformation using oneVPL::VPP primitives"
  291. " which force onevpl::GSource to produce tranformed media frames."
  292. " For exploring list of supported transformations please find out "
  293. " vpp_* related stuff in"
  294. " gapi/include/opencv2/gapi/streaming/onevpl/cfg_params.hpp"
  295. " Pay attention that to obtain expected result In this case VPP "
  296. " transformation must match network input params."
  297. " Please vote/create issue about exporting network params using GAPI\n"
  298. "/******************************************************/" << std::endl;
  299. }
  300. #endif // HAVE_INF_ENGINE
  301. auto kernels = cv::gapi::kernels
  302. < custom::OCVLocateROI
  303. , custom::OCVBBoxes>();
  304. auto networks = cv::gapi::networks(face_net);
  305. auto face_detection_args = cv::compile_args(networks, kernels);
  306. if (streaming_queue_capacity != 0) {
  307. face_detection_args += cv::compile_args(cv::gapi::streaming::queue_capacity{ streaming_queue_capacity });
  308. }
  309. // Create source
  310. cv::Ptr<cv::gapi::wip::IStreamSource> cap;
  311. try {
  312. if (is_gpu(device_id)) {
  313. cap = cv::gapi::wip::make_onevpl_src(file_path, source_cfgs,
  314. device_id,
  315. accel_device_ptr,
  316. accel_ctx_ptr);
  317. } else {
  318. cap = cv::gapi::wip::make_onevpl_src(file_path, source_cfgs);
  319. }
  320. std::cout << "oneVPL source desription: " << cap->descr_of() << std::endl;
  321. } catch (const std::exception& ex) {
  322. std::cerr << "Cannot create source: " << ex.what() << std::endl;
  323. return -1;
  324. }
  325. cv::GMetaArg descr = cap->descr_of();
  326. auto frame_descr = cv::util::get<cv::GFrameDesc>(descr);
  327. // Now build the graph
  328. cv::GFrame in;
  329. auto size = cv::gapi::streaming::size(in);
  330. auto roi = custom::LocateROI::on(size, std::cref(device_id));
  331. auto blob = cv::gapi::infer<custom::FaceDetector>(roi, in);
  332. cv::GArray<cv::Rect> rcs = cv::gapi::parseSSD(blob, size, 0.5f, true, true);
  333. auto out_frame = cv::gapi::wip::draw::renderFrame(in, custom::BBoxes::on(rcs, roi));
  334. auto out = cv::gapi::streaming::BGR(out_frame);
  335. cv::GStreamingCompiled pipeline;
  336. try {
  337. pipeline = cv::GComputation(cv::GIn(in), cv::GOut(out))
  338. .compileStreaming(std::move(face_detection_args));
  339. } catch (const std::exception& ex) {
  340. std::cerr << "Exception occured during pipeline construction: " << ex.what() << std::endl;
  341. return -1;
  342. }
  343. // The execution part
  344. // TODO USE may set pool size from outside and set queue_capacity size,
  345. // compile arg: cv::gapi::streaming::queue_capacity
  346. pipeline.setSource(std::move(cap));
  347. pipeline.start();
  348. size_t frames = 0u;
  349. cv::TickMeter tm;
  350. cv::VideoWriter writer;
  351. if (!output.empty() && !writer.isOpened()) {
  352. const auto sz = cv::Size{frame_descr.size.width, frame_descr.size.height};
  353. writer.open(output, cv::VideoWriter::fourcc('M','J','P','G'), 25.0, sz);
  354. CV_Assert(writer.isOpened());
  355. }
  356. cv::Mat outMat;
  357. tm.start();
  358. while (pipeline.pull(cv::gout(outMat))) {
  359. cv::imshow("Out", outMat);
  360. cv::waitKey(1);
  361. if (!output.empty()) {
  362. writer << outMat;
  363. }
  364. ++frames;
  365. }
  366. tm.stop();
  367. std::cout << "Processed " << frames << " frames" << " (" << frames / tm.getTimeSec() << " FPS)" << std::endl;
  368. return 0;
  369. }
  370. namespace cfg {
  371. typename cv::gapi::wip::onevpl::CfgParam create_from_string(const std::string &line) {
  372. using namespace cv::gapi::wip;
  373. if (line.empty()) {
  374. throw std::runtime_error("Cannot parse CfgParam from emply line");
  375. }
  376. std::string::size_type name_endline_pos = line.find(':');
  377. if (name_endline_pos == std::string::npos) {
  378. throw std::runtime_error("Cannot parse CfgParam from: " + line +
  379. "\nExpected separator \":\"");
  380. }
  381. std::string name = line.substr(0, name_endline_pos);
  382. std::string value = line.substr(name_endline_pos + 1);
  383. return cv::gapi::wip::onevpl::CfgParam::create(name, value,
  384. /* vpp params strongly optional */
  385. name.find("vpp.") == std::string::npos);
  386. }
  387. }