Google Cloud Vision APIを試した所感

# 超えるべき壁
有料だったけど勇気を出して試してみた

### 実際は
無料だった
勝手に課金されることはないし、課金される前に通知が来るみたい
※大量に利用すると課金されそう

# 環境
AWS(Amazon Linux)
PHP7

# 試したこと
とある画像をAPIに投げて、返って来たJSONを読む。

ソースコード

```php:
<?php
	// APIキー
	$api_key = "" ;

	// リファラー (許可するリファラーを設定した場合)
	$referer = "https://...com/" ;

	// 画像へのパス
	$image_path = "./image.jpg" ;

	// リクエスト用のJSONを作成
	$json = json_encode( array(
		"requests" => array(
			array(
				"image" => array(
					"content" => base64_encode( file_get_contents( $image_path ) ) ,
				) ,
				"features" => array(
					array(
						"type" => "FACE_DETECTION" ,
						"maxResults" => 3 ,
					) ,
					array(
						"type" => "LANDMARK_DETECTION" ,
						"maxResults" => 3 ,
					) ,
					array(
						"type" => "LOGO_DETECTION" ,
						"maxResults" => 3 ,
					) ,
					array(
						"type" => "LABEL_DETECTION" ,
						"maxResults" => 3 ,
					) ,
					array(
						"type" => "TEXT_DETECTION" ,
						"maxResults" => 3 ,
					) ,
					array(
						"type" => "SAFE_SEARCH_DETECTION" ,
						"maxResults" => 3 ,
					) ,
					array(
						"type" => "IMAGE_PROPERTIES" ,
						"maxResults" => 3 ,
					) ,
				) ,
			) ,
		) ,
	) ) ;

	// リクエストを実行
	$curl = curl_init() ;
	curl_setopt( $curl, CURLOPT_URL, "https://vision.googleapis.com/v1/images:annotate?key=" . $api_key ) ;
	curl_setopt( $curl, CURLOPT_HEADER, true ) ;
	curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, "POST" ) ;
	curl_setopt( $curl, CURLOPT_HTTPHEADER, array( "Content-Type: application/json" ) ) ;
	curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false ) ;
	curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ) ;
	if( isset($referer) && !empty($referer) ) curl_setopt( $curl, CURLOPT_REFERER, $referer ) ;
	curl_setopt( $curl, CURLOPT_TIMEOUT, 15 ) ;
	curl_setopt( $curl, CURLOPT_POSTFIELDS, $json ) ;
	$res1 = curl_exec( $curl ) ;
	$res2 = curl_getinfo( $curl ) ;
	curl_close( $curl ) ;

	// 取得したデータ
	$json = substr( $res1, $res2["header_size"] ) ;				// 取得したJSON
	$header = substr( $res1, 0, $res2["header_size"] ) ;		// レスポンスヘッダー

	// 出力
	echo "<h2>JSON</h2>" ;
	echo $json ;

	echo "<h2>ヘッダー</h2>" ;
	echo $header ;

```

## インプット

