PaddleOCRHelper.cs
4.2 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
121
122
123
124
125
126
127
128
129
using Model;
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;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BLL
{
public class PaddleOCRHelper
{
//static string baseUrl = ConfigHelper.Config.Get("PaddleServiceBase", "http://localhost:8090/paddle/getOcr");
static string baseUrl = ConfigHelper.Config.Get("paddleOcr_HttpPath", "http://localhost:8090/paddle/getOcr");
/// <summary>
/// 开始检测
/// </summary>
/// <param name="pythonExePath">python解释器路径</param>
/// <param name="pythonFile">python文件</param>
/// <param name="imgPath">图像文件路径</param>
/// <returns></returns>
public static string StartTest(string imgPath)
{
string ocr = "";
for (int i = 0; i < 2; i++)
{
if (!AppIsRun())
{
LogNet.log.Info("ocr程序未允许,启动");
try
{
var paddle = "paddleOCR.exe";
Process process = new Process();
process.StartInfo = new ProcessStartInfo();
process.StartInfo.FileName = paddle;
process.StartInfo.WorkingDirectory = Application.StartupPath + "\\paddle";
process.Start();
}
catch (Exception ex)
{
LogNet.log.Error("打开paddleOCR失败", ex);
}
Thread.Sleep(5000);
}
if (AppIsRun())
{
try
{
ocr = StartCplusOcr(imgPath);
break;
}
catch (Exception ex) {
LogNet.log.Error("OCR失败", ex);
AppKill();
}
}
}
return ocr;
}
[HandleProcessCorruptedStateExceptions]
static string StartCplusOcr(string imgPath)
{
LogNet.log.Error("进入OcrGet请求");
string json=Http.Get($"{baseUrl}?ver=cplus&imgPath={imgPath}");
LogNet.log.Error($"返回数据为:{json}");
Result result= JsonConvert.DeserializeObject<Result>(json);
return result?.data??"";
}
static string StartPythonOcr(string imgPath)
{
if (!AppIsRun())
{
var onnxexe = ".\\paddle\\paddleOCR.exe";
Process.Start(onnxexe);
Thread.Sleep(2000);
}
string json = Http.Get($"{baseUrl}?ver=python&imgPath={imgPath}");
Result result = JsonConvert.DeserializeObject<Result>(json);
return result?.data ?? "";
}
static bool AppIsRun()
{
Process[] processes = Process.GetProcessesByName("paddleOCR");
if (processes.Length > 0)
{
return true;
}
LogNet.log.Info("paddleOCR 未在运行,启动程序");
return false;
}
static void AppKill()
{
Process[] processes = Process.GetProcessesByName("paddleOCR");
foreach(var p in processes)
{
p.Kill();
}
LogNet.log.Info("paddleOCR AppKill");
}
public class Result
{
/// <summary>
/// 状态码,0为正常
/// </summary>
public int code { get; set; } = 0;
/// <summary>
/// 返回数据
/// </summary>
public string data { get; set; } = "";
/// <summary>
/// 提示信息
/// </summary>
public string msg { get; set; } = "ok";
/// <summary>
/// 版本
/// </summary>
public string ver { get; set; } = "";
}
}
}