你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

com.azure.ai.formrecognizer

Azure 表单识别器是 Microsoft Azure 提供的基于云的服务,它利用机器学习从各种类型的表单中提取信息。 它旨在自动执行表单识别、数据提取和表单理解的过程。 Azure 表单识别器可以处理结构化表单(例如发票、收据和调查)以及非结构化表单数据,例如合同、协议和财务报告。

该服务使用高级光学字符识别 (OCR) 技术从自定义表单中提取文本和键值对,使组织能够自动执行需要手动执行的数据输入任务。 它可以识别和提取表单中的日期、地址、发票编号、行项和其他相关数据点等信息。

Azure 表单识别器 客户端库允许 Java 开发人员与 Azure 表单识别器 服务进行交互。 它提供一组类和方法,用于抽象 Azure 表单识别器的基础 RESTful API,从而更轻松地将服务集成到 Java 应用程序中。

Azure 表单识别器 客户端库提供以下功能:

  1. 表单识别:它允许您提交表单以提取文本、键值对、表和表单字段等信息。 可以分析结构化和非结构化文档。
  2. 模型管理:它使你能够通过提供标记的训练数据来训练自定义模型。 还可以列出和删除现有模型。
  3. 识别结果:它提供检索和解释分析结果的方法,包括提取的文本和字段值、置信度分数和表单布局信息。
  4. 轮询和回调:它包括轮询服务以检查分析操作的状态或注册回调以在分析完成时接收通知的机制。

入门

Azure 表单识别器库提供和 等FormRecognizerAsyncClientFormRecognizerClient分析客户端,以连接到 表单识别器 Azure 认知服务,以分析表单中的信息并将其提取到结构化数据中。 它还提供训练客户端(如 FormTrainingClientFormTrainingAsyncClient )以从自定义表单生成和管理模型。

注意:此客户端仅支持 com.azure.ai.formrecognizer.FormRecognizerServiceVersion#V2_1 和 更低版本。 建议使用较新的服务版本 DocumentAnalysisClientDocumentModelAdministrationClient

若要使用 API 版本 2022-08-31 及更新,请参阅 迁移指南

服务客户端是开发人员使用 Azure 表单识别器的交互点。 FormRecognizerClient 是同步服务客户端, FormRecognizerAsyncClient 是异步服务客户端。 本文档中显示的示例使用名为 DefaultAzureCredential 的凭据对象进行身份验证,该对象适用于大多数方案,包括本地开发和生产环境。 此外,我们建议使用 托管标识 在生产环境中进行身份验证。 可以在 Azure 标识文档中找到有关不同身份验证方式及其相应凭据类型的详细信息。

示例:使用 DefaultAzureCredential 构造 FormRecognizerClient

下面的代码示例演示如何 FormRecognizerClient使用“DefaultAzureCredentialBuilder”对其进行配置来创建 。

 FormRecognizerClient formRecognizerClient = new FormRecognizerClientBuilder()
     .endpoint("{endpoint}")
     .credential(new DefaultAzureCredentialBuilder().build())
     .buildClient();
 

此外,请参阅下面的代码示例,以用于 AzureKeyCredential 创建客户端。

 FormRecognizerClient formRecognizerClient = new FormRecognizerClientBuilder()
     .credential(new AzureKeyCredential("{key}"))
     .endpoint("{endpoint}")
     .buildClient();
 

让我们看一下下面的分析客户端方案及其各自的用法。



使用预生成模型分析表单

表单识别器模型及其关联的输出,以帮助你选择最佳模型来满足文档方案需求。

可以使用特定于域的模型,也可以训练根据特定业务需求和用例定制的自定义模型。

示例:使用 URL 源识别收据中的数据

