Quickstart: Using the command line

This quickstart walks you through the process of:

  • Copying a set of videos into Google Cloud Storage.
  • Creating CSV files that list videos and their labels.
  • Using AutoML Video to create your dataset, and train and use your model.

Before you begin

Set up your project

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. In the GCP Console, on the project selector page, select or create a GCP project.

    Go to the Project selector page

  3. Make sure that billing is enabled for your Google Cloud Platform project.

    Learn how to enable billing

  4. Enable the Cloud AutoML and Storage APIs.

    Enable the APIs

  5. Install the gcloud command line tool.
  6. Follow the instructions to create a service account and download a key file for that account.
  7. Set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path to the service account key file that you downloaded when you created the service account.
    export GOOGLE_APPLICATION_CREDENTIALS=key-file
  8. Set the PROJECT_ID environment variable to your Project ID.
    export PROJECT_ID=your-project-id
    The AutoML API calls and resource names include your Project ID in them. The PROJECT_ID environment variable provides a convenient way to specify the ID.
  9. Add yourself and your service account to the AutoML Editor IAM role.
    1. Replace your-userid@your-domain with your user account.
    2. Replace service-account-name with the name of your new service account, for example service-account1@myproject.iam.gserviceaccount.com.
    gcloud auth login
    gcloud projects add-iam-policy-binding $PROJECT_ID \
       --member="user:your-userid@your-domain" \
       --role="roles/automl.admin"
    gcloud projects add-iam-policy-binding $PROJECT_ID \
       --member="serviceAccount:service-account-name" \
       --role="roles/automl.editor"
    

Create a dataset and import training data

Create a dataset

Decide on a name for your dataset and use the following curl command to create a new dataset with that name.

The curl command uses the gcloud auth application-default print-access-token command to obtain an access token for a service account that you set up earlier in the topic and. The path to the service account key file is stored in the GOOGLE_APPLICATION_CREDENTIALS environment variable.

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/datasets -d '{
    "displayName": "your-dataset-name",
    "videoClassificationDatasetMetadata": { }
}'

You should receive a response that contains the dataset id for your new dataset. For example: VCN4798585402963263488.

{
  "name": "projects/my-project/locations/us-central1/datasets/VCN4798585402963263488",
  "displayName": "my_dataset_01",
  "createTime": "2018-10-18T21:18:13.975412Z",
  "videoClassificationDatasetMetadata": {}
}

Import training data

Import the sample training data into your dataset. The importData command takes, as input, the path to the CSV that contains paths to the training and test data CSV files. These files are made available in the "automl-video-demo-data" bucket on Google Cloud Storage.

  • Replace your-dataset-id with the dataset identifier for your dataset (not the display name). For example: VCN4798585402963263488.
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/datasets/your-dataset-id:importData \
-d '{
  "inputConfig": {
    "gcsSource": {
       "inputUris": ["gs://automl-video-demo-data/hmdb_split1.csv"]
     }
  }
}'

You should receive an operation id for your import data operation. For example: VCN7506374678919774208.

{
  "name": "projects/915378992997/locations/us-central1/operations/VCN7506374678919774208",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.automl.v1beta1.OperationMetadata",
    "createTime": "2018-10-18T21:23:15.181451Z",
    "updateTime": "2018-10-18T21:23:15.181451Z",
    "importDataDetails": {}
  }
}

Get the status of the import operation

You can query the status of your import data operation by using the following curl command.

  • Replace your-operation-id with the operation id for your import data operation.
curl -X GET -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/operations/your-operation-id

The import operation can take some time to complete. When the import task is finished, you will see done: true in the status of the operation with no errors listed, as shown in the following example.

{
  "name": "projects/915378992997/locations/us-central1/operations/VCN7506374678919774208",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.automl.v1beta1.OperationMetadata",
    "createTime": "2018-10-18T21:23:15.181451Z",
    "updateTime": "2018-10-18T21:26:10.354502Z",
    "importDataDetails": {}
  },
  "done": true,
  "response": {
    "@type": "type.googleapis.com/google.protobuf.Empty"
  }
}

