Create the service object
To access Google APIs using the official client SDKs, you create a service object based on the API's discovery document, which describes the API to the SDK. You'll need to fetch it from the Vision API's discovery service, using your credentials:
Java
Node.js
Python
Send a face detection request
To construct a request to the Vision API, first consult the API
documentation. In this case, you'll be asking the images
resource
to annotate
your image. A request to this API takes the form of an object with
a requests
list. Each item in this list contains two bits of information:
- The base64-encoded image data
- A list of features you'd like annotated about that image.
For this example, you'll simply request FACE_DETECTION
annotation on one
image, and return the relevant portion of the response:
Java
Node.js
Python
Process the response
Congratulations - you've detected the faces in your image! The response to our face annotation request includes a bunch of metadata about the detected faces, which include coordinates of a polygon encompassing the face. At this point, though, this is only a list of numbers. Let's use them to confirm that you have, in fact, found the faces in your image. We'll draw polygons onto a copy of the image, using the coordinates returned by the Vision API:
Put it all together
Java
...
To build and run the sample, run the following commands from the sample code directory:
mvn clean compile assembly:single java -cp target/vision-face-detection-1.0-SNAPSHOT-jar-with-dependencies.jar \ com.google.cloud.vision.samples.facedetect.FaceDetectApp \ data/face.jpg \ output.jpg
Node.js
To run the sample, run the following command from the sample code directory:
node faceDetection resources/face.png