Objectives
In this sample, you'll use the Google Vision API to detect faces in an image. To prove to yourself that the faces were detected correctly, you'll then use that data to draw a box around each face.
Costs
In this document, you use the following billable components of Google Cloud:
- Cloud Vision
To generate a cost estimate based on your projected usage,
use the pricing calculator.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Google Cloud Vision API.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Google Cloud Vision API.
- Set up your environment for using Application Default Credentials.
-
Set up language-specific tasks and tools:
C#
- Install the Google Client Library
- Install Visual Studio 2012/2015.
Java
- Install Java.
- API reference.
Download and install the Apache Maven build system. Maven will ensure that the Google API Client Library and the Vision API client libraries are installed when the project is built, because we have included them in the
pom.xml
....
Node.js
- Install the Google Client Library
- Install node.js.
- API reference.
Install npm and node-canvas. The sample code includes a
package.json
to install all dependencies using the command:npm install
. Note that node-canvas has additional dependencies you may need to install - see the node-canvas installation doc for more information.
PHP
- Install the Google Client Library
- Install PHP and Composer.
- API reference.
- Install and enable GD for PHP.
Python
- Install the Google Client Library
- Install Python.
- API reference.
- Install pillow.
Ruby
- Install the Google Client Library
- Install Ruby and Bundler.
- API reference.
- Install ImageMagick.
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
Python
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.