From 163f6b1a23cd3cefb93ceda2e585e4a9df102324 Mon Sep 17 00:00:00 2001 From: Nirupa Anantha Kumar Date: Fri, 5 Apr 2019 12:20:23 -0700 Subject: [PATCH] docs(samples): add video-intelligence beta samples (#156) * Adding video-intelligence beta samples * Skipping tests till fixed --- ...genceClassificationDataset.v1beta1.test.js | 77 +++++++++++ ...ligenceClassificationModel.v1beta1.test.js | 102 ++++++++++++++ ...genceClassificationPredict.v1beta1.test.js | 45 ++++++ .../classification/create-dataset.v1beta1.js | 68 +++++++++ .../classification/create-model.v1beta1.js | 59 ++++++++ .../classification/delete-dataset.v1beta1.js | 59 ++++++++ .../classification/delete-model.v1beta1.js | 59 ++++++++ .../display-evaluation.v1beta1.js | 129 ++++++++++++++++++ .../classification/export-data.v1beta1.js | 71 ++++++++++ .../classification/get-dataset.v1beta1.js | 62 +++++++++ .../get-model-evaluations.v1beta1.js | 129 ++++++++++++++++++ .../classification/get-model.v1beta1.js | 66 +++++++++ .../get-operation-status.v1beta1.js | 57 ++++++++ .../classification/import-data.v1beta1.js | 71 ++++++++++ .../classification/list-datasets.v1beta1.js | 66 +++++++++ .../list-model-evaluations.v1beta1.js | 126 +++++++++++++++++ .../classification/list-models.v1beta1.js | 70 ++++++++++ .../list-operation-status.v1beta1.js | 66 +++++++++ .../classification/predict.v1beta1.js | 79 +++++++++++ 19 files changed, 1461 insertions(+) create mode 100644 automl/test/automlVideoIntelligenceClassificationDataset.v1beta1.test.js create mode 100644 automl/test/automlVideoIntelligenceClassificationModel.v1beta1.test.js create mode 100644 automl/test/automlVideoIntelligenceClassificationPredict.v1beta1.test.js create mode 100644 automl/video-intelligence/classification/create-dataset.v1beta1.js create mode 100644 automl/video-intelligence/classification/create-model.v1beta1.js create mode 100644 automl/video-intelligence/classification/delete-dataset.v1beta1.js create mode 100644 automl/video-intelligence/classification/delete-model.v1beta1.js create mode 100644 automl/video-intelligence/classification/display-evaluation.v1beta1.js create mode 100644 automl/video-intelligence/classification/export-data.v1beta1.js create mode 100644 automl/video-intelligence/classification/get-dataset.v1beta1.js create mode 100644 automl/video-intelligence/classification/get-model-evaluations.v1beta1.js create mode 100644 automl/video-intelligence/classification/get-model.v1beta1.js create mode 100644 automl/video-intelligence/classification/get-operation-status.v1beta1.js create mode 100644 automl/video-intelligence/classification/import-data.v1beta1.js create mode 100644 automl/video-intelligence/classification/list-datasets.v1beta1.js create mode 100644 automl/video-intelligence/classification/list-model-evaluations.v1beta1.js create mode 100644 automl/video-intelligence/classification/list-models.v1beta1.js create mode 100644 automl/video-intelligence/classification/list-operation-status.v1beta1.js create mode 100644 automl/video-intelligence/classification/predict.v1beta1.js diff --git a/automl/test/automlVideoIntelligenceClassificationDataset.v1beta1.test.js b/automl/test/automlVideoIntelligenceClassificationDataset.v1beta1.test.js new file mode 100644 index 0000000000..aef6ccb483 --- /dev/null +++ b/automl/test/automlVideoIntelligenceClassificationDataset.v1beta1.test.js @@ -0,0 +1,77 @@ +/** + * Copyright 2019, Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const {assert} = require('chai'); +const execa = require('execa'); + +/** Tests for AutoML Video Intelligence Classification "Dataset API" sample. */ + +const cmdDataset = 'node automlVideoIntelligenceDataset.js'; + +// TODO(developer): Before running the test cases, +// set the environment variables PROJECT_ID, REGION_NAME and +// change the value of datasetId +const projectId = process.env.PROJECT_ID; +//const computeRegion = process.env.REGION_NAME; +const bucket = projectId + '-video'; +const datasetName = 'test_video_dataset'; +const filter = 'videoClassificationDatasetMetadata:*'; +const datasetId = 'VCN1802794449273618432'; +const importDataCsv = 'gs://automl-video-demo-data/hmdb_split1.csv'; + +const exec = async cmd => (await execa.shell(cmd)).stdout; + +describe.skip(`DatasetAPI`, () => { + it(`should create, import and delete a dataset`, async () => { + // Create dataset + let output = await exec(`${cmdDataset} create-dataset "${datasetName}"`); + const parsedOut = output.split('\n'); + const outputDatasetId = parsedOut[1].split(':')[1].trim(); + assert.match(output, /Dataset display name:/); + + // Import data + output = await exec( + `${cmdDataset} import-data "${outputDatasetId}" "${importDataCsv}"` + ); + assert.match(output, /Processing import.../); + + // Delete dataset + output = await exec(`${cmdDataset} delete-dataset "${outputDatasetId}"`); + assert.match(output, /Dataset delete details:/); + }); + + it(`should list datasets`, async () => { + // List dataset + const output = await exec(`${cmdDataset} list-datasets "${filter}"`); + assert.match(output, /List of datasets:/); + }); + + it(`should get preexisting dataset`, async () => { + // Get dataset + const output = await exec(`${cmdDataset} get-dataset "${datasetId}"`); + assert.match(output, /Dataset display name:/); + }); + + it(`should export dataset`, async () => { + // Export data + const outputUri = 'gs://' + bucket + '/' + datasetId; + const output = await exec( + `${cmdDataset} export-data "${datasetId}" "${outputUri}"` + ); + assert.match(output, /Processing export.../); + }); +}); diff --git a/automl/test/automlVideoIntelligenceClassificationModel.v1beta1.test.js b/automl/test/automlVideoIntelligenceClassificationModel.v1beta1.test.js new file mode 100644 index 0000000000..86ed0a55c8 --- /dev/null +++ b/automl/test/automlVideoIntelligenceClassificationModel.v1beta1.test.js @@ -0,0 +1,102 @@ +/** + * Copyright 2019, Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const {assert} = require('chai'); +const execa = require('execa'); + +/** Tests for AutoML Video Intelligence Classification "Model API" sample. */ + +const cmdModel = 'node automlVideoIntelligenceModel.js'; + +// TODO(developer): Before running the test cases, +// set the environment variables PROJECT_ID, REGION_NAME and +// change the values of datasetId +//const projectId = process.env.PROJECT_ID; +//const computeRegion = process.env.REGION_NAME; +const filter = 'videoClassificationModelMetadata:*'; +const datasetId = 'VCN1653190499151904768'; +const testModelName = 'test_video_model'; + +const exec = async cmd => (await execa.shell(cmd)).stdout; + +describe.skip(`Video Intelligence ModelAPI`, () => { + it(`should create a model`, async () => { + // Create model + let output = await exec( + `${cmdModel} create-model "${datasetId}" "${testModelName}"` + ); + const operationName = output + .split('\n')[0] + .split(':')[1] + .trim(); + assert.match(output, /Training started.../); + + output = await exec(`${cmdModel} get-operation-status "${operationName}"`); + assert.match(output, /Operation details:/); + }); + + it(`should list models, get and delete a model. list, get and display model + evaluations from preexisting models`, async () => { + // List models + let output = await exec(`${cmdModel} list-models "${filter}"`); + const parsedOut = output.split('\n'); + const ouputModelId = parsedOut[3].split(':')[1].trim(); + assert.match(output, /List of models:/); + + // Get model + output = await exec(`${cmdModel} get-model "${ouputModelId}"`); + assert.match(output, /Model name:/); + + // List model evaluations + output = await exec(`${cmdModel} list-model-evaluations "${ouputModelId}"`); + const parsedModelEvaluation = output.split('\n'); + const modelEvaluationId = parsedModelEvaluation[3].split(':')[1].trim(); + assert.match(output, /Model evaluation Id:/); + + // Get model evaluation + output = await exec( + `${cmdModel} get-model-evaluation "${ouputModelId}" ` + + `"${modelEvaluationId}"` + ); + assert.match(output, /Model evaluation Id:/); + + // Display evaluation + output = await exec(`${cmdModel} display-evaluation "${ouputModelId}"`); + assert.match(output, /Model Evaluation ID:/); + + // Delete model + output = await exec(`${cmdModel} delete-model "${ouputModelId}"`); + assert.match(output, /Model delete details:/); + }); + + it(`should list and get operation status`, async () => { + // List operation status + let output = await exec(`${cmdModel} list-operations-status`); + const operationFullId = output + .split('\n')[3] + .split(':')[1] + .trim(); + assert.match(output, /Operation details:/); + + // Get operation status + // Poll operation status, here confirming that operation is not complete yet + output = await exec( + `${cmdModel} get-operation-status "${operationFullId}"` + ); + assert.match(output, /Operation details:/); + }); +}); diff --git a/automl/test/automlVideoIntelligenceClassificationPredict.v1beta1.test.js b/automl/test/automlVideoIntelligenceClassificationPredict.v1beta1.test.js new file mode 100644 index 0000000000..4613089e8a --- /dev/null +++ b/automl/test/automlVideoIntelligenceClassificationPredict.v1beta1.test.js @@ -0,0 +1,45 @@ +/** + * Copyright 2019, Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const {assert} = require('chai'); +const execa = require('execa'); + +/** Tests for AutoML Video Intelligence Classification "Prediction API" sample. + */ + +const cmdPredict = 'node automlVideoIntelligencePrediction.js'; + +// TODO(developer): Before running the test cases, +// set the environment variables PROJECT_ID, REGION_NAME and +// change the values of modelId, inputUri and outputUriPrefix +//const projectId = process.env.PROJECT_ID; +//const computeRegion = process.env.REGION_NAME; +const modelId = 'VCN5018751611309129728'; +const inputUri = 'gs://video-intelligence/input-csv/annotateVideo.csv'; +const outputUriPrefix = 'gs://video-intelligence/'; + +const exec = async cmd => (await execa.shell(cmd)).stdout; + +describe.skip(`Video Intelligence PredictionAPI`, () => { + it(`should run prediction from preexisting model`, async () => { + // Run prediction on 'annotate_video.csv' from gcs inputUri + const output = await exec( + `${cmdPredict} predict "${modelId}" "${inputUri}" "${outputUriPrefix}"` + ); + assert.match(output, /Operation name:/); + }); +}); diff --git a/automl/video-intelligence/classification/create-dataset.v1beta1.js b/automl/video-intelligence/classification/create-dataset.v1beta1.js new file mode 100644 index 0000000000..39f2be02d9 --- /dev/null +++ b/automl/video-intelligence/classification/create-dataset.v1beta1.js @@ -0,0 +1,68 @@ +/** + * Copyright 2019, Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetName = 'YOUR_DATASET_NAME' +) { + // [START automl_video_intelligence_classification_create_dataset] + const automl = require(`@google-cloud/automl`); + const util = require(`util`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to create a dataset. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetName = '[DATASET_NAME]' e.g., "myDataset”; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // Set dataset name and metadata. + const myDataset = { + displayName: datasetName, + videoClassificationDatasetMetadata: {}, + }; + + // Create a dataset with the dataset metadata in the region. + client + .createDataset({parent: projectLocation, dataset: myDataset}) + .then(responses => { + const dataset = responses[0]; + + // Display the dataset information. + console.log(`Dataset name: ${dataset.name}`); + console.log(`Dataset Id: ${dataset.name.split(`/`).pop(-1)}`); + console.log(`Dataset display name: ${dataset.displayName}`); + console.log(`Dataset example count: ${dataset.exampleCount}`); + console.log( + `Video classification dataset metadata: ${util.inspect( + dataset.videoClassificationDatasetMetadata, + false, + null + )}` + ); + }) + .catch(err => { + console.error(err); + }); + // [END automl_video_intelligence_classification_create_dataset] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/video-intelligence/classification/create-model.v1beta1.js b/automl/video-intelligence/classification/create-model.v1beta1.js new file mode 100644 index 0000000000..d43811b345 --- /dev/null +++ b/automl/video-intelligence/classification/create-model.v1beta1.js @@ -0,0 +1,59 @@ +/** + * Copyright 2019, Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID', + modelName = 'MODEL_NAME' +) { + // [START automl_video_intelligence_classification_create_model] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to create a model. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g., "VCN7209576908164431872"; + // const modelName = '[MODEL_NAME]' e.g., "myModel"; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // Set datasetId, model name and model metadata for the dataset. + const myModel = { + displayName: modelName, + datasetId: datasetId, + videoClassificationModelMetadata: {}, + }; + + // Create a model with the model metadata in the region. + client + .createModel({parent: projectLocation, model: myModel}) + .then(responses => { + const initialApiResponse = responses[1]; + console.log(`Training operation name: ${initialApiResponse.name}`); + console.log(`Training started...`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_video_intelligence_classification_create_model] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/video-intelligence/classification/delete-dataset.v1beta1.js b/automl/video-intelligence/classification/delete-dataset.v1beta1.js new file mode 100644 index 0000000000..43b13a10fb --- /dev/null +++ b/automl/video-intelligence/classification/delete-dataset.v1beta1.js @@ -0,0 +1,59 @@ +/** + * Copyright 2019, Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID' +) { + // [START automl_video_intelligence_classification_delete_dataset] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to delete a dataset. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g., "VCN7209576908164431872"; + + // Get the full path of the dataset. + const datasetFullId = client.datasetPath(projectId, computeRegion, datasetId); + + // Delete a dataset. + client + .deleteDataset({name: datasetFullId}) + .then(responses => { + const operation = responses[0]; + return operation.promise(); + }) + .then(responses => { + // The final result of the operation. + const operationDetails = responses[2]; + + // Get the dataset delete details. + console.log('Dataset delete details:'); + console.log(`\tOperation details:`); + console.log(`\t\tName: ${operationDetails.name}`); + console.log(`\t\tDone: ${operationDetails.done}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_video_intelligence_classification_delete_dataset] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/video-intelligence/classification/delete-model.v1beta1.js b/automl/video-intelligence/classification/delete-model.v1beta1.js new file mode 100644 index 0000000000..59eed47187 --- /dev/null +++ b/automl/video-intelligence/classification/delete-model.v1beta1.js @@ -0,0 +1,59 @@ +/** + * Copyright 2019, Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID' +) { + // [START automl_video_intelligence_classification_delete_model] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to delete a model. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "VCN7209576908164431872"; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Delete a model. + client + .deleteModel({name: modelFullId}) + .then(responses => { + const operation = responses[0]; + return operation.promise(); + }) + .then(responses => { + // The final result of the operation. + const operationDetails = responses[2]; + + // Get the Model delete details. + console.log('Model delete details:'); + console.log(`\tOperation details:`); + console.log(`\t\tName: ${operationDetails.name}`); + console.log(`\tDone: ${operationDetails.done}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_video_intelligence_classification_delete_model] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/video-intelligence/classification/display-evaluation.v1beta1.js b/automl/video-intelligence/classification/display-evaluation.v1beta1.js new file mode 100644 index 0000000000..fdccb56ffe --- /dev/null +++ b/automl/video-intelligence/classification/display-evaluation.v1beta1.js @@ -0,0 +1,129 @@ +/** + * Copyright 2019, Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID', + filter = 'FILTER_EXPRESSION' +) { + // [START automl_video_intelligence_classification_display_evaluation] + const automl = require(`@google-cloud/automl`); + const math = require(`mathjs`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to display model evaluation. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "VCN7209576908164431872"; + // const filter_ = '[FILTER_EXPRESSIONS]' + // e.g., "videoClassificationModelMetadata:*"; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // List all the model evaluations in the model by applying filter. + client + .listModelEvaluations({parent: modelFullId, filter: filter}) + .then(respond => { + const response = respond[0]; + + // Iterate through the results. + let modelEvaluationId = ``; + for (const element of response) { + // There is evaluation for each class in a model and for overall model. + // Get only the evaluation of overall model. + if (!element.annotationSpecId) { + modelEvaluationId = element.name.split(`/`).pop(-1); + } + } + console.log(`Model Evaluation ID: ${modelEvaluationId}`); + + // Resource name for the model evaluation. + const modelEvaluationFullId = client.modelEvaluationPath( + projectId, + computeRegion, + modelId, + modelEvaluationId + ); + + // Get a model evaluation. + client + .getModelEvaluation({name: modelEvaluationFullId}) + .then(responses => { + const modelEvaluation = responses[0]; + const classMetrics = modelEvaluation.classificationEvaluationMetrics; + const confidenceMetricsEntries = classMetrics.confidenceMetricsEntry; + + // Showing model score based on threshold of 0.5 + for (const confidenceMetricsEntry of confidenceMetricsEntries) { + if (confidenceMetricsEntry.confidenceThreshold === 0.5) { + console.log( + `Precision and recall are based on a ` + + `score threshold of 0.5` + ); + console.log( + `Model precision: ${math.round( + confidenceMetricsEntry.precision * 100, + 2 + )} %` + ); + console.log( + `Model recall: ${math.round( + confidenceMetricsEntry.recall * 100, + 2 + )} %` + ); + console.log( + `Model f1 score: ${math.round( + confidenceMetricsEntry.f1Score * 100, + 2 + )} %` + ); + console.log( + `Model precision@1: ${math.round( + confidenceMetricsEntry.precisionAt1 * 100, + 2 + )} %` + ); + console.log( + `Model recall@1: ${math.round( + confidenceMetricsEntry.recallAt1 * 100, + 2 + )} %` + ); + console.log( + `Model f1 score@1: ${math.round( + confidenceMetricsEntry.f1ScoreAt1 * 100, + 2 + )} %` + ); + } + } + }) + .catch(err => { + console.error(err); + }); + }) + .catch(err => { + console.error(err); + }); + // [END automl_video_intelligence_classification_display_evaluation] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/video-intelligence/classification/export-data.v1beta1.js b/automl/video-intelligence/classification/export-data.v1beta1.js new file mode 100644 index 0000000000..418c2bc387 --- /dev/null +++ b/automl/video-intelligence/classification/export-data.v1beta1.js @@ -0,0 +1,71 @@ +/** + * Copyright 2019, Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID', + gcsOutputUri = 'GCS_DIRECTORY' +) { + // [START automl_video_intelligence_classification_export_data] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to export a dataset to a + * Google Cloud Storage bucket. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g.,"VCN7209576908164431872"; + // const gcsOutputUri = '[GCS_OUTPUT_URI]' e.g., "gs:///”, + // `Google Cloud Storage URI for the export directory`; + + // Get the full path of the dataset. + const datasetFullId = client.datasetPath(projectId, computeRegion, datasetId); + + // Set the output URI + const outputConfig = { + gcsDestination: { + outputUriPrefix: gcsOutputUri, + }, + }; + + // Export the data to the output URI. + client + .exportData({name: datasetFullId, outputConfig: outputConfig}) + .then(responses => { + const operation = responses[0]; + console.log(`Processing export...`); + return operation.promise(); + }) + .then(responses => { + // The final result of the operation. + const operationDetails = responses[2]; + + // Get the data export details. + console.log('Data export details:'); + console.log(`\tOperation details:`); + console.log(`\t\tName: ${operationDetails.name}`); + console.log(`\t\tDone: ${operationDetails.done}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_video_intelligence_classification_export_data] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/video-intelligence/classification/get-dataset.v1beta1.js b/automl/video-intelligence/classification/get-dataset.v1beta1.js new file mode 100644 index 0000000000..04b2b54162 --- /dev/null +++ b/automl/video-intelligence/classification/get-dataset.v1beta1.js @@ -0,0 +1,62 @@ +/** + * Copyright 2019, Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID' +) { + // [START automl_video_intelligence_classification_get_dataset] + const automl = require(`@google-cloud/automl`); + const util = require(`util`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to get a dataset by ID. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g., "VCN7209576908164431872"; + + // Get the full path of the dataset. + const datasetFullId = client.datasetPath(projectId, computeRegion, datasetId); + + // Get all the information about a given dataset. + client + .getDataset({name: datasetFullId}) + .then(responses => { + const dataset = responses[0]; + + // Display the dataset information. + console.log(`Dataset name: ${dataset.name}`); + console.log(`Dataset Id: ${dataset.name.split(`/`).pop(-1)}`); + console.log(`Dataset display name: ${dataset.displayName}`); + console.log(`Dataset example count: ${dataset.exampleCount}`); + console.log( + `Video classification dataset metadata: ${util.inspect( + dataset.videoClassificationDatasetMetadata, + false, + null + )}` + ); + }) + .catch(err => { + console.error(err); + }); + // [END automl_video_intelligence_classification_get_dataset] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/video-intelligence/classification/get-model-evaluations.v1beta1.js b/automl/video-intelligence/classification/get-model-evaluations.v1beta1.js new file mode 100644 index 0000000000..d8cbefa104 --- /dev/null +++ b/automl/video-intelligence/classification/get-model-evaluations.v1beta1.js @@ -0,0 +1,129 @@ +/** + * Copyright 2019, Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID', + modelEvaluationId = 'MODEL_EVALUATION_ID' +) { + // [START automl_video_intelligence_classification_get_model_evaluation] + const automl = require(`@google-cloud/automl`); + const math = require(`mathjs`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to get model evaluations. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "VCN7209576908164431872"; + // const modelEvaluationId = '[MODEL_EVALUATION_ID]' + // e.g., "3806191078210741236"; + + // Get the full path of the model evaluation. + const modelEvaluationFullId = client.modelEvaluationPath( + projectId, + computeRegion, + modelId, + modelEvaluationId + ); + + // Get complete detail of the model evaluation. + client + .getModelEvaluation({name: modelEvaluationFullId}) + .then(responses => { + const response = responses[0]; + + const confidenceMetricsEntries = + response.classificationEvaluationMetrics.confidenceMetricsEntry; + + // Display the model evaluations information. + console.log(`\nModel evaluation name: ${response.name}`); + console.log( + `Model evaluation Id: ${response.name + .split(`/`) + .slice(-1) + .pop()}` + ); + console.log( + `Model evaluation annotation spec Id: ${response.annotationSpecId}` + ); + console.log(`Model evaluation display name: ${response.displayName}`); + console.log( + `Model evaluation example count: ${response.evaluatedExampleCount}` + ); + console.log(`Video classification evaluation metrics:`); + console.log( + `\tModel auPrc: ${math.round( + response.classificationEvaluationMetrics.auPrc, + 6 + )}` + ); + console.log(`\tConfidence metrics entries:`); + + for (const confidenceMetricsEntry of confidenceMetricsEntries) { + console.log( + `\t\tModel confidenceThreshold: ${math.round( + confidenceMetricsEntry.confidenceThreshold, + 6 + )}` + ); + console.log( + `\t\tModel recall: ${math.round( + confidenceMetricsEntry.recall * 100, + 2 + )} %` + ); + console.log( + `\t\tModel precision: ${math.round( + confidenceMetricsEntry.precision * 100, + 2 + )} %` + ); + console.log( + `\t\tModel f1 score: ${math.round( + confidenceMetricsEntry.f1Score * 100, + 2 + )} %` + ); + console.log( + `\t\tModel recall@1: ${math.round( + confidenceMetricsEntry.recallAt1 * 100, + 2 + )} %` + ); + console.log( + `\t\tModel precision@1: ${math.round( + confidenceMetricsEntry.precisionAt1 * 100, + 2 + )} %` + ); + console.log( + `\t\tModel f1 score@1: ${math.round( + confidenceMetricsEntry.f1ScoreAt1 * 100, + 2 + )} % \n` + ); + } + }) + .catch(err => { + console.error(err); + }); + // [END automl_video_intelligence_classification_get_model_evaluation] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/video-intelligence/classification/get-model.v1beta1.js b/automl/video-intelligence/classification/get-model.v1beta1.js new file mode 100644 index 0000000000..e88e179a57 --- /dev/null +++ b/automl/video-intelligence/classification/get-model.v1beta1.js @@ -0,0 +1,66 @@ +/** + * Copyright 2019, Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID' +) { + // [START automl_video_intelligence_classification_get_model] + const automl = require(`@google-cloud/automl`); + const util = require(`util`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to get model details. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "VCN7209576908164431872"; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Get complete detail of the model. + client + .getModel({name: modelFullId}) + .then(responses => { + const model = responses[0]; + + // Display the model information. + console.log(`Model name: ${model.name}`); + console.log(`Model Id: ${model.name.split(`/`).pop(-1)}`); + console.log(`Model display name: ${model.displayName}`); + console.log(`Dataset Id: ${model.datasetId}`); + + if (model.modelMetadata === `videoClassificationModelMetadata`) { + console.log( + `Video Classification Model Metadata: ${util.inspect( + model.videoClassificationModelMetadata, + false, + null + )}` + ); + } + console.log(`Model deployment state: ${model.deploymentState}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_video_intelligence_classification_get_model] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/video-intelligence/classification/get-operation-status.v1beta1.js b/automl/video-intelligence/classification/get-operation-status.v1beta1.js new file mode 100644 index 0000000000..bc994be285 --- /dev/null +++ b/automl/video-intelligence/classification/get-operation-status.v1beta1.js @@ -0,0 +1,57 @@ +/** + * Copyright 2019, Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +`use strict`; +async function main(operationFullId = 'OPERATION_FULL_ID') { + // [START automl_video_intelligence_classification_get_operation_status] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to get operation status. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const operationFullId = '[OPERATION_FULL_ID]' + // eg., "projects//locations/us-central1/operations/", + // `Full name of an operation`; + + // Get the latest state of a long-running operation. + client.operationsClient + .getOperation({name: operationFullId}) + .then(responses => { + const response = responses[0]; + console.log(`Operation details:`); + console.log(`\tName: ${response.name}`); + console.log(`\tMetadata:`); + console.log(`\t\tType Url: ${response.metadata.typeUrl}`); + console.log(`\tDone: ${response.done}`); + + if (response.response) { + console.log(`\tResponse:`); + console.log(`\t\tType Url: ${response.response.typeUrl}`); + } + + if (response.error) { + console.log(`\tResponse:`); + console.log(`\t\tError code: ${response.error.code}`); + console.log(`\t\tError message: ${response.error.message}`); + } + }) + .catch(err => { + console.error(err); + }); + // [END automl_video_intelligence_classification_get_operation_status] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/video-intelligence/classification/import-data.v1beta1.js b/automl/video-intelligence/classification/import-data.v1beta1.js new file mode 100644 index 0000000000..1a4eefd418 --- /dev/null +++ b/automl/video-intelligence/classification/import-data.v1beta1.js @@ -0,0 +1,71 @@ +/** + * Copyright 2019, Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID', + gcsPath = 'GCS_PATH' +) { + // [START automl_video_intelligence_classification_import_data] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to import labeled items. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g.,"VCN7209576908164431872"; + // const gcsPath = '[GCS_PATH]' e.g., "gs:///”, + // `.csv paths in AutoML Video Intelligence Detection CSV format`; + + // Get the full path of the dataset. + const datasetFullId = client.datasetPath(projectId, computeRegion, datasetId); + + // Get the multiple Google Cloud Storage URIs. + const inputUris = gcsPath.split(`,`); + const inputConfig = { + gcsSource: { + inputUris: inputUris, + }, + }; + + // Import the data from the input URI. + client + .importData({name: datasetFullId, inputConfig: inputConfig}) + .then(responses => { + const operation = responses[0]; + console.log(`Processing import...`); + return operation.promise(); + }) + .then(responses => { + // The final result of the operation. + const operationDetails = responses[2]; + + // Get the data import details. + console.log('Data import details:'); + console.log(`\tOperation details:`); + console.log(`\t\tName: ${operationDetails.name}`); + console.log(`\t\tDone: ${operationDetails.done}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_video_intelligence_classification_import_data] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/video-intelligence/classification/list-datasets.v1beta1.js b/automl/video-intelligence/classification/list-datasets.v1beta1.js new file mode 100644 index 0000000000..fb15f2a096 --- /dev/null +++ b/automl/video-intelligence/classification/list-datasets.v1beta1.js @@ -0,0 +1,66 @@ +/** + * Copyright 2019, Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + filter = 'FILTER_EXPRESSION' +) { + // [START automl_video_intelligence_classification_list_datasets] + const automl = require(`@google-cloud/automl`); + const util = require(`util`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to list all datasets. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const filter_ = '[FILTER_EXPRESSIONS]' + // e.g., "videoClassificationDatasetMetadata:*"; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // List all the datasets available in the region by applying filter. + client + .listDatasets({parent: projectLocation, filter: filter}) + .then(responses => { + const dataset = responses[0]; + + // Display the dataset information. + console.log(`List of datasets:`); + for (let i = 0; i < dataset.length; i++) { + console.log(`\nDataset name: ${dataset[i].name}`); + console.log(`Dataset Id: ${dataset[i].name.split(`/`).pop(-1)}`); + console.log(`Dataset display name: ${dataset[i].displayName}`); + console.log(`Dataset example count: ${dataset[i].exampleCount}`); + console.log( + `Video classification dataset metadata: ${util.inspect( + dataset[i].videoClassificationDatasetMetadata, + false, + null + )}` + ); + } + }) + .catch(err => { + console.error(err); + }); + // [END automl_video_intelligence_classification_list_datasets] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/video-intelligence/classification/list-model-evaluations.v1beta1.js b/automl/video-intelligence/classification/list-model-evaluations.v1beta1.js new file mode 100644 index 0000000000..f95cf5cafd --- /dev/null +++ b/automl/video-intelligence/classification/list-model-evaluations.v1beta1.js @@ -0,0 +1,126 @@ +/** + * Copyright 2019, Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID', + filter = 'FILTER_EXPRESSION' +) { + // [START automl_video_intelligence_classification_list_model_evaluations] + const automl = require(`@google-cloud/automl`); + const math = require(`mathjs`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to list model evaluations. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "VCN7209576908164431872"; + // const filter = '[FILTER_EXPRESSIONS]' + // e.g., "videoClassificationModelMetadata:*"; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // List all the model evaluations in the model by applying filter. + client + .listModelEvaluations({parent: modelFullId, filter: filter}) + .then(responses => { + const element = responses[0]; + console.log(`List of model evaluations:`); + for (let i = 0; i < element.length; i++) { + const confidenceMetricsEntries = + element[i].classificationEvaluationMetrics.confidenceMetricsEntry; + + // Display the model evaluations information. + console.log(`\nModel evaluation name: ${element[i].name}`); + console.log( + `Model evaluation Id: ${element[i].name + .split(`/`) + .slice(-1) + .pop()}` + ); + console.log( + `Model evaluation annotation spec Id: ${element[i].annotationSpecId}` + ); + console.log(`Model evaluation display name: ${element[i].displayName}`); + console.log( + `Model evaluation example count: ${element[i].evaluatedExampleCount}` + ); + console.log(`Video classification evaluation metrics:`); + console.log( + `\tModel auPrc: ${math.round( + element[i].classificationEvaluationMetrics.auPrc, + 6 + )}` + ); + console.log(`\tConfidence metrics entries:`); + + for (const confidenceMetricsEntry of confidenceMetricsEntries) { + console.log( + `\t\tModel confidenceThreshold: ${math.round( + confidenceMetricsEntry.confidenceThreshold, + 2 + )}` + ); + console.log( + `\t\tModel recall: ${math.round( + confidenceMetricsEntry.recall * 100, + 2 + )} %` + ); + console.log( + `\t\tModel precision: ${math.round( + confidenceMetricsEntry.precision * 100, + 2 + )} %` + ); + console.log( + `\t\tModel f1 score: ${math.round( + confidenceMetricsEntry.f1Score * 100, + 2 + )} %` + ); + console.log( + `\t\tModel recall@1: ${math.round( + confidenceMetricsEntry.recallAt1 * 100, + 2 + )} %` + ); + console.log( + `\t\tModel precision@1: ${math.round( + confidenceMetricsEntry.precisionAt1 * 100, + 2 + )} %` + ); + console.log( + `\t\tModel f1 score@1: ${math.round( + confidenceMetricsEntry.f1ScoreAt1 * 100, + 2 + )} % \n` + ); + } + } + }) + .catch(err => { + console.error(err); + }); + // [END automl_video_intelligence_classification_list_model_evaluations] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/video-intelligence/classification/list-models.v1beta1.js b/automl/video-intelligence/classification/list-models.v1beta1.js new file mode 100644 index 0000000000..c86698708e --- /dev/null +++ b/automl/video-intelligence/classification/list-models.v1beta1.js @@ -0,0 +1,70 @@ +/** + * Copyright 2019, Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + filter = 'FILTER_EXPRESSION' +) { + // [START automl_video_intelligence_classification_list_models] + const automl = require(`@google-cloud/automl`); + const util = require(`util`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to list all models. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const filter_ = '[FILTER_EXPRESSIONS]' + // e.g., "videoClassificationModelMetadata:*"; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // List all the models available in the region by applying filter. + client + .listModels({parent: projectLocation, filter: filter}) + .then(responses => { + const model = responses[0]; + + // Display the model information. + console.log(`List of models:`); + for (let i = 0; i < model.length; i++) { + console.log(`\nModel name: ${model[i].name}`); + console.log(`Model Id: ${model[i].name.split(`/`).pop(-1)}`); + console.log(`Model display name: ${model[i].displayName}`); + console.log(`Dataset Id: ${model[i].datasetId}`); + + if (model[i].modelMetadata === `videoClassificationModelMetadata`) { + console.log( + `Video Classification model metadata: ${util.inspect( + model[i].videoClassificationModelMetadata, + false, + null + )}` + ); + } + console.log(`Model deployment state: ${model[i].deploymentState}`); + } + }) + .catch(err => { + console.error(err); + }); + // [END automl_video_intelligence_classification_list_models] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/video-intelligence/classification/list-operation-status.v1beta1.js b/automl/video-intelligence/classification/list-operation-status.v1beta1.js new file mode 100644 index 0000000000..f293bc80e0 --- /dev/null +++ b/automl/video-intelligence/classification/list-operation-status.v1beta1.js @@ -0,0 +1,66 @@ +/** + * Copyright 2019, Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + filter = 'FILTER_EXPRESSION' +) { + // [START automl_video_intelligence_classification_list_operations_status] + const automl = require(`@google-cloud/automl`); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const filter_ = '[FILTER_EXPRESSIONS]'; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // List all the operations available in the region by applying filter. + client.operationsClient + .listOperations({name: projectLocation, filter: filter}) + .then(responses => { + const response = responses[0]; + console.log(`List of operations:`); + for (let i = 0; i < response.length; i++) { + console.log(`\n\tOperation details:`); + console.log(`\t\tName: ${response[i].name}`); + console.log(`\t\tMetadata:`); + console.log(`\t\t\tType Url: ${response[i].metadata.typeUrl}`); + console.log(`\t\tDone: ${response[i].done}`); + + if (response[i].response) { + console.log(`\t\tResponse:`); + console.log(`\t\t\tType Url: ${response[i].response.typeUrl}`); + } + + if (response[i].error) { + console.log(`\t\tResponse:`); + console.log(`\t\t\tError code: ${response[i].error.code}`); + console.log(`\t\t\tError message: ${response[i].error.message}`); + } + } + }) + .catch(err => { + console.error(err); + }); + // [END automl_video_intelligence_classification_list_operations_status] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/video-intelligence/classification/predict.v1beta1.js b/automl/video-intelligence/classification/predict.v1beta1.js new file mode 100644 index 0000000000..1cd204073c --- /dev/null +++ b/automl/video-intelligence/classification/predict.v1beta1.js @@ -0,0 +1,79 @@ +/** + * Copyright 2019, Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +`use strict`; +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'YOUR_MODEL_ID', + inputUri = 'GCS_PATH', + outputUriPrefix = 'GCS_DIRECTORY_PATH' +) { + // [START automl_video_intelligence_classification_predict] + const automl = require(`@google-cloud/automl`); + + // Create client for prediction service. + const client = new automl.v1beta1.PredictionServiceClient(); + + /** + * Demonstrates using the AutoML client to classify the video intelligence + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "IOD2122286140026257408"; + // const inputUri = '[GCS_PATH]' e.g., "gs:///", + // `the GCS bucket path of csv file which contains path of the video + // to be classified.`; + // const outputUriPrefix = '[GCS_DIRECTORY_PATH]' + // e.g., "gs:///", + // `the output GCS bucket folder path which contains one csv file and + // json file for each video classification.`; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Set the input URI + const inputConfig = { + gcsSource: { + inputUris: [inputUri], + }, + }; + + // Set the output URI + const outputUri = outputUriPrefix; + const outputConfig = { + gcsDestination: { + outputUriPrefix: outputUri, + }, + }; + + // Get the latest state of a long-running operation. + client + .batchPredict({ + name: modelFullId, + inputConfig: inputConfig, + outputConfig: outputConfig, + }) + .then(responses => { + const response = responses[1]; + console.log(`Operation name: ${response.name}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_video_intelligence_classification_predict] +} +main(...process.argv.slice(2)).catch(console.error());