LoginSignup
2
0

More than 1 year has passed since last update.

FormRecognizerでの請求書解析(javascript版)

Posted at

概要

前回はFormRecognizerStudioを使って請求書の解析を行いましたが、今回はjavascriptからFormRecognizerを呼び出して請求書の解析を行います。

FoormRecognizerの呼出し方

FormRecognizerをコードから呼び出すにはRESTとSDKの2つの方法が用意されています。
RESTを使用する場合、画像URLをPOSTし解析が終わるのを待ってから結果をGETで取得する必要があり手間がかかります。
一方SDKを使用すればそういった手間を省けますので、特段理由がない限りSDKを使用する方が便利かと思います。
なお本稿執筆時(2023年3月)時点では下記のSDKが提供されているようです。

  • C#
  • Java
  • JavaScript
  • Python
    本稿ではJavaScriptを用いて実装を行います。

実装

まずはドキュメントのサンプルコードを参考にFormRecognizerを呼び出して結果を出力してみます。
なお下記コード内のタグは書き換える必要があります。

タグ 説明
<your-key> FormRecognizer
<resource-name> FormRecognizerのリソースグループ
<your-url> 解析したい画像を保存したBlobのurl
index.js
const { AzureKeyCredential, DocumentAnalysisClient } = require("@azure/ai-form-recognizer");
    const key = "<your-key>";
    const endpoint = "https://<resource-name>.cognitiveservices.azure.com/";
    const invoiceUrl = "<your-url>"

async function main() {
    const client = new DocumentAnalysisClient(endpoint, new AzureKeyCredential(key));
    const poller = await client.beginAnalyzeDocumentFromUrl("prebuilt-invoice", invoiceUrl);
    const {
        documents
    } = await poller.pollUntilDone();
    console.log(JSON.stringify(documents);

main().catch((error) => {
    console.error("An error occurred:", error);
    process.exit(1);
});

結果はそれなりのサイズになりますので詳細は省略しますが、
JSONのfieldsタグ以下の部分を確認していただくと請求金額などを確認することができます。

テスト

作成したコードを使って、前回使用したものと同じ自作請求書を解析させてみます。

主要な数字のみでの確認ではありますが、正しい結果を取得できているようです。
sample-01.png

結果

フィールド名
InvoiceTotal 3500000
Description コンサルタント料
Quantity 1
UnitPrice 3500000
Amount 3500000
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0