次の方法で共有


com.azure.ai.formrecognizer

Azure Form Recognizer は、機械学習を利用してさまざまな種類のフォームから情報を抽出する、Microsoft Azure によって提供されるクラウドベースのサービスです。 フォーム認識、データ抽出、およびフォーム理解のプロセスを自動化するように設計されています。 Azure Form Recognizerでは、請求書、領収書、アンケートなどの構造化されたフォームや、契約、契約、財務レポートなどの非構造化フォーム データを処理できます。

このサービスでは、高度な光学式文字認識 (OCR) テクノロジを使用してカスタム フォームからテキストとキーと値のペアを抽出し、組織は手動で作業する必要があるデータ入力タスクを自動化できます。 日付、住所、請求書番号、明細、その他の関連データ ポイントなどの情報をフォームから認識して抽出できます。

Azure Form Recognizer クライアント ライブラリを使用すると、Java 開発者は Azure Form Recognizer サービスと対話できます。 Azure Form Recognizerの基になる RESTful API を抽象化するクラスとメソッドのセットが用意されているため、サービスを Java アプリケーションに簡単に統合できます。

Azure Form Recognizer クライアント ライブラリには、次の機能があります。

  1. フォーム認識: フォームを送信して、テキスト、キーと値のペア、テーブル、フォーム フィールドなどの情報を抽出できます。 構造化ドキュメントと非構造化ドキュメントの両方を分析できます。
  2. モデル管理: ラベル付きのトレーニング データを提供することで、カスタム モデルをトレーニングできます。 既存のモデルを一覧表示および削除することもできます。
  3. 結果の認識: 抽出されたテキストとフィールドの値、信頼度スコア、フォーム レイアウト情報など、分析結果を取得して解釈するメソッドを提供します。
  4. ポーリングとコールバック: 分析操作の状態をチェックするためのサービスのポーリング、または分析が完了したときに通知を受信するためのコールバックの登録を行うメカニズムが含まれています。

作業の開始

Azure Form Recognizer ライブラリは、 などのFormRecognizerAsyncClient分析クライアントを提供し、FormRecognizerClientForm Recognizer Azure Cognitive Service に接続して、フォームから情報を分析し、構造化データに抽出します。 また、カスタム フォームからモデルを構築および管理するための や FormTrainingAsyncClient などのFormTrainingClientトレーニング クライアントも提供します。

メモ:このクライアントは、 以下のみをサポートします com.azure.ai.formrecognizer.FormRecognizerServiceVersion#V2_1 。 新しいサービス バージョン DocumentAnalysisClientDocumentModelAdministrationClientを使用することをお勧めします。

API バージョン 2022-08-31 以降を使用するには、 移行ガイド を参照してください。

サービス クライアントは、開発者が Azure Form Recognizerを使用するための対話のポイントです。 FormRecognizerClient は同期サービス クライアントであり、 FormRecognizerAsyncClient 非同期サービス クライアントです。 このドキュメントに示す例では、認証に DefaultAzureCredential という名前の資格情報オブジェクトを使用します。これは、ローカルの開発環境や運用環境を含むほとんどのシナリオに適しています。 さらに、運用環境での認証に マネージド ID を 使用することをお勧めします。 認証のさまざまな方法とそれに対応する資格情報の種類の詳細については、 Azure ID のドキュメントを参照してください

サンプル: DefaultAzureCredential を使用して FormRecognizerClient を構築する

次のコード サンプルは、'DefaultAzureCredentialBuilder' を使用して を構成する の作成 FormRecognizerClientを示しています。

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

さらに、クライアントの作成に使用する次のコード サンプルを参照 AzureKeyCredential してください。

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

分析クライアントのシナリオとそれぞれの使用方法を次に示します。



事前構築済みモデルを使用したフォームの分析

モデルと関連する出力をForm Recognizerして、ドキュメント シナリオのニーズに対応するための最適なモデルを選択するのに役立ちます。

ドメイン固有のモデルを使用したり、特定のビジネス ニーズやユース ケースに合わせてカスタマイズされたカスタム モデルをトレーニングしたりできます。

サンプル: 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

このクラスは、Form Recognizer Azure Cognitive Service に接続するための非同期クライアントを提供します。

FormRecognizerClient

このクラスは、Form Recognizer Azure Cognitive Service に接続するための同期クライアントを提供します。

FormRecognizerClientBuilder

このクラスは、 と のFormRecognizerClientインスタンス化に役立つ fluent builder API を提供しFormRecognizerAsyncClient、それぞれ buildClient} を呼び出buildClient()し、buildAsyncClient()目的のクライアントのインスタンスを構築します。

列挙型

FormRecognizerServiceVersion

Azure のバージョンForm Recognizerこのクライアント ライブラリでサポートされています。