如何在 Python 中使用 API
Aspose.OMR 为 Python 的公共 API(omr.api)提供的 FOSS 故意保持精简:OmrEngine、TemplateProcessor、ImageCollection 和 FormValidationLogic。本指南展示了它们在生成-识别工作流中的协同方式。
分步指南
步骤 1:安装软件包
git clone https://github.com/aspose-omr-foss/Aspose.OMR-FOSS-for-Python.git
cd Aspose.OMR-FOSS-for-Python
pip install -e .python -c "import omr; print('Aspose.OMR FOSS for Python is ready.')"步骤 2:导入所需类
from omr.api import OmrEngine, FormValidationLogic步骤 3:生成并保存模板
engine = OmrEngine()
gen = engine.Generate("final-exam.txt")
gen.Save("out", "final-exam")步骤 4:加载用于识别的模板
如果希望对已禁用多选的问题触发违规,而不是静默接受,请传入 FormValidationLogic.Exception 而不是默认的 Ignore。
processor = engine.GetTemplateProcessor("out/final-exam.omr", FormValidationLogic.Exception)步骤 5:识别并导出
result = processor.Recognize("scan.png")
print(result.GetCsv())常见问题与解决方案
FormValidationLogic.Exception 永不抛出,即使在多标记的问题上也是如此。 此检查仅在具有 Multiselect 已禁用; GlobalPageSettings.Multiselect 默认启用,因此请确认已为您期望为单选的题目显式关闭。
我需要一次识别多个扫描。 使用 TemplateProcessor.RecognizeFolder(folderPath, recognitionThreshold) 而不是调用 Recognize() 在循环中。
我在已经被使用的名称下注册了一个镜像。 ImageCollection.Add 不会覆盖 — 检查 Contains(name) 首先,或者为每个镜像使用唯一名称。
常见问题
是否可以安全地在多个模板中重复使用 OmrEngine?
是的 — 它是无状态的。
如何在不重新扫描的情况下,用不同的阈值重新给结果打分?
调用 TemplateProcessor.Recalculate(result, recognitionThreshold)。
如何将内存中的图像嵌入标记中,而不是从磁盘读取?
使用 ImageCollection.Add(name, image_bytes) 注册它,然后在标记中引用为 ?image=<name>。