Form1.cs
6.4 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
using Newtonsoft;
using Newtonsoft.Json;
namespace paddleOCR
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = "image files (*.jpg)|*.jpg|All files (*.*)|*.*";
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog.FileName;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
string path = System.Windows.Forms.Application.StartupPath + @"\paddleOCR.py";//py文件路径
if (string.IsNullOrEmpty(textBox1.Text))
{
return;
}
if (string.IsNullOrEmpty(textBox3.Text))
{
return;
}
this.Invoke(new Action(() =>
{
this.textBox2.Text = StartTest(textBox3.Text, path, textBox1.Text);
this.label1.Text = $"elapsed:{(DateTime.Now - startTime).TotalSeconds.ToString("f2")}s";
try
{
Stream s = File.Open(System.Windows.Forms.Application.StartupPath + @"\ocr_result.jpg", FileMode.Open);
pictureBox1.Image = Bitmap.FromStream(s);
s.Close();
s.Dispose();
}
catch
{
}
}));
}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
}
}
private Process progressTest;
DateTime startTime;
/// <summary>
/// 开始检测
/// </summary>
/// <param name="pythonExePath">python解释器路径</param>
/// <param name="pythonFile">python文件</param>
/// <param name="imgPath">图像文件路径</param>
/// <returns></returns>
public string StartTest(string pythonExePath, string pythonFile, string imgPath)
{
string state = "";
string sArguments = pythonFile + " " + imgPath;
this.pictureBox1.Image = null;
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = pythonExePath + " ";//环境路径需要配置好
start.Arguments = sArguments;
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
start.RedirectStandardInput = true;
start.RedirectStandardError = true;
start.CreateNoWindow = true;
startTime = DateTime.Now;
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);
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);
}
else
{
state = "";
}
}
break;
}
}
return state;
}
else
return "";
}
public 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;
}
public void outputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{
this.Invoke(new Action(() =>
{
this.textBox2.Text = e.Data;
this.label1.Text = $"elapsed:{(DateTime.Now - startTime).TotalSeconds.ToString("f2")}s";
try
{
Stream s = File.Open(System.Windows.Forms.Application.StartupPath + @"\ocr_result.jpg", FileMode.Open);
pictureBox1.Image = Bitmap.FromStream(s);
s.Close();
s.Dispose();
}
catch
{
}
}));
}
}
private void button3_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.InitialDirectory = "c:\\";
openFileDialog.Filter = "python files (*.exe)|*.exe|All files (*.*)|*.*";
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog.FileName;
}
}
}
}
}