LoginSignup
13
10

More than 3 years have passed since last update.

背景抜き remove.bg のAPIとPower Platform連携

Last updated at Posted at 2021-02-23

はじめに

 背景画像を一瞬で綺麗に抜いてくれる remove.bgで提供されるAPIに、Power Autotameで接続、Power Appsでアプリ化することをやってみました。また、背景を抜いた後のデータをOne Drive for Businessに保存し、Power BIで表示しました。

動作イメージ

 こちらの動画をご覧ください。 remove.bg APIとPower Apps、Power Automate連携 - YouTube

IMG_6556.PNG

本APIに関するページ

 こちらが、APIのドキュメントやサンプル等あります。いろんな言語の説明等ありますが、Power AutomateやPower Appsはありませんでしたw 利用料金等はremove.bgの説明をご参照願います。
 取得した画像のサイズや、人物等の判定の仕方等細かな設定ができます。

API Key

 会員ログイン後、同APIのページからAPI Keyを控えておきます。
image.png
image.png

Power Appsの要点

image.png

画像のURLを入れてボタンを押すと、Power Automateを呼び出し、API呼び出し取得した画像データをBase64で取得。それを表示しています。
ビフォーが左、アフターが右です。

[抜く]ボタンの設定は以下です。Power AutomateにURLを渡し、戻り値を変数に入れます。
image.png

UpdateContext({locResponse:画像抜き.Run(TextInput1.Text)})

左の画像表示は以下です。
image.png

Concatenate("data:image/png;base64,", locResponse.newimage)

Power Automateの呼び出しについて、
- 引数を変更したとき
- Power Automate側でOneDrive保存を追加したとき
など呼び出しのエラーが出やすくなり、解決になやみました。結果は再接続等でうまくいきました。

image.png

Power Automateの要点

フローの全体像はこのようになっています。

image.png

HTTPコネクターは以下です。
image.png

Methodは「POST」(Putと間違えて設定し時間を要した...)

URIはこちら、

https://api.remove.bg/v1.0/removebg

ヘッダーは X-API-Keyに各自のAPIキー、accept:にimage/*、Content-Typeをapplication/jsonにします。

本文(Body)のJSONのサンプルは以下です。必要に応じて書き換えてください。

{
  "image_url": "@{variables('orgURL')}",
  "image_file_b64": "",
  "size": "full",
  "type": "auto",
  "type_level": 1,
  "format": "auto",
  "crop": true
}

取得したデータからbase64の部分であるbody('HTTP')?['$content']を変数に入れます。
image.png
それをPower Appsに応答で返します。
image.png

ファイルの保管は、One Driveにファイル名を現在時刻にして、png形式で保管しています。
image.png

base64のテキストデータをBinaryに変換します。

image.png

Power BIの要点

 One Driveにたまった画像は、同じM365内の組織で閲覧する、という前提であればPower BIで画像付きで表示できます。
image.png

接続はSharePointフォルダーから行います。
image.png

image.png

この部分は、OneDriveのURLから「_layouts/」の前までの部分ですね。
image.png

Power Queryのフィルターで画像を保存しているサブフォルダー名を含む形でフィルターします。

image.png

画像のURLの列を作成します。

image.png

画像URLはデータカテゴリで画像のURLを指定します。

image.png

Power BI Desktopでは、画像部分が表示できませんが、Power BI Serviceに発行した後、表示できるようになります。

image.png

おわりに

 APIを呼び出すアプリは初めてでしたが、やってみると面白いですね!

13
10
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
13
10