diff --git a/#research/Passport Demand Study Sample Letter.jpg b/#research/Passport Demand Study Sample Letter.jpg new file mode 100644 index 0000000..242a9fa Binary files /dev/null and b/#research/Passport Demand Study Sample Letter.jpg differ diff --git a/#research/Passport Demand Study Sample Letter.png b/#research/Passport Demand Study Sample Letter.png new file mode 100644 index 0000000..5245f53 Binary files /dev/null and b/#research/Passport Demand Study Sample Letter.png differ diff --git a/#research/Request-Letter-for-Portable-ID-Scanner-docx-1-320.webp b/#research/Request-Letter-for-Portable-ID-Scanner-docx-1-320.webp new file mode 100644 index 0000000..134c26f Binary files /dev/null and b/#research/Request-Letter-for-Portable-ID-Scanner-docx-1-320.webp differ diff --git a/#research/eng.traineddata b/#research/tessdata/eng.traineddata similarity index 100% rename from #research/eng.traineddata rename to #research/tessdata/eng.traineddata diff --git a/#research/pdf.ttf b/#research/tessdata/pdf.ttf similarity index 100% rename from #research/pdf.ttf rename to #research/tessdata/pdf.ttf diff --git a/#research/test-barcode-1.jpg b/#research/test-barcode-1.jpg new file mode 100644 index 0000000..cb4e21f Binary files /dev/null and b/#research/test-barcode-1.jpg differ diff --git a/#research/test-handwriting-1.jpg b/#research/test-handwriting-1.jpg new file mode 100644 index 0000000..e1c3fe0 Binary files /dev/null and b/#research/test-handwriting-1.jpg differ diff --git a/Form1.Designer.cs b/Form1.Designer.cs index 01ecf79..a41de51 100644 --- a/Form1.Designer.cs +++ b/Form1.Designer.cs @@ -29,23 +29,60 @@ private void InitializeComponent() { button1 = new Button(); + richTextBox1 = new RichTextBox(); + button2 = new Button(); + button3 = new Button(); SuspendLayout(); // // button1 // - button1.Location = new Point(574, 237); + button1.Location = new Point(549, 15); button1.Name = "button1"; - button1.Size = new Size(75, 23); + button1.Size = new Size(208, 59); button1.TabIndex = 0; - button1.Text = "button1"; + button1.Text = "Tesseract"; button1.UseVisualStyleBackColor = true; button1.Click += button1_Click; // + // richTextBox1 + // + richTextBox1.DetectUrls = false; + richTextBox1.HideSelection = false; + richTextBox1.Location = new Point(9, 15); + richTextBox1.Name = "richTextBox1"; + richTextBox1.Size = new Size(511, 402); + richTextBox1.TabIndex = 1; + richTextBox1.Text = ""; + richTextBox1.WordWrap = false; + // + // button2 + // + button2.Location = new Point(549, 122); + button2.Name = "button2"; + button2.Size = new Size(208, 59); + button2.TabIndex = 0; + button2.Text = "ClearImage"; + button2.UseVisualStyleBackColor = true; + button2.Click += button2_Click; + // + // button3 + // + button3.Location = new Point(549, 249); + button3.Name = "button3"; + button3.Size = new Size(208, 59); + button3.TabIndex = 2; + button3.Text = "button1"; + button3.UseVisualStyleBackColor = true; + button3.Click += button3_Click; + // // Form1 // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(button3); + Controls.Add(richTextBox1); + Controls.Add(button2); Controls.Add(button1); Name = "Form1"; Text = "Form1"; @@ -55,5 +92,8 @@ #endregion private Button button1; + private RichTextBox richTextBox1; + private Button button2; + private Button button3; } } diff --git a/Form1.cs b/Form1.cs index 1b69e1d..39ee0ab 100644 --- a/Form1.cs +++ b/Form1.cs @@ -1,88 +1,197 @@ using System.Diagnostics; +using System.Windows.Forms; +using Inlite.ClearImageNet; using Tesseract; namespace WinFormsApp3 { public partial class Form1 : Form { + private readonly string _basePath; + private readonly string _tessDataPath; + public Form1() { InitializeComponent(); + + _basePath = Path.GetFullPath(Path.GetDirectoryName(Application.ExecutablePath) + "\\..\\..\\..\\"); + _tessDataPath = Path.Combine(_basePath, "#research", "tessdata"); + } + + protected override void OnLoad(EventArgs e) + { + base.OnLoad(e); + + richTextBox1.Clear(); } private void button1_Click(object sender, EventArgs e) { - var basePath = Path.GetFullPath(Path.GetDirectoryName(Application.ExecutablePath) + "\\..\\..\\..\\"); + var testImagePath = Path.Combine(_basePath, "#research", "Passport Demand Study Sample Letter.jpg"); - var testImagePath = Path.Combine(basePath, "Scan_ADFPA_Letter_page-0001.jpg"); + richTextBox1.Text += ParseOCR(testImagePath); + richTextBox1.Text += Environment.NewLine; + } + + private void button2_Click(object sender, EventArgs e) + { + var testImagePath = Path.Combine(_basePath, "#research", "Passport Demand Study Sample Letter.jpg"); + + var barcodes = ReadBarcode(testImagePath); + foreach (var barcode in barcodes) + { + richTextBox1.Text += $"Barcode Type = {barcode.Type} | Value = {barcode.Text} | Page = {barcode.Page}"; + richTextBox1.Text += Environment.NewLine; + } + } + + private void button3_Click(object sender, EventArgs e) + { + var testImagePath = Path.Combine(_basePath, "#research", "Passport Demand Study Sample Letter.jpg"); + + richTextBox1.Text += Environment.NewLine; + + } + + ////var basePath = Path.GetFullPath(Path.GetDirectoryName(Application.ExecutablePath) + "\\..\\..\\..\\"); + + + ////var tt2 = File.Exists(testImagePath); + + //try + //{ + // using (var engine = new TesseractEngine(_tessDataPath, "eng", EngineMode.Default)) + // { + // using (var img = Pix.LoadFromFile(testImagePath)) + // { + // using (var page = engine.Process(img)) + // { + // Console.WriteLine("Mean Confidence = {0}", page.GetMeanConfidence()); + + // //var tt1 = page.GetLSTMBoxText(0); + // //var tt1 = page.GetAltoText(0); + // //var tt1 = page.GetBoxText(0); + // //var tt1 = page.GetHOCRText(0); + // //var tt1 = page.GetMeanConfidence(); + // var tt1 = page.GetText(); + // //var tt1 = page.GetUNLVText(); + + + + + // //Console.WriteLine("Text (GetText): \r\n{0}", text); + // //Console.WriteLine("Text (iterator):"); + // //using (var iter = page.GetIterator()) + // //{ + // // iter.Begin(); + + // // do + // // { + // // do + // // { + // // do + // // { + // // do + // // { + // // if (iter.IsAtBeginningOf(PageIteratorLevel.Block)) + // // { + // // Console.WriteLine(""); + // // } + + // // Console.Write(iter.GetText(PageIteratorLevel.Word)); + // // Console.Write(" "); + + // // if (iter.IsAtFinalOf(PageIteratorLevel.TextLine, PageIteratorLevel.Word)) + // // { + // // Console.WriteLine(); + // // } + // // } while (iter.Next(PageIteratorLevel.TextLine, PageIteratorLevel.Word)); + + // // if (iter.IsAtFinalOf(PageIteratorLevel.Para, PageIteratorLevel.TextLine)) + // // { + // // Console.WriteLine(); + // // } + // // } while (iter.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine)); + // // } while (iter.Next(PageIteratorLevel.Block, PageIteratorLevel.Para)); + // // } while (iter.Next(PageIteratorLevel.Block)); + // //} + // } + // } + // } + //} + //catch (Exception exc) + //{ + // //Trace.TraceError(exc.ToString()); + // Console.WriteLine("Unexpected Error: " + exc.Message); + // //Console.WriteLine("Details: "); + // //Console.WriteLine(exc.ToString()); + //} + //} + + + private string ParseOCR(string filename) + { + string response = null; try { - using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default)) + using (var engine = new TesseractEngine(_tessDataPath, "eng", EngineMode.Default)) { - using (var img = Pix.LoadFromFile(testImagePath)) + using (var img = Pix.LoadFromFile(filename)) { using (var page = engine.Process(img)) { + Debug.WriteLine("Mean Confidence = {0}", page.GetMeanConfidence()); + + //var tt2 = page.GetAltoText(0); + //var tt3 = page.GetBoxText(0); + //var tt4 = page.GetHOCRText(0); //var tt1 = page.GetLSTMBoxText(0); - //var tt1 = page.GetAltoText(0); - //var tt1 = page.GetBoxText(0); - //var tt1 = page.GetHOCRText(0); - //var tt1 = page.GetMeanConfidence(); - //var tt1 = page.GetText(); + //var tt5 = page.GetMeanConfidence(); + //var tt7 = page.GetTsvText(0); + //var tt6 = page.GetUNLVText(); - var tt1 = page.GetUNLVText(); - - - //Console.WriteLine("Mean confidence: {0}", page.GetMeanConfidence()); - - //Console.WriteLine("Text (GetText): \r\n{0}", text); - //Console.WriteLine("Text (iterator):"); - //using (var iter = page.GetIterator()) - //{ - // iter.Begin(); - - // do - // { - // do - // { - // do - // { - // do - // { - // if (iter.IsAtBeginningOf(PageIteratorLevel.Block)) - // { - // Console.WriteLine(""); - // } - - // Console.Write(iter.GetText(PageIteratorLevel.Word)); - // Console.Write(" "); - - // if (iter.IsAtFinalOf(PageIteratorLevel.TextLine, PageIteratorLevel.Word)) - // { - // Console.WriteLine(); - // } - // } while (iter.Next(PageIteratorLevel.TextLine, PageIteratorLevel.Word)); - - // if (iter.IsAtFinalOf(PageIteratorLevel.Para, PageIteratorLevel.TextLine)) - // { - // Console.WriteLine(); - // } - // } while (iter.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine)); - // } while (iter.Next(PageIteratorLevel.Block, PageIteratorLevel.Para)); - // } while (iter.Next(PageIteratorLevel.Block)); - //} + response = page.GetText(); } } } } catch (Exception exc) { - Trace.TraceError(exc.ToString()); - Console.WriteLine("Unexpected Error: " + exc.Message); - Console.WriteLine("Details: "); - Console.WriteLine(exc.ToString()); + // error } + + return response; + } + + private Barcode[] ReadBarcode(string filename) + { + var result = new Barcode[0]; + + try + { + using (BarcodeReader reader = new BarcodeReader()) + { + reader.Code39 = true; + reader.Code128 = true; + reader.Upca = true; + reader.Upce = true; + reader.QR = true; + + result = reader.Read(filename, 0); + + //foreach (var barcode in result) + //{ + // Debug.WriteLine($"Barcode type: {barcode.Type} Barcode page: {barcode.Page} Text: {barcode.Text}"); + //} + } + } + catch (Exception exc) + { + Debug.WriteLine(exc.Message); + } + + return result; } } diff --git a/WinFormsApp3.csproj b/WinFormsApp3.csproj index c8cf3ec..19a8fd7 100644 --- a/WinFormsApp3.csproj +++ b/WinFormsApp3.csproj @@ -2,13 +2,14 @@ WinExe - net8.0-windows + net8.0-windows8.0 enable true enable +