PaddleOCRSharp,2022年,你来的晚了些,一款.NET离线使用的高精度OCR

一款免费且离线的.NET使用的OCR,爱你又恨你!恨你来的太晚了。

PaddleOCRSharp

本项目是一个基于百度飞桨PaddleOCR)的C++代码修改并封装的.NET的工具类库。包含文本识别、文本检测、基于文本检测结果的统计分析的表格识别功能,同时针对小图识别不准的情况下,做了优化,提高识别准确率。包含总模型仅8.6M的超轻量级中文OCR,单模型支持中英文数字组合识别、竖排文本识别、长文本识别。同时支持多种文本检测。
项目封装极其简化,实际调用仅几行代码,极大的方便了中下游开发者的使用和降低了PaddleOCR的使用入门级别,同时提供不同的.NET框架使用,方便各个行业应用开发与部署。Nuget包即装即用,可以离线部署,不需要网络就可以识别的高精度中英文OCR。

本项目中PaddleOCR.dll文件是基于开源项目PaddleOCR)的C++代码修改而成的C++动态库,基于opencv的x64编译而成的。

本项目已经适配PaddleOCR)最新版release2.5,并支持PP-OCRv3模型。
超轻量OCR系统PP-OCRv3:中英文、纯英文以及多语言场景精度再提升5% – 11%!

如果使用v3模型,请设置OCR识别参数OCRParameter对象的属性rec_img_h:

rec_img_h=48

本项目只能在X64的CPU上编译和使用,因此不支持32位,暂不支持Linux平台,只能在avx指令集上的CPU上使用。

本项目目前支持以下NET框架:

net35;net40;net45;net451;net452;net46;net461;net462;net47;net471;net472;net48;
netstandard2.0;netcoreapp3.1;
net5.0;net6.0;

方便各个行业应用开发与部署。

C++示例代码

#include 
#include 
#include 
#include "string"
#include 
#include 
using namespace std;
#pragma comment (lib,"PaddleOCR.lib")
extern "C" {
  /// 
  /// PaddleOCREngine引擎初始化
  /// 
  /// 
  /// 
  /// 
  /// 
  /// 
  /// 
  __declspec(dllimport) int* Initialize(char* det_infer, char* cls_infer, char* rec_infer, char* keys, OCRParameter  parameter);
  /// 
  /// 文本检测
  /// 
  /// 
  /// 
  /// 返回结果
  /// 
  __declspec(dllimport) int  Detect(int* engine, char* imagefile, LpOCRResult* pOCRResult);
  /// 
  /// 释放引擎对象
  /// 
  /// 
  __declspec(dllimport) void FreeEngine(int* engine);
  /// 
  /// 释放文本识别结果对象
  /// 
  /// 
  __declspec(dllimport) void FreeDetectResult(LpOCRResult pOCRResult);
};

std::wstring string2wstring(const std::string& s)
{
  int len;
  int slength = (int)s.length() + 1;
  len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
  wchar_t* buf = new wchar_t[len];
  MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
  std::wstring r(buf);
  delete[] buf;
  return r;
}

int main()
{
  LpOCRResult lpocrreult;
  OCRParameter parameter;
  /*parameter.enable_mkldnn = false;*/
  char path[MAX_PATH];

  GetCurrentDirectoryA(MAX_PATH, path);

  string cls_infer(path);
  cls_infer += "\\inference\\ch_ppocr_mobile_v2.0_cls_infer";
  string rec_infer(path);
  rec_infer += "\\inference\\ch_PP-OCRv2_rec_infer";
  string det_infer(path);
  det_infer += "\\inference\\ch_PP-OCRv2_det_infer";
  string ocrkeys(path);
  ocrkeys += "\\inference\\ppocr_keys.txt";
  string imagefile(path);
  imagefile += "\\test.jpg";

  int*  pEngine = Initialize(const_cast(det_infer.c_str()),
                           const_cast(cls_infer.c_str()),
                           const_cast(rec_infer.c_str()),
                           const_cast(ocrkeys.c_str()),
                           parameter);

  int  cout = Detect(pEngine, const_cast(imagefile.c_str()), &lpocrreult);
  std::wcout.imbue(std::locale("chs"));
  for (size_t i = 0; i < cout; i++)
  {
      wstring ss = (WCHAR*)(lpocrreult->pOCRText[i].ptext);
      std::wcout << ss;
  }
  FreeDetectResult(lpocrreult);
  FreeEngine(pEngine);
  std::cin.get();
}*>*>*>*>*>

.NET示例代码

OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
            if (ofd.ShowDialog() != DialogResult.OK) return;
            var imagebyte = File.ReadAllBytes(ofd.FileName);
            Bitmap bitmap = new Bitmap(new MemoryStream(imagebyte));

            OCRModelConfig config = null;
            OCRParameter oCRParameter = null;
            OCRResult ocrResult = new OCRResult();
            using (PaddleOCREngine engine = new PaddleOCREngine(config, oCRParameter))
            {
                ocrResult = engine.DetectText(bmp);
            }
            if (ocrResult != null)
            {
                MessageBox.Show(ocrResult.Text,"识别结果");
            }

PaddleOCRSharp,2022年,你来的晚了些,一款.NET离线使用的高精度OCR

PaddleOCRSharp,2022年,你来的晚了些,一款.NET离线使用的高精度OCR

PaddleOCRSharp项目地址:
码云:https://gitee.com/raoyutian/paddle-ocrsharp
github:https://github.com/raoyutian/PaddleOCRSharp

QQ群:318860399

Original: https://www.cnblogs.com/raoyutian/p/15759232.html
Author: 明月心技术学堂
Title: PaddleOCRSharp,2022年,你来的晚了些,一款.NET离线使用的高精度OCR

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/567675/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球