PaddleOCR.Onnx
一款基于Paddle的OCR,項目使用ONNX模型,速度更快。本項目同時支持X64和X86的CPU上使用。本項目是一個基于PaddleOCR的C++代碼修改并封裝的.NET的工具類庫。包含文本識別、文本檢測、基于文本檢測結果的統計分析的表格識別功能,同時針對小圖識別不準的情況下,做了優化,提高識別準確率。包含總模型僅8.6M的超輕量級中文OCR,單模型支持中英文數字組合識別、豎排文本識別、長文本識別。同時支持多種文本檢測。項目封裝極其簡化,實際調用僅幾行代碼,極大的方便了中下游開發者的使用和降低了PaddleOCR的使用入門級別,同時提供不同的.NET框架使用,方便各個行業應用開發與部署。Nuget包即裝即用,可以離線部署,不需要網絡就可以識別的高精度中英文OCR。
目前不支持win7及以下操作系統。
本項目目前支持以下NET框架:
net461;net462;net47;net471;net48;
netstandard2.0;netcoreapp3.1;
net5.0;net6.0;
OCR識別模型庫支持官方所有的模型,也支持自己訓練的模型。
本項目部署自帶的一種輕量版8.6M模型庫、服務器版模型庫(更準確,需要自行下載),可以自行更改模型庫適用實際需求。
PaddleOCR模型下載地址
模型需要轉成ONNX格式才能被本項目所使用。
如果需要修改成服務器版模型庫,參考代碼如下:(假設服務器版模型庫在運行目錄的文件夾inferenceserver下)
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;//服務器中英文模型//OCRModelConfig config = new OCRModelConfig();//string root = Environment.CurrentDirectory;//string modelPathroot = root + @"\inferenceserver";//config.det_infer = modelPathroot + @"\ch_ppocr_server_v2.0_det_infer.onnx";//config.cls_infer = modelPathroot + @"\ch_ppocr_mobile_v2.0_cls_infer.onnx";//config.rec_infer = modelPathroot + @"\ch_ppocr_server_v2.0_rec_infer.onnx";//config.keys = modelPathroot + @"\ppocr_keys.txt";//英文和數字模型OCRModelConfig config = new OCRModelConfig();string root = Environment.CurrentDirectory;string modelPathroot = root + @"\en";config.det_infer = modelPathroot + @"\ch_PP-OCRv2_det_infer";config.cls_infer = modelPathroot + @"\ch_ppocr_mobile_v2.0_cls_infer.onnx";config.rec_infer = modelPathroot + @"\en_number_mobile_v2.0_rec_infer.onnx";config.keys = modelPathroot + @"\en_dict.txt";OCRParameter oCRParameter = new OCRParameter ();OCRResult ocrResult = new OCRResult();//建議程序全局初始化一次即可,不必每次識別都初始化,容易報錯。PaddleOCREngine engine = new PaddleOCREngine(config, oCRParameter);{ocrResult = engine.DetectText(bitmap );}if (ocrResult != null){MessageBox.Show(ocrResult.Text,"識別結果");}//不再用OCR時,請把PaddleOCREngine釋放
.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 = new OCRParameter ();OCRResult ocrResult = new OCRResult();//建議程序全局初始化一次即可,不必每次識別都初始化,容易報錯。PaddleOCREngine engine = new PaddleOCREngine(config, oCRParameter);{ocrResult = engine.DetectText(bitmap );}if (ocrResult != null){MessageBox.Show(ocrResult.Text,"識別結果");}//不再用OCR時,請把PaddleOCREngine釋放