PaddleOCRSharp,2022年,你來的晚了些,一款.NET離線使用的高精度OCR


一款免費且離線的.NET使用的OCR,愛你又恨你!恨你來的太晚了。

PaddleOCRSharp

     

本項目是一個基於百度飛槳[PaddleOCR](https://github.com/paddlepaddle/PaddleOCR)的C++代碼修改並封裝的.NET的工具類庫。包含文本識別、文本檢測、基於文本檢測結果的統計分析的表格識別功能,同時針對小圖識別不准的情況下,做了優化,提高識別准確率。包含總模型僅8.6M的超輕量級中文OCR,單模型支持中英文數字組合識別、豎排文本識別、長文本識別。同時支持多種文本檢測。
項目封裝極其簡化,實際調用僅幾行代碼,極大的方便了中下游開發者的使用和降低了PaddleOCR的使用入門級別,同時提供不同的.NET框架使用,方便各個行業應用開發與部署。Nuget包即裝即用,可以離線部署,不需要網絡就可以識別的高精度中英文OCR。

本項目中PaddleOCR.dll文件是基於開源項目[PaddleOCR](https://github.com/paddlepaddle/PaddleOCR)的C++代碼修改而成的C++動態庫,基於opencv的x64編譯而成的。

**本項目已經適配[PaddleOCR](https://github.com/paddlepaddle/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 <iostream>
#include <Windows.h> #include <tchar.h> #include "string" #include <include/Parameter.h> #include <string.h> using namespace std; #pragma comment (lib,"PaddleOCR.lib") extern "C" {  /// <summary>  /// PaddleOCREngine引擎初始化  /// </summary>  /// <param name="det_infer"></param>  /// <param name="cls_infer"></param>  /// <param name="rec_infer"></param>  /// <param name="keys"></param>  /// <param name="parameter"></param>  /// <returns></returns>  __declspec(dllimport) int* Initialize(char* det_infer, char* cls_infer, char* rec_infer, char* keys, OCRParameter parameter);  /// <summary>  /// 文本檢測  /// </summary>  /// <param name="engine"></param>  /// <param name="imagefile"></param>  /// <param name="pOCRResult">返回結果</param>  /// <returns></returns>  __declspec(dllimport) int Detect(int* engine, char* imagefile, LpOCRResult* pOCRResult);  /// <summary>  /// 釋放引擎對象  /// </summary>  /// <param name="engine"></param>  __declspec(dllimport) void FreeEngine(int* engine);  /// <summary>  /// 釋放文本識別結果對象  /// </summary>  /// <param name="pOCRResult"></param>  __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<char*>(det_infer.c_str()),  const_cast<char*>(cls_infer.c_str()),  const_cast<char*>(rec_infer.c_str()),  const_cast<char*>(ocrkeys.c_str()),  parameter);   int cout = Detect(pEngine, const_cast<char*>(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項目地址: 
碼雲:https://gitee.com/raoyutian/paddle-ocrsharp
github:https://github.com/raoyutian/PaddleOCRSharp

QQ群:318860399


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM