如何在GitHub Copilot上使用IronOCR
.NET团队在使用已建立的Visual Studio解决方案进行文档捕获、应付账款、索赔处理或记录管理时,不需要更换编辑器即可评估GitHub Copilot,因为它支持现有的工作流程。
Copilot在Visual Studio、VS Code、JetBrains IDEs以及GitHub.com上运行。 IronOCR在.NET中本地处理识别。
有关IronOCR用于照片和手写的模型的详细信息,请参考照片阅读指南以及手写指南。
快速入门在您当前的IDE中,说明方法必须返回的内容以及模型本来需要调用的任何默认值。
Add a ReadDeliveryNote method to Imports/DeliveryNotes.cs that uses IronOCR (the
IronOcr NuGet package, namespace IronOcr) to read a scanned delivery note and
return both its text and the barcode values found in the same pass. Barcode
reading is off by default, so switch it on before the read. Take the license key
from the IRONOCR_LICENSE_KEY environment variable, never inline. Then build and
fix any errors.
在编辑器启动的环境中设置IRONOCR_LICENSE_KEY,并查看许可证密钥指南以了解其他保存密钥的地方。 没有它,第一次读取会在许可证检查时停止,Copilot无法运行其编写的OCR代码。
最小工作流程(5 个步骤)
- 从NuGet下载C# OCR库
- 将您的IronOCR约定存入
.github/copilot-instructions.md - 对于单个文件使用内联聊天,当涉及多个文件时使用代理模式
- 当云代理将独立工作时,请编写详细问题
- 在拉取请求中使用API参考确认每个IronOCR成员
Copilot如何适应已经在Visual Studio中的项目中?
对于单文件的IronOCR更改,使用内联聊天。 当更改涉及多个文件时,切换到代理模式,因为它可以构建项目并运行OCR代码。 GitHub上的云代理接收分配的问题,并返回一个拉取请求。
如何为Copilot设置IronOCR约定?
将IronOCR约定提交为.github/copilot-instructions.md,Copilot Chat、代理模式和云代理都会读取它。 内联完成忽略它。
# Copilot instructions
## OCR
- Use IronOCR for all OCR work. The NuGet package is IronOcr on Windows and
IronOcr.Linux on Linux and Docker. The namespace is IronOcr. Do not add
another OCR engine or a cloud OCR service.
- Documentation: https://ironsoftware.com/ocr/csharp/llms.txt
Skill file: https://ironsoftware.com/ocr/csharp/skill.md
- Target the IronOcr version pinned in Directory.Packages.props. Do not upgrade
it as part of an unrelated change.
- Never inline a license key. Read it from configuration or the
IRONOCR_LICENSE_KEY environment variable.
- Always dispose OcrInput with a using block.
- Barcode reading is off by default. Set Configuration.ReadBarCodes only where needed.
- Apply only the image filters that fix a visible defect.
- Do not invent IronOCR API names. If a member is uncertain, say so and cite
the documentation rather than guessing.
## Testing
- Every OCR change must be run against pages with known values.
- Assert on extracted fields. Never assert on the full text of a page.
## Pull requests
- Say which IronOCR APIs the change introduces, and link the page for each.
- If the OCR tests fail, check the runner environment before changing code
that the issue did not ask you to touch.
如何为一个文件夹指定自己的OCR语言规则?
IronOCR设置通常根据来源而有所不同,例如德国供应商发票和英语仓库扫描。 将德语规则放入.github/instructions/ocr-german.instructions.md。 每当正在处理的文件与德语文件顶部的路径模式匹配时,Copilot都会将该文件与存储库范围的文件一起应用。
---
applyTo: "Imports/De/**"
---
- Invoices in this folder are German. Set Language = OcrLanguage.German and add
English with AddSecondaryLanguage(OcrLanguage.English) for supplier names.
- German comes from the IronOcr.Languages.German NuGet package. Language packs
keep their own version numbers, so never align this one with IronOcr.
- Amounts use German formatting, such as 1.234,56. Parse them with the de-DE culture.
使用最新的德语包2020.11.2读取的一个德语样本,并将英语作为辅助语言,返回了"Gesamtbetrag: 3.271,07 EUR",该行的置信度为96.6。
是什么让云代理能够处理一个问题?
仅报告错误发票总数的问题让云代理找到了有问题的文件、受影响的扫描件和正确的总数。
Title: Totals misread on rotated scans from the warehouse scanner
**Files:** Imports/InvoiceReader.cs, Imports/ImportJob.cs
**IronOcr version:** as pinned in Directory.Packages.props
**Framework:** .NET 8
**Expected:** Every invoice in TestData/warehouse/ returns the total recorded in
TestData/warehouse/expected.csv, with line confidence of at least 80.
**Actual:** About a third of warehouse scans return totals with one wrong digit.
The pages are rotated a few degrees and have speckle. Invoices from the email
inbox read correctly. No exception is thrown.
**Constraints:** Keep the Total due parsing as it is. Do not change the public
signature of InvoiceReader.ReadTotal. No new packages.
**Acceptance:** Existing tests pass, plus a new test that reads every scan in
TestData/warehouse/ and matches expected.csv.
邮件发送的发票读取正确的备注指引调查重点放在扫描质量而非解析上。
为什么云代理的测试在IronOCR项目上失败?
默认情况下,云代理在一个临时的Ubuntu Linux运行器上构建和测试。 IronOcr包仅提供Windows本地运行时,因此运行器需要IronOcr.Linux。 一旦合并到默认分支,在.github/workflows/copilot-setup-steps.yml处的设置工作流及其名为copilot-setup-steps的工作设定就会弥补这一差距。
Write .github/workflows/copilot-setup-steps.yml for this repository.
The job must be named copilot-setup-steps exactly, or it is ignored. Trigger it
manually, and on push and pull request limited to that one file path.
Run it on ubuntu-latest with read access to repository contents. Check out the
repository, set up the .NET SDK for the version this project targets, install
the native dependencies IronOCR needs on Ubuntu, then restore packages.
For those dependencies read
https://ironsoftware.com/ocr/csharp/get-started/linux/ and use what it lists for
Ubuntu rather than a list from memory. Note in a comment that the list can
change between releases.
The runner is Linux, so OCR there needs the IronOcr.Linux package this
repository's instructions name. Show me the finished YAML, then any project
change the runner still needs to restore IronOcr.Linux.
- 许可证密钥:云代理无法读取Actions、Codespaces或Dependabot密钥,因此需要一个代理密钥。 在设置下添加密钥,然后在Secrets和变量下,选择Agents,使用编辑器读取的相同名称,代理将其作为环境变量接收。
- 失败的设置步骤:它不会停止云代理。 代理跳过剩余步骤并开始工作,因此从Actions选项卡运行工作流一次,并确认它通过。
合并前应检查什么?
像对待同事一样审查Copilot的拉取请求。
/tests Add xUnit tests for this change: every scan in TestData/warehouse/ returns
the invoice number and total in its expected.csv, a page without a total returns
null rather than a guess, and reads below 80 confidence are flagged. Assert on
extracted fields, not on the full page text.
新的IronOCR版本可以更改间距和换行符而不更改值。 AI编程助手指南将此审查变成可重复的测试。
如何诊断错误的OCR结果?
错误的结果通常有三个原因之一。 页面需要图像滤镜或更高的DPI,文档的语言包从未添加,或者读取目标在错误的区域。 空的条形码集合也可能意味着Configuration.ReadBarCodes从未设置。
Delivery notes in TestData/notes/ return their text, but about a third
come back with an empty barcode collection, while the same notes decode fine on
the handheld scanner.
Explain the cause before changing anything, then fix it in ReadDeliveryNote only.
Do not change the text extraction. Report how many notes return a barcode before
and after. Measure the current count first and base the explanation on what you
measured, not on a guess.
在没有测量优先指令的运行中,Copilot最初将问题归咎于分辨率,然后将缺失的条形码追溯到打印成浅灰色的QR码,并通过裁剪固定区域并选择一种灰色来恢复它们。 在第二组中,条形码打印得较低,该修复未能读取其中的任何一个。 有了指令,Copilot先进行测量,排除对比度、二值化、放大和裁剪,并在应用锐化滤镜后添加第二次读取。 始终在Copilot之前未见过的页面上测试修复。
如果在本地识别工作但在运行器或容器中失败,给Copilot提供完整异常消息而不是摘要。
在实际运行中这些提示生成了什么?
本指南中在编辑器中运行的每个提示都是按顺序发送的,经过两次聊天,针对一个接收样本的应用程序,一个.NET 9控制台项目和具有已知值的扫描。 以下的每个结果都在聊天之外重新测量。
- 带条形码的交付说明在修复前从9个中的6个增加到修复后的9个,同样的修复在第二组中读取了12个中的8个浅色条形码。
/tests套件通过了14个中的14个。它还在未询问的情况下更改了ImportJob的控制台输出,并在构建记录上而不是在扫描中测试了信心阈值。- 在所有四个提示之后,Release版本已清理,无需手动编辑。
- 有两个部分与页面的运行不同。 有人将许可证密钥粘贴到测试提示中,Copilot将其排除在每个文件之外。设置工作流、云代理、其问题和代理密钥需要GitHub Actions或GitHub库,因此它们都未运行。
这些数字背后的助理版本、模型、时间和测试机器与运行记录中的其他指南一起列出。
故障排除
- 如果拉取请求构建但首次读取抛出许可证异常,请在IronOCR中应用许可证密钥
- 如果Linux运行器缺少本地依赖关系,请在Linux上的IronOCR
- 如果提取的值与扫描不同,请提高OCR准确性
- IronOCR Runtimes Folder,如果本机文件在构建输出中缺失
有问题吗?
如果您有任何问题,请联系support@ironsoftware.com
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。