fix: 修复偶现OCR多一个空格的问题

This commit is contained in:
MistEO
2022-10-11 23:46:20 +08:00
parent aff8b932d6
commit 321f4d6816
3 changed files with 17 additions and 6 deletions

View File

@@ -87,7 +87,7 @@ bool asst::OcrPack::load(const std::filesystem::path& path)
}
std::vector<asst::TextRect> asst::OcrPack::recognize(const cv::Mat& image, const asst::TextRectProc& pred,
bool without_det)
bool without_det, bool trim)
{
size_t size = 0;
@@ -135,6 +135,9 @@ std::vector<asst::TextRect> asst::OcrPack::recognize(const cv::Mat& image, const
cv::rectangle(draw, utils::make_rect<cv::Rect>(rect), cv::Scalar(0, 0, 255), 2);
#endif
raw_result.emplace_back(tr);
if (trim) {
utils::string_trim(tr.text);
}
if (!pred || pred(tr)) {
result.emplace_back(std::move(tr));
}
@@ -146,7 +149,7 @@ std::vector<asst::TextRect> asst::OcrPack::recognize(const cv::Mat& image, const
}
std::vector<asst::TextRect> asst::OcrPack::recognize(const cv::Mat& image, const Rect& roi,
const asst::TextRectProc& pred, bool without_det)
const asst::TextRectProc& pred, bool without_det, bool trim)
{
auto rect_cor = [&roi, &pred, &without_det](TextRect& tr) -> bool {
if (without_det) {
@@ -160,7 +163,7 @@ std::vector<asst::TextRect> asst::OcrPack::recognize(const cv::Mat& image, const
};
Log.trace("OcrPack::recognize | roi:", roi);
cv::Mat roi_img = image(utils::make_rect<cv::Rect>(roi));
return recognize(roi_img, rect_cor, without_det);
return recognize(roi_img, rect_cor, without_det, trim);
}
#ifdef _WIN32

View File

@@ -23,9 +23,9 @@ namespace asst
virtual bool load(const std::filesystem::path& path) override;
std::vector<TextRect> recognize(const cv::Mat& image, const TextRectProc& pred = nullptr,
bool without_det = false);
bool without_det = false, bool trim = true);
std::vector<TextRect> recognize(const cv::Mat& image, const Rect& roi, const TextRectProc& pred = nullptr,
bool without_det = false);
bool without_det = false, bool trim = true);
private:
friend class SingletonHolder<OcrPack>;

View File

@@ -127,6 +127,13 @@ namespace asst::utils
return str;
}
inline void string_trim(std::string& s)
{
auto not_space = [](unsigned char c) { return !std::isspace(c); };
s.erase(ranges::find_if(s | views::reverse, not_space).base(), s.end());
s.erase(s.begin(), ranges::find_if(s, not_space));
}
inline std::string get_format_time()
{
char buff[128] = { 0 };
@@ -306,7 +313,7 @@ namespace asst::utils
bool empty() const noexcept { return user_dir_.empty(); }
const std::filesystem::path& get() const noexcept { return user_dir_; }
bool set(const char* dir)
{
{
auto temp = path(dir);
if (!std::filesystem::exists(temp) || !std::filesystem::is_directory(temp)) {
return false;
@@ -314,6 +321,7 @@ namespace asst::utils
user_dir_ = temp;
return true;
}
private:
std::filesystem::path user_dir_;
};