You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
2.8 KiB
C++

#include "pch.h"
#include "ATW.h"
#include "OCRConfig.h"
#include "LibEasyOCR-CPP.h"
#include "EasyOCR_Detector.h"
#include "EasyOCR_Recognizer.h"
uns::EasyOCR_Detector G_Detector;
uns::EasyOCR_Recognizer G_Recognizer;
uns::OCRConfig& uns::EasyOCR_CPP::GlobalOCRConfig()
{
return G_OCRConfig;
}
void uns::EasyOCR_CPP::SetRecognitionModel(const std::wstring& reco_model, easyocr::CharsetType language)
{
G_OCRConfig.SetLanguage(language);
G_OCRConfig.SetRecognizeModelPath(reco_model);
}
bool uns::EasyOCR_CPP::CleanupOCR()
{
bool detect = G_Detector.UnInit();
bool recognize = G_Recognizer.UnInit();
return (detect && recognize);
}
bool uns::EasyOCR_CPP::InitDetectionModel()
{
return G_Detector.Init();
}
bool uns::EasyOCR_CPP::InitRecognitionModel()
{
return G_Recognizer.Init();
}
uns::easyocr::EOCRD_Rects uns::EasyOCR_CPP::Detect(const cv::Mat& img)
{
if (img.empty() || (!G_Detector.RecheckModelInfo()))
return {};
return G_Detector(img);
}
uns::easyocr::EOCR_Result uns::EasyOCR_CPP::Recognize(const cv::Mat& img)
{
if (img.empty() || (!G_Recognizer.RecheckModelInfo()))
return {};
return G_Recognizer(img);
}
uns::easyocr::EOCR_ResultSet uns::EasyOCR_CPP::Recognize(const cv::Mat& img, const easyocr::EOCRD_Rects& rects)
{
if (img.empty() || (!G_Recognizer.RecheckModelInfo()))
return {};
return G_Recognizer(img, rects);
}
uns::easyocr::EOCR_ResultSet uns::EasyOCR_CPP::FullAuto(const cv::Mat& img)
{
if (img.empty() || (!G_Detector.RecheckModelInfo()) || (!G_Recognizer.RecheckModelInfo()))
return {};
auto rects = G_Detector(img);
if (rects.empty())
return {};
else
return G_Recognizer(img, rects);
}
std::string uns::EOCR_SupportTools::WtoA(const std::wstring& wstr)
{
return ::WtoA(wstr);
}
std::wstring uns::EOCR_SupportTools::AtoW(const std::string& str)
{
return ::AtoW(str);
}
std::string uns::EOCR_SupportTools::AtoUTF8(const std::string& str)
{
return ::AtoUTF8(str);
}
std::string uns::EOCR_SupportTools::UTF8toA(const std::string& utf8)
{
return ::UTF8toA(utf8);
}
cv::Mat uns::EOCR_SupportTools::ReadImage(const std::string& file, bool blur, int blur_size)
{
if (!uns::OCRToolBox::CheckFile(::AtoW(file)))
return {};
try
{
if (!blur)
return cv::imread(file);
else
{
cv::Mat img = cv::imread(file);
if (img.empty())
return {};
cv::blur(img, img, cv::Size(blur_size, blur_size));
return img;
}
}
catch (...)
{
return {};
}
}
cv::Mat uns::EOCR_SupportTools::ReadImage(const std::wstring& file, bool blur, int blur_size)
{
if (!uns::OCRToolBox::CheckFile(file))
return {};
try
{
if (!blur)
return cv::imread(::WtoA(file));
else
{
cv::Mat img = cv::imread(::WtoA(file));
if (img.empty())
return {};
cv::blur(img, img, cv::Size(blur_size, blur_size));
return img;
}
}
catch (...)
{
return {};
}
}