-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMainForm.cs
382 lines (331 loc) · 9.85 KB
/
MainForm.cs
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* Created by SharpDevelop.
* User: nk1449
* Date: 2017/05/29
* Time: 11:56
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading.Tasks;
using Tesseract;
using System.Text;
namespace UyghurOCR
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
Random grand = new System.Random();
TesseractEngine gOcr;
OCRText gOcrTxtForm = null;
private Microsoft.Win32.RegistryKey gRegKey= Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"Software\Kenjsoft\KenjiResim");
static string gImgExts;
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
System.Reflection.Assembly asm =System.Reflection.Assembly.GetExecutingAssembly();
this.Icon=new Icon(asm.GetManifestResourceStream("UyghurOCR.OCAq.ico"));
var codecs = ImageCodecInfo.GetImageEncoders();
foreach (var codec in codecs)
{
gImgExts += codec.FilenameExtension + ";";
}
}
bool IsImage(string filename)
{
return gImgExts.IndexOf(Path.GetExtension(filename),StringComparison.OrdinalIgnoreCase) == -1? false:true;
}
void MainFormFormClosing(object sender, FormClosingEventArgs e)
{
if(gRegKey!=null){
gRegKey.SetValue("LAST",label1.Text);
}
}
void ChkLangSelectedIndexChanged(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
string lang = "";
char[] tr = {'+'};
if(tilUyghurUKIJ.Checked){
lang += "ukij";
}
if(tilUyghurche.Checked){
lang += "+uig";
}
if(tilEnglizche.Checked){
lang += "+eng";
}
if(tilTurkche.Checked){
lang += "+tur";
}
if(tilXenzuche.Checked){
lang += "+chi_sim";
}
if(tilRusche.Checked){
lang += "+rus";
}
lang = lang.Trim(tr);
System.Diagnostics.Debug.WriteLine(lang);
if(gOcr!=null){
gOcr.Dispose();
gOcr = null;
}
if(lang.Length == 0)
{
MessageBox.Show("Hech qandaq tilni tallimidingiz","Uyghurche OCR Agahlandurush",MessageBoxButtons.OK,MessageBoxIcon.Warning);
butRecAll.Enabled = false;
butRecognize.Enabled = false;
}
else
{
butRecAll.Enabled = true;
butRecognize.Enabled = true;
gOcr= new TesseractEngine(@".\tessdata",lang,EngineMode.LstmOnly);
Text ="Addiy Uyghurche OCR(Tesseract OCR[V" + gOcr.Version + "] Neshri Ishlitilgen)";
}
this.Cursor = Cursors.Default;
Invalidate();
}
async void ButtonTonu(object sender, EventArgs e)
{
EnableAll(false);
Bitmap roibmp;
Pix roipix;
Rectangle roi = ramka.getRoi();
Cursor=Cursors.WaitCursor;
roibmp = ramka.Image.Clone(roi,ramka.Image.PixelFormat);
// roibmp.SetResolution(400,400);
roipix = PixConverter.ToPix(roibmp); //.Deskew().Scale(1.0f,1.0f);
roibmp.Dispose();
Task<string> ocr = Task.Run<string>(() =>{
return DoOCR(roipix);
});
string txt = await ocr;
roipix.Dispose();
if(gOcrTxtForm==null || gOcrTxtForm.IsDisposed){
gOcrTxtForm=new OCRText();
gOcrTxtForm.Show();
}
gOcrTxtForm.SetText(txt);
Cursor=Cursors.Default;
EnableAll(true);
}
string DoOCR(Pix pix){
if(rdAuto.Checked){
gOcr.DefaultPageSegMode = PageSegMode.Auto;
}
else if(rdSingle.Checked){
gOcr.DefaultPageSegMode = PageSegMode.SingleBlock;
}
Page pg = gOcr.Process(pix);
String buf = pg.GetText();
pix.Dispose();
pg.Dispose();
return buf.Replace("ی","ي").Replace("ه","ە").Replace("\n",Environment.NewLine);
}
void MainFormLoad(object sender, EventArgs e)
{
}
void listAllImg(){
listBox1.Items.Clear();
if(!Directory.Exists(label1.Text)) return;
String[] images = Directory.GetFiles(label1.Text,"*.*");
foreach(string afile in images){
if(IsImage(afile)){
listBox1.Items.Add(Path.GetFileName(afile));
}
}
if(listBox1.Items.Count>0){
listBox1.SelectedIndex = 0;
//ShowImg();
}
}
void ShowImg(){
String path = Path.Combine(label1.Text, (string)listBox1.SelectedItem);
Bitmap bimg = new Bitmap(path);
ramka.Image=bimg;
}
void ButtonNextClick(object sender, EventArgs e)
{
int index = listBox1.SelectedIndex;
index = index +1;
if(index<listBox1.Items.Count){
listBox1.SelectedIndex = index;
}
//ShowImg();
}
void ListBox1SelectedIndexChanged(object sender, EventArgs e)
{
ShowImg();
}
void Label1Click(object sender, EventArgs e)
{
FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.SelectedPath = label1.Text;
dlg.Description = "Resimler bar qisquchni tallang:";
dlg.ShowNewFolderButton = false;
DialogResult drs = dlg.ShowDialog(this);
if(drs == DialogResult.OK){
label1.Text = dlg.SelectedPath;
listAllImg();
}
}
void MainFormShown(object sender, EventArgs e)
{
String lastdir =null;
if(gRegKey!=null){
lastdir=(string)gRegKey.GetValue("LAST");
}
if(lastdir==null || Directory.Exists(lastdir)==false){
label1.Text=Path.Combine(Application.StartupPath,"sinaq_resim");
listAllImg();
}
else{
label1.Text = lastdir;
listAllImg();
}
tilUyghurUKIJ.Checked=true;
}
void ButPdftoImageClick(object sender, EventArgs e)
{
OpenFileDialog ofd=new OpenFileDialog();
ofd.Title="PDF hojjetning atini tallang";
ofd.Filter ="PDF file(*.pdf)|*.pdf";
if(ofd.ShowDialog()!= DialogResult.OK) return;
String pdfFile=ofd.FileName;
String path=System.IO.Path.GetDirectoryName(pdfFile)+"\\"+System.IO.Path.GetFileNameWithoutExtension(pdfFile);
if(Directory.Exists(path)==false){
Directory.CreateDirectory(path);
}
EnableAll(false);
this.Cursor=System.Windows.Forms.Cursors.WaitCursor;
if(ExtractImage(pdfFile,path)){
label1.Text = path;
listAllImg();
}
this.Cursor=System.Windows.Forms.Cursors.Arrow;
EnableAll(true);
}
private bool ExtractImage(String pdfFile, string imgPath)
{
try{
PDFHelper hlp=new PDFHelper();
hlp.ExtractImages(pdfFile,imgPath, progressBar1);
return true;
}catch(Exception ee){
// MessageBox.Show(ee.Message,"Xataliq koruldi");
System.Diagnostics.Debug.WriteLine(ee.StackTrace);
return false;
}
}
void Button2Click(object sender, EventArgs e)
{
Bitmap roibmp;
Pix roipix;
EnableAll(false);
Rectangle roi = ramka.getRoi();
Cursor=Cursors.WaitCursor;
roibmp = ramka.Image.Clone(roi,ramka.Image.PixelFormat);
roipix = PixConverter.ToPix(roibmp).Deskew();
// roipix = PixConverter.ToPix(roibmp);
roibmp.Dispose();
System.Diagnostics.Debug.WriteLine(roipix.XRes + " = " + roipix.YRes);
Bitmap newbm = PixConverter.ToBitmap(roipix);
roipix.Dispose();
ramka.Image = newbm;
Cursor=Cursors.Default;
EnableAll(true);
}
void MainFormDragEnter(object sender, DragEventArgs e)
{
String[] file=(String[])e.Data.GetData(DataFormats.FileDrop);
if(IsImage(file[0]))
{
e.Effect= DragDropEffects.All;
}
}
void MainFormDragDrop(object sender, DragEventArgs e)
{
String[] file=(String[])e.Data.GetData(DataFormats.FileDrop);
string imgFile=file[0];
Bitmap bimg = new Bitmap(imgFile);
ramka.Image=bimg;
}
void EnableAll(bool vv){
butPDF.Enabled = vv;
butDeskew.Enabled = vv;
butNext.Enabled = vv;
butRecognize.Enabled = vv;
label1.Enabled = vv;
listBox1.Enabled = vv;
groupBox1.Enabled = vv;
groupBox2.Enabled = vv;
ramka.Enabled = vv;
butRecAll.Enabled = vv;
}
async void Button3Click(object sender, EventArgs e)
{
if(butRecAll.Text.Equals("Toxta")){
butRecAll.Enabled=false;
butRecAll.Text="Hemmini Tonu";
return;
}
listBox1.SelectedIndex = 0;
progressBar1.Visible = true;
progressBar1.Value = 1;
progressBar1.Maximum = listBox1.Items.Count;
Bitmap roibmp;
Pix roipix;
String fileName = Path.Combine(label1.Text, "OCR_Text.txt");
if(gOcrTxtForm==null || gOcrTxtForm.IsDisposed){
gOcrTxtForm=new OCRText();
gOcrTxtForm.Show();
}
gOcrTxtForm.Text = fileName;
EnableAll(false);
Cursor=Cursors.WaitCursor;
butRecAll.Text="Toxta";
butRecAll.Enabled = true;
while(butRecAll.Text.Equals("Toxta")){
Rectangle roi = ramka.getRoi();
roibmp = ramka.Image.Clone(roi,ramka.Image.PixelFormat);
roipix = PixConverter.ToPix(roibmp).Deskew().Scale(1.0f,1.0f);
roibmp.Dispose();
Task<string> ocr = Task.Run<string>(() =>{
return DoOCR(roipix);
});
string txt = await ocr;
roipix.Dispose();
// fileName = String.Format("imla_ocr_{0:0000}.txt",listBox1.SelectedIndex);
try{
fileName = Path.GetFileNameWithoutExtension((string)listBox1.SelectedItem)+".txt";
fileName = Path.Combine(label1.Text,fileName);
File.WriteAllText(fileName,txt,Encoding.UTF8);
}catch(Exception ee){
System.Diagnostics.Debug.WriteLine(ee.Message);
}
gOcrTxtForm.SetText(txt);
File.AppendAllText(fileName,txt,Encoding.UTF8);
if((listBox1.SelectedIndex+1)>=listBox1.Items.Count){
break;
}
ButtonNextClick(null,null);
progressBar1.Value = listBox1.SelectedIndex+1;
}
progressBar1.Visible = false;
Cursor=Cursors.Default;
EnableAll(true);
// butRecAll.Enabled = true;
butRecAll.Text="Hemmini Tonu";
}
}
}