Skip to content

Commit 721a8f1

Browse files
nirupa-kumarAce Nassri
authored and
Ace Nassri
committed
docs(samples): add vision beta samples (#157)
* Vision beta samples * Vision beta samples * Vision beta samples * Vision beta samples * fixing tests * fixing tests * fixing tests * fixing tests * fixing tests
1 parent 5030725 commit 721a8f1

23 files changed

+1688
-2
lines changed

automl/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
"repository": "googleapis/nodejs-automl",
1313
"private": true,
1414
"scripts": {
15-
"test": "mocha --timeout 600000"
15+
"test": "mocha --timeout 600000 --recursive"
1616
},
1717
"dependencies": {
1818
"@google-cloud/automl": "^0.2.0",
1919
"mathjs": "^5.5.0",
20-
"yargs": "^13.2.1"
20+
"yargs": "^13.2.1",
21+
"execa":"^1.0.0"
2122
},
2223
"devDependencies": {
2324
"mocha": "^6.0.1",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* Copyright 2019, Google LLC
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
const {assert} = require('chai');
19+
const cp = require('child_process');
20+
21+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
22+
23+
/** Tests for AutoML Vision Object Detection "Dataset API" sample. */
24+
// TODO(developer): Before running the test cases,
25+
// set the environment variables PROJECT_ID, REGION_NAME and
26+
// change the value of datasetId
27+
const projectId = 'nodejs-docs-samples';
28+
const computeRegion = 'us-central1';
29+
const outputPath = 'gs://nodejs-docs-samples/VISION_OBJECT_DETECTION/';
30+
const datasetName = 'test_vision_create_dataset';
31+
const filter = 'imageObjectDetectionDatasetMetadata:*';
32+
const datasetId = 'ICN3946265060617537378';
33+
const importDataCsv = 'gs://nodejs-docs-samples-vcm/flowerTraindata20lines.csv';
34+
35+
describe('Vision Object Detection DatasetAPI', () => {
36+
it.skip(`should create, import and delete a dataset`, async () => {
37+
// Create dataset
38+
let output = await execSync(
39+
`node vision/object-detection/create-dataset.v1beta1.js "${projectId}" "${computeRegion}" "${datasetName}"`
40+
);
41+
const parsedOut = output.split('\n');
42+
const outputDatasetId = parsedOut[1].split(':')[1].trim();
43+
assert.match(output, /Dataset display name:/);
44+
45+
// Import data
46+
output = await execSync(
47+
`node vision/object-detection/import-data.v1beta1.js "${projectId}" "${computeRegion}" "${outputDatasetId}" "${importDataCsv}"`
48+
);
49+
assert.match(output, /Processing import.../);
50+
51+
// Delete dataset
52+
output = await execSync(
53+
`node vision/object-detection/delete-dataset.v1beta1.js "${projectId}" "${computeRegion}" "${outputDatasetId}"`
54+
);
55+
assert.match(output, /Dataset delete details:/);
56+
});
57+
58+
it.skip(`should list datasets`, async () => {
59+
// List datasets
60+
const output = await execSync(
61+
`node vision/object-detection/list-datasets.v1beta1.js "${projectId}" "${computeRegion}" "${filter}"`
62+
);
63+
assert.match(output, /List of datasets:/);
64+
});
65+
66+
it.skip(`should get preexisting dataset`, async () => {
67+
// Get dataset
68+
const output = await execSync(
69+
`node vision/object-detection/get-dataset.v1beta1.js "${projectId}" "${computeRegion}" "${datasetId}"`
70+
);
71+
assert.match(output, /Dataset display name:/);
72+
});
73+
74+
it.skip(`should export dataset`, async () => {
75+
// Export data
76+
const outputUri = outputPath + datasetId;
77+
const output = await execSync(
78+
`node vision/object-detection/export-data.v1beta1.js "${projectId}" "${computeRegion}" "${datasetId}" "${outputUri}"`
79+
);
80+
assert.match(output, /Processing export.../);
81+
});
82+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/**
2+
* Copyright 2019, Google LLC
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
const {assert} = require('chai');
19+
const cp = require('child_process');
20+
21+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
22+
23+
/** Tests for AutoML Vision Object Detection "Model API" sample. */
24+
// TODO(developer): Before running the test cases,
25+
// set the environment variables PROJECT_ID, REGION_NAME and
26+
// change the value of datasetId, deployModelId and undeployModelId
27+
const projectId = 'nodejs-docs-samples';
28+
const computeRegion = 'us-central1';
29+
const filter = 'imageObjectDetectionModelMetadata:*';
30+
const datasetId = 'ICN3217071205693347964';
31+
const testModelName = 'birds2_201804101601_base';
32+
const deployModelId = 'IOD1728502647608049664';
33+
const undeployModelId = 'IOD3348109663601164288';
34+
35+
describe(' Vision Object Detection ModelAPI', () => {
36+
it.skip(`should create a model`, async () => {
37+
let output = await execSync(
38+
`node vision/object-detection/create-model.v1beta1.js "${projectId}" "${computeRegion}" "${datasetId}" "${testModelName}"`
39+
);
40+
const operationName = output
41+
.split('\n')[0]
42+
.split(':')[1]
43+
.trim();
44+
assert.match(output, /Training started.../);
45+
46+
output = await execSync(
47+
`node vision/object-detection/get-operation-status.v1beta1.js "${operationName}"`
48+
);
49+
assert.match(output, /Operation details:/);
50+
});
51+
52+
it.skip(`should list models, get and delete a model. list, get and display model
53+
evaluations from preexisting models`, async () => {
54+
// List models
55+
let output = await execSync(
56+
`node vision/object-detection/list-models.v1beta1.js "${projectId}" "${computeRegion}" "${filter}"`
57+
);
58+
const parsedOut = output.split('\n');
59+
const outputModelId = parsedOut[3].split(':')[1].trim();
60+
assert.match(output, /List of models:/);
61+
62+
// Get Model
63+
output = await execSync(
64+
`node vision/object-detection/get-model.v1beta1.js "${projectId}" "${computeRegion}" "${outputModelId}"`
65+
);
66+
assert.match(output, /Model name:/);
67+
68+
// List model evaluation
69+
output = await execSync(
70+
`node vision/object-detection/list-model-evaluations.v1beta1.js "${projectId}" "${computeRegion}" "${outputModelId}"`
71+
);
72+
const parsedModelEvaluation = output.split('\n');
73+
const modelEvaluationId = parsedModelEvaluation[3].split(':')[1].trim();
74+
assert.match(output, /Model evaluation Id:/);
75+
76+
// Get model evaluation
77+
output = await execSync(
78+
`node vision/object-detection/get-model-evaluation.v1beta1.js "${projectId}" "${computeRegion}" "${outputModelId}"` +
79+
` "${modelEvaluationId}"`
80+
);
81+
assert.match(output, /Model evaluation Id:/);
82+
83+
// Display evaluation
84+
output = await execSync(
85+
`node vision/object-detection/display-evaluation.v1beta1.js "${projectId}" "${computeRegion}" "${outputModelId}" `
86+
);
87+
assert.match(output, /Model Evaluation ID:/);
88+
89+
// Delete Model
90+
output = await execSync(
91+
`node vision/object-detection/delete-model.v1beta1.js "${projectId}" "${computeRegion}" "${outputModelId}"`
92+
);
93+
assert.match(output, /Model delete details:/);
94+
});
95+
96+
it.skip(`should list and get operation status`, async () => {
97+
// List operation status
98+
let output = await execSync(
99+
`node vision/object-detection/list-operations-status.v1beta1.js`
100+
);
101+
const parsedOut = output.split('\n');
102+
const operationFullId = parsedOut[3].split(':')[1].trim();
103+
104+
// Get operation status
105+
// Poll operation status, here confirming that operation is not complete yet
106+
output = await execSync(
107+
`node vision/object-detection/get-operation-status.v1beta1.js "${operationFullId}"`
108+
);
109+
assert.match(output, /Operation details:/);
110+
});
111+
112+
it.skip(`should deploy the model`, async () => {
113+
// Deploy the model
114+
const output = await execSync(
115+
`node vision/object-detection/deploy-model.v1beta1.js "${projectId}" "${computeRegion}" ${deployModelId}`
116+
);
117+
assert.match(output, /Name:/);
118+
});
119+
120+
it.skip(`should undeploy the model`, async () => {
121+
// Undeploy the model
122+
const output = await execSync(
123+
`node vision/object-detection/undeploy-model.v1beta1.js "${projectId}" "${computeRegion}" ${undeployModelId}`
124+
);
125+
assert.match(output, /Name:/);
126+
});
127+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright 2019, Google LLC
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
const {assert} = require('chai');
19+
const execa = require('execa');
20+
21+
/** Tests for AutoML Vision Object Detection "Prediction API" sample. */
22+
23+
// TODO(developer): Before running the test cases,
24+
// set the environment variables PROJECT_ID, REGION_NAME and change the value of modelId
25+
const projectId = 'nodejs-docs-samples';
26+
const computeRegion = 'us-central1';
27+
const modelId = '';
28+
const filePath = './resource/songbird.jpg';
29+
30+
const exec = async cmd => (await execa.shell(cmd)).stdout;
31+
32+
describe.skip('Vision Object Detection PredictionAPI', () => {
33+
it(`should run prediction from preexisting model`, async () => {
34+
// Run prediction on 'salad.jpg' in resource folder
35+
const output = await exec(
36+
`node vision/object-detection/predict.v1beta1.js "${projectId}" "${computeRegion}" "${modelId}" "${filePath}"`
37+
);
38+
assert.match(output, /Prediction results:/);
39+
});
40+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Copyright 2019, Google LLC
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
`use strict`;
17+
function main(
18+
projectId = 'YOUR_PROJECT_ID',
19+
computeRegion = 'YOUR_REGION_NAME',
20+
datasetName = 'YOUR_DATASET_NAME'
21+
) {
22+
// [START automl_vision_object_detection_create_dataset]
23+
/**
24+
* Demonstrates using the AutoML client to create a dataset.
25+
* TODO(developer): Uncomment the following lines before running the sample.
26+
*/
27+
// const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project";
28+
// const computeRegion = '[REGION_NAME]' e.g., "us-central1";
29+
// const datasetName = '[DATASET_NAME]' e.g., "myDataset";
30+
31+
//Imports the Google Cloud Automl library
32+
const {AutoMlClient} = require('@google-cloud/automl').v1beta1;
33+
34+
// Instantiates a client
35+
const automlClient = new AutoMlClient();
36+
const util = require(`util`);
37+
38+
async function createDataset() {
39+
// A resource that represents Google Cloud Platform location.
40+
const projectLocation = automlClient.locationPath(projectId, computeRegion);
41+
42+
// Set dataset name and metadata.
43+
const myDataset = {
44+
displayName: datasetName,
45+
imageObjectDetectionDatasetMetadata: {},
46+
};
47+
48+
// Create a dataset with the dataset metadata in the region.
49+
const [response] = await automlClient.createDataset({
50+
parent: projectLocation,
51+
dataset: myDataset,
52+
});
53+
54+
//const dataset = response[0];
55+
// Display the dataset information.
56+
console.log(`Dataset name: ${response.name}`);
57+
console.log(`Dataset Id: ${response.name.split(`/`).pop(-1)}`);
58+
console.log(`Dataset display name: ${response.displayName}`);
59+
console.log(`Dataset example count: ${response.exampleCount}`);
60+
console.log(
61+
`Image object detection dataset metadata: ${util.inspect(
62+
response.imageObjectDetectionDatasetMetadata,
63+
false,
64+
null
65+
)}`
66+
);
67+
}
68+
createDataset();
69+
// [END automl_vision_object_detection_create_dataset]
70+
}
71+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)