Get the list of datasets

You can get your list of datasets and the number of sample videos that were imported into the dataset by using the following command.

curl \
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
https://automl.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/datasets

You should see output similar to the following:

{
  "datasets": [
    {
      "name": "projects/my-project/locations/us-central1/datasets/VCN3940649673949184000",
      "displayName": "my_dataset_01",
      "createTime": "2018-10-18T21:18:13.975412Z",
      "exampleCount": 5097,
      "videoClassificationDatasetMetadata": {}
    }
  ]
}

Train your model

Launch a model training operation

After you have created your dataset and imported your training data into your dataset, you can train your custom model.

Train your model by using the following curl command.

  • Replace your-dataset-id with the dataset identifier for your dataset (not the display name).

  • Replace your-model-name with a name that you choose for your model.

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 -d '{
  "datasetId": "your-dataset-id",
  "displayName": "your-model-name",
  "videoClassificationModelMetadata": {},
}'

You should receive an operation id for your import data operation. For example: VCN1741767155885539328.

{
  "name": "projects/915378992997/locations/us-central1/operations/VCN1741767155885539328",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.automl.v1beta1.OperationMetadata",
    "createTime": "2018-10-18T21:32:06.730258Z",
    "updateTime": "2018-10-18T21:32:06.730258Z",
    "createModelDetails": {}
  }
}

Get the status of the model training operation

You can query the status of your model training operation by using the following curl command.

  • Replace your-operation-id with the operation id for your training operation.
curl -X GET -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/operations/your-operation-id

You should see output similar to the following. When the operation is complete, you will see done: true with no errors listed.

{
  "name": "projects/915378992997/locations/us-central1/operations/VCN1741767155885539328",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.automl.v1beta1.OperationMetadata",
    "createTime": "2018-10-18T21:32:06.730258Z",
    "updateTime": "2018-10-18T21:32:06.730258Z",
    "createModelDetails": {}
  }
}

Verify that the model is available

After your model training operation successfully completes, you can verify that your model is available by using the following command to list the models for your project.

curl -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
https://automl.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/models

You should see output similar to the following:

{
  "model": [
    {
      "name": "projects/915378992997/locations/us-central1/models/VCN2004101834179870720",
      "displayName": "my_model_01",
      "datasetId": "VCN3940649673949184000",
      "createTime": "2018-10-19T17:18:45.320701Z",
      "updateTime": "2018-10-19T17:18:45.320701Z",
      "videoClassificationModelMetadata": {}
    }
  ]
}

Make a prediction

You can request annotations (predictions) for videos by using the batchPredict command. The batchPredict command takes, as input, a CSV file stored in your Google Cloud Storage bucket that contains the paths to the videos to annotate, and the start and end times that identify the segment of video to annotate. For this quickstart, this CSV file is named hmdb_split1_train_gs.csv.

Run the following command to make a batch (asynchronous) predict request.

  • Replace your-model-id with the identifier for your model.

  • Replace your-output-bucket with a Google Cloud Storage bucket that will contain the results of your prediction.

  • Replace your-object-id with an object name that identifies where to store the output of your prediction request in your Google Cloud Storage bucket. You must have write permissions to the Google Cloud Storage bucket.

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/your-model-id:batchPredict -d '{
  "inputConfig": {
    "gcsSource": {
      "inputUris": ["gs://automl-video-demo-data/hmdb_split1_test_gs_predict.csv"]
    }
  },
  "outputConfig": {
    "gcsDestination": {
      "outputUriPrefix": "gs://your-output-bucket/your-object-id"
    }
  }
}'

You should receive an operation id for your batch predict request. For example: VCN926615623331479552.

