PaddleOCRHelper.cs
4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace paddleOCR
{
public class PaddleOCRHelper
{
public static void Init()
{
int code=eyemInitOCRRecognizer(".\\config.txt");
}
static string pythonEnvPath = ConfigHelper.Config.Get("pythonEnvPath", "C:\\ProgramData\\miniconda3\\envs\\paddle_env\\");
private static Process progressTest;
/// <summary>
/// 开始检测
/// </summary>
/// <param name="pythonExePath">python解释器路径</param>
/// <param name="pythonFile">python文件</param>
/// <param name="imgPath">图像文件路径</param>
/// <returns></returns>
public static string StartPythonOcr(string imgPath)
{
string state = "";
try
{
string sArguments = ".\\paddleOCR.py" + " " + imgPath;
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = pythonEnvPath + "python.exe" + " ";//环境路径需要配置好
start.Arguments = sArguments;
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
start.RedirectStandardInput = true;
start.RedirectStandardError = true;
start.CreateNoWindow = true;
using (progressTest = Process.Start(start))
{
state = progressTest.StandardOutput.ReadToEnd();
progressTest.WaitForExit(30000);
//// 异步获取命令行内容
//progressTest.BeginOutputReadLine();
//// 为异步获取订阅事件
//progressTest.OutputDataReceived += new DataReceivedEventHandler(outputDataReceived);
}
string[] result = state.Split(new string[] { "\r\n" }, StringSplitOptions.None);
state = "";
if (result != null && result.Length > 1)
{
foreach (var item in result.Reverse())
{
if (item.Contains("OCR-Result:"))
{
var ocrR = item.Substring(12);
if (!string.IsNullOrEmpty(ocrR))
{
var lst = DeserializeJsonToList<string>(ocrR);
if (lst != null && lst.Count > 0)
{
state = string.Join(" ", lst);
//LogNet.log.Info($"Paddle Python OCR匹配[" + $"{(DateTime.Now - startTime).TotalSeconds.ToString("f2")}s" + "]:" + state);
}
else
{
state = "";
}
}
break;
}
}
return state;
}
else
return "";
}
catch (Exception ex)
{
//LogNet.log.Error("Paddle OCR匹配异常", ex);
}
return "";
}
[HandleProcessCorruptedStateExceptions]
public static string StartCPlusOcr(string imgPath)
{
StringBuilder sb = new StringBuilder(1024);
try
{
eyemOCRRecognizer(imgPath,ref sb);
}
catch (Exception ex)
{
return ex.Message;
}
return sb.ToString().Trim();
}
static List<T> DeserializeJsonToList<T>(string json) where T : class
{
JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(json);
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>));
List<T> list = o as List<T>;
return list;
}
[DllImport("PaddleOCRSDK.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemInitOCRRecognizer(string extractorModelPath);
[DllImport("PaddleOCRSDK.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemOCRRecognizer(string path, ref StringBuilder lpszContent);
}
}