Training and Using a Custom Model

This code snippet shows how to train and use a custom model.

C++

#include <eVision/EasyOcr/EasyDeepOCR/ETextReader.h>
using namespace eVision;
namespace EDO = eVision::EasyDeepOCR;
namespace EDL = eVision::EasyDeepLearning;

// Create a dataset object.
EDL::EDataset dataset;

// Add your image
int imgId = dataset.AddImage("path/to/image");
// Add an annotation for the text "example" located at the rectangle centered at (128, 128) with size 204x64
std::vector<EDO::ETextLine> texts;
texts.push_back(EDO::ETextLine(ERectangle(EPoint(128.0f, 128.0f), 204.0f, 64.0f), "example"));
dataset.SetOCRAnnotations(imgId, texts);

// Create a EDeepOCRTool object
EDL::EDeepOCRTool tool;
// Set the model you want to train.
tool.SetModel(EasyDeepLearning::EDeepOCRModel_Location);
// Train (autmatic number of iterations)
tool.Train(dataset);
tool.WaitForTrainingCompletion();

// Create a ETextReader object
EDO::ETextReader reader;

// Use the model that we just trained
reader.SetLocationCustomModel(tool);

// Use another custom model trained previously.
reader.SetRecognitionCustomModelPath("/path/to/exported/recognition_tool.edltool");

// Save the reader with the two custom models.
reader.Save("/path/to/reader.edo");

EDO::ETextReader reader2;
reader2.Load("/path/to/reader.edo"); // reader2 contains the same custom models as reader1.

C#

using eVision;
using System.Collections.Generic;
using EDO = eVision.EasyDeepOCR;
using EDL = eVision.EasyDeepLearning;
// Create a dataset object.
EDL.EDataset dataset = new EDL.EDataset();

// Add your image
int imgId = dataset.AddImage("path/to/image");
// Add an annotation for the text "example" located at the rectangle centered at (128, 128) with size 204x64
EDO.ETextLine[] texts = new EDO.ETextLine[1];
texts[0] = new EDO.ETextLine(new ERectangle(new EPoint(128.0f, 128.0f), 204.0f, 64.0f), "example");
dataset.SetOCRAnnotations(imgId, texts);

// Create a EDeepOCRTool object
EDL.EDeepOCRTool tool = new EDL.EDeepOCRTool();
// Set the model you want to train.
tool.Model = EDL.EDeepOCRModel.Location;
// Train (autmatic number of iterations)
tool.Train(dataset);
tool.WaitForTrainingCompletion();

// Create a ETextReader object
EDO.ETextReader reader = new EDO.ETextReader();

// Use the model that we just trained
reader.LocationCustomModel = tool;

// Use another custom model trained previously.
reader.RecognitionCustomModelPath = "/path/to/exported/recognition_tool.edltool";

// Save the reader with the two custom models.
reader.Save("/path/to/reader.edo");

EDO.ETextReader reader2 = new EDO.ETextReader();
reader2.Load("/path/to/reader.edo"); // reader2 contains the same custom models as reader1.

Python

import eVision
from eVision import EasyDeepOCR as EDO
from eVision import EasyDeepLearning as EDL

# Create a dataset object.
dataset = EDL.EDataset()

# Add your image
imgId = dataset.AddImage("path/to/image")
# Add an annotation for the text "example" located at the rectangle centered at (128, 128) with size 204x64
texts = [EDO.ETextLine(eVision.ERectangle(eVision.EPoint(128.0, 128.0), 204.0, 64.0), "example")]
dataset.SetOCRAnnotations(imgId, texts)

# Create a EDeepOCRTool object
tool = EDL.EDeepOCRTool()
# Set the model you want to train.
tool.Model = EDL.EDeepOCRModel.Location
# Train (autmatic number of iterations)
tool.Train(dataset)
tool.WaitForTrainingCompletion()

# Create a ETextReader object
reader = EDO.ETextReader()

# Use the model that we just trained
reader.LocationCustomModel = tool

# Use another custom model trained previously.
reader.RecognitionCustomModelPath = "/path/to/exported/recognition_tool.edltool"

# Save the reader with the two custom models.
reader.Save("/path/to/reader.edo")

reader2 = EDO.ETextReader()
reader2.Load("/path/to/reader.edo") # reader2 contains the same custom models as reader1.