add dll version and its usage
parent
b7d97547be
commit
d070976d35
@ -0,0 +1,146 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{4855fd5b-4d48-4ce9-8ac1-8be3d89a206d}</ProjectGuid>
|
||||
<RootNamespace>DLLUsageDemo</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<IncludePath>F:\vcpkg\installed\x64-windows\include\opencv4;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<IncludePath>F:\vcpkg\installed\x64-windows\include\opencv4;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="DLLUsageDemo.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="LibEasyOCR-CPP.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="源文件">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="头文件">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="资源文件">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="DLLUsageDemo.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="LibEasyOCR-CPP.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -0,0 +1,89 @@
|
||||
#pragma once
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <opencv2/core/core.hpp>
|
||||
|
||||
#ifdef LIBEASYOCRCPP_EXPORTS
|
||||
#define LIBEASYOCRCPP_API _declspec(dllexport)
|
||||
#else
|
||||
#define LIBEASYOCRCPP_API _declspec(dllimport)
|
||||
#ifdef _DEBUG
|
||||
#pragma comment(lib, "LibEasyOCR-CPPd.lib")
|
||||
#else
|
||||
#pragma comment(lib, "LibEasyOCR-CPP.lib")
|
||||
#endif
|
||||
#endif
|
||||
|
||||
namespace uns
|
||||
{
|
||||
namespace easyocr
|
||||
{
|
||||
enum class LIBEASYOCRCPP_API GPUUsage
|
||||
{
|
||||
ForceGPU,
|
||||
PreferGPU,
|
||||
CPUOnly
|
||||
};
|
||||
|
||||
enum class LIBEASYOCRCPP_API CharsetType
|
||||
{
|
||||
EN,
|
||||
EN_CH
|
||||
};
|
||||
|
||||
using EOCR_Result = std::pair<std::wstring, float>;
|
||||
using EOCR_ResultSet = std::map<size_t, EOCR_Result>;
|
||||
using EOCRD_Rects = std::vector<std::vector<cv::Point2f>>;
|
||||
}
|
||||
|
||||
class LIBEASYOCRCPP_API OCRConfig
|
||||
{
|
||||
public:
|
||||
OCRConfig();
|
||||
OCRConfig(const OCRConfig& obj) = delete;
|
||||
OCRConfig(const std::wstring& detect_model, const std::wstring& reco_model, easyocr::CharsetType language, easyocr::GPUUsage gpu = easyocr::GPUUsage::PreferGPU);
|
||||
|
||||
public:
|
||||
easyocr::GPUUsage GetGPUUsage() const;
|
||||
easyocr::CharsetType GetLanguage() const;
|
||||
|
||||
std::wstring GetDetectModelPath() const;
|
||||
std::wstring GetRecognizeModelPath() const;
|
||||
|
||||
public:
|
||||
void SetGPUUsage(easyocr::GPUUsage usage);
|
||||
void SetLanguage(easyocr::CharsetType type);
|
||||
void SetDetectModelPath(const std::wstring& path);
|
||||
void SetRecognizeModelPath(const std::wstring& path);
|
||||
};
|
||||
|
||||
class LIBEASYOCRCPP_API EasyOCR_CPP
|
||||
{
|
||||
public:
|
||||
static OCRConfig& GlobalOCRConfig();
|
||||
static void SetRecognitionModel(const std::wstring& reco_model, easyocr::CharsetType language);
|
||||
|
||||
static bool CleanupOCR();
|
||||
static bool InitDetectionModel();
|
||||
static bool InitRecognitionModel();
|
||||
|
||||
static easyocr::EOCRD_Rects Detect(const cv::Mat& img);
|
||||
static easyocr::EOCR_Result Recognize(const cv::Mat& img);
|
||||
static easyocr::EOCR_ResultSet Recognize(const cv::Mat& img, const easyocr::EOCRD_Rects& rects);
|
||||
|
||||
static easyocr::EOCR_ResultSet FullAuto(const cv::Mat& img);
|
||||
};
|
||||
|
||||
class LIBEASYOCRCPP_API EOCR_SupportTools
|
||||
{
|
||||
public:
|
||||
static std::string WtoA(const std::wstring& wstr);
|
||||
static std::wstring AtoW(const std::string& str);
|
||||
static std::string AtoUTF8(const std::string& str);
|
||||
static std::string UTF8toA(const std::string& utf8);
|
||||
|
||||
static cv::Mat ReadImage(const std::string& file, bool blur = false, int blur_size = 3);
|
||||
static cv::Mat ReadImage(const std::wstring& file, bool blur = false, int blur_size = 3);
|
||||
};
|
||||
}
|
Binary file not shown.
Binary file not shown.
@ -0,0 +1,214 @@
|
||||
/* Copyright (C) 2011 阙荣文
|
||||
*
|
||||
* 这是一个开源免费软件,您可以自由的修改和发布.
|
||||
* 禁止用作商业用途.
|
||||
*
|
||||
* 联系原作者: querw@sina.com
|
||||
*/
|
||||
|
||||
// ATW.h: interface for the CBase64 class.
|
||||
// by Ted.Que - Que's C++ Studio
|
||||
// 2010-11-12
|
||||
// 转换字符编码
|
||||
#include "pch.h"
|
||||
#include "ATW.h"
|
||||
|
||||
|
||||
std::string __do_w_to_a_utf8(const wchar_t* pwszText, UINT uCodePage)
|
||||
{
|
||||
// 空指针输入
|
||||
if (pwszText == NULL) return "";
|
||||
|
||||
// 无法计算需要的长度.
|
||||
int nNeedSize = WideCharToMultiByte(uCodePage, 0, pwszText, -1, NULL, 0, NULL, NULL);
|
||||
if (0 == nNeedSize) return "";
|
||||
|
||||
// 分配空间,转换.
|
||||
char* pRet = new char[nNeedSize + 1]; // 虽然返回WideCharToMultiByte的长度是包含 null 字符的长度, 还是多+一个字符.
|
||||
memset(pRet, 0, nNeedSize + 1);
|
||||
|
||||
std::string strRet("");
|
||||
if (0 == WideCharToMultiByte(uCodePage, 0, pwszText, -1, pRet, nNeedSize, NULL, NULL))
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
strRet = pRet;
|
||||
}
|
||||
|
||||
delete[]pRet;
|
||||
return strRet;
|
||||
}
|
||||
|
||||
std::wstring __do_a_utf8_to_w(const char* pszText, UINT uCodePage)
|
||||
{
|
||||
// 空指针
|
||||
if (pszText == NULL) return L"";
|
||||
|
||||
// 计算长度
|
||||
int nNeedSize = MultiByteToWideChar(uCodePage, 0, pszText, -1, NULL, 0);
|
||||
if (0 == nNeedSize) return L"";
|
||||
|
||||
// 分配空间,转换
|
||||
std::wstring strRet(L"");
|
||||
wchar_t* pRet = new wchar_t[nNeedSize + 1];
|
||||
memset(pRet, 0, (nNeedSize + 1) * sizeof(wchar_t));
|
||||
if (0 == MultiByteToWideChar(uCodePage, 0, pszText, -1, pRet, nNeedSize))
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
strRet = pRet;
|
||||
}
|
||||
delete[]pRet;
|
||||
return strRet;
|
||||
}
|
||||
|
||||
std::string WtoA(const std::wstring& strText)
|
||||
{
|
||||
return __do_w_to_a_utf8(strText.c_str(), CP_ACP);
|
||||
}
|
||||
|
||||
std::string WtoA(const wchar_t* pwszText)
|
||||
{
|
||||
return __do_w_to_a_utf8(pwszText, CP_ACP);
|
||||
}
|
||||
|
||||
std::wstring AtoW(const std::string& strText)
|
||||
{
|
||||
return __do_a_utf8_to_w(strText.c_str(), CP_ACP);
|
||||
}
|
||||
|
||||
std::wstring AtoW(const char* pszText)
|
||||
{
|
||||
return __do_a_utf8_to_w(pszText, CP_ACP);
|
||||
}
|
||||
|
||||
std::string WtoUTF8(const std::wstring& strText)
|
||||
{
|
||||
return __do_w_to_a_utf8(strText.c_str(), CP_UTF8);
|
||||
}
|
||||
|
||||
std::string WtoUTF8(const wchar_t* pwszText)
|
||||
{
|
||||
return __do_w_to_a_utf8(pwszText, CP_UTF8);
|
||||
}
|
||||
|
||||
std::wstring UTF8toW(const std::string& strText)
|
||||
{
|
||||
return __do_a_utf8_to_w(strText.c_str(), CP_UTF8);
|
||||
}
|
||||
|
||||
std::wstring UTF8toW(const char* pszText)
|
||||
{
|
||||
return __do_a_utf8_to_w(pszText, CP_UTF8);
|
||||
}
|
||||
|
||||
std::string UTF8toA(const std::string& src)
|
||||
{
|
||||
return WtoA(UTF8toW(src));
|
||||
}
|
||||
|
||||
std::string UTF8toA(const char* src)
|
||||
{
|
||||
return WtoA(UTF8toW(src));
|
||||
}
|
||||
|
||||
std::string AtoUTF8(const std::string& src)
|
||||
{
|
||||
return WtoUTF8(AtoW(src));
|
||||
}
|
||||
|
||||
std::string AtoUTF8(const char* src)
|
||||
{
|
||||
return WtoUTF8(AtoW(src));
|
||||
}
|
||||
/*
|
||||
UTF-8 编码最多可以有6个字节
|
||||
|
||||
1字节 0xxxxxxx
|
||||
2字节 110xxxxx 10xxxxxx
|
||||
3字节 1110xxxx 10xxxxxx 10xxxxxx
|
||||
4字节 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
||||
5字节 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
|
||||
6字节 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
|
||||
|
||||
*/
|
||||
|
||||
// 返回值说明:
|
||||
// 0 -> 输入字符串符合UTF-8编码规则
|
||||
// -1 -> 检测到非法的UTF-8编码首字节
|
||||
// -2 -> 检测到非法的UTF-8字节编码的后续字节.
|
||||
|
||||
int IsTextUTF8(const char* pszSrc)
|
||||
{
|
||||
const unsigned char* puszSrc = (const unsigned char*)pszSrc; // 一定要无符号的,有符号的比较就不正确了.
|
||||
// 看看有没有BOM表示 EF BB BF
|
||||
if (puszSrc[0] != 0 && puszSrc[0] == 0xEF &&
|
||||
puszSrc[1] != 0 && puszSrc[1] == 0xBB &&
|
||||
puszSrc[2] != 0 && puszSrc[2] == 0xBF)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 如果没有 BOM标识
|
||||
bool bIsNextByte = false;
|
||||
int nBytes = 0; // 记录一个字符的UTF8编码已经占用了几个字节.
|
||||
const unsigned char* pCur = (const unsigned char*)pszSrc; // 指针游标用无符号字符型. 因为高位为1, 如果用 char 型, 会变为负数,不利于编程时候的比较操作.
|
||||
|
||||
while (pCur[0] != 0)
|
||||
{
|
||||
if (!bIsNextByte)
|
||||
{
|
||||
bIsNextByte = true;
|
||||
if ((pCur[0] >> 7) == 0)
|
||||
{
|
||||
bIsNextByte = false; nBytes = 1; bIsNextByte = false;
|
||||
} // 最高位为0, ANSI 兼容的.
|
||||
else if ((pCur[0] >> 5) == 0x06)
|
||||
{
|
||||
nBytes = 2;
|
||||
} // 右移5位后是 110 -> 2字节编码的UTF8字符的首字节
|
||||
else if ((pCur[0] >> 4) == 0x0E)
|
||||
{
|
||||
nBytes = 3;
|
||||
} // 右移4位后是 1110 -> 3字节编码的UTF8字符的首字节
|
||||
else if ((pCur[0] >> 3) == 0x1E)
|
||||
{
|
||||
nBytes = 4;
|
||||
} // 右移3位后是 11110 -> 4字节编码的UTF8字符的首字节
|
||||
else if ((pCur[0] >> 2) == 0x3E)
|
||||
{
|
||||
nBytes = 5;
|
||||
} // 右移2位后是 111110 -> 5字节编码的UTF8字符的首字节
|
||||
else if ((pCur[0] >> 1) == 0x7E)
|
||||
{
|
||||
nBytes = 6;
|
||||
} // 右移1位后是 1111110 -> 6字节编码的UTF8字符的首字节
|
||||
else
|
||||
{
|
||||
nBytes = -1; // 非法的UTF8字符编码的首字节
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((pCur[0] >> 6) == 0x02) // 首先,后续字节必须以 10xxx 开头
|
||||
{
|
||||
nBytes--;
|
||||
if (nBytes == 1) bIsNextByte = false; // 当 nBytes = 1时, 说明下一个字节应该是首字节.
|
||||
}
|
||||
else
|
||||
{
|
||||
nBytes = -2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 下跳一个字符
|
||||
pCur++;
|
||||
}
|
||||
|
||||
if (nBytes == 1) return 0;
|
||||
else return nBytes;
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
/* Copyright (C) 2011 阙荣文
|
||||
*
|
||||
* 这是一个开源免费软件,您可以自由的修改和发布.
|
||||
* 禁止用作商业用途.
|
||||
*
|
||||
* 联系原作者: querw@sina.com
|
||||
*/
|
||||
|
||||
/*
|
||||
|
||||
1. 实现USC的压缩,Unicode <-> UTF-8.
|
||||
2. 实现多字节码 <-> Unicode
|
||||
|
||||
对于Unicode字符串,用 wstring 存储.
|
||||
对于非Unicode字符串用 string 存储. ANSI, GB2312 都一样
|
||||
对于UTF-8串,也用 string 存储, UTF-8的编码串中不会有null出现.
|
||||
*/
|
||||
|
||||
/*
|
||||
不要使用ATL 中的 USES_CONVERSION; A2W, A2T, W2A 等的宏, 由于这些宏都调用 alloca() 函数在函数栈中分配内存.
|
||||
虽然非常方便,函数返回后自动回收, 但是有溢出的危险, 函数栈只有 1M 的大小.
|
||||
|
||||
以下的函数使用的空间都是在堆中分配的,比较安全.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <string>
|
||||
#if defined(_WIN32) || defined(WIN32)
|
||||
#include "Windows.h"
|
||||
#endif
|
||||
|
||||
#if defined(_UNICODE) || defined(UNICODE)
|
||||
#define TtoA WtoA
|
||||
#define AtoT AtoW
|
||||
#define WtoT(a) (a)
|
||||
#define TtoW(a) (a)
|
||||
typedef std::wstring _tstring;
|
||||
#else
|
||||
#define TtoA(a) (a)
|
||||
#define AtoT(a) (a)
|
||||
#define WtoT WtoA
|
||||
#define TtoW AtoW
|
||||
typedef std::string _tstring;
|
||||
#endif
|
||||
|
||||
std::string WtoA(const wchar_t* pwszSrc);
|
||||
std::string WtoA(const std::wstring &strSrc);
|
||||
|
||||
std::wstring AtoW(const char* pszSrc);
|
||||
std::wstring AtoW(const std::string &strSrc);
|
||||
|
||||
std::string WtoUTF8(const wchar_t* pwszSrc);
|
||||
std::string WtoUTF8(const std::wstring &strSrc);
|
||||
|
||||
std::wstring UTF8toW(const char* pszSrc);
|
||||
std::wstring UTF8toW(const std::string &strSr);
|
||||
|
||||
std::string AtoUTF8(const char* src);
|
||||
std::string AtoUTF8(const std::string &src);
|
||||
|
||||
std::string UTF8toA(const char* src);
|
||||
std::string UTF8toA(const std::string &src);
|
||||
|
||||
// 检测一个以 null 结尾的字符串是否是UTF-8, 如果返回0, 也只表示这个串刚好符合UTF8的编码规则.
|
||||
// 返回值说明:
|
||||
// 1 -> 输入字符串符合UTF-8编码规则
|
||||
// -1 -> 检测到非法的UTF-8编码首字节
|
||||
// -2 -> 检测到非法的UTF-8字节编码的后续字节.
|
||||
int IsTextUTF8(const char* pszSrc);
|
@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef LIBEASYOCRCPP_EXPORTS
|
||||
#define LIBEASYOCRCPP_API _declspec(dllexport)
|
||||
#else
|
||||
#define LIBEASYOCRCPP_API _declspec(dllimport)
|
||||
#ifdef _DEBUG
|
||||
#pragma comment(lib, "LibEasyOCR-CPPd.lib")
|
||||
#else
|
||||
#pragma comment(lib, "LibEasyOCR-CPP.lib")
|
||||
#endif
|
||||
#endif
|
@ -0,0 +1,135 @@
|
||||
#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 {};
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "OCRConfig.h"
|
||||
#include <opencv2/core/core.hpp>
|
||||
#include "LibEasyOCR-CPP-Export.h"
|
||||
|
||||
namespace uns
|
||||
{
|
||||
namespace easyocr
|
||||
{
|
||||
using EOCR_Result = std::pair<std::wstring, float>;
|
||||
using EOCR_ResultSet = std::map<size_t, EOCR_Result>;
|
||||
using EOCRD_Rects = std::vector<std::vector<cv::Point2f>>;
|
||||
}
|
||||
|
||||
class LIBEASYOCRCPP_API EasyOCR_CPP
|
||||
{
|
||||
public:
|
||||
static OCRConfig& GlobalOCRConfig();
|
||||
static void SetRecognitionModel(const std::wstring& reco_model, easyocr::CharsetType language);
|
||||
|
||||
static bool CleanupOCR();
|
||||
static bool InitDetectionModel();
|
||||
static bool InitRecognitionModel();
|
||||
|
||||
static easyocr::EOCRD_Rects Detect(const cv::Mat& img);
|
||||
static easyocr::EOCR_Result Recognize(const cv::Mat& img);
|
||||
static easyocr::EOCR_ResultSet Recognize(const cv::Mat& img, const easyocr::EOCRD_Rects& rects);
|
||||
|
||||
static easyocr::EOCR_ResultSet FullAuto(const cv::Mat& img);
|
||||
};
|
||||
|
||||
class LIBEASYOCRCPP_API EOCR_SupportTools
|
||||
{
|
||||
public:
|
||||
static std::string WtoA(const std::wstring& wstr);
|
||||
static std::wstring AtoW(const std::string& str);
|
||||
static std::string AtoUTF8(const std::string& str);
|
||||
static std::string UTF8toA(const std::string& utf8);
|
||||
|
||||
static cv::Mat ReadImage(const std::string& file, bool blur = false, int blur_size = 3);
|
||||
static cv::Mat ReadImage(const std::wstring& file, bool blur = false, int blur_size = 3);
|
||||
};
|
||||
}
|
@ -0,0 +1,181 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{1900cf83-6bce-41c9-b022-0645107abd57}</ProjectGuid>
|
||||
<RootNamespace>LibEasyOCRCPP</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<IncludePath>F:\vcpkg\installed\x64-windows\include\opencv4;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<IncludePath>F:\vcpkg\installed\x64-windows\include\opencv4;$(IncludePath)</IncludePath>
|
||||
<TargetName>$(ProjectName)d</TargetName>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;LIBEASYOCRCPP_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<PrecompiledHeader>Use</PrecompiledHeader>
|
||||
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableUAC>false</EnableUAC>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;LIBEASYOCRCPP_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<PrecompiledHeader>Use</PrecompiledHeader>
|
||||
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableUAC>false</EnableUAC>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;LIBEASYOCRCPP_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<PrecompiledHeader>Use</PrecompiledHeader>
|
||||
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableUAC>false</EnableUAC>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;LIBEASYOCRCPP_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<PrecompiledHeader>Use</PrecompiledHeader>
|
||||
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableUAC>false</EnableUAC>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="ATW.h" />
|
||||
<ClInclude Include="EasyOCR_Detector.h" />
|
||||
<ClInclude Include="EasyOCR_Recognizer.h" />
|
||||
<ClInclude Include="framework.h" />
|
||||
<ClInclude Include="LibEasyOCR-CPP-Export.h" />
|
||||
<ClInclude Include="LibEasyOCR-CPP.h" />
|
||||
<ClInclude Include="OCRCharset.h" />
|
||||
<ClInclude Include="OCRConfig.h" />
|
||||
<ClInclude Include="OCRToolBox.h" />
|
||||
<ClInclude Include="pch.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="ATW.cpp" />
|
||||
<ClCompile Include="dllmain.cpp" />
|
||||
<ClCompile Include="EasyOCR_Detector.cpp" />
|
||||
<ClCompile Include="EasyOCR_Recognizer.cpp" />
|
||||
<ClCompile Include="LibEasyOCR-CPP.cpp" />
|
||||
<ClCompile Include="OCRCharset.cpp" />
|
||||
<ClCompile Include="OCRConfig.cpp" />
|
||||
<ClCompile Include="OCRToolBox.cpp" />
|
||||
<ClCompile Include="pch.cpp">
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
@ -0,0 +1,78 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="源文件">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="头文件">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="资源文件">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="framework.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="pch.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="LibEasyOCR-CPP.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="ATW.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="EasyOCR_Detector.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="EasyOCR_Recognizer.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="OCRCharset.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="OCRConfig.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="OCRToolBox.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="LibEasyOCR-CPP-Export.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="dllmain.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="pch.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="ATW.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="EasyOCR_Detector.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="EasyOCR_Recognizer.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="OCRCharset.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="OCRConfig.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="OCRToolBox.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="LibEasyOCR-CPP.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace uns
|
||||
{
|
||||
class OCRCharset
|
||||
{
|
||||
private:
|
||||
static std::map<int, std::wstring> en_charmap;
|
||||
static std::map<int, std::wstring> ch_en_charmap;
|
||||
|
||||
public:
|
||||
OCRCharset() = delete;
|
||||
OCRCharset(const OCRCharset&) = delete;
|
||||
|
||||
public:
|
||||
static std::wstring GetChar(int index);
|
||||
static std::wstring GetString(const std::vector<int>& indexs);
|
||||
};
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
#include "pch.h"
|
||||
#include "OCRConfig.h"
|
||||
|
||||
uns::OCRConfig uns::G_OCRConfig;
|
||||
|
||||
uns::OCRConfig::OCRConfig()
|
||||
{
|
||||
language = easyocr::CharsetType::EN;
|
||||
gpu_usage = easyocr::GPUUsage::PreferGPU;
|
||||
}
|
||||
|
||||
uns::OCRConfig::OCRConfig(const std::wstring& detect_model, const std::wstring& reco_model, easyocr::CharsetType language, easyocr::GPUUsage gpu)
|
||||
{
|
||||
gpu_usage = gpu;
|
||||
this->language = language;
|
||||
detect_model_path = detect_model;
|
||||
recognize_model_path = reco_model;
|
||||
}
|
||||
|
||||
uns::easyocr::GPUUsage uns::OCRConfig::GetGPUUsage() const
|
||||
{
|
||||
return gpu_usage;
|
||||
}
|
||||
|
||||
uns::easyocr::CharsetType uns::OCRConfig::GetLanguage() const
|
||||
{
|
||||
return language;
|
||||
}
|
||||
|
||||
std::wstring uns::OCRConfig::GetDetectModelPath() const
|
||||
{
|
||||
return detect_model_path;
|
||||
}
|
||||
|
||||
std::wstring uns::OCRConfig::GetRecognizeModelPath() const
|
||||
{
|
||||
return recognize_model_path;
|
||||
}
|
||||
|
||||
void uns::OCRConfig::SetGPUUsage(easyocr::GPUUsage usage)
|
||||
{
|
||||
gpu_usage = usage;
|
||||
}
|
||||
|
||||
void uns::OCRConfig::SetLanguage(easyocr::CharsetType type)
|
||||
{
|
||||
language = type;
|
||||
}
|
||||
|
||||
void uns::OCRConfig::SetDetectModelPath(const std::wstring& path)
|
||||
{
|
||||
detect_model_path = path;
|
||||
}
|
||||
|
||||
void uns::OCRConfig::SetRecognizeModelPath(const std::wstring& path)
|
||||
{
|
||||
recognize_model_path = path;
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
#include "pch.h"
|
||||
#include "OCRToolBox.h"
|
||||
#include "OCRConfig.h"
|
||||
|
||||
bool uns::OCRToolBox::CheckFile(const std::wstring& file)
|
||||
{
|
||||
namespace fs = std::filesystem;
|
||||
try
|
||||
{
|
||||
if (file.empty())
|
||||
return false;
|
||||
if (!fs::exists(file))
|
||||
return false;
|
||||
if (!fs::is_regular_file(file))
|
||||
return false;
|
||||
auto perms = fs::status(file).permissions();
|
||||
return ((perms & fs::perms::owner_read) != fs::perms::none && (perms & fs::perms::owner_write) != fs::perms::none);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void uns::OCRToolBox::InitOrtSessionOptions(Ort::SessionOptions& se_opt)
|
||||
{
|
||||
se_opt.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_ALL);
|
||||
se_opt.EnableCpuMemArena(); // 启用内存池
|
||||
se_opt.EnableMemPattern(); // 启用内存模式优化
|
||||
}
|
||||
|
||||
bool uns::OCRToolBox::AutoSelectEP(const OrtApi* ort, Ort::SessionOptions& se_opt, bool& fallback_to_cpu)
|
||||
{
|
||||
fallback_to_cpu = false;
|
||||
if (G_OCRConfig.GetGPUUsage() == easyocr::GPUUsage::CPUOnly)
|
||||
return true;
|
||||
try
|
||||
{
|
||||
OrtStatusPtr status = OrtSessionOptionsAppendExecutionProvider_CUDA(se_opt, 0);
|
||||
if (status)
|
||||
{
|
||||
ort->ReleaseStatus(status);
|
||||
if (G_OCRConfig.GetGPUUsage() == easyocr::GPUUsage::ForceGPU)
|
||||
return false;
|
||||
se_opt = Ort::SessionOptions(); //Reset Session Options (Fallback to CPU)
|
||||
fallback_to_cpu = true;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return true; //GPU Enable Successful
|
||||
}
|
||||
catch (const Ort::Exception&)
|
||||
{
|
||||
se_opt = Ort::SessionOptions(); //Reset Session Options (Fallback to CPU)
|
||||
fallback_to_cpu = true;
|
||||
return true;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void uns::OCRToolBox::GetInputOutputNames(Ort::Session* ort_session, IONames& input_names, IONamesStorage& input_ns, IONames& output_names, IONamesStorage& output_ns)
|
||||
{
|
||||
input_ns.clear();
|
||||
output_ns.clear();
|
||||
input_names.clear();
|
||||
output_names.clear();
|
||||
|
||||
if (ort_session == nullptr)
|
||||
return;
|
||||
|
||||
auto input_name_alloc = ort_session->GetInputNameAllocated(0, Ort::AllocatorWithDefaultOptions());
|
||||
std::string input_name = input_name_alloc.get();
|
||||
input_ns = { input_name };
|
||||
for (auto& name : input_ns)
|
||||
input_names.push_back(name.c_str());
|
||||
|
||||
auto output_name_alloc = ort_session->GetOutputNameAllocated(0, Ort::AllocatorWithDefaultOptions());
|
||||
std::string output_name = output_name_alloc.get();
|
||||
output_ns = { output_name };
|
||||
for (auto& name : output_ns)
|
||||
output_names.push_back(name.c_str());
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
#include <map>
|
||||
#include <functional>
|
||||
#include <filesystem>
|
||||
#include <opencv2/core/core.hpp>
|
||||
#include <onnxruntime_cxx_api.h>
|
||||
|
||||
namespace uns
|
||||
{
|
||||
using VecInt = std::vector<int>;
|
||||
using VecFloat = std::vector<float>;
|
||||
using IONames = std::vector<const char*>;
|
||||
using IONamesStorage = std::vector<std::string>;
|
||||
using EOCR_Result = std::pair<std::wstring, float>;
|
||||
using EOCR_ResultSet = std::map<size_t, EOCR_Result>;
|
||||
using EOCRD_Rects = std::vector<std::vector<cv::Point2f>>;
|
||||
|
||||
class OCRToolBox
|
||||
{
|
||||
public:
|
||||
static bool CheckFile(const std::wstring& file);
|
||||
static void InitOrtSessionOptions(Ort::SessionOptions& se_opt);
|
||||
static bool AutoSelectEP(const OrtApi* ort, Ort::SessionOptions& se_opt, bool& fallback_to_cpu);
|
||||
static void GetInputOutputNames(Ort::Session* ort_session, IONames& input_names, IONamesStorage& input_ns, IONames& output_names, IONamesStorage& output_ns);
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue