Using EasyOCR2

Detecting the Characters

EasyOCR2 first segment pixels to detect the blobs in an image. It then places textboxes over the detected blobs following a user-defined topology specifying the number of lines, words and characters in the text (see Setting the Topology).

You can specify a character type (letter, digit and/or symbol) for each character in the text, to improve the recognition rate and speed.

To find characters in an image:

#include <eVision/EasyOcr/EasyOCR2/EOCR2.h>
using namespace eVision;
// Load an Image
EImageBW8 image;
image.Load("image.tif");
// Attach a ROI to the image
EROIBW8 roi;
roi.Attach(&image, 50, 224, 340, 96);
// Create an EOCR2 instance
EOCR2 ocr2;
// Enable the topology usage
ocr2.SetEnabledTopology(true);
// Set the expected character sizes
ocr2.SetCharsWidthRange(EIntegerRange(25, 25));
ocr2.SetCharsHeight(37);
// Set the text polarity, in this case WhiteOnBlack
ocr2.SetTextPolarity(EasyOCR2TextPolarity_WhiteOnBlack);
// Set the topology
ocr2.SetTopology(".{10}\n.{3} .{4}");
// Detect the text in the image. The output Text structure contains:
// - an individual textbox for each character
// - an individual bitmap image for each character
// - a threshold value to binarize the bitmap image for each character
// All structured in a hierarchy with Lines -> Words -> Characters
EOCR2Text text = ocr2.Detect(roi);
using eVision;
// Load an Image
EImageBW8 image = new EImageBW8();
image.Load("image.tif");
// Attach a ROI to the image
EROIBW8 roi = new EROIBW8();
roi.Attach(image, 50, 224, 340, 96);
// Create an EOCR2 instance
EOCR2 ocr2 = new EOCR2();
// Enable the topology usage
ocr2.EnabledTopology = true;
// Set the expected character sizes
ocr2.CharsWidthRange = new EIntegerRange(25, 25);
ocr2.CharsHeight = 37;
// Set the text polarity, in this case WhiteOnBlack
ocr2.TextPolarity = EasyOCR2TextPolarity.WhiteOnBlack;
// Set the topology
ocr2.Topology = ".{10}\n.{3} .{4}";
// Detect the text in the image. The output Text structure contains:
// - an individual textbox for each character
// - an individual bitmap image for each character
// - a threshold value to binarize the bitmap image for each character
// All structured in a hierarchy with Lines –> Words -> Characters
EOCR2Text text = ocr2.Detect(roi);
import eVision
# Load an Image
image = eVision.EImageBW8()
image.Load("image.tif")
# Attach a ROI to the image
roi = eVision.EROIBW8()
roi.Attach(image, 50, 224, 340, 96)
# Create an EOCR2 instance
ocr2 = eVision.EOCR2()
# Enable the topology usage
ocr2.EnabledTopology = True;
# Set the expected character sizes
ocr2.CharsWidthRange = eVision.EIntegerRange(25, 25)
ocr2.CharsHeight = 37
# Set the text polarity, in this case WhiteOnBlack
ocr2.TextPolarity = eVision.EasyOCR2TextPolarity.WhiteOnBlack
# Set the topology
ocr2.Topology = ".{10}\n.{3} .{4}"
# Detect the text in the image. The output Text structure contains:
# - an individual textbox for each character
# - an individual bitmap image for each character
# - a threshold value to binarize the bitmap image for each character
# All structured in a hierarchy with Lines -> Words -> Characters
text = ocr2.Detect(roi)

Hint

Don’t forget to setup the parameters:

Hint

See also

Setting a Topology

The parameter (Topology .NET Python) specifies the structure of the text (number of lines, words and characters) as well as the type of characters in the text.

Note

  • The box-fitting method uses this parameter to fit the textboxes to the detected blobs.

  • The recognition method limits the number of candidates for each character based on the topology.

  • The topology uses the following modified regular expression (Regex) wildcards:

    • “.” (dot) represents any character (not including a space).

    • “L” represents an alphabetic character:

      • “Lu” represents an uppercase alphabetic character.

      • “Ll” represents a lowercase alphabetic character.

    • “N” represents a digit.

    • “P” represents a punctuation character among: ! “ \# % & ‘ ( ) \* , - . / : ; \< \> ? @ \[ \\ \] \_ { \| } ~

    • “S” represents a symbol among: \$ + - \< = \> \| ~

    • “\n” represents a line break.

    • “ ” (space) represents a space between two words.

  • You can combine these wildcards.

    • For example: [LN] represents an alphanumeric character.

  • To specify multiple characters:

    • Add {n} at the end for n characters.

    • If the amount of characters is uncertain, specify {n,m} for a minimum of n characters and a maximum of m characters.

