CogOCRMaxTool AutoTune Method Cognex VisionPro 9.5

For use with OCRMaxTool Tuning

Tunes the OCRMax Tool. Tuning searches for parameters that produce a line result which matches correctRecord which is known to be correct.

AutoTune takes into account previously AutoTuned records and current tool parameters when tuning.

Namespace: Cognex.VisionPro.OCRMax
Assembly: Cognex.VisionPro.OCRMax (in Cognex.VisionPro.OCRMax.dll) Version: 69.0.0.0
Syntax

public void AutoTune(
	CogOCRMaxTuneRecord correctRecord
)

Parameters

correctRecord
Type: Cognex.VisionPro.OCRMax CogOCRMaxTuneRecord
An OCRMax tune record which the user has verified to be correct. The tune record should be a representative example of the types of images you wish the tool to read.
Exceptions

ExceptionCondition
ArgumentNullException
  • Thrown if correctRecord is null.
ArgumentException
  • Thrown if correctRecord does not contain the same number of CharacterCodes, MarkRects, and CellRects.
InvalidOperationException
  • Thrown if TuneParams is null.
  • Thrown if TuneData is null.
  • Thrown if Segmenter is null.
  • Thrown if ClassifierRunParams is null.
  • Thrown if Classifier.TrainParams is null.
CogOCRMaxInvalidTuneRecordException
  • Thrown if the correctRecord has an unallocated image
  • Thrown if correctRecord contains no character codes
  • Thrown if correctRecord character codes contains an unknown character marker
  • Thrown if correctRecord has a mark rect which has an angle or skew greater than 45 degrees relative to the region
  • Thrown if correctRecord number of mark rects != number of cell rects != number of character codes
Remarks

A "correct" record contains the image, region, and correct string of character codes, and the mark and cell rects which correctly identify the location of each character in the image.

Examples

' Create a new CogOCRMaxTool to demonstrate OCRMax tuning.
Dim myOCRMaxTool As New CogOCRMaxTool()

' Load an image which contains the text "Date:10/01/2009"
Dim ifPath As String = System.Environment.GetEnvironmentVariable("VPRO_ROOT")  & _
                       "\images\OCRMax_dotmatrix_multiline.idb"

Dim imageFileTool As New CogImageFileTool()
imageFileTool.[Operator].Open(ifPath, CogImageFileModeConstants.Read)
imageFileTool.Run()
Dim image As CogImage8Grey = TryCast(imageFileTool.OutputImage, CogImage8Grey)
myOCRMaxTool.InputImage = image

' Set up the OCRMaxTool's Region so that it contains the text
' we wish to read.
Dim region As New CogRectangleAffine()
region.SetCenterLengthsRotationSkew(236.822, 65.6591, 433.645, 43.7727, 0, 0)
myOCRMaxTool.Region = region

' Run the OCRMaxTool on the image
myOCRMaxTool.Run()

' Note that the OCRMaxTool was unable to correctly read the string.

' Here we extract a tune record from the OCRMaxTool's line result.
Dim extractedRecord As CogOCRMaxTuneRecord = _
  myOCRMaxTool.LineResult.CreateTuneRecordFromResult(myOCRMaxTool.InputImage, _
                                                     myOCRMaxTool.Region)

' A collection of UTF-32 character codes is created which represents
' the string contained in the image.
Dim characterCodes As New CogOCRMaxCharKeyCollection("Date:10/01/2009", "?")

' Here we update the extracted tune records with the 
' character codes we expect the tool to read.
extractedRecord.CharacterCodes = characterCodes

' Now use the OCRMaxTool's Auto Correct feature to locate the 
' enclosing cell and mark rectangle of each character.
Dim autoCorrectResult As CogOCRMaxTuneRecordCollection = _
  myOCRMaxTool.AutoCorrect(extractedRecord)

' Here we assume the first autoCorrectResult is correct.
' In practice we would want to display the autoCorrectResults 
' and get confirmation before proceeding to the next step.
Dim correctedRecord As CogOCRMaxTuneRecord = autoCorrectResult(0)

' To display the record call:
' CogGraphicCollection myGraphics = correctRecord.CreateTuneRecordGraphics("?");
' myCogDisplay.Image = correctRecord.Image;
' myCogDisplay.StaticGraphics.AddList(myGraphics, "tuneRecordGraphics");

' Note that if none of the autoCorrectResult records are actually correct, we
' must choose one of the records and manually modify it so that it is correct.
' The process to manually modify a record is more complex and is beyond the 
' scope of this sample.

' Note that if you do manually correct an autoCorrectResult record before
' passing it to AutoTune you must also set the SegmenterCorrected and
' ClassifierCorrected properties of the record to indicate that it was
' manually corrected.

' Once we have a correct record, the OCRMaxTool can be tuned.
' Tuning the OCRMaxTool updates the Segmenter and Classifier.Font
' properties of the OCRMaxTool such that the new settings can 
' successfully read the tune record image (and future images
' which are similar).
myOCRMaxTool.AutoTune(correctedRecord)

