Merge pull request #29806 from varun-jaiswal17:dnn-ort-utf8-path-fix

add UTF-8 paths decoding before passing them to ONNX Runtime
This commit is contained in:
Alexander Smorkalov
2026-08-27 18:18:24 +03:00
committed by GitHub
3 changed files with 40 additions and 13 deletions

View File

@@ -2774,7 +2774,7 @@ void Net::Impl::collectOrtProfileData() const
}
// Read the JSON entirely into memory, then parse it from the string.
std::ifstream in(profile_path.get());
std::ifstream in(toOrtPath(profile_path.get()).c_str());
if (!in.is_open()) {
CV_LOG_WARNING(NULL, "DNN/ORT: failed to open profile JSON " << profile_path.get());
return;

View File

@@ -48,6 +48,13 @@ typedef std::unordered_map<std::string, int64_t> NamesHash;
#ifdef HAVE_ONNXRUNTIME
struct OrtNamesCache;
#ifdef _WIN32
typedef std::wstring OrtPathString;
#else
typedef std::string OrtPathString;
#endif
OrtPathString toOrtPath(const std::string& utf8Path);
#endif
/** @brief Single entry in a @ref PerfProfile.

View File

@@ -16,6 +16,9 @@
#ifdef HAVE_ONNXRUNTIME
#include <onnxruntime_cxx_api.h>
#ifdef _WIN32
#include <windows.h>
#endif
#endif
namespace cv {
@@ -23,6 +26,31 @@ namespace dnn {
CV__DNN_INLINE_NS_BEGIN
#ifdef HAVE_ONNXRUNTIME
OrtPathString toOrtPath(const std::string& utf8Path)
{
#ifdef _WIN32
if (utf8Path.empty())
return std::wstring();
const int len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
utf8Path.c_str(), (int)utf8Path.size(), NULL, 0);
if (len <= 0)
{
CV_LOG_WARNING(NULL, "DNN/ONNX/ORT: path is not valid UTF-8, cannot pass it to ONNX Runtime: "
<< utf8Path);
return std::wstring();
}
std::wstring wide((size_t)len, L'\0');
MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
utf8Path.c_str(), (int)utf8Path.size(), &wide[0], len);
return wide;
#else
return utf8Path;
#endif
}
void Net::Impl::refreshOrtMainGraphOutputs()
{
CV_Assert(mainGraph && ort_session);
@@ -96,20 +124,12 @@ void Net::Impl::finalizeOrt()
ort_profile_data.clear();
if (profilingMode != DNN_PROFILE_NONE) {
ort_profile_path_prefix = cv::tempfile("opencv_ort_profile_");
#ifdef _WIN32
std::wstring w_profile_path(ort_profile_path_prefix.begin(), ort_profile_path_prefix.end());
opts.EnableProfiling(w_profile_path.c_str());
#else
opts.EnableProfiling(ort_profile_path_prefix.c_str());
#endif
const OrtPathString profilePath = toOrtPath(ort_profile_path_prefix);
opts.EnableProfiling(profilePath.c_str());
}
#ifdef _WIN32
std::wstring wpath(modelFileName.begin(), modelFileName.end());
ort_session = std::make_shared<Ort::Session>(*ort_env, wpath.c_str(), opts);
#else
ort_session = std::make_shared<Ort::Session>(*ort_env, modelFileName.c_str(), opts);
#endif
const OrtPathString modelPath = toOrtPath(modelFileName);
ort_session = std::make_shared<Ort::Session>(*ort_env, modelPath.c_str(), opts);
preferableTarget = target;
ortNeedsReinit = false;