8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

UiPath Document UnderstandingをPythonなどの外部から利用する方法

Posted at

今回はUiPathのDocument Understandingを外部プログラムから使用する方法を解説します。
例えば、UiPathが提供している日本語のAI-OCR処理をPythonから使いたい場合などです。

Pythonなどの外部プログラムからDocument Understandingのデジタル化などの処理を実行したい場合には、次の3つのステップが必要です。

・外部アプリケーションの登録
・Document Understandingプロジェクトの作成
・呼び出し

Studioのアクティビティから呼び出す場合には認証などが自動的に行われますので、エンドポイント等を指定するだけで簡単に実行可能です。

外部アプリケーションの登録

Automation Cloudの管理画面にアクセスします。
外部アプリケーション登録を行うため、External applicationページをクリックします。
image.png

新たにアプリケーションを登録します。
アプリケーションを追加をクリックします。
image.png

シークレットIDを新しく生成します。
アプリケーションIDとシークレットIDをコピーします。
シークレットIDは生成した時のみ表示されますので、大事に保管しておいてください。
image.png

スコープを追加から、権限を追加します。
image.png

Documetn Understanding関連の権限を追加します。
image.png

Document Understandingプロジェクトの作成

Automation CloudのDocument Understandingページに移動します。
プロジェクトを新規に作成します。
image.png

Document Understanding機能を管理画面から有効化しておく必要があります。
有効化には管理者権限が必要です。
新規プロジェクト作成にも権限が必要です。

プロジェクト設定でOCRメソッドを選びます。
日本語の場合は「UiPath Chinese Japanese Korean OCR」を選択します。
image.png

アクセス権を管理より、外部アプリケーションにロールを割り当てます。
image.png

image.png

呼び出し

まずSwaggerから呼び出せることを確認しましょう。
Swaggerを使用することによって、プログラムを書かずにAPIをアクセスすることができます。
Document Understanding右上のREST APIからFrameworkを選択します。
image.png

認証します。
Authorizeをクリックします。
image.png

外部アプリケーションのIDを指定します。
image.png

正常に認証されると、このような画面が出てきます。
Staging環境のため、Token URLはみなさんと違う可能性があります。
image.png

デジタル化を行います。
DigitalizationのstartのTry it outをクリックします。
image.png

プロジェクトIDはURLから取得します。
https://staging.uipath.com/xxxx/xxxxxxxx/du_/projects/342646c9-ae59-ee11-9937-000d3a2e596b/」の場合には、342646c9-ae59-ee11-9937-000d3a2e596bがプロジェクトIDになります。

設定後にExecuteを押して実行します。
image.png

正しく実行できていれば、レスポンスにDocument IDが返ってきます。
これは後で使用するため、コピーしておきます。
image.png

結果の取得を行います。
Digitalizationのresultを使用します。
image.png

プロジェクトIDとドキュメントIDを指定して実行します。
image.png

後はこの処理をPythonなどの呼び出したいアプリケーションで実装するだけです。
(以下はコードの一部抜粋のため、このままでは動きません。)

sample.py
        # 認証
        response = requests.post('https://cloud.uipath.com/identity_/connect/token', data = {
        # response = requests.post('https://staging.uipath.com/identity/connect/token', data={
            'client_id': UIPATH_APP_ID,
            'client_secret': UIPATH_APP_SECRET,
            'grant_type': 'client_credentials',
            'scope': "Du.Digitization.Api Du.Classification.Api Du.Extraction.Api Du.Validation.Api"
        })

        # デジタライズ
        response = requests.post(UIPATH_DU_API_ENDPOINT + '/projects/' + PROJECT_ID + '/digitization/start?api-version=1', files=[
            ('File', (path, open(path, 'rb'), 'application/octet-stream'))
        ], headers={
            'Authorization': 'Bearer ' + token
        })

        # 結果の取得
        digitization_response = requests.get(UIPATH_DU_API_ENDPOINT + '/projects/' + PROJECT_ID + '/digitization/result/' + doc_id + '?api-version=1', headers={
                    'Authorization': 'Bearer ' + token
        })

8
2
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
8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?