diff --git a/fastdeploy/utils/utils.cc b/fastdeploy/utils/utils.cc index c39b6adab8..5b95427d82 100644 --- a/fastdeploy/utils/utils.cc +++ b/fastdeploy/utils/utils.cc @@ -15,6 +15,12 @@ #include "fastdeploy/utils/utils.h" #include +#include +#include + +#ifdef _WIN32 +#include +#endif namespace fastdeploy { @@ -48,18 +54,53 @@ FDLogger& FDLogger::operator<<(std::ostream& (*os)(std::ostream&)) { return *this; } -bool ReadBinaryFromFile(const std::string& file, std::string* contents) { - std::ifstream fin(file, std::ios::in | std::ios::binary); - if (!fin.is_open()) { - FDERROR << "Failed to open file: " << file << " to read." << std::endl; +// using os_string = std::filesystem::path::string_type; +#ifdef _WIN32 +using os_string = std::wstring; +#else +using os_string = std::string; +#endif + +os_string to_osstring(std::string_view utf8_str) +{ +#ifdef _WIN32 + int len = MultiByteToWideChar(CP_UTF8, 0, utf8_str.data(), (int)utf8_str.size(), nullptr, 0); + os_string result(len, 0); + MultiByteToWideChar(CP_UTF8, 0, utf8_str.data(), (int)utf8_str.size(), result.data(), len); + return result; +#else + return std::string(utf8_str); +#endif +} + +bool ReadBinaryFromFile(const std::string& path, std::string* contents) +{ + if (!contents) { + return false; + } + auto& result = *contents; + result.clear(); + + std::ifstream file(to_osstring(path), std::ios::binary | std::ios::ate); + if (!file.is_open()) { return false; } - fin.seekg(0, std::ios::end); - contents->clear(); - contents->resize(fin.tellg()); - fin.seekg(0, std::ios::beg); - fin.read(&(contents->at(0)), contents->size()); - fin.close(); + + auto fileSize = file.tellg(); + if (fileSize != -1) { + result.resize(fileSize); + file.seekg(0, std::ios::beg); + file.read(const_cast(result.data()), fileSize); + } + else { + // no size available, read to EOF + constexpr auto chunksize = 4096; + std::string chunk(chunksize, 0); + while (!file.fail()) { + file.read(const_cast(chunk.data()), chunksize); + result.insert(result.end(), chunk.data(), chunk.data() + file.gcount()); + } + } return true; } diff --git a/fastdeploy/vision/ocr/ppocr/rec_postprocessor.cc b/fastdeploy/vision/ocr/ppocr/rec_postprocessor.cc index d93c16907a..84402897e2 100644 --- a/fastdeploy/vision/ocr/ppocr/rec_postprocessor.cc +++ b/fastdeploy/vision/ocr/ppocr/rec_postprocessor.cc @@ -15,17 +15,21 @@ #include "fastdeploy/vision/ocr/ppocr/rec_postprocessor.h" #include "fastdeploy/utils/perf.h" #include "fastdeploy/vision/ocr/ppocr/utils/ocr_utils.h" +#include "fastdeploy/utils/utils.h" namespace fastdeploy { namespace vision { namespace ocr { std::vector ReadDict(const std::string& path) { - std::ifstream in(path); + std::string content; + ReadBinaryFromFile(path, &content); + std::stringstream in(std::move(content)); FDASSERT(in, "Cannot open file %s to read.", path.c_str()); std::string line; std::vector m_vec; while (getline(in, line)) { + if (!line.empty() && *line.rbegin() == '\r') line.pop_back(); m_vec.push_back(line); } m_vec.insert(m_vec.begin(), "#"); // blank char for ctc