2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Power AutomateでSharePointに置いてある手書き書類をGPT-4oに読み込ませてOCRしてみる

Last updated at Posted at 2024-06-05

GPT-4oの読み取り精度が高く、OCRに結構使えるよねという話題があります。

Power Automateでもファイルを読み込みOpenAIのAPIに投げることでOCR解析してくれます。

全体のフロー

手順紹介

0. トリガー: 手動トリガー

試すのに便利

1. アクション: シェアポイントからファイルコンテンツを取得

シェアポイントのアクションで、ファイルコンテンツの取得を選択し、読み取る予定の写真ファイルを選択します。ちなみにOpenAIのAPIでGPT-4oを使う場合、現状画像ファイルにする必要があり、pngjpegなどを用意するか変換して使います。

某書類.pngなどの画像ファイルを読み込みましょう。

2. アクション: Base64変換し、変数に格納

ファイルコンテンツの中から取得した画像をBase64変換し、文字列変数に格納します。

  • Name: 画像 (なんでも良いですが便宜上、"画像"という名前に)
  • Type: String
  • Value: base64(body('ファイル_コンテンツの取得')) らしい

スクリーンショット 2024-06-05 19.56.39.png

設定するとこんな感じです。

スクリーンショット 2024-06-05 19.58.50.png

3. アクション: (JSONの)作成

以下を作成します。

OpenAIのAPIへのリクエストになります。

JSONオブジェクトはこちらの記事を参考にしています。

{
  "model": "gpt-4o",
  "messages": [
    {
      "role": "system",
      "content": "You are an Optical Character Recognition (OCR) machine. You will extract all the characters from the image file in the URL provided by the user, and you will only privide the extracted text in your response. As an OCR machine, You can only respond with the extracted text. Be sure to output in JSON.  \n  ## 出力形式  \n - body:{String}"
    },
    {
      "role": "user",
      "content": [
        {
          "type": "image_url",
          "image_url": {
            "url": "data:image/png;base64,@{variables('画像')}",
            "detail": "high"
          }
        }
      ]
    }
  ],
  "temperature": 1,
  "response_format": {
    "type": "json_object"
  }
}

少し解説

"response_format": { "type": "json_object" }ここの部分でJSONモード指定をしてレスポンスのデータを扱いやすくしています。ここが現状のPower AutomateのOpenAIモジュールだと出来ないんですよね。。

"url": "data:image/png;base64,@{variables('画像')}",この動的コンテンツの設定が重要です。このやり方でBase64指定のときはdata:image/png;base64,の部分は自分で書かないといけません。

またsystemロールに指定している以下の英文はプロンプトになります。こちらの記事のものをベースに使わせてもらっています。

You are an Optical Character Recognition (OCR) machine. You will extract all the characters from the image file in the URL provided by the user, and you will only privide the extracted text in your response. As an OCR machine, You can only respond with the extracted text. Be sure to output in JSON. \n ## 出力形式 \n - body:{String}

この部分はGPTへの命令や制約条件なのでここは各自使いたいGPTをイメージしてきましょう。

4. アクション: HTTP

先ほど作成したJSONデータをOpenAIのAPIに送ります。

HTTPモジュールを追加して情報を入れます。ここはAPIキーを入れて送るだけでシンプル。コピペがめんどい。

  • url: https://api.openai.com/v1/chat/completions
  • method: post
  • headers:
    • Content-Type/ application/json
    • Authorization / Bearer APIキー
  • body: 出力

5. アクション: JSONの解析

APIから帰ってきた結果を処理します。JSONの解析モジュールを利用します。

結果 & まとめ

テスト実行すると一番最後のJSONの解析の出力に手書き書類から抜き出されたテキストが抽出されます。

どんな書類だったかは見せられてないですが、精度がかなり良かったです。

GPT-4oこんな感じでOCRにも利用できますね。もっと活用していきたいところです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?