Annotating images

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.

The maximum lifespan for a custom model is two years. You must create and train a new model to continue classifying content after that amount of time.

Using curl

To make it more convenient to run the curl samples in this topic, set the following environment variable. Replace project-id with the name of your GCP project.

export PROJECT_ID="project-id"

Predict Example

Web UI

  1. Open the AutoML Vision UI 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.

  2. Click the row for the model you want to use to label your images.

  3. Click the Predict tab just below the title bar.

  4. Click Upload Images to upload the images that you want to label.

Command-line

To make a prediction for an image, supply the contents of your image in the imageBytes field. The contents of your image must be base64-encoded. For information on base64-encoding the contents of an image, see Base64 Encoding.

The following example requests a prediction for an image.

  • Replace model-id with the ID of your model. The ID is the last element of the name of your model. For example, if the name of your model is projects/434039606874/locations/us-central1/models/3745331181667467569, then the ID of your model is 3745331181667467569.
curl -X POST \
  -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
  -H "Content-Type: application/json" \
  https://automl.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/models/model-id:predict \
  -d '{
        "payload" : {
          "image": {
            "imageBytes" : "/9j/4AAQSkZJRgABAQAAAQ … "
          },
        }
      }'

You should see output similar to the following:

{
  "payload": [
    {
      "displayName": "Lily",
      "classification": {
        "score": 0.8989502
      }
    },
    {
      "displayName": "Daisy",
      "classification": {
        "score": 0.10098731
      }
    },
    {
      "displayName": "--other--",
      "classification": {
        "score": 6.2490464e-05
      }
    }
  ]
}

The predictions from your AutoML Vision model are contained in the payload field.

  • displayName is the label predicted by the AutoML Vision model.
  • score represents a confidence level that the specified label applies to the image. It ranges from 0 (no confidence) to 1 (high confidence).

Python

Before you can run this code example, you must install the Python Client Libraries.

  • The model_full_id parameter is the full name of your model. For example: projects/434039606874/locations/us-central1/models/3745331181667467569.
def automl_predict(model_full_id):
  """ # prediction """
  prediction_client = automl_v1beta1.PredictionServiceClient()
  # TODO replace file_path with your test file path
  file_path = 'IMG_test.jpg'
  with open(file_path, 'rb') as ff:
    content = ff.read()
  payload = {'image': {
    'image_bytes': content
        }
  }
  params = {}
  request = prediction_client.predict(model_full_id, payload, params)
  print('\nPrediction results: {}', response)

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud AutoML Vision