EngineBase.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.Drawing;
  15. using System.Runtime.InteropServices;
  16. using System;
  17. using System.Drawing.Imaging;
  18. using System.IO;
  19. using System.Reflection;
  20. namespace PaddleOCRSharp
  21. {
  22. /// <summary>
  23. /// PaddleOCR识别引擎对象
  24. /// </summary>
  25. public abstract class EngineBase : IDisposable
  26. {
  27. /// <summary>
  28. /// PaddleOCR.dll自定义加载路径,默认为空,如果指定则需在引擎实例化前赋值。
  29. /// </summary>
  30. public static string PaddleOCRdllPath { get; set; }
  31. internal const string PaddleOCRdllName = "PaddleOCR.dll";
  32. internal const string yt_CPUCheckdllName = "yt_CPUCheck.dll";
  33. #region PaddleOCR API
  34. [DllImport(yt_CPUCheckdllName, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
  35. internal static extern int IsCPUSupport();
  36. [DllImport("kernel32.dll")]
  37. private extern static IntPtr LoadLibrary(String path);
  38. #endregion
  39. /// <summary>
  40. /// 初始化
  41. /// </summary>
  42. public EngineBase()
  43. {
  44. //此行代码无实际意义,用于后面的JsonHelper.DeserializeObject的首次加速,首次初始化会比较慢,放在此处预热。
  45. var temp = JsonHelper.DeserializeObject<TextBlock>("{}");
  46. try
  47. {
  48. string osVersion = $"{Environment.OSVersion.Version.Major}.{Environment.OSVersion.Version.Minor}";
  49. if (osVersion == "6.1")
  50. {
  51. #region win7
  52. try
  53. {
  54. string root = GetRootDirectory();
  55. string dllPath = root + @"\inference\win7_dll\";
  56. if (Directory.Exists(dllPath))
  57. {
  58. string Envpath = Environment.GetEnvironmentVariable("path", EnvironmentVariableTarget.Process);
  59. if (!string.IsNullOrEmpty(Envpath))
  60. {
  61. Environment.SetEnvironmentVariable("path", Envpath + ";" + dllPath, EnvironmentVariableTarget.Process);
  62. }
  63. }
  64. }
  65. catch
  66. {
  67. throw new Exception($"Win7依赖dll动态加载失败。请手动复制文件夹【inference\\win7_dll】文件到PaddleOCR.dll目录。");
  68. }
  69. #endregion
  70. }
  71. if (!string.IsNullOrEmpty(PaddleOCRdllPath))
  72. {
  73. string Envpath = Environment.GetEnvironmentVariable("path", EnvironmentVariableTarget.Process);
  74. if (!string.IsNullOrEmpty(Envpath))
  75. {
  76. Environment.SetEnvironmentVariable("path", Envpath + ";" + PaddleOCRdllPath, EnvironmentVariableTarget.Process);
  77. LoadLibrary(System.IO.Path.Combine(PaddleOCRdllPath, PaddleOCRdllName));
  78. LoadLibrary(System.IO.Path.Combine(PaddleOCRdllPath, "onnxruntime.dll"));
  79. }
  80. }
  81. }
  82. catch (Exception e)
  83. {
  84. throw new Exception("设置自定义加载路径失败。"+e.Message);
  85. }
  86. }
  87. #region private
  88. /// <summary>
  89. /// 获取程序的当前路径;
  90. /// </summary>
  91. /// <returns></returns>
  92. internal string GetRootDirectory()
  93. {
  94. string root = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  95. #if NET46_OR_GREATER || NETCOREAPP
  96. if(string.IsNullOrEmpty(root))
  97. {
  98. return AppContext.BaseDirectory;
  99. }
  100. #endif
  101. return root;
  102. }
  103. /// <summary>
  104. /// 环境监测
  105. /// </summary>
  106. internal protected void CheckEnvironment()
  107. {
  108. #if NET35
  109. #else
  110. if (!Environment.Is64BitProcess) throw new Exception("暂不支持32位程序使用本OCR");
  111. #endif
  112. }
  113. /// <summary>
  114. /// Convert Image to Byte[]
  115. /// </summary>
  116. /// <param name="image"></param>
  117. /// <returns></returns>
  118. internal protected byte[] ImageToBytes(Image image)
  119. {
  120. ImageFormat format = image.RawFormat;
  121. using (MemoryStream ms = new MemoryStream())
  122. {
  123. if (format.Guid == ImageFormat.Jpeg.Guid)
  124. {
  125. image.Save(ms, ImageFormat.Jpeg);
  126. }
  127. else if (format.Guid == ImageFormat.Png.Guid)
  128. {
  129. image.Save(ms, ImageFormat.Png);
  130. }
  131. else if (format.Guid == ImageFormat.Bmp.Guid)
  132. {
  133. image.Save(ms, ImageFormat.Bmp);
  134. }
  135. else if (format.Guid == ImageFormat.Gif.Guid)
  136. {
  137. image.Save(ms, ImageFormat.Gif);
  138. }
  139. else if (format.Guid == ImageFormat.Icon.Guid)
  140. {
  141. image.Save(ms, ImageFormat.Icon);
  142. }
  143. else
  144. {
  145. image.Save(ms, ImageFormat.Png);
  146. }
  147. byte[] buffer = new byte[ms.Length];
  148. ms.Seek(0, SeekOrigin.Begin);
  149. ms.Read(buffer, 0, buffer.Length);
  150. return buffer;
  151. }
  152. }
  153. #endregion
  154. /// <summary>
  155. /// 释放内存
  156. /// </summary>
  157. public virtual void Dispose()
  158. {
  159. }
  160. }
  161. }