Skip to content

Commit

Permalink
docs(samples): add vision beta samples (#157)
Browse files Browse the repository at this point in the history
* Vision beta samples

* Vision beta samples

* Vision beta samples

* Vision beta samples

* fixing tests

* fixing tests

* fixing tests

* fixing tests

* fixing tests
  • Loading branch information
nirupa-kumar authored Apr 5, 2019
1 parent cde50b9 commit 303162d
Show file tree
Hide file tree
Showing 23 changed files with 1,688 additions and 2 deletions.
5 changes: 3 additions & 2 deletions automl/snippets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
"repository": "googleapis/nodejs-automl",
"private": true,
"scripts": {
"test": "mocha --timeout 600000"
"test": "mocha --timeout 600000 --recursive"
},
"dependencies": {
"@google-cloud/automl": "^0.2.0",
"mathjs": "^5.5.0",
"yargs": "^13.2.1"
"yargs": "^13.2.1",
"execa":"^1.0.0"
},
"devDependencies": {
"mocha": "^6.0.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* 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 cp = require('child_process');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

/** Tests for AutoML Vision Object Detection "Dataset API" sample. */
// TODO(developer): Before running the test cases,
// set the environment variables PROJECT_ID, REGION_NAME and
// change the value of datasetId
const projectId = 'nodejs-docs-samples';
const computeRegion = 'us-central1';
const outputPath = 'gs://nodejs-docs-samples/VISION_OBJECT_DETECTION/';
const datasetName = 'test_vision_create_dataset';
const filter = 'imageObjectDetectionDatasetMetadata:*';
const datasetId = 'ICN3946265060617537378';
const importDataCsv = 'gs://nodejs-docs-samples-vcm/flowerTraindata20lines.csv';

describe('Vision Object Detection DatasetAPI', () => {
it.skip(`should create, import and delete a dataset`, async () => {
// Create dataset
let output = await execSync(
`node vision/object-detection/create-dataset.v1beta1.js "${projectId}" "${computeRegion}" "${datasetName}"`
);
const parsedOut = output.split('\n');
const outputDatasetId = parsedOut[1].split(':')[1].trim();
assert.match(output, /Dataset display name:/);

// Import data
output = await execSync(
`node vision/object-detection/import-data.v1beta1.js "${projectId}" "${computeRegion}" "${outputDatasetId}" "${importDataCsv}"`
);
assert.match(output, /Processing import.../);

// Delete dataset
output = await execSync(
`node vision/object-detection/delete-dataset.v1beta1.js "${projectId}" "${computeRegion}" "${outputDatasetId}"`
);
assert.match(output, /Dataset delete details:/);
});

it.skip(`should list datasets`, async () => {
// List datasets
const output = await execSync(
`node vision/object-detection/list-datasets.v1beta1.js "${projectId}" "${computeRegion}" "${filter}"`
);
assert.match(output, /List of datasets:/);
});

it.skip(`should get preexisting dataset`, async () => {
// Get dataset
const output = await execSync(
`node vision/object-detection/get-dataset.v1beta1.js "${projectId}" "${computeRegion}" "${datasetId}"`
);
assert.match(output, /Dataset display name:/);
});

it.skip(`should export dataset`, async () => {
// Export data
const outputUri = outputPath + datasetId;
const output = await execSync(
`node vision/object-detection/export-data.v1beta1.js "${projectId}" "${computeRegion}" "${datasetId}" "${outputUri}"`
);
assert.match(output, /Processing export.../);
});
});
127 changes: 127 additions & 0 deletions automl/snippets/test/automlVisionObjectDetectionModel.v1beta1.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/**
* 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 cp = require('child_process');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

/** Tests for AutoML Vision Object Detection "Model API" sample. */
// TODO(developer): Before running the test cases,
// set the environment variables PROJECT_ID, REGION_NAME and
// change the value of datasetId, deployModelId and undeployModelId
const projectId = 'nodejs-docs-samples';
const computeRegion = 'us-central1';
const filter = 'imageObjectDetectionModelMetadata:*';
const datasetId = 'ICN3217071205693347964';
const testModelName = 'birds2_201804101601_base';
const deployModelId = 'IOD1728502647608049664';
const undeployModelId = 'IOD3348109663601164288';

describe(' Vision Object Detection ModelAPI', () => {
it.skip(`should create a model`, async () => {
let output = await execSync(
`node vision/object-detection/create-model.v1beta1.js "${projectId}" "${computeRegion}" "${datasetId}" "${testModelName}"`
);
const operationName = output
.split('\n')[0]
.split(':')[1]
.trim();
assert.match(output, /Training started.../);

output = await execSync(
`node vision/object-detection/get-operation-status.v1beta1.js "${operationName}"`
);
assert.match(output, /Operation details:/);
});

it.skip(`should list models, get and delete a model. list, get and display model
evaluations from preexisting models`, async () => {
// List models
let output = await execSync(
`node vision/object-detection/list-models.v1beta1.js "${projectId}" "${computeRegion}" "${filter}"`
);
const parsedOut = output.split('\n');
const outputModelId = parsedOut[3].split(':')[1].trim();
assert.match(output, /List of models:/);

// Get Model
output = await execSync(
`node vision/object-detection/get-model.v1beta1.js "${projectId}" "${computeRegion}" "${outputModelId}"`
);
assert.match(output, /Model name:/);

// List model evaluation
output = await execSync(
`node vision/object-detection/list-model-evaluations.v1beta1.js "${projectId}" "${computeRegion}" "${outputModelId}"`
);
const parsedModelEvaluation = output.split('\n');
const modelEvaluationId = parsedModelEvaluation[3].split(':')[1].trim();
assert.match(output, /Model evaluation Id:/);

// Get model evaluation
output = await execSync(
`node vision/object-detection/get-model-evaluation.v1beta1.js "${projectId}" "${computeRegion}" "${outputModelId}"` +
` "${modelEvaluationId}"`
);
assert.match(output, /Model evaluation Id:/);

// Display evaluation
output = await execSync(
`node vision/object-detection/display-evaluation.v1beta1.js "${projectId}" "${computeRegion}" "${outputModelId}" `
);
assert.match(output, /Model Evaluation ID:/);

// Delete Model
output = await execSync(
`node vision/object-detection/delete-model.v1beta1.js "${projectId}" "${computeRegion}" "${outputModelId}"`
);
assert.match(output, /Model delete details:/);
});

it.skip(`should list and get operation status`, async () => {
// List operation status
let output = await execSync(
`node vision/object-detection/list-operations-status.v1beta1.js`
);
const parsedOut = output.split('\n');
const operationFullId = parsedOut[3].split(':')[1].trim();

// Get operation status
// Poll operation status, here confirming that operation is not complete yet
output = await execSync(
`node vision/object-detection/get-operation-status.v1beta1.js "${operationFullId}"`
);
assert.match(output, /Operation details:/);
});

it.skip(`should deploy the model`, async () => {
// Deploy the model
const output = await execSync(
`node vision/object-detection/deploy-model.v1beta1.js "${projectId}" "${computeRegion}" ${deployModelId}`
);
assert.match(output, /Name:/);
});

it.skip(`should undeploy the model`, async () => {
// Undeploy the model
const output = await execSync(
`node vision/object-detection/undeploy-model.v1beta1.js "${projectId}" "${computeRegion}" ${undeployModelId}`
);
assert.match(output, /Name:/);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* 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 Vision Object Detection "Prediction API" sample. */

// TODO(developer): Before running the test cases,
// set the environment variables PROJECT_ID, REGION_NAME and change the value of modelId
const projectId = 'nodejs-docs-samples';
const computeRegion = 'us-central1';
const modelId = '';
const filePath = './resource/songbird.jpg';

const exec = async cmd => (await execa.shell(cmd)).stdout;

describe.skip('Vision Object Detection PredictionAPI', () => {
it(`should run prediction from preexisting model`, async () => {
// Run prediction on 'salad.jpg' in resource folder
const output = await exec(
`node vision/object-detection/predict.v1beta1.js "${projectId}" "${computeRegion}" "${modelId}" "${filePath}"`
);
assert.match(output, /Prediction results:/);
});
});
71 changes: 71 additions & 0 deletions automl/snippets/vision/object-detection/create-dataset.v1beta1.js
Original file line number Diff line number Diff line change
@@ -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`;
function main(
projectId = 'YOUR_PROJECT_ID',
computeRegion = 'YOUR_REGION_NAME',
datasetName = 'YOUR_DATASET_NAME'
) {
// [START automl_vision_object_detection_create_dataset]
/**
* 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";

//Imports the Google Cloud Automl library
const {AutoMlClient} = require('@google-cloud/automl').v1beta1;

// Instantiates a client
const automlClient = new AutoMlClient();
const util = require(`util`);

async function createDataset() {
// A resource that represents Google Cloud Platform location.
const projectLocation = automlClient.locationPath(projectId, computeRegion);

// Set dataset name and metadata.
const myDataset = {
displayName: datasetName,
imageObjectDetectionDatasetMetadata: {},
};

// Create a dataset with the dataset metadata in the region.
const [response] = await automlClient.createDataset({
parent: projectLocation,
dataset: myDataset,
});

//const dataset = response[0];
// Display the dataset information.
console.log(`Dataset name: ${response.name}`);
console.log(`Dataset Id: ${response.name.split(`/`).pop(-1)}`);
console.log(`Dataset display name: ${response.displayName}`);
console.log(`Dataset example count: ${response.exampleCount}`);
console.log(
`Image object detection dataset metadata: ${util.inspect(
response.imageObjectDetectionDatasetMetadata,
false,
null
)}`
);
}
createDataset();
// [END automl_vision_object_detection_create_dataset]
}
main(...process.argv.slice(2));
Loading

0 comments on commit 303162d

Please sign in to comment.