image.jpg
![image.jpg](https://qiita-image-store.s3.amazonaws.com/0/2567/77594cae-95b3-c22a-60a0-b1fdb4236dce.jpeg)


## アウトプット

返って来たJSON

```json:
{
    "responses": [
        {
            "landmarkAnnotations": [
                {
                    "mid": "/m/0b5bpb",
                    "description": "Kōchi Castle",
                    "score": 0.44856742,
                    "boundingPoly": {
                        "vertices": [
                            {
                                "x": 216,
                                "y": 79
                            },
                            {
                                "x": 809,
                                "y": 79
                            },
                            {
                                "x": 809,
                                "y": 250
                            },
                            {
                                "x": 216,
                                "y": 250
                            }
                        ]
                    },
                    "locations": [
                        {
                            "latLng": {
                                "latitude": 33.560712,
                                "longitude": 133.53148
                            }
                        }
                    ]
                }
            ],
            "labelAnnotations": [
                {
                    "mid": "/m/03ty65",
                    "description": "chinese architecture",
                    "score": 0.98161763
                },
                {
                    "mid": "/m/07yr8h",
                    "description": "historic site",
                    "score": 0.86368334
                },
                {
                    "mid": "/m/0cgh4",
                    "description": "building",
                    "score": 0.84951127
                }
            ],
            "textAnnotations": [
                {
                    "locale": "ja",
                    "description": "観ー\n10\nOR503覧日\n毛成27 10 17\n7\n",
                    "boundingPoly": {
                        "vertices": [
                            {
                                "x": 153,
                                "y": 461
                            },
                            {
                                "x": 233,
                                "y": 461
                            },
                            {
                                "x": 233,
                                "y": 702
                            },
                            {
                                "x": 153,
                                "y": 702
                            }
                        ]
                    }
                },
                {
                    "description": "観ー",
                    "boundingPoly": {
                        "vertices": [
                            {
                                "x": 205,
                                "y": 523
                            },
                            {
                                "x": 233,
                                "y": 524
                            },
                            {
                                "x": 232,
                                "y": 538
                            },
                            {
                                "x": 204,
                                "y": 537
                            }
                        ]
                    }
                },
                {
                    "description": "10",
                    "boundingPoly": {
                        "vertices": [
                            {
                                "x": 157,
                                "y": 617
                            },
                            {
                                "x": 177,
                                "y": 616
                            },
                            {
                                "x": 178,
                                "y": 635
                            },
                            {
                                "x": 158,
                                "y": 636
                            }
                        ]
                    }
                },
                {
                    "description": "OR503",
                    "boundingPoly": {
                        "vertices": [
                            {
                                "x": 205,
                                "y": 461
                            },
                            {
                                "x": 225,
                                "y": 462
                            },
                            {
                                "x": 221,
                                "y": 543
                            },
                            {
                                "x": 201,
                                "y": 542
                            }
                        ]
                    }
                },
                {
                    "description": "覧",
                    "boundingPoly": {
                        "vertices": [
                            {
                                "x": 201,
                                "y": 543
                            },
                            {
                                "x": 218,
                                "y": 544
                            },
                            {
                                "x": 217,
                                "y": 565
                            },
                            {
                                "x": 200,
                                "y": 564
                            }
                        ]
                    }
                },
                {
                    "description": "日",
                    "boundingPoly": {
                        "vertices": [
                            {
                                "x": 202,
                                "y": 564
                            },
                            {
                                "x": 217,
                                "y": 565
                            },
                            {
                                "x": 216,
                                "y": 585
                            },
                            {
                                "x": 201,
                                "y": 584
                            }
                        ]
                    }
                },
                {
                    "description": "毛",
                    "boundingPoly": {
                        "vertices": [
                            {
                                "x": 168,
                                "y": 484
                            },
                            {
                                "x": 190,
                                "y": 486
                            },
                            {
                                "x": 187,
                                "y": 516
                            },
                            {
                                "x": 165,
                                "y": 514
                            }
                        ]
                    }
                },
                {
                    "description": "成",
                    "boundingPoly": {
                        "vertices": [
                            {
                                "x": 165,
                                "y": 514
                            },
                            {
                                "x": 187,
                                "y": 516
                            },
                            {
                                "x": 184,
                                "y": 546
                            },
                            {
                                "x": 162,
                                "y": 544
                            }
                        ]
                    }
                },
                {
                    "description": "27",
                    "boundingPoly": {
                        "vertices": [
                            {
                                "x": 162,
                                "y": 549
                            },
                            {
                                "x": 184,
                                "y": 551
                            },
                            {
                                "x": 182,
                                "y": 571
                            },
                            {
                                "x": 160,
                                "y": 569
                            }
                        ]
                    }
                },
                {
                    "description": "10",
                    "boundingPoly": {
                        "vertices": [
                            {
                                "x": 159,
                                "y": 614
                            },
                            {
                                "x": 179,
                                "y": 616
                            },
                            {
                                "x": 177,
                                "y": 637
                            },
                            {
                                "x": 157,
                                "y": 635
                            }
                        ]
                    }
                },
                {
                    "description": "17",
                    "boundingPoly": {
                        "vertices": [
                            {
                                "x": 154,
                                "y": 684
                            },
                            {
                                "x": 171,
                                "y": 685
                            },
                            {
                                "x": 169,
                                "y": 702
                            },
                            {
                                "x": 153,
                                "y": 701
                            }
                        ]
                    }
                },
                {
                    "description": "7",
                    "boundingPoly": {
                        "vertices": [
                            {
                                "x": 176,
                                "y": 550
                            },
                            {
                                "x": 185,
                                "y": 550
                            },
                            {
                                "x": 186,
                                "y": 570
                            },
                            {
                                "x": 177,
                                "y": 570
                            }
                        ]
                    }
                }
            ],
            "safeSearchAnnotation": {
                "adult": "VERY_UNLIKELY",
                "spoof": "VERY_UNLIKELY",
                "medical": "VERY_UNLIKELY",
                "violence": "VERY_UNLIKELY"
            },
            "imagePropertiesAnnotation": {
                "dominantColors": {
                    "colors": [
                        {
                            "color": {
                                "red": 120,
                                "green": 187,
                                "blue": 251
                            },
                            "score": 0.17604385,
                            "pixelFraction": 0.18412979
                        },
                        {
                            "color": {
                                "red": 29,
                                "green": 18,
                                "blue": 13
                            },
                            "score": 0.081117406,
                            "pixelFraction": 0.053451326
                        },
                        {
                            "color": {
                                "red": 241,
                                "green": 241,
                                "blue": 234
                            },
                            "score": 0.050774563,
                            "pixelFraction": 0.025427729
                        },
                        {
                            "color": {
                                "red": 91,
                                "green": 83,
                                "blue": 85
                            },
                            "score": 0.041599318,
                            "pixelFraction": 0.10589971
                        },
                        {
                            "color": {
                                "red": 156,
                                "green": 155,
                                "blue": 154
                            },
                            "score": 0.041129667,
                            "pixelFraction": 0.070501477
                        },
                        {
                            "color": {
                                "red": 111,
                                "green": 176,
                                "blue": 253
                            },
                            "score": 0.01182378,
                            "pixelFraction": 0.084070794
                        },
                        {
                            "color": {
                                "red": 153,
                                "green": 108,
                                "blue": 70
                            },
                            "score": 0.0022962536,
                            "pixelFraction": 0.00064896757
                        },
                        {
                            "color": {
                                "red": 149,
                                "green": 152,
                                "blue": 87
                            },
                            "score": 0.0010106539,
                            "pixelFraction": 0.0075516226
                        },
                        {
                            "color": {
                                "red": 41,
                                "green": 25,
                                "blue": 11
                            },
                            "score": 0.077093177,
                            "pixelFraction": 0.02660767
                        },
                        {
                            "color": {
                                "red": 62,
                                "green": 45,
                                "blue": 29
                            },
                            "score": 0.066319123,
                            "pixelFraction": 0.034513276
                        }
                    ]
                }
            }
        }
    ]
}
```

# 所感

- ポジティブ
    - すごく詳しくなって返ってくる！
    - 遺跡とか座標も一緒に返ってくる！
    - 画像のどの位置で判断した返ってくる！
- ネガティブ
    - 画像のパターン次第で勝手に帰ってくるのでイレギュラーは人間が仕分けしないといけない
    - 人物とか、感情とか、アダルトとかも全部同じAPIで帰ってくるので、パターンわけしないといけない
    - ニッチな使い方するのに全機能分は勿体無い
    - ここからサービスを考える場合はいいけど、サービスからこのAPIの使い所を判断するのはコストがかかりそう


# 参考

[Google Cloud Vision API（画像解析）を30分で試す](http://qiita.com/t-fujiwara/items/7e1f7c52a73887519ac1)