Objectives
In this sample, you'll use the Google Cloud 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
This tutorial uses billable components of Cloud Platform, including:
- Google Cloud Vision API
Use the Pricing Calculator to generate a cost estimate based on your projected usage. New Cloud Platform users might be eligible for a free trial.
Before you begin
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
-
Select or create a GCP project.
-
Make sure that billing is enabled for your 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.
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:
C#
Node.js
PHP
...
Python
Ruby
...
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:
C#
Node.js
PHP
Python
Ruby
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:
C#
Node.js
We use the node-canvas library to draw onto images.
PHP
We use the GD extension to draw onto images.
Python
Ruby
We use the rmagick gem to draw onto images.
...
Put it all together
C#
To build the sample, open the Solution file dotnet-doc-samples/vision/api/Vision.sln
in Visual Studio and build the solution.
To run the sample:
C:\...\> cd dotnet-docs-samples\vision\api\DetectFaces\bin\Debug
C:\...\bin\Debug> DetectFaces ..\..\..\VisionTest\data\face.png
Node.js
To run the sample, run the following command from the sample code directory:
node faceDetection face.png
PHP
To run the sample, run the following command from the sample code directory:
composer install php vision.php face images/face.png output-image.png
Python
Ruby
To run the sample, run the following command from the sample code directory:
bundle install bundle exec ruby draw_box_around_faces.rb images/face.png output-image.png
Cleaning up
To avoid incurring charges to your Google Cloud Platform account for the resources used in this tutorial:
- In the GCP Console, go to the Projects page.
-
In the project list, select the project you
want to delete and click Delete project.
- In the dialog, type the project ID, and then click Shut down to delete the project.