Demo entry 4094619

Tesseract OCR

   

Submitted by anonymous on Mar 18, 2016 at 23:58
Language: C#. Code size: 2.9 kB.

using System;
using System.Drawing;
using System.Drawing.Imaging;
using OCR.TesseractWrapper;

namespace TestTesseract
{
    class Program
    {
        static void Main()
        {
            DoOcr();
            Console.WriteLine("Finished.");
            Console.ReadKey();
        }
        private static void DoOcr()
        {
            const string fileImage = @"D:\example\image\sample (1).jpg";
            const string language = "eng";
            const string tessdata = @"D:\example\tesseract-ocr\tessdata\";
            var oem = 1;
            var processor = new TesseractProcessor();
            processor.Init(tessdata, language, oem);
            processor.SetPageSegMode(ePageSegMode.PSM_SINGLE_LINE);
            processor.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
            var result = GetTextImage(fileImage, processor);
            Console.WriteLine("Recognize Text: {0}", result);
            processor.Dispose();
        }
        private static string GetTextImage(string filePath, TesseractProcessor processor)
        {
            var scaleVal = 3f;
            var textImage = "";
            using (var imgBmp = Image.FromFile(filePath) as Bitmap)
            {
                if (imgBmp != null)
                {
                    SetPixelColor(imgBmp, false);
                    imgBmp.Save(@"D:\example\image\sample (1) cleared 1.png", ImageFormat.Png);//after the first cleaning
                    using (var imgBmpScaled = new Bitmap(imgBmp, new Size((int)(imgBmp.Width * scaleVal), (int)(imgBmp.Height * scaleVal))))
                    {
                        imgBmp.Save(@"D:\example\image\sample (1) scaled.png", ImageFormat.Png);//after scaled
                        SetPixelColor(imgBmpScaled);
                        imgBmp.Save(@"D:\example\image\sample (1) cleared 2.png", ImageFormat.Png);//after the second cleaning
                        textImage = processor.Recognize(imgBmpScaled).Replace("\n", "").Replace(" ", "");
                        processor.ClearResults();
                    }
                }
            }
            return textImage;
        }
        private static void SetPixelColor(Bitmap imgBmp, bool hasBeenCleared = true) //type 0 dafault, image has has been cleared.
        {
            var bgColor = Color.White;
            var textColor = Color.Black;
            for (var x = 0; x < imgBmp.Width; x++)
            {
                for (var y = 0; y < imgBmp.Height; y++)
                {
                    var pixel = imgBmp.GetPixel(x, y);
                    var isCloserToWhite = hasBeenCleared ? ((pixel.R + pixel.G + pixel.B)/3) > 180 : ((pixel.R + pixel.G + pixel.B)/3) > 120;
                    imgBmp.SetPixel(x, y, isCloserToWhite ? bgColor : textColor);
                }
            }
        }
    }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).