顔検出は、画像に含まれる複数の人物の顔を検出します。さらに感情や wearing headwear
など、関連する主な顔属性についても識別します。
特定の個人の顔認証はサポートされていません。
使ってみる
Google Cloud を初めて使用される方は、アカウントを作成して、実際のシナリオでの Cloud Vision API のパフォーマンスを評価してください。新規のお客様には、ワークロードの実行、テスト、デプロイができる無料クレジット $300 分を差し上げます。
Cloud Vision API の無料トライアル顔検出リクエスト
Google Cloud プロジェクトと認証のセットアップ
Google Cloud プロジェクトをまだ作成していない場合は、ここで作成します。手順については、このセクションを開いてください。
- Google Cloud アカウントにログインします。Google Cloud を初めて使用する場合は、アカウントを作成して、実際のシナリオでの Google プロダクトのパフォーマンスを評価してください。新規のお客様には、ワークロードの実行、テスト、デプロイができる無料クレジット $300 分を差し上げます。
-
Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。
-
Cloud プロジェクトに対して課金が有効になっていることを確認します。詳しくは、プロジェクトで課金が有効になっているかどうかを確認する方法をご覧ください。
-
Vision API を有効にします。
- Google Cloud CLI をインストールします。
-
gcloud CLI を初期化するには:
gcloud init
-
Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。
-
Cloud プロジェクトに対して課金が有効になっていることを確認します。詳しくは、プロジェクトで課金が有効になっているかどうかを確認する方法をご覧ください。
-
Vision API を有効にします。
- Google Cloud CLI をインストールします。
-
gcloud CLI を初期化するには:
gcloud init
ローカル画像で顔を検出する
Vision API を使用して、ローカル画像ファイルに特徴検出を実行できます。
REST リクエストの場合は、リクエストの本文で画像ファイルのコンテンツを base64 エンコード文字列として送信します。
gcloud
とクライアント ライブラリ リクエストの場合は、リクエストにローカル イメージへのパスを指定します。
REST
リクエストのデータを使用する前に、次のように置き換えます。
- BASE64_ENCODED_IMAGE: バイナリ画像データの base64 表現(ASCII 文字列)。これは次のような文字列になります。
/9j/4QAYRXhpZgAA...9tAVx/zDQDlGxn//2Q==
- RESULTS_INT:(省略可)返される結果の整数値。
"maxResults"
フィールドとその値を省略した場合、API はデフォルト値の 10 を返します。このフィールドは、TEXT_DETECTION
、DOCUMENT_TEXT_DETECTION
、CROP_HINTS
の各機能タイプには適用されません。 - PROJECT_ID: Google Cloud プロジェクト ID。
HTTP メソッドと URL:
POST https://vision.googleapis.com/v1/images:annotate
JSON 本文のリクエスト:
{ "requests": [ { "image": { "content": "BASE64_ENCODED_IMAGE" }, "features": [ { "maxResults": RESULTS_INT, "type": "FACE_DETECTION" } ] } ] }
リクエストを送信するには、次のいずれかのオプションを選択します。
curl
リクエスト本文を request.json
という名前のファイルに保存して、次のコマンドを実行します。
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: PROJECT_ID" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://vision.googleapis.com/v1/images:annotate"
PowerShell
リクエスト本文を request.json
という名前のファイルに保存して、次のコマンドを実行します。
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "PROJECT_ID" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://vision.googleapis.com/v1/images:annotate" | Select-Object -Expand Content
リクエストが成功すると、サーバーは 200 OK
HTTP ステータス コードと JSON 形式のレスポンスを返します。
FACE_DETECTION
レスポンスには、検出されたすべての顔の境界ボックス、顔で検出されたランドマーク(眼、鼻、口など)、顔の信頼性の評価、画像のプロパティ(喜び、悲しみ、怒り、驚きなど)が含まれます。
レスポンス
{ "responses": [ { "faceAnnotations": [ { "boundingPoly": { "vertices": [ { "x": 1126, "y": 224 }, { "x": 2144, "y": 224 }, { "x": 2144, "y": 1407 }, { "x": 1126, "y": 1407 } ] }, "fdBoundingPoly": { "vertices": [ { "x": 1149, "y": 495 }, { "x": 1979, "y": 495 }, { "x": 1979, "y": 1325 }, { "x": 1149, "y": 1325 } ] }, "landmarks": [ { "type": "LEFT_EYE", "position": { "x": 1380.8185, "y": 743.84045, "z": 0.0021395613 } }, { "type": "RIGHT_EYE", "position": { "x": 1662.0685, "y": 755.5167, "z": -132.41025 } }, { "type": "LEFT_OF_LEFT_EYEBROW", "position": { "x": 1299.746, "y": 678.32825, "z": 70.48484 } }, { "type": "RIGHT_OF_LEFT_EYEBROW", "position": { "x": 1425.6302, "y": 681.8628, "z": -87.506454 } }, { "type": "LEFT_OF_RIGHT_EYEBROW", "position": { "x": 1558.2202, "y": 686.09814, "z": -151.19424 } }, { "type": "RIGHT_OF_RIGHT_EYEBROW", "position": { "x": 1774.0068, "y": 700.8744, "z": -147.01884 } }, { "type": "MIDPOINT_BETWEEN_EYES", "position": { "x": 1482.9568, "y": 744.6472, "z": -123.55287 } }, { "type": "NOSE_TIP", "position": { "x": 1445.8673, "y": 921.27625, "z": -202.87003 } }, { "type": "UPPER_LIP", "position": { "x": 1471.0441, "y": 1031.8804, "z": -137.91678 } }, { "type": "LOWER_LIP", "position": { "x": 1478.706, "y": 1140.3444, "z": -116.650894 } }, { "type": "MOUTH_LEFT", "position": { "x": 1397.0453, "y": 1074.5051, "z": -10.362965 } }, { "type": "MOUTH_RIGHT", "position": { "x": 1655.8596, "y": 1077.2307, "z": -110.53521 } }, { "type": "MOUTH_CENTER", "position": { "x": 1478.6444, "y": 1084.2994, "z": -116.58564 } }, { "type": "NOSE_BOTTOM_RIGHT", "position": { "x": 1575.067, "y": 946.60516, "z": -132.10355 } }, { "type": "NOSE_BOTTOM_LEFT", "position": { "x": 1396.0128, "y": 944.48035, "z": -52.012077 } }, { "type": "NOSE_BOTTOM_CENTER", "position": { "x": 1470.0845, "y": 970.4061, "z": -139.32004 } }, { "type": "LEFT_EYE_TOP_BOUNDARY", "position": { "x": 1369.846, "y": 731.5155, "z": -18.276585 } }, { "type": "LEFT_EYE_RIGHT_CORNER", "position": { "x": 1429.0255, "y": 758.3745, "z": -23.542086 } }, { "type": "LEFT_EYE_BOTTOM_BOUNDARY", "position": { "x": 1371.9097, "y": 775.1022, "z": -0.9920981 } }, { "type": "LEFT_EYE_LEFT_CORNER", "position": { "x": 1334.5302, "y": 752.3428, "z": 52.589348 } }, { "type": "LEFT_EYE_PUPIL", "position": { "x": 1369.7902, "y": 752.99426, "z": -4.5812535 } }, { "type": "RIGHT_EYE_TOP_BOUNDARY", "position": { "x": 1646.1317, "y": 740.8714, "z": -149.13914 } }, { "type": "RIGHT_EYE_RIGHT_CORNER", "position": { "x": 1722.3821, "y": 765.43964, "z": -130.75653 } }, { "type": "RIGHT_EYE_BOTTOM_BOUNDARY", "position": { "x": 1659.9559, "y": 784.7147, "z": -132.99072 } }, { "type": "RIGHT_EYE_LEFT_CORNER", "position": { "x": 1593.8276, "y": 765.21747, "z": -102.17816 } }, { "type": "RIGHT_EYE_PUPIL", "position": { "x": 1652.9113, "y": 762.5403, "z": -139.53206 } }, { "type": "LEFT_EYEBROW_UPPER_MIDPOINT", "position": { "x": 1354.2014, "y": 642.7664, "z": -27.636467 } }, { "type": "RIGHT_EYEBROW_UPPER_MIDPOINT", "position": { "x": 1653.919, "y": 652.8384, "z": -169.58447 } }, { "type": "LEFT_EAR_TRAGION", "position": { "x": 1359.5302, "y": 899.9487, "z": 427.43317 } }, { "type": "RIGHT_EAR_TRAGION", "position": { "x": 1989.5599, "y": 921.3098, "z": 129.99654 } }, { "type": "FOREHEAD_GLABELLA", "position": { "x": 1487.4016, "y": 680.26874, "z": -130.2485 } }, { "type": "CHIN_GNATHION", "position": { "x": 1492.2446, "y": 1278.0455, "z": -77.825676 } }, { "type": "CHIN_LEFT_GONION", "position": { "x": 1330.9834, "y": 1094.7004, "z": 301.41083 } }, { "type": "CHIN_RIGHT_GONION", "position": { "x": 1897.9647, "y": 1114.2906, "z": 32.6488 } } ], "rollAngle": 1.7871192, "panAngle": -25.329906, "tiltAngle": -0.32616663, "detectionConfidence": 0.8218701, "landmarkingConfidence": 0.5853151, "joyLikelihood": "VERY_LIKELY", "sorrowLikelihood": "VERY_UNLIKELY", "angerLikelihood": "VERY_UNLIKELY", "surpriseLikelihood": "VERY_UNLIKELY", "underExposedLikelihood": "VERY_UNLIKELY", "blurredLikelihood": "VERY_UNLIKELY", "headwearLikelihood": "VERY_UNLIKELY" }, { "boundingPoly": { "vertices": [ { "x": 168, "y": 1305 }, { "x": 793, "y": 1305 }, { "x": 793, "y": 1844 }, { "x": 168, "y": 1844 } ] }, "fdBoundingPoly": { "vertices": [ { "x": 234, "y": 1454 }, { "x": 701, "y": 1454 }, { "x": 701, "y": 1844 }, { "x": 234, "y": 1844 } ] }, "landmarks": [ { "type": "LEFT_EYE", "position": { "x": 355.4812, "y": 1633.7351, "z": -0.00096174295 } }, { "type": "RIGHT_EYE", "position": { "x": 563.05676, "y": 1598.2274, "z": -15.432735 } }, { "type": "LEFT_OF_LEFT_EYEBROW", "position": { "x": 295.2572, "y": 1596.3945, "z": 27.602434 } }, { "type": "RIGHT_OF_LEFT_EYEBROW", "position": { "x": 399.66183, "y": 1574.5125, "z": -34.742764 } }, { "type": "LEFT_OF_RIGHT_EYEBROW", "position": { "x": 489.41595, "y": 1561.8435, "z": -41.881912 } }, { "type": "RIGHT_OF_RIGHT_EYEBROW", "position": { "x": 604.66504, "y": 1553.2301, "z": 2.8025007 } }, { "type": "MIDPOINT_BETWEEN_EYES", "position": { "x": 445.71527, "y": 1603.4773, "z": -46.01044 } }, { "type": "NOSE_TIP", "position": { "x": 458.00195, "y": 1706.4338, "z": -113.76316 } }, { "type": "UPPER_LIP", "position": { "x": 469.05304, "y": 1780.2344, "z": -78.998 } }, { "type": "LOWER_LIP", "position": { "x": 480.0647, "y": 1842.2673, "z": -73.402145 } }, { "type": "MOUTH_LEFT", "position": { "x": 398.64343, "y": 1820.1232, "z": -23.800583 } }, { "type": "MOUTH_RIGHT", "position": { "x": 558.57007, "y": 1797.7144, "z": -36.007656 } }, { "type": "MOUTH_CENTER", "position": { "x": 475.6739, "y": 1809.3279, "z": -68.785324 } }, { "type": "NOSE_BOTTOM_RIGHT", "position": { "x": 519.20447, "y": 1717.9144, "z": -47.30806 } }, { "type": "NOSE_BOTTOM_LEFT", "position": { "x": 410.96085, "y": 1739.2937, "z": -38.647636 } }, { "type": "NOSE_BOTTOM_CENTER", "position": { "x": 464.29404, "y": 1739.8107, "z": -74.77752 } }, { "type": "LEFT_EYE_TOP_BOUNDARY", "position": { "x": 358.2863, "y": 1615.2714, "z": -11.324151 } }, { "type": "LEFT_EYE_RIGHT_CORNER", "position": { "x": 395.53445, "y": 1626.9333, "z": -2.3256185 } }, { "type": "LEFT_EYE_BOTTOM_BOUNDARY", "position": { "x": 355.713, "y": 1645.1017, "z": -3.0947497 } }, { "type": "LEFT_EYE_LEFT_CORNER", "position": { "x": 325.97894, "y": 1637.1458, "z": 19.892412 } }, { "type": "LEFT_EYE_PUPIL", "position": { "x": 357.76205, "y": 1629.1317, "z": -4.846803 } }, { "type": "RIGHT_EYE_TOP_BOUNDARY", "position": { "x": 543.97064, "y": 1589.2532, "z": -26.101141 } }, { "type": "RIGHT_EYE_RIGHT_CORNER", "position": { "x": 586.45276, "y": 1600.4672, "z": -0.70706856 } }, { "type": "RIGHT_EYE_BOTTOM_BOUNDARY", "position": { "x": 560.2284, "y": 1612.6013, "z": -17.874813 } }, { "type": "RIGHT_EYE_LEFT_CORNER", "position": { "x": 515.07983, "y": 1609.0634, "z": -11.378595 } }, { "type": "RIGHT_EYE_PUPIL", "position": { "x": 548.1346, "y": 1602.3792, "z": -20.216436 } }, { "type": "LEFT_EYEBROW_UPPER_MIDPOINT", "position": { "x": 342.54276, "y": 1561.4143, "z": -13.184053 } }, { "type": "RIGHT_EYEBROW_UPPER_MIDPOINT", "position": { "x": 543.37683, "y": 1533.3728, "z": -29.198135 } }, { "type": "LEFT_EAR_TRAGION", "position": { "x": 276.39093, "y": 1762.4656, "z": 228.13643 } }, { "type": "RIGHT_EAR_TRAGION", "position": { "x": 698.24506, "y": 1702.8165, "z": 193.92969 } }, { "type": "FOREHEAD_GLABELLA", "position": { "x": 443.33896, "y": 1564.7924, "z": -45.18563 } }, { "type": "CHIN_GNATHION", "position": { "x": 493.31305, "y": 1928.5846, "z": -57.822914 } }, { "type": "CHIN_LEFT_GONION", "position": { "x": 305.68286, "y": 1868.2653, "z": 135.39107 } }, { "type": "CHIN_RIGHT_GONION", "position": { "x": 685.4797, "y": 1815.4688, "z": 104.89257 } } ], "rollAngle": -7.3906693, "panAngle": -4.5714307, "tiltAngle": 7.181163, "detectionConfidence": 0.7173322, "landmarkingConfidence": 0.5315869, "joyLikelihood": "VERY_UNLIKELY", "sorrowLikelihood": "VERY_UNLIKELY", "angerLikelihood": "VERY_UNLIKELY", "surpriseLikelihood": "VERY_UNLIKELY", "underExposedLikelihood": "VERY_UNLIKELY", "blurredLikelihood": "POSSIBLE", "headwearLikelihood": "VERY_UNLIKELY" } ] } ] }
Go
このサンプルを試す前に、Vision クイックスタート: クライアント ライブラリの使用にある Go の設定手順を完了してください。詳細については、Vision Go API のリファレンス ドキュメントをご覧ください。
// detectFaces gets faces from the Vision API for an image at the given file path.
func detectFaces(w io.Writer, file string) error {
ctx := context.Background()
client, err := vision.NewImageAnnotatorClient(ctx)
if err != nil {
return err
}
defer client.Close()
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
image, err := vision.NewImageFromReader(f)
if err != nil {
return err
}
annotations, err := client.DetectFaces(ctx, image, nil, 10)
if err != nil {
return err
}
if len(annotations) == 0 {
fmt.Fprintln(w, "No faces found.")
} else {
fmt.Fprintln(w, "Faces:")
for i, annotation := range annotations {
fmt.Fprintln(w, " Face", i)
fmt.Fprintln(w, " Anger:", annotation.AngerLikelihood)
fmt.Fprintln(w, " Joy:", annotation.JoyLikelihood)
fmt.Fprintln(w, " Surprise:", annotation.SurpriseLikelihood)
}
}
return nil
}
Java
このサンプルを試す前に、Vision API クイックスタート: クライアント ライブラリの使用の Java の設定手順を完了してください。詳細については、Vision API Java のリファレンス ドキュメントをご覧ください。
import com.google.cloud.vision.v1.AnnotateImageRequest;
import com.google.cloud.vision.v1.AnnotateImageResponse;
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
import com.google.cloud.vision.v1.FaceAnnotation;
import com.google.cloud.vision.v1.Feature;
import com.google.cloud.vision.v1.Image;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import com.google.protobuf.ByteString;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class DetectFaces {
public static void detectFaces() throws IOException {
// TODO(developer): Replace these variables before running the sample.
String filePath = "path/to/your/image/file.jpg";
detectFaces(filePath);
}
// Detects faces in the specified local image.
public static void detectFaces(String filePath) throws IOException {
List<AnnotateImageRequest> requests = new ArrayList<>();
ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
Image img = Image.newBuilder().setContent(imgBytes).build();
Feature feat = Feature.newBuilder().setType(Feature.Type.FACE_DETECTION).build();
AnnotateImageRequest request =
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();
for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
System.out.format("Error: %s%n", res.getError().getMessage());
return;
}
// For full list of available annotations, see http://g.co/cloud/vision/docs
for (FaceAnnotation annotation : res.getFaceAnnotationsList()) {
System.out.format(
"anger: %s%njoy: %s%nsurprise: %s%nposition: %s",
annotation.getAngerLikelihood(),
annotation.getJoyLikelihood(),
annotation.getSurpriseLikelihood(),
annotation.getBoundingPoly());
}
}
}
}
}
Node.js
このサンプルを試す前に、Vision クイックスタート: クライアント ライブラリの使用にある Node.js の設定手順を完了してください。詳細については、Vision Node.js API のリファレンス ドキュメントをご覧ください。
// Imports the Google Cloud client library
const vision = require('@google-cloud/vision');
// Creates a client
const client = new vision.ImageAnnotatorClient();
async function detectFaces() {
/**
* TODO(developer): Uncomment the following line before running the sample.
*/
// const fileName = 'Local image file, e.g. /path/to/image.png';
const [result] = await client.faceDetection(fileName);
const faces = result.faceAnnotations;
console.log('Faces:');
faces.forEach((face, i) => {
console.log(` Face #${i + 1}:`);
console.log(` Joy: ${face.joyLikelihood}`);
console.log(` Anger: ${face.angerLikelihood}`);
console.log(` Sorrow: ${face.sorrowLikelihood}`);
console.log(` Surprise: ${face.surpriseLikelihood}`);
});
}
detectFaces();
Python
このサンプルを試す前に、Vision クイックスタート: クライアント ライブラリの使用にある Python の設定手順を完了してください。詳細については、Vision Python API のリファレンス ドキュメントをご覧ください。
def detect_faces(path):
"""Detects faces in an image."""
from google.cloud import vision
import io
client = vision.ImageAnnotatorClient()
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
response = client.face_detection(image=image)
faces = response.face_annotations
# Names of likelihood from google.cloud.vision.enums
likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
'LIKELY', 'VERY_LIKELY')
print('Faces:')
for face in faces:
print('anger: {}'.format(likelihood_name[face.anger_likelihood]))
print('joy: {}'.format(likelihood_name[face.joy_likelihood]))
print('surprise: {}'.format(likelihood_name[face.surprise_likelihood]))
vertices = (['({},{})'.format(vertex.x, vertex.y)
for vertex in face.bounding_poly.vertices])
print('face bounds: {}'.format(','.join(vertices)))
if response.error.message:
raise Exception(
'{}\nFor more info on error messages, check: '
'https://cloud.google.com/apis/design/errors'.format(
response.error.message))
その他の言語
C#: クライアント ライブラリ ページの C# の設定手順を行ってから、.NET 用の Vision リファレンス ドキュメントをご覧ください。
PHP: クライアント ライブラリ ページの PHP の設定手順を行ってから、PHP 用の Vision リファレンス ドキュメントをご覧ください。
Ruby: クライアント ライブラリ ページの Ruby の設定手順を行ってから、Ruby 用の Vision リファレンス ドキュメントをご覧ください。
リモート画像で顔を検出する
Vision API を使用すると、Cloud Storage またはウェブ上にあるリモート画像ファイルに特徴検出を実行できます。リモート ファイル リクエストを送信するには、リクエストの本文でファイルのウェブ URL または Cloud Storage URI を指定します。
REST
リクエストのデータを使用する前に、次のように置き換えます。
- CLOUD_STORAGE_IMAGE_URI: Cloud Storage バケット内の有効な画像ファイルへのパス。少なくとも、ファイルに対する読み取り権限が必要です。たとえば次のように指定します。
gs://cloud-samples-data/vision/face/faces.jpeg
- RESULTS_INT:(省略可)返される結果の整数値。
"maxResults"
フィールドとその値を省略した場合、API はデフォルト値の 10 を返します。このフィールドは、TEXT_DETECTION
、DOCUMENT_TEXT_DETECTION
、CROP_HINTS
の各機能タイプには適用されません。 - PROJECT_ID: Google Cloud プロジェクト ID。
HTTP メソッドと URL:
POST https://vision.googleapis.com/v1/images:annotate
JSON 本文のリクエスト:
{ "requests": [ { "image": { "source": { "imageUri": "CLOUD_STORAGE_IMAGE_URI" } }, "features": [ { "maxResults": RESULTS_INT, "type": "FACE_DETECTION" } ] } ] }
リクエストを送信するには、次のいずれかのオプションを選択します。
curl
リクエスト本文を request.json
という名前のファイルに保存して、次のコマンドを実行します。
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: PROJECT_ID" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://vision.googleapis.com/v1/images:annotate"
PowerShell
リクエスト本文を request.json
という名前のファイルに保存して、次のコマンドを実行します。
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "PROJECT_ID" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://vision.googleapis.com/v1/images:annotate" | Select-Object -Expand Content
リクエストが成功すると、サーバーは 200 OK
HTTP ステータス コードと JSON 形式のレスポンスを返します。
FACE_DETECTION
レスポンスには、検出されたすべての顔の境界ボックス、顔で検出されたランドマーク(眼、鼻、口など)、顔の信頼性の評価、画像のプロパティ(喜び、悲しみ、怒り、驚きなど)が含まれます。
レスポンス
{ "responses": [ { "faceAnnotations": [ { "boundingPoly": { "vertices": [ { "x": 1126, "y": 224 }, { "x": 2144, "y": 224 }, { "x": 2144, "y": 1407 }, { "x": 1126, "y": 1407 } ] }, "fdBoundingPoly": { "vertices": [ { "x": 1149, "y": 495 }, { "x": 1979, "y": 495 }, { "x": 1979, "y": 1325 }, { "x": 1149, "y": 1325 } ] }, "landmarks": [ { "type": "LEFT_EYE", "position": { "x": 1380.8185, "y": 743.84045, "z": 0.0021395613 } }, { "type": "RIGHT_EYE", "position": { "x": 1662.0685, "y": 755.5167, "z": -132.41025 } }, { "type": "LEFT_OF_LEFT_EYEBROW", "position": { "x": 1299.746, "y": 678.32825, "z": 70.48484 } }, { "type": "RIGHT_OF_LEFT_EYEBROW", "position": { "x": 1425.6302, "y": 681.8628, "z": -87.506454 } }, { "type": "LEFT_OF_RIGHT_EYEBROW", "position": { "x": 1558.2202, "y": 686.09814, "z": -151.19424 } }, { "type": "RIGHT_OF_RIGHT_EYEBROW", "position": { "x": 1774.0068, "y": 700.8744, "z": -147.01884 } }, { "type": "MIDPOINT_BETWEEN_EYES", "position": { "x": 1482.9568, "y": 744.6472, "z": -123.55287 } }, { "type": "NOSE_TIP", "position": { "x": 1445.8673, "y": 921.27625, "z": -202.87003 } }, { "type": "UPPER_LIP", "position": { "x": 1471.0441, "y": 1031.8804, "z": -137.91678 } }, { "type": "LOWER_LIP", "position": { "x": 1478.706, "y": 1140.3444, "z": -116.650894 } }, { "type": "MOUTH_LEFT", "position": { "x": 1397.0453, "y": 1074.5051, "z": -10.362965 } }, { "type": "MOUTH_RIGHT", "position": { "x": 1655.8596, "y": 1077.2307, "z": -110.53521 } }, { "type": "MOUTH_CENTER", "position": { "x": 1478.6444, "y": 1084.2994, "z": -116.58564 } }, { "type": "NOSE_BOTTOM_RIGHT", "position": { "x": 1575.067, "y": 946.60516, "z": -132.10355 } }, { "type": "NOSE_BOTTOM_LEFT", "position": { "x": 1396.0128, "y": 944.48035, "z": -52.012077 } }, { "type": "NOSE_BOTTOM_CENTER", "position": { "x": 1470.0845, "y": 970.4061, "z": -139.32004 } }, { "type": "LEFT_EYE_TOP_BOUNDARY", "position": { "x": 1369.846, "y": 731.5155, "z": -18.276585 } }, { "type": "LEFT_EYE_RIGHT_CORNER", "position": { "x": 1429.0255, "y": 758.3745, "z": -23.542086 } }, { "type": "LEFT_EYE_BOTTOM_BOUNDARY", "position": { "x": 1371.9097, "y": 775.1022, "z": -0.9920981 } }, { "type": "LEFT_EYE_LEFT_CORNER", "position": { "x": 1334.5302, "y": 752.3428, "z": 52.589348 } }, { "type": "LEFT_EYE_PUPIL", "position": { "x": 1369.7902, "y": 752.99426, "z": -4.5812535 } }, { "type": "RIGHT_EYE_TOP_BOUNDARY", "position": { "x": 1646.1317, "y": 740.8714, "z": -149.13914 } }, { "type": "RIGHT_EYE_RIGHT_CORNER", "position": { "x": 1722.3821, "y": 765.43964, "z": -130.75653 } }, { "type": "RIGHT_EYE_BOTTOM_BOUNDARY", "position": { "x": 1659.9559, "y": 784.7147, "z": -132.99072 } }, { "type": "RIGHT_EYE_LEFT_CORNER", "position": { "x": 1593.8276, "y": 765.21747, "z": -102.17816 } }, { "type": "RIGHT_EYE_PUPIL", "position": { "x": 1652.9113, "y": 762.5403, "z": -139.53206 } }, { "type": "LEFT_EYEBROW_UPPER_MIDPOINT", "position": { "x": 1354.2014, "y": 642.7664, "z": -27.636467 } }, { "type": "RIGHT_EYEBROW_UPPER_MIDPOINT", "position": { "x": 1653.919, "y": 652.8384, "z": -169.58447 } }, { "type": "LEFT_EAR_TRAGION", "position": { "x": 1359.5302, "y": 899.9487, "z": 427.43317 } }, { "type": "RIGHT_EAR_TRAGION", "position": { "x": 1989.5599, "y": 921.3098, "z": 129.99654 } }, { "type": "FOREHEAD_GLABELLA", "position": { "x": 1487.4016, "y": 680.26874, "z": -130.2485 } }, { "type": "CHIN_GNATHION", "position": { "x": 1492.2446, "y": 1278.0455, "z": -77.825676 } }, { "type": "CHIN_LEFT_GONION", "position": { "x": 1330.9834, "y": 1094.7004, "z": 301.41083 } }, { "type": "CHIN_RIGHT_GONION", "position": { "x": 1897.9647, "y": 1114.2906, "z": 32.6488 } } ], "rollAngle": 1.7871192, "panAngle": -25.329906, "tiltAngle": -0.32616663, "detectionConfidence": 0.8218701, "landmarkingConfidence": 0.5853151, "joyLikelihood": "VERY_LIKELY", "sorrowLikelihood": "VERY_UNLIKELY", "angerLikelihood": "VERY_UNLIKELY", "surpriseLikelihood": "VERY_UNLIKELY", "underExposedLikelihood": "VERY_UNLIKELY", "blurredLikelihood": "VERY_UNLIKELY", "headwearLikelihood": "VERY_UNLIKELY" }, { "boundingPoly": { "vertices": [ { "x": 168, "y": 1305 }, { "x": 793, "y": 1305 }, { "x": 793, "y": 1844 }, { "x": 168, "y": 1844 } ] }, "fdBoundingPoly": { "vertices": [ { "x": 234, "y": 1454 }, { "x": 701, "y": 1454 }, { "x": 701, "y": 1844 }, { "x": 234, "y": 1844 } ] }, "landmarks": [ { "type": "LEFT_EYE", "position": { "x": 355.4812, "y": 1633.7351, "z": -0.00096174295 } }, { "type": "RIGHT_EYE", "position": { "x": 563.05676, "y": 1598.2274, "z": -15.432735 } }, { "type": "LEFT_OF_LEFT_EYEBROW", "position": { "x": 295.2572, "y": 1596.3945, "z": 27.602434 } }, { "type": "RIGHT_OF_LEFT_EYEBROW", "position": { "x": 399.66183, "y": 1574.5125, "z": -34.742764 } }, { "type": "LEFT_OF_RIGHT_EYEBROW", "position": { "x": 489.41595, "y": 1561.8435, "z": -41.881912 } }, { "type": "RIGHT_OF_RIGHT_EYEBROW", "position": { "x": 604.66504, "y": 1553.2301, "z": 2.8025007 } }, { "type": "MIDPOINT_BETWEEN_EYES", "position": { "x": 445.71527, "y": 1603.4773, "z": -46.01044 } }, { "type": "NOSE_TIP", "position": { "x": 458.00195, "y": 1706.4338, "z": -113.76316 } }, { "type": "UPPER_LIP", "position": { "x": 469.05304, "y": 1780.2344, "z": -78.998 } }, { "type": "LOWER_LIP", "position": { "x": 480.0647, "y": 1842.2673, "z": -73.402145 } }, { "type": "MOUTH_LEFT", "position": { "x": 398.64343, "y": 1820.1232, "z": -23.800583 } }, { "type": "MOUTH_RIGHT", "position": { "x": 558.57007, "y": 1797.7144, "z": -36.007656 } }, { "type": "MOUTH_CENTER", "position": { "x": 475.6739, "y": 1809.3279, "z": -68.785324 } }, { "type": "NOSE_BOTTOM_RIGHT", "position": { "x": 519.20447, "y": 1717.9144, "z": -47.30806 } }, { "type": "NOSE_BOTTOM_LEFT", "position": { "x": 410.96085, "y": 1739.2937, "z": -38.647636 } }, { "type": "NOSE_BOTTOM_CENTER", "position": { "x": 464.29404, "y": 1739.8107, "z": -74.77752 } }, { "type": "LEFT_EYE_TOP_BOUNDARY", "position": { "x": 358.2863, "y": 1615.2714, "z": -11.324151 } }, { "type": "LEFT_EYE_RIGHT_CORNER", "position": { "x": 395.53445, "y": 1626.9333, "z": -2.3256185 } }, { "type": "LEFT_EYE_BOTTOM_BOUNDARY", "position": { "x": 355.713, "y": 1645.1017, "z": -3.0947497 } }, { "type": "LEFT_EYE_LEFT_CORNER", "position": { "x": 325.97894, "y": 1637.1458, "z": 19.892412 } }, { "type": "LEFT_EYE_PUPIL", "position": { "x": 357.76205, "y": 1629.1317, "z": -4.846803 } }, { "type": "RIGHT_EYE_TOP_BOUNDARY", "position": { "x": 543.97064, "y": 1589.2532, "z": -26.101141 } }, { "type": "RIGHT_EYE_RIGHT_CORNER", "position": { "x": 586.45276, "y": 1600.4672, "z": -0.70706856 } }, { "type": "RIGHT_EYE_BOTTOM_BOUNDARY", "position": { "x": 560.2284, "y": 1612.6013, "z": -17.874813 } }, { "type": "RIGHT_EYE_LEFT_CORNER", "position": { "x": 515.07983, "y": 1609.0634, "z": -11.378595 } }, { "type": "RIGHT_EYE_PUPIL", "position": { "x": 548.1346, "y": 1602.3792, "z": -20.216436 } }, { "type": "LEFT_EYEBROW_UPPER_MIDPOINT", "position": { "x": 342.54276, "y": 1561.4143, "z": -13.184053 } }, { "type": "RIGHT_EYEBROW_UPPER_MIDPOINT", "position": { "x": 543.37683, "y": 1533.3728, "z": -29.198135 } }, { "type": "LEFT_EAR_TRAGION", "position": { "x": 276.39093, "y": 1762.4656, "z": 228.13643 } }, { "type": "RIGHT_EAR_TRAGION", "position": { "x": 698.24506, "y": 1702.8165, "z": 193.92969 } }, { "type": "FOREHEAD_GLABELLA", "position": { "x": 443.33896, "y": 1564.7924, "z": -45.18563 } }, { "type": "CHIN_GNATHION", "position": { "x": 493.31305, "y": 1928.5846, "z": -57.822914 } }, { "type": "CHIN_LEFT_GONION", "position": { "x": 305.68286, "y": 1868.2653, "z": 135.39107 } }, { "type": "CHIN_RIGHT_GONION", "position": { "x": 685.4797, "y": 1815.4688, "z": 104.89257 } } ], "rollAngle": -7.3906693, "panAngle": -4.5714307, "tiltAngle": 7.181163, "detectionConfidence": 0.7173322, "landmarkingConfidence": 0.5315869, "joyLikelihood": "VERY_UNLIKELY", "sorrowLikelihood": "VERY_UNLIKELY", "angerLikelihood": "VERY_UNLIKELY", "surpriseLikelihood": "VERY_UNLIKELY", "underExposedLikelihood": "VERY_UNLIKELY", "blurredLikelihood": "POSSIBLE", "headwearLikelihood": "VERY_UNLIKELY" } ] } ] }
Go
このサンプルを試す前に、Vision クイックスタート: クライアント ライブラリの使用にある Go の設定手順を完了してください。詳細については、Vision Go API のリファレンス ドキュメントをご覧ください。
// detectFaces gets faces from the Vision API for an image at the given file path.
func detectFacesURI(w io.Writer, file string) error {
ctx := context.Background()
client, err := vision.NewImageAnnotatorClient(ctx)
if err != nil {
return err
}
image := vision.NewImageFromURI(file)
annotations, err := client.DetectFaces(ctx, image, nil, 10)
if err != nil {
return err
}
if len(annotations) == 0 {
fmt.Fprintln(w, "No faces found.")
} else {
fmt.Fprintln(w, "Faces:")
for i, annotation := range annotations {
fmt.Fprintln(w, " Face", i)
fmt.Fprintln(w, " Anger:", annotation.AngerLikelihood)
fmt.Fprintln(w, " Joy:", annotation.JoyLikelihood)
fmt.Fprintln(w, " Surprise:", annotation.SurpriseLikelihood)
}
}
return nil
}
Java
このサンプルを試す前に、Vision API クイックスタート: クライアント ライブラリの使用の Java の設定手順を完了してください。詳細については、Vision API Java のリファレンス ドキュメントをご覧ください。
import com.google.cloud.vision.v1.AnnotateImageRequest;
import com.google.cloud.vision.v1.AnnotateImageResponse;
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
import com.google.cloud.vision.v1.FaceAnnotation;
import com.google.cloud.vision.v1.Feature;
import com.google.cloud.vision.v1.Image;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import com.google.cloud.vision.v1.ImageSource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class DetectFacesGcs {
public static void detectFacesGcs() throws IOException {
// TODO(developer): Replace these variables before running the sample.
String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg";
detectFacesGcs(filePath);
}
// Detects faces in the specified remote image on Google Cloud Storage.
public static void detectFacesGcs(String gcsPath) throws IOException {
List<AnnotateImageRequest> requests = new ArrayList<>();
ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
Image img = Image.newBuilder().setSource(imgSource).build();
Feature feat = Feature.newBuilder().setType(Feature.Type.FACE_DETECTION).build();
AnnotateImageRequest request =
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();
for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
System.out.format("Error: %s%n", res.getError().getMessage());
return;
}
// For full list of available annotations, see http://g.co/cloud/vision/docs
for (FaceAnnotation annotation : res.getFaceAnnotationsList()) {
System.out.format(
"anger: %s%njoy: %s%nsurprise: %s%nposition: %s",
annotation.getAngerLikelihood(),
annotation.getJoyLikelihood(),
annotation.getSurpriseLikelihood(),
annotation.getBoundingPoly());
}
}
}
}
}
Node.js
このサンプルを試す前に、Vision クイックスタート: クライアント ライブラリの使用にある Node.js の設定手順を完了してください。詳細については、Vision Node.js API のリファレンス ドキュメントをご覧ください。
// Imports the Google Cloud client libraries
const vision = require('@google-cloud/vision');
// Creates a client
const client = new vision.ImageAnnotatorClient();
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const bucketName = 'Bucket where the file resides, e.g. my-bucket';
// const fileName = 'Path to file within bucket, e.g. path/to/image.png';
// Performs face detection on the gcs file
const [result] = await client.faceDetection(`gs://${bucketName}/${fileName}`);
const faces = result.faceAnnotations;
console.log('Faces:');
faces.forEach((face, i) => {
console.log(` Face #${i + 1}:`);
console.log(` Joy: ${face.joyLikelihood}`);
console.log(` Anger: ${face.angerLikelihood}`);
console.log(` Sorrow: ${face.sorrowLikelihood}`);
console.log(` Surprise: ${face.surpriseLikelihood}`);
});
Python
このサンプルを試す前に、Vision クイックスタート: クライアント ライブラリの使用にある Python の設定手順を完了してください。詳細については、Vision Python API のリファレンス ドキュメントをご覧ください。
def detect_faces_uri(uri):
"""Detects faces in the file located in Google Cloud Storage or the web."""
from google.cloud import vision
client = vision.ImageAnnotatorClient()
image = vision.Image()
image.source.image_uri = uri
response = client.face_detection(image=image)
faces = response.face_annotations
# Names of likelihood from google.cloud.vision.enums
likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
'LIKELY', 'VERY_LIKELY')
print('Faces:')
for face in faces:
print('anger: {}'.format(likelihood_name[face.anger_likelihood]))
print('joy: {}'.format(likelihood_name[face.joy_likelihood]))
print('surprise: {}'.format(likelihood_name[face.surprise_likelihood]))
vertices = (['({},{})'.format(vertex.x, vertex.y)
for vertex in face.bounding_poly.vertices])
print('face bounds: {}'.format(','.join(vertices)))
if response.error.message:
raise Exception(
'{}\nFor more info on error messages, check: '
'https://cloud.google.com/apis/design/errors'.format(
response.error.message))
gcloud
顔検出を行うには、次の例のように gcloud ml vision detect-faces
コマンドを実行します。
gcloud ml vision detect-faces gs://cloud-samples-data/vision/face/faces.jpeg
その他の言語
C#: クライアント ライブラリ ページの C# の設定手順を行ってから、.NET 用の Vision リファレンス ドキュメントをご覧ください。
PHP: クライアント ライブラリ ページの PHP の設定手順を行ってから、PHP 用の Vision リファレンス ドキュメントをご覧ください。
Ruby: クライアント ライブラリ ページの Ruby の設定手順を行ってから、Ruby 用の Vision リファレンス ドキュメントをご覧ください。
試してみる
顔の検出を試してみましょう。すでに指定済みの画像(gs://cloud-samples-data/vision/face/faces.jpeg
)を使用することも、独自の画像を指定することもできます。[実行] を選択してリクエストを送信します。
リクエストの本文:
{ "requests": [ { "features": [ { "maxResults": 10, "type": "FACE_DETECTION" } ], "image": { "source": { "imageUri": "gs://cloud-samples-data/vision/face/faces.jpeg" } } } ] }