Last active
April 26, 2024 05:19
-
-
Save aspose-com-gists/c81d318fb45264d04826d6d2a9ba4431 to your computer and use it in GitHub Desktop.
OMR Sheet Reader in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import com.aspose.omr.OmrEngine; | |
import com.aspose.omr.RecognitionResult; | |
import com.aspose.omr.TemplateProcessor; | |
import java.io.FileOutputStream; | |
import java.io.PrintWriter; | |
// OMR Sheet Reader in Java - OMR Sheet PNG | |
public class Main { | |
public static void main(String[] args) throws Exception { | |
// Define working directories. | |
String[] UserImages = new String[] { "/Sheet.png" }; | |
String[] UserImagesNoExt = new String[] { "/files/" }; | |
String templatePath = "/Sheet.omr"; | |
// Create an object of the OmrEngine class. | |
OmrEngine engine = new OmrEngine(); | |
// Invoke the getTemplateProcessor method to get template processor. | |
TemplateProcessor templateProcessor = engine.getTemplateProcessor(templatePath); | |
// Set a custom threshold to use in recalculation whose range is (0 to 100). Lower the value - the fewer black pixels required for a bubble to be counted as filled and vice versa. | |
int CustomThreshold = 0; | |
// Loop through the images. | |
for (int i = 0; i < UserImages.length; i++) | |
{ | |
String image = UserImages[i]; | |
String imagePath = image; | |
// Recognize image by calling the recognizeImage method. | |
RecognitionResult result = templateProcessor.recognizeImage(imagePath); | |
// Call the getCsv method to get the export CSV string and save the CSV to the output folder. | |
String stringRes = result.getCsv(); | |
String outputName = UserImagesNoExt[i] + ".csv"; | |
PrintWriter wr = new PrintWriter(new FileOutputStream(outputName), true); | |
wr.println(stringRes); | |
// Invoke the recalculate method to recalculate recognition results with custom threshold. | |
templateProcessor.recalculate(result, CustomThreshold); | |
stringRes = result.getCsv(); | |
// Save recalculated results in a CSV file. | |
outputName = UserImagesNoExt[i] + "_recalculated.csv"; | |
wr = new PrintWriter(new FileOutputStream(outputName), true); | |
wr.println(stringRes); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment