google cloud vision を composer でインスコしようとしたら
インスコできなかった。
直書きで挑戦。
hoge.php
//画像の顔の位置を取得
$projectId = config('app.gcp_project_id'); //プロジェクトIDをLaravel configから読み取る
$keyFile = json_decode(file_get_contents(config_path('gcp.json')), true);
$apiKey = "YOURAPIキー"; // APIキーを取得
$imagePath = storage_path("girl.png"); //storage/girl.png
$imageData = base64_encode(file_get_contents($imagePath));
$payload = json_encode([
"requests" => [
[
"image" => [
"content" => $imageData
],
"features" => [
[
"type" => "FACE_DETECTION"
]
]
]
]
]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://vision.googleapis.com/v1/images:annotate?key=" . $apiKey);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
// レスポンスをデコードして顔の位置情報を取得
$response_data = json_decode($result, true);
$faceAnnotations = $response_data['responses'][0]['faceAnnotations'];
foreach ($faceAnnotations as $face) {
$vertices = $face['fdBoundingPoly']['vertices'];
print_r($vertices);
}
1024 x 1024 の顔写真で利用
返された値は、顔が画像内で占める矩形領域の4つの頂点の座標です。具体的には、次のようになります:
最初の点([x] => 463, [y] => 206)は矩形の左上の頂点の座標です。
2番目の点([x] => 830, [y] => 206)は矩形の右上の頂点の座標です。
3番目の点([x] => 830, [y] => 576)は矩形の右下の頂点の座標です。
4番目の点([x] => 463, [y] => 576)は矩形の左下の頂点の座標です。
返却値
Array ( [0] => Array ( [x] => 463 [y] => 206 ) [1] => Array ( [x] => 830 [y] => 206 ) [2] => Array ( [x] => 830 [y] => 576 ) [3] => Array ( [x] => 463 [y] => 576 ) )
以下のような感じで、顔の座標が返される。