After you have created (trained) a model, you can request a prediction for an
image using the predict
method. The predict
method applies labels to your image based on the primary
object of the image that your model predicts.
Online (individual) prediction
This section describes sending an individual file for annotation. This request will immediately return a response.
You can also send a batch of files for annotation. Batch file annotation is a long-running operation, and stores results in a Cloud Storage bucket of your choosing.
Web UI
Open the Vision Dashboard and click the lightbulb icon in the left navigation bar to display the available models.
To view the models for a different project, select the project from the drop-down list in the upper right of the title bar.
Click the row for the model you want to use to label your images.
Click the Test & Use tab just below the title bar.
Click Upload Images to upload the images that you want to label.
REST
To test prediction you must first deploy your Cloud-hosted model.
Before using any of the request data, make the following replacements:
- project-id: your GCP project ID.
- model-id: the ID of your model, from the
response when you created the model. The ID is the last element of the name of your model.
For example:
- model name:
projects/project-id/locations/location-id/models/IOD4412217016962778756
- model id:
IOD4412217016962778756
- model name:
- base64-encoded-image: the base64
representation (ASCII string) of your binary image data. This string should look similar to the
following string:
/9j/4QAYRXhpZgAA...9tAVx/zDQDlGxn//2Q==
. Visit the base64 encode topic for more information.
Field-specific considerations:
scoreThreshold
- A value from 0 to 1. Only values with score thresholds of at least this value will be displayed. The default value is 0.5.
HTTP method and URL:
POST https://automl.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/models/MODEL_ID:predict
Request JSON body:
{ "payload": { "image": { "imageBytes": "BASE64_ENCODED_IMAGE" } }, "params": { "scoreThreshold": "0.5" } }
To send your request, choose one of these options:
curl
Save the request body in a file called request.json
,
and execute the following command:
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://automl.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/models/MODEL_ID:predict"
PowerShell
Save the request body in a file called request.json
,
and execute the following command:
$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://automl.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/models/MODEL_ID:predict" | Select-Object -Expand Content
Output is returned in JSON form. The predictions from your
AutoML Vision model are contained in the payload
field:
displayName
is the object's label predicted by the AutoML Vision model.score
represents a confidence level that the specified label applies to the image. It ranges from0
(no confidence) to1
(high confidence).
{ "payload": [ { "annotationSpecId": "7922029656637702144", "classification": { "score": 0.9960259 }, "displayName": "roses" } ] }
Go
Before trying this sample, follow the setup instructions for this language on the Client Libraries page.
Java
Before trying this sample, follow the setup instructions for this language on the Client Libraries page.
Node.js
Before trying this sample, follow the setup instructions for this language on the Client Libraries page.
Python
Before trying this sample, follow the setup instructions for this language on the Client Libraries page.
Additional languages
C#: Please follow the C# setup instructions on the client libraries page and then visit the AutoML Vision reference documentation for .NET.
PHP: Please follow the PHP setup instructions on the client libraries page and then visit the AutoML Vision reference documentation for PHP.
Ruby: Please follow the Ruby setup instructions on the client libraries page and then visit the AutoML Vision reference documentation for Ruby.