{
  "name": "projects/915378992997/locations/us-central1/operations/VCN926615623331479552",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.automl.v1beta1.OperationMetadata",
    "createTime": "2018-10-19T21:22:27.003694Z",
    "updateTime": "2018-10-19T21:22:27.003694Z",
    "batchPredictDetails": {}
  }
}

Get the status of the predict operation

You can query the status of your batch predict operation by using the following curl command.

  • Replace your-operation-id with the operation id for your batch predict operation.
curl -X GET -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/operations/your-operation-id

Depending on the number of videos that you specified in your CSV file, the batch predict task can take some time to complete. When the task is finished, you will see done: true in the status of the operation with no errors listed, as shown in the following example.

{
  "name": "projects/915378992997/locations/us-central1/operations/VCN926615623331479552",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.automl.v1beta1.OperationMetadata",
    "createTime": "2018-10-18T21:23:15.181451Z",
    "updateTime": "2018-10-18T21:26:10.354502Z",
    "importDataDetails": {}
  },
  "done": true,
  "response": {
    "@type": "type.googleapis.com/google.protobuf.Empty"
  }
}

When the batch predict task is complete, the output of the prediction is stored in the Google Cloud Storage bucket that you specified in your command. There is a JSON file for each video segment. For example:

my-video-01.avi.json

{
  "input_uri": "automl-video-demo-data/sample_video.avi",
  "segment_classification_annotations": [ {
    "annotation_spec": {
      "display_name": "ApplyLipstick",
      "description": "ApplyLipstick"
    },
    "segments": [ {
      "segment": {
        "start_time_offset": {
        },
        "end_time_offset": {
          "seconds": 4,
          "nanos": 960000000
        }
      },
      "confidence": 0.43253016
    }, {
      "segment": {
        "start_time_offset": {
        },
        "end_time_offset": {
          "seconds": 4,
          "nanos": 960000000
        }
      },
      "confidence": 0.56746984
    } ],
    "frames": [ ]
  } ],
  "shot_classification_annotations": [ {
    "annotation_spec": {
      "display_name": "ApplyLipstick",
      "description": "ApplyLipstick"
    },
    "segments": [ {
      "segment": {
        "start_time_offset": {
        },
        "end_time_offset": {
          "seconds": 5
        }
      },
      "confidence": 0.43253016
    }, {
      "segment": {
        "start_time_offset": {
        },
        "end_time_offset": {
          "seconds": 5
        }
      },
      "confidence": 0.56746984
    } ],
    "frames": [ ]
  } ],
  "one_second_sliding_window_classification_annotations": [ {
    "annotation_spec": {
      "display_name": "ApplyLipstick",
      "description": "ApplyLipstick"
    },
    "segments": [ ],
    "frames": [ {
      "time_offset": {
        "nanos": 800000000
      },
      "confidence": 0.54533803
    }, {
      "time_offset": {
        "nanos": 800000000
      },
      ...
      "confidence": 0.57945728
    }, {
      "time_offset": {
        "seconds": 4,
        "nanos": 300000000
      },
      "confidence": 0.42054281
    } ]
  } ],
  "object_annotations": [ ],
  "error": {
    "details": [ ]
  }
}

Cleanup

If you no longer need your custom model and the related dataset, you can delete them.

List models

You can list the models for your project, along with their identifiers, by using the following command:

curl -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
https://automl.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/models

Delete a model

You can delete a model by using the following command.

  • Replace your-model-id with the identifier for your model.
curl -X DELETE -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/your-model-id

List datasets

You can list the datasets for your project, along with their identifiers, by using the following command:

curl -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
https://automl.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/datasets

Delete a dataset

You can delete a dataset by using the following command.

  • Replace your-dataset-id with the identifier for your model.
curl -X DELETE -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/datasets/your-dataset-id
Var denne side nyttig? Giv os en anmeldelse af den:

Send feedback om...

AutoML Video Intelligence