using Leadtools;
using Leadtools.Barcode;
using Leadtools.Codecs;
using Leadtools.Forms.Processing.Omr;
using Leadtools.Forms.Processing.Omr.Fields;
using Leadtools.Ocr;
public static IRecognitionForm RecognizeForm(ITemplateForm template, OmrEngine engine)
{
string answerKey = Path.Combine(LEAD_VARS.ImagesDir, @"Forms\OMR Processing\Exam\answer-key.tif");
// create an IRecognitionForm which will contain the results of the recognition operation
IRecognitionForm answers = engine.CreateRecognitionForm();
answers.Name = "Answers";
using (RasterImage answerKeyImage = engine.EnginesObject.RasterCodecs.Load(answerKey))
{
// add each page in the image to the IRecognitionForm
for (int i = 0; i < answerKeyImage.PageCount; i++)
{
answerKeyImage.Page = i + 1;
answers.Pages.AddPage(answerKeyImage);
}
// this recognizes the IRecognitionForm against the specified template
answers.Recognize(template);
// finally, print out the results of the recognition operation
Console.Write("Answers:");
for (int i = 0; i < answers.Pages[0].Fields.Count; i++)
{
OmrField field = answers.Pages[0].Fields[i] as OmrField;
if (field != null)
{
OmrFieldResult omrFieldResult = (OmrFieldResult)field.Result;
Console.Write(omrFieldResult.Text + "\t");
}
}
Console.WriteLine();
}
return answers;
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images";
}
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import org.junit.*;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import static org.junit.Assert.*;
import leadtools.*;
import leadtools.barcode.*;
import leadtools.codecs.*;
import leadtools.forms.processing.omr.*;
import leadtools.forms.processing.omr.fields.*;
import leadtools.ocr.*;
public OmrEngine CreateOmrEngine() {
final String OCR_LEAD_RUNTIME_DIR = "C:\\LEADTOOLS23\\Bin\\Common\\OcrLEADRuntime";
OmrEngine omrEngine = new OmrEngine();
// the OmrEngine requires an OcrEngine to be associated with it and for that
// engine to be started
OcrEngine engine = OcrEngineManager.createEngine(OcrEngineType.LEAD);
engine.startup(null, null, null, OCR_LEAD_RUNTIME_DIR);
omrEngine.getEnginesObject().setOcrEngine(engine);
omrEngine.getEnginesObject().setBarcodeEngine(new BarcodeEngine());
// the OmrEngine also has a property for a RasterCodecs object we can use for
// convenience
RasterCodecs codecs = new RasterCodecs();
codecs.getOptions().getLoad().setResolution(300);
codecs.getOptions().getLoad().setXResolution(300);
codecs.getOptions().getLoad().setYResolution(300);
codecs.getOptions().getRasterizeDocument().getLoad().setResolution(300);
codecs.getOptions().getRasterizeDocument().getLoad().setXResolution(300);
codecs.getOptions().getRasterizeDocument().getLoad().setYResolution(300);
omrEngine.getEnginesObject().setRasterCodecs(codecs);
return omrEngine;
}
public static ITemplateForm CreateNewTemplate(RasterImage templateImage, OmrEngine engine) {
ITemplateForm template = engine.createTemplateForm();
template.setName("Example Template");
// add each page in the source image to the template
// templates can be constructed from any number of rasterimages from different
// sources
for (int i = 0; i < templateImage.getPageCount(); i++) {
templateImage.setPage(i + 1);
template.getPages().addPage(templateImage);
}
return template;
}
public static void AddZonesToTemplate(ITemplateForm template) {
// create two different zones with definitive boundaries
OmrField firstNameField = new OmrField();
firstNameField.setBounds(new LeadRect(152, 768, 788, 1276));
firstNameField.setName("FirstName");
firstNameField.setPageNumber(1);
OmrField testField = new OmrField();
testField.setBounds(new LeadRect(148, 2067, 384, 900));
testField.setPageNumber(1);
testField.setName("Questions 1-15");
// parse the template document for any Omr regions in the boundaries
template.extractInfo(1, new Field[] { firstNameField, testField });
// get the options for the first field (this field is an Alphabetic "Name"
// field)
OmrFieldOptions firstNameOptions = firstNameField.getOptions();
firstNameOptions.setFieldOrientation(OmrFieldOrientation.COLUMN_WISE); // this region should be decomposed into
// columns
firstNameOptions.setTextFormat(OmrTextFormat.AGGREGATED); // these values are contiguous and merged together (such
// as individual letters in a name)
firstNameOptions.setGradeThisField(false); // this region will not be included in statistics generation
firstNameField.setOptions(firstNameOptions);
OmrFieldOptions testFieldOptions = testField.getOptions();
testFieldOptions.setFieldOrientation(OmrFieldOrientation.ROW_WISE); // this region should be decomposed into rows
testFieldOptions.setTextFormat(OmrTextFormat.CSV); // these values are independent and kept separate (such as
// multiple choice answers)
testFieldOptions.setGradeThisField(true); // this region will have statistics generated for it
testFieldOptions.setCorrectGrade(1.0); // the relative weight of this region if this matches the answer key
// (reward for correct answer)
testFieldOptions.setIncorrectGrade(0.0); // relative weight of this region if it does not match the answer key
// (active penalty for incorrect answers)
testFieldOptions.setNoResponseGrade(0.0); // relative weight of this region if no response present (active penalty
// for failing to answer)
testField.setOptions(testFieldOptions);
// GenerateOmrFieldValues.Generate() is a helper function to make value sets
// based on the dimension
// for this dimension and orientation, the output range is:
// [0] 0 -> 25
// [1] 1 -> 26
// [2] 25 -> 0
// [3] 26 -> 1
// [4] A -> Z
// [5] Z -> A
List<List<String>> availableFirstNameRanges = GenerateOmrFieldValues.generate(firstNameField.getRowsCount(),
firstNameField.getColumnsCount(), firstNameField.getOptions().getFieldOrientation());
firstNameField.setValues(availableFirstNameRanges.get(4)); // this index contains the basic English alphabet
// a custom list can also be used as long as it contains the same number of
// elements based on dimension and orientation
// a row-wise orientation with four columns in each row requires four values
testField.setValues(new ArrayList<String>(List.of("A", "B", "C", "D")));
template.getPages().getPage(1).getFields().add(firstNameField);
template.getPages().getPage(1).getFields().add(testField);
}
public void IRecognitionFormRecognizeExample() {
final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images";
OmrEngine omrEngine = CreateOmrEngine();
assertTrue(omrEngine.getEnginesObject().getOcrEngine().isStarted());
// create template for recognition
String templateFileName = combine(LEAD_VARS_IMAGES_DIR, "Forms\\OMR Processing\\Exam\\exam.tif");
RasterImage templateImage = omrEngine.getEnginesObject().getRasterCodecs().load(templateFileName, 0,
CodecsLoadByteOrder.BGR, 1, 1);
ITemplateForm template = CreateNewTemplate(templateImage, omrEngine);
AddZonesToTemplate(template);
assertTrue(template.getPages().getPage(1) != null);
// create an IRecognitionForm which will contain the results of the recognition
// operation
String answerKey = combine(LEAD_VARS_IMAGES_DIR, "Forms\\OMR Processing\\Exam\\answer-key.tif");
IRecognitionForm answers = omrEngine.createRecognitionForm();
answers.setName("Answers");
RasterImage answerKeyImage = omrEngine.getEnginesObject().getRasterCodecs().load(answerKey);
// add each page in the image to the IRecognitionForm
for (int i = 0; i < answerKeyImage.getPageCount(); i++) {
answerKeyImage.setPage(i + 1);
answers.getPages().addPage(answerKeyImage);
}
// this recognizes the IRecognitionForm against the specified template
answers.recognize(template);
// finally, print out the results of the recognition operation
System.out.println("Answers:");
for (int i = 0; i < answers.getPages().getPage(1).getFields().size(); i++) {
OmrField field = (OmrField) answers.getPages().getPage(1).getFields().get(i);
if (field != null) {
OmrFieldResult omrFieldResult = (OmrFieldResult) field.getResult();
assertTrue(omrFieldResult != null);
System.out.print(omrFieldResult.getText() + "\t");
}
}
System.out.println();
}