Learning Characters

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

C++

#include <eVision/EasyOcr/EasyOCR/EOCR.h>
using namespace eVision;
// Image constructor
EImageBW8 srcImage;
// EOCR constructor
EOCR ocr;
// Text to be learned (all digits)
// Assuming the image contains this text
const std::string text = "0123456789";
// ...
// Create a new font
ocr.NewFont(8, 11);
// Adjust the segmentation parameters
ocr.SetTextColor(EOCRColor_BlackOnWhite);
ocr.SetMinCharWidth(15);
ocr.SetMinCharWidth(50);
ocr.SetMinCharHeight(15);
ocr.SetMinCharHeight(75);
ocr.SetNoiseArea(15);
// Segment the characters
ocr.BuildObjects(&srcImage);
ocr.FindAllChars(&srcImage);
// Learn the characters
ocr.LearnPatterns(&srcImage, text, EOCRClass_Digit);
// Save the font into a file
ocr.Save("myFont.ocr");

C#

using eVision;
// Image constructor
EImageBW8 srcImage = new EImageBW8();
// EOCR constructor
EOCR ocr = new EOCR();
// Text to be learned (all digits)
// Assuming the image contains this text
string text = "0123456789";
// ...
// Create a new font
ocr.NewFont(8, 11);
// Adjust the segmentation parameters
ocr.TextColor = EOCRColor.BlackOnWhite;
ocr.MinCharWidth = 15;
ocr.MaxCharWidth = 50;
ocr.MinCharHeight = 15;
ocr.MaxCharHeight = 75;
ocr.NoiseArea = 15;
// Segment the characters
ocr.BuildObjects(srcImage);
ocr.FindAllChars(srcImage);
// Learn the characters
ocr.LearnPatterns(srcImage, text, (int)EOCRClass.Digit);
// Save the font into a file
ocr.Save("myFont.ocr");

Python

import eVision
# Image constructor
src_image = eVision.EImageBW8()
# EOCR constructor
ocr = eVision.EOCR()
# Text to be learned (all digits)
# Assuming the image contains this text
text = "0123456789"
# ...
# Create a new font
ocr.NewFont(8, 11)
# Adjust the segmentation parameters
ocr.TextColor = eVision.EOCRColor.BlackOnWhite
ocr.MinCharWidth = 15
ocr.MinCharWidth = 50
ocr.MinCharHeight = 15
ocr.MinCharHeight = 75
ocr.NoiseArea = 15
# Segment the characters
ocr.BuildObjects(src_image)
ocr.FindAllChars(src_image)
# Learn the characters
ocr.LearnPatterns(src_image, text, eVision.EOCRClass.Digit)
# Save the font into a file
ocr.Save("myFont.ocr")