Aspose一直致力于研究用于执行文件间格式转换,对文件进行操作(例如创建、版本、操作等)的文件格式API。好消息来啦!专门用于Java平台识别光学标识并管理转换的API控件Aspose.OMR for Java首次公开发行啦!
Aspose.OMR for Java是一种可从多种图像格式识别光学标记,包括PNG,GIF,JPEG,TIFF,BMP的API。API将输出保存为CSV和JSON格式,并且可以在执行OMR操作时以文本格式显示结果。可点击文末“了解更多”下载体验。
Aspose.OMR for Java数据处理API功能
- 识别扫描的图像和照片
- 准确率高
- 处理旋转和透视图像
- 从TXT文件生成OMR模板
- 识别来自测试,考试,问卷,调查的数据
- 将结果保存为CSV和JSON格式
接下来,让我们快速浏览一下上述Java API的功能,以了解如何识别各种图像格式的光学标记,以及如何从包含MCQ的调查,问卷或测试中捕获人类标记的数据。
用Java动态创建OMR模板
Aspose.OMR for Java提供了从创建OMR模板到识别光学标记以捕获数据的完整功能集。该API支持通过简单的文本标记生成OMR模板文件或图像。您只需将模板的文本标记传递给API,它将为您生成模板。以下是OMR模板的示例文本标记。
我们可以简单地将文本标记保存在扩展名为.txt的文本文件中。完成后,可以使用以下步骤生成模板:
- 创建一个OmrEngine对象。
- 调用OmrEngine.generateTemplate()方法,该方法接受标记文本文件的路径。
- 使用GenerationResult.save方法保存模板。
下面的代码示例演示如何使用Java从文本标记生成OMR模板。
String outputDirectory = \"GenerationResult\";
String[] GenerationMarkups = new String[] { \"Sheet.txt\", \"Grid.txt\", \"AsposeTest.txt\" };
String[] GenerationMarkupsNoExt = new String[] { \"Sheet\", \"Grid\", \"AsposeTest\" };
OmrEngine engine = new OmrEngine();
for (int i = 0; i < GenerationMarkups.length; i++) { // call template generation providing path to the txt file with markup GenerationResult res = engine.generateTemplate(GenerationMarkups[i]); // check in case of errors if (res.getErrorCode() != 0) { System.out.println(\"ERROR CODE: \" + res.getErrorCode()); } // save generation result: image and .omr template res.save(outputDirectory, GenerationMarkupsNoExt[i]); }
输出结果:
使用Java的图像中的光学标记识别(OMR)
为了在图像中执行OMR,只需要两件事–准备的OMR模板(.omr)和图像(用户填写的表单/工作表)就可以执行OMR。API支持以下图像格式的OMR:
- JPEG
- PNG
- GIF
- TIFF
- BMP
以下是在图像中执行OMR的步骤:
- 创建OmrEngine对象。
- 创建TemplateProcessor对象,并使用OMR模板的路径对其进行初始化。
- 使用TemplateProcessor.recognizeImage()方法识别图像,并以CSV或JSON格式获取结果。
下面的代码示例演示如何使用Java识别图像中的光学标记:
String[] UserImages = new String[] { \"Sheet1.jpg\", \"Sheet2.jpg\" };
String[] UserImagesNoExt = new String[] { \"Sheet1\", \"Sheet2\" };
String outputDirectory = \"Result\";
String templatePath = \"Sheet.omr\";
// initialize engine and get template processor providing path to the .omr file
OmrEngine engine = new OmrEngine();
TemplateProcessor templateProcessor = engine.getTemplateProcessor(templatePath);
System.out.println(\"Template loaded.\");
// images loop
for (int i = 0; i < UserImages.length; i++) { // path to the image to be recognized String imagePath = UserImages[i]; System.out.println(\"Processing image: \" + imagePath); // recognize image and receive result RecognitionResult result = templateProcessor.recognizeImage(imagePath); // export results as csv string String csvResult = result.getCsv(); String json = result.getJson(); // save csv to the output folder PrintWriter wr = new PrintWriter(new FileOutputStream(UserImagesNoExt[i] + \".csv\"), true); wr.println(csvResult); }
对OMR使用自定义识别阈值
可以通过在0到100之间定义一个自定义阈值来微调OMR结果。增大阈值会使API在识别答案时更加严格。可以在TemplateProcessor.recognizeImage()方法中将阈值设置 为第二个参数,如以下Java代码示例所示。
String[] UserImages = new String[] { \"Sheet1.jpg\", \"Sheet2.jpg\" };
String[] UserImagesNoExt = new String[] { \"Sheet1\", \"Sheet2\" };
String outputDirectory = \"Result\";
String templatePath = \"Sheet.omr\";
int customThreshold = 40;
// initialize engine and get template processor providing path to the .omr file
OmrEngine engine = new OmrEngine();
TemplateProcessor templateProcessor = engine.getTemplateProcessor(templatePath);
System.out.println(\"Template loaded.\");
// images loop
for (int i = 0; i < UserImages.length; i++) { // path to the image to be recognized String imagePath = UserImages[i]; System.out.println(\"Processing image: \" + imagePath); // recognize image and receive result RecognitionResult result = templateProcessor.recognizeImage(imagePath, customThreshold); // export results as csv string String csvResult = result.getCsv(); String json = result.getJson(); // save csv to the output folder PrintWriter wr = new PrintWriter(new FileOutputStream(UserImagesNoExt[i] + \".csv\"), true); wr.println(csvResult); }
重新计算OMR结果
在某些情况下,您可能需要使用不同的阈值重新计算OMR结果。在这种情况下,不必一次又一次地调用TemplateProcessor.recognizeImage(),而是可以使用TemplateProcessor.recalculate()方法配置用于自动重新计算的API,以提高图像处理效率。下面的代码示例演示如何实现OMR结果的重新计算。
String[] UserImages = new String[] { \"Sheet1.jpg\", \"Sheet2.jpg\" };
String[] UserImagesNoExt = new String[] { \"Sheet1\", \"Sheet2\" };
String outputDirectory = \"Result\";
String templatePath = \"Sheet.omr\";
// init engine and get template processor
OmrEngine engine = new OmrEngine();
TemplateProcessor templateProcessor = engine.getTemplateProcessor(templatePath);
System.out.println(\"Template loaded.\");
// Set custom threshold to use in recalculation
// this value is in range (0 to 100)
// represents the percentage of required black pixels on bubble image to be recognized
// i.e. the lower the value - the less black pixels required for bubble to be counted as filled and vice versa
int CustomThreshold = 40;
// images loop
for (int i = 0; i < UserImages.length; i++)
{
String image = UserImages[i];
String imagePath = image;
System.out.println(\"Processing image: \" + imagePath);
// recognize image RecognitionResult
result = templateProcessor.recognizeImage(imagePath);
// get export csv string
String stringRes = result.getCsv();
// save csv to output folder
String outputName = UserImagesNoExt[i] + \".csv\";
PrintWriter wr = new PrintWriter(new FileOutputStream(outputName), true);
wr.println(stringRes);
System.out.println(\"Export done. Path: \" + outputName);
// recalculate recognition results with custom threshold
templateProcessor.recalculate(result, CustomThreshold);
// get export csv string
stringRes = result.getCsv();
// save recalculated results outputName = UserImagesNoExt[i] + \"_recalculated.csv\";
wr = new PrintWriter(new FileOutputStream(outputName), true);
wr.println(stringRes);
System.out.println(\"Recalculated result export done. Path: \" + outputName);
System.out.println();
}