![見出し画像](https://assets.st-note.com/production/uploads/images/155102730/rectangle_large_type_2_80dcb11c1e8f59e261a01187299d2f75.png?width=1200)
Azure AI Document Intelligenceを試す
はじめに
Azure AI Document Intelligence はサイトの説明では「機械学習モデルを使用してアプリケーションとワークフローでのデータ処理を自動化する」とあります。簡単に言ってしまえばOCRのAPIを提供するものです。
このDocument IntelligenceでREST APIを用いてマークダウン形式の結果の出力方法を確認していきます。
Document Intelligenceのデプロイ
Document IntelligenceのAPIを使用するためには、AzuleのサインインとDocument Intelligのデプロイが必要です。
両方ともすでにいろいろなサイトで紹介されていますので、ここでは割愛しますが、調べなくても簡単にデプロイできます。
なお下記サイトにあるとおり、インスタンスのデプロイ自体は無料、使用に関しても1 か月あたり500 ページまで無料なので、お気軽に試すことができます。
Azure AI Document Intelligence の価格
Document Intelligenceを実行する
デプロイが終わるとキーとエンドポイントが生成されますので、メモに控えておきます。
次にOCR対象のファイルを準備します。
ここではAzuleが以下サンプルを用意してくれているので、それを使います。
ドキュメントを分析して結果を取得する
その中で今回はマークダウン形式の結果を確認したいため、ここでは事前構築済みレイアウトの予約確認のサンプルを選択します。
さらにAzuleがcurl文まで用意してくれているので、まずは一番シンプルな設定で実行します。
curl -i -X POST "%DI_ENDPOINT%/documentintelligence/documentModels/{modelId}:analyze?api-version=2024-02-29-preview" -H "Content-Type: application/json" -H "Ocp-Apim-Subscription-Key: %DI_KEY%" --data-ascii "{'urlSource': '<document-url>'}"
curl文の中で実行側で書き換える箇所が4つあります。
%DI_ENDPOINT%:メモに控えたエンドポイントを設定
{modelId}:マークダウンを出力するので固定で"prebuilt-layout"を設定
%DI_KEY%:メモに控えたキーを設定
<document-url>:予約確認のサンプルのURLを設定
これらを書き換えて実行した結果が以下になります。
重要なのがoperation-locationで、中に含まれるxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxの部分がresltIDとなり、次の結果取得に使用します。なおresltIDを抜き出す必要は無く、operation-locationのURLをそのまま実行してOCRの結果が取得できます。
HTTP/2 202
content-length: 0
operation-location: https://xxxxxxxxx.cognitiveservices.azure.com/documentintelligence/documentModels/prebuilt-layout/analyzeResults/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx?api-version=2024-02-29-preview
x-envoy-upstream-service-time: 239
apim-request-id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
strict-transport-security: max-age=31536000; includeSubDomains; preload
x-content-type-options: nosniff
x-ms-region: East US
date: Sat, 21 Sep 2024 06:38:48 GMT
Document Intelligenceの結果を取得する
operation-locationのURLを使った、次のcurl文で結果を取得します。
当然ですが、ここでも%DI_KEY%の設定が必要です。
curl -i -X GET -H "Ocp-Apim-Subscription-Key: %DI_KEY%" https://xxxxxxxxx.cognitiveservices.azure.com/documentintelligence/documentModels/prebuilt-layout/analyzeResults/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx?api-version=2024-02-29-preview
得られた結果が以下となります。
analyzeResult.contentが読み取った結果で、contentFormatがtextなのでマークダウンでないそのままの文字列となります。
これだと表のデータが扱いづらいことになるため、マークダウンでの取得が便利です。
{
"status": "succeeded",
"createdDateTime": "2024-09-21T06:38:48Z",
"lastUpdatedDateTime": "2024-09-21T06:38:50Z",
"analyzeResult": {
"apiVersion": "2024-02-29-preview",
"modelId": "prebuilt-layout",
"stringIndexType": "textElements",
"content": "Liberty's Delightful Sinful Bakery & Café 765 Halifax St. Clearwater, FL 33756\nOur reference: 3456623\nYour reference: 2334566\nReceived from: 8556 Indian Summer Ave. New Haven, CT 06511\nName Summer River Tel. 34456632 E-mail email6@libertydelightfulsinful.com\nLiberty booking contact 40 River Street East Northport, NY 11731\nBooking Confirmation - ORIGINAL\nOur reference: 3456623 Your reference: 2334566 EURH234\nBooking date:\nBL/NO: Summary: 45×72\nContract No: 05-Dec-2018 334566 :unselected: Opt. A :unselected: Opt. B\nExport: Common\nExport empty pick up depot(s) 97 Morris Lane Sterling Heights, MI 48310\nFrom\nTo\nBy\nETD\nETA\n118 Queen Street Hoboken, NJ 07030\n52 West Trenton St. Harleysville, PA 19438\ncause science slow\n09-Dec-2018 19:00\n09-Dec-2020 11:00\n9 Ketch Harbour Ave. Vincentown, NJ\n75 Fawn Street Peabody, MA 01960\ntone late spoken\n12-Dec-2018 10:00\n19-Dec-2020\nDeadline\nLocation\nDate/Time (local)\nRequired action\nTable\nHarleysville (PA)\n08-Dec-2019\nFlight\nHarleysville (PA)\n08-Dec-2019 13:00\nTwo more days and all his problems would be solved.\nRound\nHarleysville (PA)\n09-Dec-2019\nAccent\nHarleysville (PA)\n10-Dec-2019\nMonkey\nHarleysville (PA)\n11-Dec-2019\nRoute\nHarleysville (PA)\n11-Dec-2019\nPeanuts don't grow on trees.",
"pages": [
省略
],
"styles": [],
"contentFormat": "text",
"sections": [
省略
],
"figures": [
省略
]
}
}
Document Intelligenceの結果をマークダウンにする
方法は難しくは無く、URLパラメータとしてoutputContentFormat=markdownを付与すればOKです。
またオプションもありますので、下記を参照して追加できます。
Document Models - Analyze Document
curl -i -X POST "%DI_ENDPOINT%/documentintelligence/documentModels/{modelId}:analyze?api-version=2024-02-29-preview&outputContentFormat=markdown" -H "Content-Type: application/json" -H "Ocp-Apim-Subscription-Key: %DI_KEY%" --data-ascii "{'urlSource': '<document-url>'}"
上記を実行して、さらに結果取得まで行なったものが以下となります。
contentFormatがmarkdownとなり、contentの内容がマークダウン形式になっています。
{
"status": "succeeded",
"createdDateTime": "2024-09-21T06:58:10Z",
"lastUpdatedDateTime": "2024-09-21T06:58:12Z",
"analyzeResult": {
"apiVersion": "2024-02-29-preview",
"modelId": "prebuilt-layout",
"stringIndexType": "textElements",
"content": "Liberty's Delightful Sinful Bakery & Café 765 Halifax St. Clearwater, FL 33756\n\nOur reference: 3456623\n\nYour reference: 2334566\n\n<figure>\n\n![](figures/0)\n\n</figure>\n\n\nReceived from: 8556 Indian Summer Ave. New Haven, CT 06511\n\nName Summer River Tel. 34456632 E-mail email6@libertydelightfulsinful.com\n\nLiberty booking contact 40 River Street East Northport, NY 11731\n\n\n# Booking Confirmation - ORIGINAL\n\n<figure>\n\n![](figures/1)\n\n<!-- FigureContent=\"Our reference: 3456623 Your reference: 2334566 EURH234 Booking date: BL/NO: Summary: 45×72 Contract No: 05-Dec-2018 334566 :unselected: Opt. A :unselected: Opt. B Export: Common\" -->\n\n</figure>\n\n\nExport empty pick up depot(s) 97 Morris Lane Sterling Heights, MI 48310\n\n| From | To | By | ETD | ETA |\n| - | - | - | - | - |\n| 118 Queen Street Hoboken, NJ 07030 | 52 West Trenton St. Harleysville, PA 19438 | cause science slow | 09-Dec-2018 19:00 | 09-Dec-2020 11:00 |\n| 9 Ketch Harbour Ave. Vincentown, NJ | 75 Fawn Street Peabody, MA 01960 | tone late spoken | 12-Dec-2018 10:00 | 19-Dec-2020 |\n\n| Deadline | Location | Date/Time (local) | Required action |\n| - | - | - | - |\n| Table | Harleysville (PA) | 08-Dec-2019 | |\n| Flight | Harleysville (PA) | 08-Dec-2019 13:00 | Two more days and all his problems would be solved. |\n| Round | Harleysville (PA) | 09-Dec-2019 | |\n| Accent | Harleysville (PA) | 10-Dec-2019 | |\n| Monkey | Harleysville (PA) | 11-Dec-2019 | |\n| Route | Harleysville (PA) | 11-Dec-2019 | Peanuts don't grow on trees. |\n",
"pages": [
省略
],
"contentFormat": "markdown",
"sections": [
省略
],
"figures": [
省略
]
}
}
上だと分かりづらいので、content部分だけ抜き出して整形します。
Liberty's Delightful Sinful Bakery & Café 765 Halifax St. Clearwater, FL 33756
Our reference: 3456623
Your reference: 2334566
<figure>
![](figures/0)
</figure>
Received from: 8556 Indian Summer Ave. New Haven, CT 06511
Name Summer River Tel. 34456632 E-mail email6@libertydelightfulsinful.com
Liberty booking contact 40 River Street East Northport, NY 11731
# Booking Confirmation - ORIGINAL
<figure>
![](figures/1)
<!-- FigureContent=\"Our reference: 3456623 Your reference: 2334566 EURH234 Booking date: BL/NO: Summary: 45×72 Contract No: 05-Dec-2018 334566 :unselected: Opt. A :unselected: Opt. B Export: Common\" -->
</figure>
Export empty pick up depot(s) 97 Morris Lane Sterling Heights, MI 48310
| From | To | By | ETD | ETA |
| - | - | - | - | - |
| 118 Queen Street Hoboken, NJ 07030 | 52 West Trenton St. Harleysville, PA 19438 | cause science slow | 09-Dec-2018 19:00 | 09-Dec-2020 11:00 |
| 9 Ketch Harbour Ave. Vincentown, NJ | 75 Fawn Street Peabody, MA 01960 | tone late spoken | 12-Dec-2018 10:00 | 19-Dec-2020 |
| Deadline | Location | Date/Time (local) | Required action |
| - | - | - | - |
| Table | Harleysville (PA) | 08-Dec-2019 | |
| Flight | Harleysville (PA) | 08-Dec-2019 13:00 | Two more days and all his problems would be solved. |
| Round | Harleysville (PA) | 09-Dec-2019 | |
| Accent | Harleysville (PA) | 10-Dec-2019 | |
| Monkey | Harleysville (PA) | 11-Dec-2019 | |
| Route | Harleysville (PA) | 11-Dec-2019 | Peanuts don't grow on trees. |
表部分がマークダウン形式になっていることが分かります。
さらにこれをHTML形式で表示したのが以下となります。
![](https://assets.st-note.com/img/1726902346-LIGcr3nTvDElAmgbKP9wfzkp.png?width=1200)
まとめ
Document Intelligenceを使って簡単にOCRを行い、マークダウンが取得できました。
Azuleを今回初めて触ったのですが、実行してみると非常に簡単かつ便利で、OCRも精度が良さそうに感じました。
ただ実行できてしまえば大したことはないものの、modelIdやoutputContentFormatの設定はcurlとは別のページを確認したりと少し読みづらいものになっていますので、この記事が今後実行される方の参考になればと思います。