Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: ReadBinaryFromFile supports Chinese path #2560

Merged
merged 4 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 51 additions & 10 deletions fastdeploy/utils/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
#include "fastdeploy/utils/utils.h"

#include <sstream>
#include <fstream>
#include <string_view>

#ifdef _WIN32
#include <Windows.h>
#endif

namespace fastdeploy {

Expand Down Expand Up @@ -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<char*>(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<char*>(chunk.data()), chunksize);
result.insert(result.end(), chunk.data(), chunk.data() + file.gcount());
}
}
return true;
}

Expand Down
6 changes: 5 additions & 1 deletion fastdeploy/vision/ocr/ppocr/rec_postprocessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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<std::string> 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
Expand Down
Loading