' All tuned records are stored in the OCRMaxTool's TuneData property.
' Copies of the tuned records can be accessed like this:      
'   CogOCRMaxTuneRecordCollection tunedRecords = 
'     myOCRMaxTool.TuneData.GetTuneRecords();

' Tune Records can be removed from the OCRMaxTool's TuneData like this:     
'   myOCRMaxTool.TuneData.RemoveTuneRecord(0); 

' After the OCRMaxTool has been tuned it should be able to
' succesfully read the image.
myOCRMaxTool.Run()

' Check to see that the tool now reads the record correctly
Assert.IsTrue(myOCRMaxTool.LineResult.ResultString = "Date:10/01/2009")

' At this point the user would continue running the OCRMaxTool
' on new images until an image which could not be read is encountered.

' When this happens the steps, starting with extracting the tune record
' from the failing result, are repeated.
// Create a new CogOCRMaxTool to demonstrate OCRMax tuning.
CogOCRMaxTool myOCRMaxTool = new CogOCRMaxTool();

// Load an image which contains the text "Date:10/01/2009" 
string ifPath = System.Environment.GetEnvironmentVariable("VPRO_ROOT") +
@"\images\OCRMax_dotmatrix_multiline.idb";

CogImageFileTool imageFileTool = new CogImageFileTool();
imageFileTool.Operator.Open(ifPath, CogImageFileModeConstants.Read);
imageFileTool.Run();
CogImage8Grey image = imageFileTool.OutputImage as CogImage8Grey;
myOCRMaxTool.InputImage = image;

// Set up the OCRMaxTool's Region so that it contains the text 
// we wish to read.
CogRectangleAffine region = new CogRectangleAffine();
region.SetCenterLengthsRotationSkew(236.822, 65.6591, 433.645, 43.7727, 0, 0);
myOCRMaxTool.Region = region;

// Run the OCRMaxTool on the image
myOCRMaxTool.Run();

// Note that the OCRMaxTool was unable to correctly read the string. 

// Here we extract a tune record from the OCRMaxTool's line result.
CogOCRMaxTuneRecord extractedRecord =
myOCRMaxTool.LineResult.CreateTuneRecordFromResult(myOCRMaxTool.InputImage,
myOCRMaxTool.Region);

// A collection of UTF-32 character codes is created which represents 
// the string contained in the image.
CogOCRMaxCharKeyCollection characterCodes =
new CogOCRMaxCharKeyCollection(@"Date:10/01/2009", "?");

// Here we update the extracted tune records with the 
// character codes we expect the tool to read.
extractedRecord.CharacterCodes = characterCodes;

// Now use the OCRMaxTool's Auto Correct feature to locate the 
// enclosing cell and mark rectangle of each character.
CogOCRMaxTuneRecordCollection autoCorrectResult =
myOCRMaxTool.AutoCorrect(extractedRecord);

// Here we assume the first autoCorrectResult is correct. 
// In practice we would want to display the autoCorrectResults 
// and get confirmation before proceeding to the next step.
CogOCRMaxTuneRecord correctedRecord = autoCorrectResult[0];

// To display the record call: 
// CogGraphicCollection myGraphics = correctRecord.CreateTuneRecordGraphics("?"); 
// myCogDisplay.Image = correctRecord.Image; 
// myCogDisplay.StaticGraphics.AddList(myGraphics, "tuneRecordGraphics"); 

// Note that if none of the autoCorrectResult records are actually correct, we 
// must choose one of the records and manually modify it so that it is correct. 
// The process to manually modify a record is more complex and is beyond the 
// scope of this sample. 

// Note that if you do manually correct an autoCorrectResult record before 
// passing it to AutoTune you must also set the SegmenterCorrected and 
// ClassifierCorrected properties of the record to indicate that it was 
// manually corrected. 

// Once we have a correct record, the OCRMaxTool can be tuned. 
// Tuning the OCRMaxTool updates the Segmenter and Classifier.Font 
// properties of the OCRMaxTool such that the new settings can 
// successfully read the tune record image (and future images 
// which are similar).
myOCRMaxTool.AutoTune(correctedRecord);

// All tuned records are stored in the OCRMaxTool's TuneData property. 
// Copies of the tuned records can be accessed like this: 
//   CogOCRMaxTuneRecordCollection tunedRecords = 
//     myOCRMaxTool.TuneData.GetTuneRecords(); 

// Tune Records can be removed from the OCRMaxTool's TuneData like this: 
//   myOCRMaxTool.TuneData.RemoveTuneRecord(0); 

// After the OCRMaxTool has been tuned it should be able to 
// succesfully read the image.
myOCRMaxTool.Run();

// Check to see that the tool now reads the record correctly
Assert.IsTrue(myOCRMaxTool.LineResult.ResultString == "Date:10/01/2009");

// At this point the user would continue running the OCRMaxTool 
// on new images until an image which could not be read is encountered. 

// When this happens the steps, starting with extracting the tune record 
// from the failing result, are repeated.
See Also