Tesseract OCR - How to Train For an Image Like This

Viewed 2287

I have a MVC C# application that includes a .Net wrapper for tesseract-ocr nuget. The current version I am using is v4.1.0-beta1. The image that I am try to scan is shown below

enter image description here

My aim is to extract the player name and the number just above them to the left.

I tried making the OCR scan the field/pitch area but the results are way off base. So, I decided to section off all player names and all numbers as seen in the image below. Ratings area marked in blue and player names marked in red. As you can see the name and rating are always the same distance apart.

enter image description here

My current code setup is shown below.

public void Get(HttpPostedFileBase file)
{
    using (var engine = new TesseractEngine(Path.Combine(HttpRuntime.AppDomainAppPath, "tessdata"), "eng+deu", EngineMode.Default))
    {
        var bitmap = (Bitmap)Image.FromStream(file.InputStream, true, true);

        using (var img = PixConverter.ToPix(bitmap))
        {
            SetPlayerRatings(engine, img);
        }
    }
}

private void SetPlayerRatings(TesseractEngine engine, Pix img)
{
    var width = 285;
    var height = 76;

    var textPositions = Service.Get<Formation>(this.FormationId).TextPositions.ToList();

    foreach (var textPosition in textPositions)
    {
        var playerRating = GetPlayerData(engine, img, new Rect(textPosition.X, textPosition.Y, width, height));
    }
}

private static PlayerRating GetPlayerData(TesseractEngine engine, Pix img, Rect region)
{
    using (var page = engine.Process(img, region, PageSegMode.Auto))
    {
        var playerName = page.GetText();
    }

    var ratingRegion = new Rect(region.X1, region.Y1 - 52, 80, 50);

    using (var page = engine.Process(img, ratingRegion, PageSegMode.Auto))
    {
        var playerRating = page.GetText();
    }
}

This code is producing the correct results for the 1st image.

enter image description here

Is there any way to train OCR so that I dont have to workout the X and Y co-ordinates for each player position? I would like to just specify the area of the pitch and have OCR read in the rating followed by the player name.

1 Answers

With specifying coordinates you solved several problems regarding image processing. So if you do not want to specify coordinates, you have to deal with them: e.g. removing graphics component from OCR area like T-shirt, lines.

Next idea: Tesseract API has option GetComponentImages (I expect C# wrapper should provide it too - I am not familiar with C#), so you can iterate over found components.

Related