Warning

  • Some character types are not recognized when using pretrained classifiers:

    • Only “Lu“, “N“ and “P“ are supported.

    • Using “.“ in your topology only results in a character that is either a uppercase letter, a number or a punctuation character.

    • Using “L“ only results in an uppercase letter.

    • Using another character type throws an exception.

[LuN]{3,5}PN{4} \n .{5} LL:

Represents a text comprised of 2 lines:

  • The first line has 1 word:

    • The word has 3 to 5 uppercase alphanumeric characters followed by a punctuation character and 4 digits.

  • The second line has 2 words:

    • The first word has 5 characters (of any type).

    • The second word has 2 letters (upper- or lowercase).

L{3}P N{6} \n L{3}P NNPN{4}:

Represents a text with 2 lines:

  • The first line has 2 words:

    • The first word has 3 uppercase letters followed by a punctuation mark.

    • The second word has 6 digits.

  • The second line also has two words:

    • The first word has 3 uppercase letters followed by a punctuation mark.

    • The second word has 2 digits, followed by a punctuation mark and 4 digits.

.{10} \n .{7} \n .{5} .{5} \n .{5} .{7}:

Represents a text with 4 lines:

  • The first line contains 1 word of 10 characters (of any type).

  • The second line contains 1 word of 7 characters

  • The third line contains 2 words, each of 5 characters.

  • The fourth line contains 2 words of 5 and 7 characters respectively.

Learning the Characters

In order to recognize characters, EasyOCR2 there are two modes: Using the pre-trained classifier, or learning the characters from reference data. To use a database of known characters, you can either generate it from images and/or from TrueType system fonts.

  • To learn from the detected characters in your image as described in Detecting the Characters:

    #include <eVision/EasyOcr/EasyOCR2/EOCR2.h>
    
    using namespace eVision;
    // Load an Image
    EImageBW8 image;
    image.Load("image.tif");
    // Attach a ROI to the image
    EROIBW8 roi;
    roi.Attach(&image, 50, 224, 340, 96);
    // Create an EOCR2 instance
    EOCR2 ocr2;
    // Enable the topology usage
    ocr2.SetEnabledTopology(true);
    // Set the required parameters
    ocr2.SetCharsWidthRange(EIntegerRange(25, 25));
    ocr2.SetCharsHeight(37);
    ocr2.SetTextPolarity(EasyOCR2TextPolarity_WhiteOnBlack);
    ocr2.SetTopology(".{10}\n.{3} .{4}");
    // Learn from the reference image:
    //   1) Detect the text in the image
    EOCR2Text text = ocr2.Detect(roi);
    //   2) Set the true values of the text
    text.SetText("Bt121KPJ80\n786 9512");
    //   3) Add the characters to the character database
    ocr2.Learn(text);
    // Save the character database
    ocr2.SaveCharacterDatabase("myDB.o2d");
    // Alternatively, save the model file.
    // This will store the character database and the parameter settings
    ocr2.Save("myModel.o2m");
    
    using eVision;
    
    // Load an Image
        EImageBW8 image = new EImageBW8();
        image.Load("image.tif");
        // Attach a ROI to the image
        EROIBW8 roi = new EROIBW8();
        roi.Attach(image, 50, 224, 340, 96);
        // Create an EOCR2 instance
        EOCR2 ocr2 = new EOCR2();
    	// Enable the topology usage
        ocr2.EnabledTopology = true;
        // Set the required parameters
        ocr2.CharsWidthRange = new EIntegerRange(25, 25);
        ocr2.CharsHeight = 37;
        ocr2.TextPolarity = EasyOCR2TextPolarity.WhiteOnBlack;
        ocr2.Topology = ".{10}\n.{3} .{4}";
        // Learn from the reference image:
        //   1) Detect the text in the image
        EOCR2Text text = ocr2.Detect(roi);
        //   2) Set the true values of the text
        text.Text = "Bt121KPJ80\n786 9512";
        //   3) Add the characters to the character database
        ocr2.Learn(text);
        // Save the character database
        ocr2.SaveCharacterDatabase("myDB.o2d");
        // Alternatively, save the model file.
        // This will store the character database and the parameter settings
        ocr2.Save("myModel.o2m");
    
    import eVision
    # Load an Image
    image = eVision.EImageBW8()
    image.Load("image.tif")
    # Attach a ROI to the image
    roi = eVision.EROIBW8()
    roi.Attach(image, 50, 224, 340, 96)
    # Create an EOCR2 instance
    ocr2 = eVision.EOCR2()
    # Enable the topology usage
    ocr2.EnabledTopology = True;
    # Set the required parameters
    ocr2.CharsWidthRange = eVision.EIntegerRange(25, 25)
    ocr2.CharsHeight = 37
    ocr2.TextPolarity = eVision.EasyOCR2TextPolarity.WhiteOnBlack
    ocr2.Topology = ".{10}\n.{3} .{4}"
    # Learn from the reference image:
    #   1) Detect the text in the image
    text = ocr2.Detect(roi)
    #   2) Set the true values of the text
    text.Text = "Bt121KPJ80\n786 9512"
    #   3) Add the characters to the character database
    ocr2.Learn(text)
    # Save the character database
    ocr2.SaveCharacterDatabase("myDB.o2d")
    # Alternatively, save the model file.
    # This will store the character database and the parameter settings
    ocr2.Save("myModel.o2m")
    

    Hint

    Alternatively, you can Save .NET Python, SaveCharacterDatabase .NET Python for more informations.

  • To learn directly from a True Type font instead:

Hint

Recognizing the Characters

To recognize characters, EasyOCR2 uses a pretrained classifier or a classifier that you trained on your character database.

For each input character:

  • The classifier calculates a score for all candidate outputs.

  • It returns the candidate with the highest score as the recognition result.

Use a topology to pass information to the classifier about each character. This reduces the number of candidates and improves the recognition rate (see Setting the Topology).

To recognize the characters detected as described in Detecting the Characters and learned as described in Learning the Characters:

#include <eVision/EasyOcr/EasyOCR2/EOCR2.h>
using namespace eVision;
// Load an image
EImageBW8 image;
image.Load("image.tif");
// Attach an ROI
EROIBW8 roi;
roi.Attach(&image, 50, 224, 340, 96);
// Create an EOCR2 instance
EOCR2 ocr2;
// Enable the topology usage
ocr2.SetEnabledTopology(true);
// Set the required parameters
ocr2.SetCharsWidthRange(EIntegerRange(25, 25));
ocr2.SetCharsHeight(37);
ocr2.SetTopology("[LN]{10}\nN{3} N{4}");
ocr2.SetTextPolarity(EasyOCR2TextPolarity_WhiteOnBlack);
// Add a pre-made character database to the EOCR2 instance
ocr2.AddCharactersToDatabase("myDB.o2d");
// Read text from the image
EOCR2Text result = ocr2.Read2(roi);
using eVision;
// Load an image
    EImageBW8 image = new EImageBW8();
    image.Load("image.tif");
    // Attach an ROI
    EROIBW8 roi = new EROIBW8();
    roi.Attach(image, 50, 224, 340, 96);
    // Create an EOCR2 instance
    EOCR2 ocr2 = new EOCR2();
	// Enable the topology usage
    ocr2.EnabledTopology = true;
    // Set the required parameters
    ocr2.CharsWidthRange = new EIntegerRange(25, 25);
    ocr2.CharsHeight = 37;
    ocr2.Topology = "[LN]{10}\nN{3} N{4}";
    ocr2.TextPolarity = EasyOCR2TextPolarity.WhiteOnBlack;
    // Add a pre-made character database to the EOCR2 instance
    ocr2.AddCharactersToDatabase("myDB.o2d");
    // Read text from the image
    EOCR2Text result = ocr2.Read2(roi);
import eVision
# Load an image
image = eVision.EImageBW8()
image.Load("image.tif")
# Attach an ROI
roi = eVision.EROIBW8()
roi.Attach(image, 50, 224, 340, 96)
# Create an EOCR2 instance
ocr2 = eVision.EOCR2()
# Enable the topology usage
ocr2.EnabledTopology = True;
# Set the required parameters
ocr2.CharsWidthRange = eVision.EIntegerRange(25, 25)
ocr2.CharsHeight = 37
ocr2.Topology = "[LN]{10}\nN{3} N{4}"
ocr2.TextPolarity = eVision.EasyOCR2TextPolarity.WhiteOnBlack
# Add a pre-made character database to the EOCR2 instance
ocr2.AddCharactersToDatabase("myDB.o2d")
# Read text from the image
result = ocr2.Read2(roi)

They are two familly of classifier to use:

Hint

  • In addition to the detected text, EOCR2Text .NET Python objects contain useful additional data:

    • The coordinates and the size of each textbox.

    • A bitmap image of each textbox.

    • A list of the recognition scores for each character.

Hint

  • Use Load .NET Python to read the model (.o2m) file from your disk. It contains:

    • All the detection parameters,

    • The topology,

    • The reference character database.

See also

Accelerate the Recognition

You can accelerate the character recognition speed using multithreading and/or a GPU.