はじめに
- 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