PaddleOCRHelper.cs
3.9 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
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 = "";
if (!AppIsRun())
{
try
{
var paddle = "paddleOCR.exe";
Process process = new Process();
process.StartInfo = new ProcessStartInfo();
process.StartInfo.FileName = paddle;
process.StartInfo.WorkingDirectory = ".\\paddle";
process.Start();
}
catch (Exception ex)
{
LogNet.log.Error("打开paddleOCR失败", ex);
}
Thread.Sleep(3000);
}
if(AppIsRun())
{
ocr = StartCplusOcr(imgPath);
}
if (!AppIsRun())
{
try
{
var paddle = "paddleOCR.exe";
Process process = new Process();
process.StartInfo = new ProcessStartInfo();
process.StartInfo.FileName = paddle;
process.StartInfo.WorkingDirectory = ".\\paddle";
process.Start();
}
catch (Exception ex)
{
LogNet.log.Error("打开paddleOCR失败", ex);
}
}
return ocr;
}
[HandleProcessCorruptedStateExceptions]
static string StartCplusOcr(string imgPath)
{
string json=Http.Get($"{baseUrl}?ver=cplus&imgPath={imgPath}");
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;
}
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; } = "";
}
}
}