Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active January 3, 2020 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/e23bf9c35822b97b61f2910829c5d045 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/e23bf9c35822b97b61f2910829c5d045 to your computer and use it in GitHub Desktop.
Aspose.OMR for Java
This Gist contains code snippets of Aspose.OMR for Java
// For complete examples and data files, please go to https://github.com/aspose-omr/Aspose.OMR-for-Java
String sourceDirectory = Utils.combine(Utils.getSourceDirectory(), "Generation");
String outputDirectory = Utils.combine(Utils.getOutputDirectory(), "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(Utils.combine(sourceDirectory, 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]);
}
// For complete examples and data files, please go to https://github.com/aspose-omr/Aspose.OMR-for-Java
// Initialize license object
License omrLicense = new License();
// Set license
// Pass valid license path
omrLicense.setLicense("Aspose.OMR.lic");
// For complete examples and data files, please go to https://github.com/aspose-omr/Aspose.OMR-for-Java
// Initialize license object
License omrLicense = new License();
// Set license
// Pass valid license path
omrLicense.setLicense(new java.io.FileInputStream("Aspose.OMR.lic"));
// For complete examples and data files, please go to https://github.com/aspose-omr/Aspose.OMR-for-Java
String TemplateName = "Sheet.omr";
String[] UserImages = new String[] { "Sheet1.jpg", "Sheet2.jpg" };
String[] UserImagesNoExt = new String[] { "Sheet1", "Sheet2" };
String sourceDirectory = Utils.getSourceDirectory();
String outputDirectory = Utils.combine(Utils.getOutputDirectory(), "Result");
String templatePath = Utils.combine(Utils.getSourceDirectory(), TemplateName);
// 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 = Utils.combine(sourceDirectory, 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(Utils.combine(outputDirectory, UserImagesNoExt[i] + ".csv")), true);
wr.println(csvResult);
}
// For complete examples and data files, please go to https://github.com/aspose-omr/Aspose.OMR-for-Java
String TemplateName = "Sheet.omr";
String[] UserImages = new String[] { "Sheet1.jpg", "Sheet2.jpg" };
String[] UserImagesNoExt = new String[] { "Sheet1", "Sheet2" };
String sourceDirectory = Utils.getSourceDirectory();
String outputDirectory = Utils.combine(Utils.getOutputDirectory(), "Result");
String templatePath = Utils.combine(Utils.getSourceDirectory(), TemplateName);
// 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..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 = Utils.combine(sourceDirectory, 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 = combine(outputDirectory, 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 = combine(outputDirectory, 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();
}
// For complete examples and data files, please go to https://github.com/aspose-omr/Aspose.OMR-for-Java
String TemplateName = "Sheet.omr";
String[] UserImages = new String[] { "Sheet1.jpg", "Sheet2.jpg" };
String[] UserImagesNoExt = new String[] { "Sheet1", "Sheet2" };
String sourceDirectory = Utils.getSourceDirectory();
String outputDirectory = Utils.combine(Utils.getOutputDirectory(), "Result");
String templatePath = Utils.combine(Utils.getSourceDirectory(), TemplateName);
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 = Utils.combine(sourceDirectory, 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();
String outputName = Utils.combine(outputDirectory, UserImagesNoExt[i] + "_threashold.csv");
// save csv to the output folder
PrintWriter wr = new PrintWriter(new FileOutputStream(outputName), true);
wr.println(csvResult);
System.out.println("Generated File: " + outputName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment