Learning Characters

This code snippet shows how to learn characters based on an image featuring a known text and save the corresponding character database.

C++

#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");

C#

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");

Python

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")