PaddleStructureEngine.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright (c) 2021 raoyutian Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using System.Runtime.InteropServices;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Data.Common;
  18. using System.Reflection;
  19. using System.Text;
  20. using System.Drawing;
  21. using System.IO;
  22. namespace PaddleOCRSharp
  23. {
  24. /// <summary>
  25. /// PaddleOCR NET帮助类
  26. /// </summary>
  27. public class PaddleStructureEngine:EngineBase
  28. {
  29. #region PaddleOCR API
  30. [DllImport(PaddleOCRdllName, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
  31. internal static extern void StructureInitialize(string det_infer, string rec_infer, string keys, string table_model_dir, string table_char_dict_path, StructureParameter parameter);
  32. [DllImport(PaddleOCRdllName, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
  33. internal static extern void StructureInitializejson(string det_infer, string rec_infer, string keys, string table_model_dir, string table_char_dict_path, string parameter);
  34. [DllImport(PaddleOCRdllName, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
  35. internal static extern IntPtr GetStructureDetectFile( string imagefile);
  36. [DllImport(PaddleOCRdllName, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
  37. internal static extern IntPtr GetStructureDetectByte( byte[] imagebytedata, long size);
  38. [DllImport(PaddleOCRdllName, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
  39. internal static extern IntPtr GetStructureDetectBase64( string imagebase64);
  40. [DllImport(PaddleOCRdllName, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
  41. internal static extern void FreeStructureEngine( );
  42. #endregion
  43. /// <summary>
  44. /// PaddleStructureEngine识别引擎对象初始化
  45. /// </summary>
  46. /// <param name="config">模型配置对象,如果为空则按默认值</param>
  47. /// <param name="parameter">识别参数,为空均按缺省值</param>
  48. public PaddleStructureEngine(StructureModelConfig config, StructureParameter parameter) : base()
  49. {
  50. if (IsCPUSupport() <= 0) throw new NotSupportedException($"当前CPU的指令集不支持PaddleOCR");
  51. if (parameter == null) parameter = new StructureParameter();
  52. if (config == null)
  53. {
  54. string root = GetRootDirectory();
  55. config = new StructureModelConfig();
  56. string modelPathroot = root + @"\inference";
  57. config.det_infer = modelPathroot + @"\ch_PP-OCRv3_det_infer";
  58. config.rec_infer = modelPathroot + @"\ch_PP-OCRv3_rec_infer";
  59. config.keys = modelPathroot + @"\ppocr_keys.txt";
  60. config.table_model_dir = modelPathroot + @"\ch_ppstructure_mobile_v2.0_SLANet_infer";
  61. config.table_char_dict_path = modelPathroot + @"\table_structure_dict_ch.txt";
  62. }
  63. StructureInitialize(config.det_infer, config.rec_infer, config.keys, config.table_model_dir, config.table_char_dict_path, parameter);
  64. }
  65. /// <summary>
  66. /// PaddleStructureEngine识别引擎对象初始化
  67. /// </summary>
  68. /// <param name="config">模型配置对象,如果为空则按默认值</param>
  69. /// <param name="parameterjson">识别参数Json格式,为空均按缺省值</param>
  70. public PaddleStructureEngine(StructureModelConfig config, string parameterjson) : base()
  71. {
  72. if (IsCPUSupport() <= 0) throw new NotSupportedException($"当前CPU的指令集不支持PaddleOCR");
  73. if (config == null)
  74. {
  75. string root = GetRootDirectory();
  76. config = new StructureModelConfig();
  77. string modelPathroot = root + @"\inference";
  78. config.det_infer = modelPathroot + @"\ch_PP-OCRv3_det_infer";
  79. config.rec_infer = modelPathroot + @"\ch_PP-OCRv3_rec_infer";
  80. config.keys = modelPathroot + @"\ppocr_keys.txt";
  81. config.table_model_dir = modelPathroot + @"\ch_ppstructure_mobile_v2.0_SLANet_infer";
  82. config.table_char_dict_path = modelPathroot + @"\table_structure_dict_ch.txt";
  83. }
  84. if (string.IsNullOrEmpty(parameterjson))
  85. {
  86. parameterjson = GetRootDirectory();
  87. parameterjson += @"\inference\PaddleOCRStructure.config.json";
  88. if (!File.Exists(parameterjson)) throw new FileNotFoundException(parameterjson);
  89. parameterjson = File.ReadAllText(parameterjson);
  90. }
  91. StructureInitializejson(config.det_infer, config.rec_infer, config.keys, config.table_model_dir, config.table_char_dict_path, parameterjson);
  92. }
  93. /// <summary>
  94. /// 对图像文件进行表格文本识别
  95. /// </summary>
  96. /// <param name="imagefile">图像文件</param>
  97. /// <returns>表格识别结果</returns>
  98. public string StructureDetectFile(string imagefile)
  99. {
  100. if (!System.IO.File.Exists(imagefile)) throw new Exception($"文件{imagefile}不存在");
  101. IntPtr presult = GetStructureDetectFile( imagefile);
  102. var result= Marshal.PtrToStringUni(presult);
  103. Marshal.FreeHGlobal(presult);
  104. return result;
  105. }
  106. /// <summary>
  107. ///对图像对象进行表格文本识别
  108. /// </summary>
  109. /// <param name="image">图像</param>
  110. /// <returns>表格识别结果</returns>
  111. public string StructureDetect(Image image)
  112. {
  113. if (image == null) throw new ArgumentNullException("image");
  114. var imagebyte = ImageToBytes(image);
  115. var result = StructureDetect(imagebyte);
  116. imagebyte = null;
  117. return result;
  118. }
  119. /// <summary>
  120. /// 对图像Byte数组进行表格文本识别
  121. /// </summary>
  122. /// <param name="imagebyte">图像字节数组</param>
  123. /// <returns>表格识别结果</returns>
  124. public string StructureDetect(byte[] imagebyte)
  125. {
  126. if (imagebyte == null) throw new ArgumentNullException("imagebyte");
  127. IntPtr presult= GetStructureDetectByte(imagebyte, imagebyte.LongLength);
  128. var result= Marshal.PtrToStringUni(presult);
  129. Marshal.FreeHGlobal(presult);
  130. return result;
  131. }
  132. /// <summary>
  133. /// 对图像Base64进行表格文本识别
  134. /// </summary>
  135. /// <param name="imagebase64">图像Base64</param>
  136. /// <returns>表格识别结果</returns>
  137. public string StructureDetectBase64(string imagebase64)
  138. {
  139. if (imagebase64 == null || imagebase64 == "") throw new ArgumentNullException("imagebase64");
  140. IntPtr presult= GetStructureDetectBase64( imagebase64);
  141. var result = Marshal.PtrToStringUni(presult);
  142. Marshal.FreeHGlobal(presult);
  143. return result;
  144. }
  145. #region Dispose
  146. /// <summary>
  147. /// 释放对象
  148. /// </summary>
  149. public override void Dispose()
  150. {
  151. FreeStructureEngine();
  152. }
  153. #endregion
  154. }
  155. }