Skip to content

Commit

Permalink
docs(samples): add video-intelligence beta samples (#156)
Browse files Browse the repository at this point in the history
* Adding video-intelligence beta samples

* Skipping tests till fixed
  • Loading branch information
nirupa-kumar authored and ahrarmonsur committed Nov 17, 2022
1 parent afd424d commit f0a1caf
Show file tree
Hide file tree
Showing 19 changed files with 1,461 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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.../);
});
});
102 changes: 102 additions & 0 deletions automl/test/automlVideoIntelligenceClassificationModel.v1beta1.test.js
Original file line number Diff line number Diff line change
@@ -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:/);
});
});
Original file line number Diff line number Diff line change
@@ -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:/);
});
});
68 changes: 68 additions & 0 deletions automl/video-intelligence/classification/create-dataset.v1beta1.js
Original file line number Diff line number Diff line change
@@ -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());
59 changes: 59 additions & 0 deletions automl/video-intelligence/classification/create-model.v1beta1.js
Original file line number Diff line number Diff line change
@@ -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());
Loading

0 comments on commit f0a1caf

Please sign in to comment.