feat: 集成飞桨EN模型

This commit is contained in:
MistEO
2022-10-24 23:01:47 +08:00
parent 668928c4a3
commit 2b80f65fd6
9 changed files with 130 additions and 8 deletions

View File

@@ -0,0 +1 @@
en_PP-OCRv3_det_slim_infer

View File

@@ -0,0 +1,95 @@
0
1
2
3
4
5
6
7
8
9
:
;
<
=
>
?
@
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
[
\
]
^
_
`
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
{
|
}
~
!
"
#
$
%
&
'
(
)
*
+
,
-
.
/

View File

@@ -0,0 +1 @@
en_PP-OCRv3_rec_slim_infer

View File

@@ -75,7 +75,12 @@ bool asst::OcrImageAnalyzer::analyze()
m_roi.height = m_image.rows - m_roi.y;
}
m_ocr_result = OcrPack::get_instance().recognize(m_image, m_roi, all_pred, m_without_det);
OcrPack* ocr_ptr = &WordOcr::get_instance();
if (m_use_char_model) {
ocr_ptr = &CharOcr::get_instance();
}
m_ocr_result = ocr_ptr->recognize(m_image, m_roi, all_pred, m_without_det);
ocr_ptr = nullptr;
// log.trace("ocr result", m_ocr_result);
return !m_ocr_result.empty();
@@ -149,6 +154,11 @@ void asst::OcrImageAnalyzer::set_region_of_appeared(Rect region) noexcept
}
}
void asst::OcrImageAnalyzer::set_use_char_model(bool enable) noexcept
{
m_use_char_model = enable;
}
void asst::OcrImageAnalyzer::set_pred(const TextRectProc& pred)
{
m_pred = pred;

View File

@@ -32,6 +32,7 @@ namespace asst
virtual void set_use_cache(bool is_use) noexcept;
virtual void set_region_of_appeared(Rect region) noexcept;
virtual void set_use_char_model(bool enable) noexcept;
void set_pred(const TextRectProc& pred);
virtual const std::vector<TextRect>& get_result() const noexcept;
@@ -48,5 +49,6 @@ namespace asst
bool m_without_det = false;
bool m_use_cache = false;
Rect m_region_of_appeared;
bool m_use_char_model = false;
};
}

View File

@@ -5,6 +5,7 @@
#include "Utils/NoWarningCV.h"
#include <PaddleOCR/paddle_ocr.h>
#include "Utils/Demangle.hpp"
#include "Utils/Logger.hpp"
#include "Utils/Platform.hpp"
#include "Utils/StringMisc.hpp"
@@ -95,13 +96,14 @@ std::vector<asst::TextRect> asst::OcrPack::recognize(const cv::Mat& image, const
std::vector<uchar> buf;
cv::imencode(".png", image, buf);
std::string type = utils::demangle(typeid(*this).name());
if (!without_det) {
Log.trace("Ocr System");
Log.trace("Ocr System with", type);
PaddleOcrSystem(m_ocr, buf.data(), buf.size(), false, m_boxes_buffer, m_strs_buffer, m_scores_buffer, &size,
nullptr, nullptr);
}
else {
Log.trace("Ocr Rec");
Log.trace("Ocr Rec with", type);
PaddleOcrRec(m_ocr, buf.data(), buf.size(), m_strs_buffer, m_scores_buffer, &size, nullptr, nullptr);
}

View File

@@ -13,8 +13,9 @@ struct paddle_ocr_t;
namespace asst
{
class OcrPack final : public SingletonHolder<OcrPack>, public AbstractResource
class OcrPack : public AbstractResource
{
protected:
static constexpr size_t MaxBoxSize = 256;
public:
@@ -27,8 +28,7 @@ namespace asst
std::vector<TextRect> recognize(const cv::Mat& image, const Rect& roi, const TextRectProc& pred = nullptr,
bool without_det = false, bool trim = true);
private:
friend class SingletonHolder<OcrPack>;
protected:
OcrPack();
paddle_ocr_t* m_ocr = nullptr;
@@ -38,4 +38,14 @@ namespace asst
char* m_strs_buffer[MaxBoxSize] = { nullptr };
float m_scores_buffer[MaxBoxSize] = { 0 };
};
class WordOcr final : public SingletonHolder<WordOcr>, public OcrPack
{
friend class SingletonHolder<WordOcr>;
};
class CharOcr final : public SingletonHolder<CharOcr>, public OcrPack
{
friend class SingletonHolder<CharOcr>;
};
}

View File

@@ -64,7 +64,8 @@ bool asst::ResourceLoader::load(const std::filesystem::path& path)
/* load 3rd parties resource */
LoadResourceAndCheckRet(TilePack, "Arknights-Tile-Pos"_p / "levels.json"_p);
LoadResourceAndCheckRet(OcrPack, "PaddleOCR"_p);
LoadResourceAndCheckRet(WordOcr, "PaddleOCR"_p);
LoadResourceAndCheckRet(CharOcr, "PaddleCharOCR"_p);
m_loaded = true;

View File

@@ -16,7 +16,7 @@ int main([[maybe_unused]] int argc, char** argv)
bool loaded = AsstLoadResource(cur_path.string().c_str());
// 增量更新国际服的资源
// const auto en_path = cur_path / "resource" / "global" / "YoStarEN";
// const auto en_path = cur_path / "resource" / "global" / "YoStarJP";
// loaded &= AsstLoadResource(en_path.string().c_str());
if (!loaded) {