Azure Logic AppからCustom Visionの予測APIを実行する

こんにちは、MLBお兄さんこと松村です。
本来なら MLB はもうすぐスプリングトレーニングが始まる時期ですが、いまはロックアウト中のためスプリングトレーニングどころかストーブリーグすら動いていません。涙


Azure Cognitive Services の視覚領域のサービスである Custom Vision を使う機会が最近ありました。

Custom Vision は画像分析(識別)を行うためのサービスですが、あくまでできるのは識別のためのモデルを作ることです。
そのモデルを使うアプリケーション側のロジックも必要になります。

今回は Azure Logic AppsCustom Vision コネクター を用いて、トレーニング済みの画像モデルの利用を試しました。

コネクターの作成

f:id:tech-tsubaki:20220213104701p:plain

f:id:tech-tsubaki:20220213104719p:plain

Logic Apps の Custom Vision コネクターには、予測キーと予測 URL が必要となります。
予測キーと予測 URL は Custom Vision のトレーニング済みの予測モデルの発行後に取得することができるものが該当します。

f:id:tech-tsubaki:20220213104958p:plain

予測モデルの発行については、こちらの手順を参考にしましょう。

docs.microsoft.com

分類処理の設定

作成した Custom Vision コネクター を使用して、画像 URL を指定した分類処理を定義してみます。
Custom Vision コネクターを使用するには 3つのパラメーターが必要です。

  1. プロジェクト ID
  2. Custom Vision ポータルから取得
  3. パブリッシュされた名前
  4. 発行したトレーニング済みのイテレーション名
  5. 画像の URL

f:id:tech-tsubaki:20220213105332p:plain

実行する

Web API として作成した Logic Apps を実行してみます。

$ curl --location --request POST $LOGIC_APPS_URL   --header 'Content-Type: application/json'   --data-raw '{"url":"https://github.com/dotnet-foundation/swag/raw/main/dotnet-bot/dotnet-bot-emoticon-512.png"}' | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   484  100   385  100    99    193     49  0:00:02  0:00:01  0:00:01   243
{
  "id": "282786bc-ed1a-4dfe-b9e3-1fb8fefa0bde",
  "project": "71ef6b0b-5388-4f6a-a630-7d920ed8fbaf",
  "iteration": "36831b43-cf7a-4599-be9b-52929cfd1544",
  "created": "2022-02-12T14:52:49.156Z",
  "predictions": [
    {
      "probability": 0.84127975,
      "tagId": "206c3a0c-67bf-43c3-8ca4-ed9b2095f491",
      "tagName": "dotnet"
    },
    {
      "probability": 0.15872024,
      "tagId": "73063a09-9f19-42b6-817d-3323d33b01cb",
      "tagName": "stadium"
    }
  ]
}

予測 API を実行することができ、結果を得ることができました。

コネクター作成時の注意点

Custom Vision コネクターを作成する際、「サイトの URL」は必須項目ではありません。
そのため URL を指定しない状態で Custom Vision の予測 API を実行すると認証エラーとなってしまうため、注意が必要です。

{
  "error": {
    "code": "401",
    "message": "Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource."
  }
}