Some My Experiences

Header Ads

Thursday 17 March 2016

Breaking Simple Captcha with Tesseract in C#

This post shows how to break simple captcha using Tesseract in C#. Challenges in the captcha solving is how to train or make a good algorithm so that the computer more better in recognizing the text, such as a human can read the captcha.


Here we do not use these ways, we use other solution. We will use the simpler method,
recognize image patterns and simplify the picture, then tell computer to recognize the image that we simplify by cleaning the image, clarify the characters in the picture, remove the background noise, and scale the image so that the computer can read text more easily and making captcha easier to break.

Ok, we'll start.
  • Download and extract tesseract engine. Click here (Google Code Archive) or here from my dropbox. Tesseract used is tesseractdotnet v301_r590.
  • Download and extract the tessdata folder according to your language you need. Click here to download english language.
  • Download Download sample image captcha : image (1), image (2)
  • Download code : Program.cs.

1. Open Visual Studio, click menu File>New>Project or simply Ctrl+Shift+N.

2. Expand Templates tree, select Visual C# -> Console Application. Choose .Net Framework 2.0 and named project with TestTesseract then click OK.


3. Right click "References", select "Add Reference..." to add tesseract library to the project.
4. Select "Browse" tab and click "Browse..." button.

5. Navigate to the path where the library extracted, and click "Add". Select the tesseract library then click "OK".
 

6. Tesseract library successfully added.

7. Right click "References", select "Add Reference..." to add System.Drawing to the project.
 

8. Expand "Assemblies" and select "Framework" tab. Find and select System.Drawing then click "OK".

9. Copy this code into Program.cs or use Program.cs file you downloaded.
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);
                }
            }
        }
    }
}


Note:
  • Replace value of "fileImage" variable with the location of the image.
  • Replace value of "tessdata" variable with the location where the tessdata extracted.
  • If your computer using x86 platform, jump to step 13 (skip step 10, 11 and 12).

10. Select Configuration Manager from dropdown list (see image).

11. In Platform setion click dropdown arrow and select <New..> option.

12. Select "x86" option for "New platform" and click OK.

13. Run the program by pressing "F5".

Result:
Image (1) before processing:

Image (1) after clearing:

Image (1) after scaled:

Image (1) after clarify:
 

Result Image (1) is "PMIR" :


Image (2) before processing:


Image (2) after clearing:


Image (2) after scaled:


Image (2) after clarify:


Result Image (2) is "GDMU" :

Environment:
Windows 8 64bit
Visual Studio 2012
.Net Framework 2

3 comments:

  1. Your work is truly appreciated round the clock and the globe. It is incredibly a comprehensive and helpful blog. recaptcha solver

    ReplyDelete
  2. great tutorial. now i am start learn and try it

    ReplyDelete
  3. hi can you provide this code like o github or anywhere

    ReplyDelete