4
5

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 5 years have passed since last update.

PowerShellからGoogle Cloud Vision APIを使う

Last updated at Posted at 2016-06-14

はじめに

  • PowerShellからGoogle Cloud Vision APIを使ってみました。
  • Windowsユーザならすぐにこのスクリプトを試すことができます。
  • Cloud Vision APIを利用するための手順は他の方が投稿している記事を参考にしてください。

意外と簡単に最先端の技術を体験することができますよ!

PowerShell スクリプト

# 引数に解析する画像のパス名と解析typeを指定します
param (
	[parameter (mandatory)]
	[string]$path,
	[string]$request_type = "LABEL_DETECTION"
)
# 認証用APIキー(取得したAPIキーを設定してください)
$value = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

$EndPoint = "https://vision.googleapis.com/v1/images:annotate?key=" + $value

# 画像ファイルをBase64文字列に変換する
$b64str = [convert]::ToBase64String(( get-content $path -Raw -encoding byte ))

# JSONリクエスト文字列
# 解析する画像のBase64文字列と引数で指定したTypeをセットしています
$body = @"
{
    "requests": [
        {
            "image": {
                "content": "$b64str"
            },
            "features": [
                {
                    "type": "$request_type",
                    "maxResults": 1
                }
            ]
        }
    ]
}
"@

# リクエストを投げて結果のJSON文字列表示する
Invoke-WebRequest -Uri $EndPoint -Method Post -Body $body -ContentType 'application/json' | select -ExpandProperty Content


RestAPIからJSONを取得するコマンドレットに関しては以下サイトを参考にしました。
tech.guitarrapc.com
http://tech.guitarrapc.com/entry/2013/03/04/210313

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?