下面的代码示例演示如何使用光学字符识别 (OCR) 检测和提取收据中的数据。

 String receiptUrl = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/main/sdk/formrecognizer"
         + "/azure-ai-formrecognizer/src/samples/resources/sample-forms/receipts/contoso-allinone.jpg";
 SyncPoller<FormRecognizerOperationResult, List<RecognizedForm>> syncPoller =
     formRecognizerClient.beginRecognizeReceiptsFromUrl(receiptUrl);
 List<RecognizedForm> receiptPageResults = syncPoller.getFinalResult();

 for (int i = 0; i < receiptPageResults.size(); i++) {
     RecognizedForm recognizedForm = receiptPageResults.get(i);
     Map<String, FormField> recognizedFields = recognizedForm.getFields();
     System.out.printf("----------- Recognizing receipt info for page %d -----------%n", i);
     FormField merchantNameField = recognizedFields.get("MerchantName");
     if (merchantNameField != null) {
         if (FieldValueType.STRING == merchantNameField.getValue().getValueType()) {
             String merchantName = merchantNameField.getValue().asString();
             System.out.printf("Merchant Name: %s, confidence: %.2f%n",
                 merchantName, merchantNameField.getConfidence());
         }
     }

     FormField merchantPhoneNumberField = recognizedFields.get("MerchantPhoneNumber");
     if (merchantPhoneNumberField != null) {
         if (FieldValueType.PHONE_NUMBER == merchantPhoneNumberField.getValue().getValueType()) {
             String merchantAddress = merchantPhoneNumberField.getValue().asPhoneNumber();
             System.out.printf("Merchant Phone number: %s, confidence: %.2f%n",
                 merchantAddress, merchantPhoneNumberField.getConfidence());
         }
     }

     FormField transactionDateField = recognizedFields.get("TransactionDate");
     if (transactionDateField != null) {
         if (FieldValueType.DATE == transactionDateField.getValue().getValueType()) {
             LocalDate transactionDate = transactionDateField.getValue().asDate();
             System.out.printf("Transaction Date: %s, confidence: %.2f%n",
                 transactionDate, transactionDateField.getConfidence());
         }
     }

     FormField receiptItemsField = recognizedFields.get("Items");
     if (receiptItemsField != null) {
         System.out.printf("Receipt Items: %n");
         if (FieldValueType.LIST == receiptItemsField.getValue().getValueType()) {
             List<FormField> receiptItems = receiptItemsField.getValue().asList();
             receiptItems.stream()
                 .filter(receiptItem -> FieldValueType.MAP == receiptItem.getValue().getValueType())
                 .map(formField -> formField.getValue().asMap())
                 .forEach(formFieldMap -> formFieldMap.forEach((key, formField) -> {
                     if ("Quantity".equals(key)) {
                         if (FieldValueType.FLOAT == formField.getValue().getValueType()) {
                             Float quantity = formField.getValue().asFloat();
                             System.out.printf("Quantity: %f, confidence: %.2f%n",
                                 quantity, formField.getConfidence());
                         }
                     }
                 }));
         }
     }
 }
 

还可以使用 beginRecognizeReceipts 方法通过预生成模型从本地收据中提取数据。

有关应使用哪个受支持模型的详细信息,请参阅 模型使用文档



使用带或不带标签训练的模型分析自定义窗体。

使用带或不带标签训练的模型分析自定义窗体。 自定义模型是使用你自己的数据训练的,因此它们是针对文档定制的。

有关详细信息,请参阅 使用标签训练模型

示例:使用使用标签训练的模型分析自定义表单

此示例演示如何使用使用自己的表单类型训练的模型,从自定义表单中识别表单域和其他内容。

 String trainingFilesUrl = "{SAS_URL_of_your_container_in_blob_storage}";
 boolean useTrainingLabels = true;

 SyncPoller<FormRecognizerOperationResult, CustomFormModel> trainingPoller =
     formTrainingClient.beginTraining(trainingFilesUrl,
         useTrainingLabels,
         new TrainingOptions()
             .setModelName("my model trained with labels"),
         Context.NONE);

 CustomFormModel customFormModel = trainingPoller.getFinalResult();

 // Model Info
 System.out.printf("Model Id: %s%n", customFormModel.getModelId());

 String customFormUrl = "customFormUrl";
 String modelId = customFormModel.getModelId();
 SyncPoller<FormRecognizerOperationResult, List<RecognizedForm>> recognizeFormPoller =
     formRecognizerClient.beginRecognizeCustomFormsFromUrl(modelId, customFormUrl);

 List<RecognizedForm> recognizedForms = recognizeFormPoller.getFinalResult();

 for (int i = 0; i < recognizedForms.size(); i++) {
     RecognizedForm form = recognizedForms.get(i);
     System.out.printf("----------- Recognized custom form info for page %d -----------%n", i);
     System.out.printf("Form type: %s%n", form.getFormType());
     System.out.printf("Form type confidence: %.2f%n", form.getFormTypeConfidence());
     form.getFields().forEach((label, formField) ->
         System.out.printf("Field %s has value %s with confidence score of %f.%n", label,
             formField.getValueData().getText(),
             formField.getConfidence())
     );
 }
 

有关从具有已知字段的自定义表单中提取信息的建议方法,请参阅 强键入已识别的表单

FormRecognizerAsyncClient

此类提供一个异步客户端,用于连接到 表单识别器 Azure 认知服务。

FormRecognizerClient

此类提供一个同步客户端,用于连接到 表单识别器 Azure 认知服务。

FormRecognizerClientBuilder

此类提供 Fluent Builder API 来帮助实例化 FormRecognizerClientFormRecognizerAsyncClient,调用 buildClient() buildClient} 并 buildAsyncClient() 分别构造所需客户端的实例。

枚举

FormRecognizerServiceVersion

此客户端库支持的 Azure 表单识别器版本。