diff --git a/modules/gapi/include/opencv2/gapi/infer/onnx.hpp b/modules/gapi/include/opencv2/gapi/infer/onnx.hpp index ec5950718e..f985b41d71 100644 --- a/modules/gapi/include/opencv2/gapi/infer/onnx.hpp +++ b/modules/gapi/include/opencv2/gapi/infer/onnx.hpp @@ -11,6 +11,7 @@ #include #include #include // tuple, tuple_size +#include #include #include @@ -156,13 +157,24 @@ struct GAPI_EXPORTS_W_SIMPLE OpenVINO { Constructs OpenVINO parameters based on device type information. - @param dev_type Target device type to use. ("CPU_FP32", "GPU_FP16", etc) + @param dev_type Target device type to use. ("CPU", "GPU", "GPU.0" etc) */ GAPI_WRAP explicit OpenVINO(const std::string &dev_type) : device_type(dev_type) { } + /** @brief Class constructor. + + Constructs OpenVINO parameters based on map of options passed. + + * @param params A map of parameter names and their corresponding string values. + */ + GAPI_WRAP + explicit OpenVINO(const std::map& params) + : params_map(params) { + } + /** @brief Specifies OpenVINO Execution Provider cache dir. This function is used to explicitly specify the path to save and load @@ -173,6 +185,10 @@ struct GAPI_EXPORTS_W_SIMPLE OpenVINO { */ GAPI_WRAP OpenVINO& cfgCacheDir(const std::string &dir) { + if (!params_map.empty()) { + cv::util::throw_error(std::logic_error("ep::OpenVINO cannot be changed if" + "created from the parameters map.")); + } cache_dir = dir; return *this; } @@ -187,6 +203,10 @@ struct GAPI_EXPORTS_W_SIMPLE OpenVINO { */ GAPI_WRAP OpenVINO& cfgNumThreads(size_t nthreads) { + if (!params_map.empty()) { + cv::util::throw_error(std::logic_error("ep::OpenVINO cannot be changed if" + "created from the parameters map.")); + } num_of_threads = nthreads; return *this; } @@ -200,6 +220,10 @@ struct GAPI_EXPORTS_W_SIMPLE OpenVINO { */ GAPI_WRAP OpenVINO& cfgEnableOpenCLThrottling() { + if (!params_map.empty()) { + cv::util::throw_error(std::logic_error("ep::OpenVINO cannot be changed if" + "created from the parameters map.")); + } enable_opencl_throttling = true; return *this; } @@ -216,6 +240,10 @@ struct GAPI_EXPORTS_W_SIMPLE OpenVINO { */ GAPI_WRAP OpenVINO& cfgEnableDynamicShapes() { + if (!params_map.empty()) { + cv::util::throw_error(std::logic_error("ep::OpenVINO cannot be changed if" + "created from the parameters map.")); + } enable_dynamic_shapes = true; return *this; } @@ -225,6 +253,7 @@ struct GAPI_EXPORTS_W_SIMPLE OpenVINO { size_t num_of_threads = 0; bool enable_opencl_throttling = false; bool enable_dynamic_shapes = false; + std::map params_map; }; /** diff --git a/modules/gapi/src/backends/onnx/gonnxbackend.cpp b/modules/gapi/src/backends/onnx/gonnxbackend.cpp index e2ddd3ea59..92b908da70 100644 --- a/modules/gapi/src/backends/onnx/gonnxbackend.cpp +++ b/modules/gapi/src/backends/onnx/gonnxbackend.cpp @@ -178,16 +178,26 @@ static void addTensorRTExecutionProvider(Ort::SessionOptions *session_options, static void addOpenVINOExecutionProvider(Ort::SessionOptions *session_options, const cv::gapi::onnx::ep::OpenVINO &ov_ep) { - OrtOpenVINOProviderOptions options{}; - options.device_type = ov_ep.device_type.c_str(); - options.cache_dir = ov_ep.cache_dir.c_str(); - options.num_of_threads = ov_ep.num_of_threads; - options.enable_opencl_throttling = ov_ep.enable_opencl_throttling; - options.enable_dynamic_shapes = ov_ep.enable_dynamic_shapes; - options.context = nullptr; + std::unordered_map options; try { - session_options->AppendExecutionProvider_OpenVINO(options); + // If the OpenVINO Execution Provider object was initialized with a parameters map, + // those parameters are used directly. + // Otherwise, the function constructs the options map from the individual member + // variables of the OpenVINO object. + if (ov_ep.params_map.empty()) { + options = { + {"device_type", ov_ep.device_type}, + {"cache_dir", ov_ep.cache_dir}, + {"num_of_threads", ov_ep.num_of_threads > 0 ? std::to_string(ov_ep.num_of_threads) : ""}, + {"enable_opencl_throttling", ov_ep.enable_opencl_throttling ? "True" : "False"}, + {"enable_dynamic_shapes", ov_ep.enable_dynamic_shapes ? "True" : "False"}, + }; + } else { + options.insert(ov_ep.params_map.begin(), ov_ep.params_map.end()); + } + // AppendExecutionProvider function expects a const std::unordered_map as its second argument + session_options->AppendExecutionProvider("OpenVINO", options); } catch (const std::exception &e) { std::stringstream ss; ss << "ONNX Backend: Failed to enable OpenVINO"