Skip to content

Commit 90c712b

Browse files
nnegreyJustinBeckwithbcoe
authored
docs: add vision object detection ga samples (#298)
* docs: add vision object detection ga samples * lint fix * Fix license headers and update tests * Update license headers * use spawnsync to get stderr output and update license headers Co-authored-by: Justin Beckwith <[email protected]> Co-authored-by: Benjamin E. Coe <[email protected]>
1 parent 8f20322 commit 90c712b

9 files changed

+465
-0
lines changed

automl/resources/salad.jpg

2.25 MB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2020 Google LLC
2+
//
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+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {after, describe, it} = require('mocha');
19+
const {AutoMlClient} = require('@google-cloud/automl').v1;
20+
21+
const cp = require('child_process');
22+
const uuid = require('uuid');
23+
24+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
25+
26+
const CREATE_DATASET_REGION_TAG = 'vision_object_detection_create_dataset';
27+
const LOCATION = 'us-central1';
28+
29+
describe('Automl Vision Object Detection Create Dataset Test', () => {
30+
const client = new AutoMlClient();
31+
let datasetId;
32+
33+
it('should create a dataset', async () => {
34+
const projectId = await client.getProjectId();
35+
const displayName = `test_${uuid
36+
.v4()
37+
.replace(/-/g, '_')
38+
.substring(0, 26)}`;
39+
40+
// create
41+
const create_output = await execSync(
42+
`node ${CREATE_DATASET_REGION_TAG}.js ${projectId} ${LOCATION} ${displayName}`
43+
);
44+
assert.match(create_output, /Dataset id:/);
45+
46+
datasetId = create_output.split('Dataset id: ')[1].split('\n')[0];
47+
});
48+
49+
after('delete created dataset', async () => {
50+
const projectId = await client.getProjectId();
51+
const request = {
52+
name: client.datasetPath(projectId, LOCATION, datasetId),
53+
};
54+
const [operation] = await client.deleteDataset(request);
55+
await operation.promise();
56+
});
57+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2020 Google LLC
2+
//
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+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {after, describe, it} = require('mocha');
19+
const {AutoMlClient} = require('@google-cloud/automl').v1;
20+
21+
const cp = require('child_process');
22+
23+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
24+
25+
const CREATE_MODEL_REGION_TAG = 'vision_object_detection_create_model';
26+
const LOCATION = 'us-central1';
27+
const DATASET_ID = 'IOD4700715673951666176';
28+
29+
describe('Automl Vision Object Detection Create Model Test', () => {
30+
const client = new AutoMlClient();
31+
let operationId;
32+
33+
it('should create a model', async () => {
34+
const projectId = await client.getProjectId();
35+
const create_output = execSync(
36+
`node ${CREATE_MODEL_REGION_TAG}.js ${projectId} ${LOCATION} ${DATASET_ID} object_test_create_model`
37+
);
38+
39+
assert.match(create_output, /Training started/);
40+
41+
operationId = create_output
42+
.split('Training operation name: ')[1]
43+
.split('\n')[0];
44+
});
45+
46+
after('cancel model training', async () => {
47+
await client.operationsClient.cancelOperation({name: operationId});
48+
});
49+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2020 Google LLC
2+
//
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+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
const {AutoMlClient} = require('@google-cloud/automl').v1;
20+
21+
const cp = require('child_process');
22+
23+
const DEPLOY_MODEL_REGION_TAG =
24+
'vision_object_detection_deploy_model_node_count';
25+
const LOCATION = 'us-central1';
26+
const MODEL_ID = '0000000000000000000000';
27+
28+
describe('Automl Vision Object Detection Deploy Model Test', () => {
29+
const client = new AutoMlClient();
30+
31+
it('should deploy a model with a specified node count', async () => {
32+
// As model deployment can take a long time, instead try to deploy a
33+
// nonexistent model and confirm that the model was not found, but other
34+
// elements of the request were valid.
35+
const projectId = await client.getProjectId();
36+
const args = [DEPLOY_MODEL_REGION_TAG, projectId, LOCATION, MODEL_ID];
37+
const output = cp.spawnSync('node', args, {encoding: 'utf8'});
38+
39+
assert.match(output.stderr, /NOT_FOUND/);
40+
assert.match(output.stderr, /The model does not exist./);
41+
});
42+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2020 Google LLC
2+
//
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+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {before, describe, it} = require('mocha');
19+
const {AutoMlClient} = require('@google-cloud/automl').v1;
20+
21+
const cp = require('child_process');
22+
23+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
24+
25+
const MODEL_ID = 'IOD1656537412546854912';
26+
const PREDICT_REGION_TAG = 'vision_object_detection_predict';
27+
const LOCATION = 'us-central1';
28+
29+
describe('Automl Vision Object Detection Predict Test', () => {
30+
const client = new AutoMlClient();
31+
32+
before('should verify the model is deployed', async () => {
33+
const projectId = await client.getProjectId();
34+
const request = {
35+
name: client.modelPath(projectId, LOCATION, MODEL_ID),
36+
};
37+
38+
const [response] = await client.getModel(request);
39+
if (response.deploymentState === 'DEPLOYED') {
40+
const request = {
41+
name: client.modelPath(projectId, LOCATION, MODEL_ID),
42+
};
43+
44+
const [operation] = await client.deployModel(request);
45+
46+
// Wait for operation to complete.
47+
await operation.promise();
48+
}
49+
});
50+
51+
it('should predict', async () => {
52+
const projectId = await client.getProjectId();
53+
const filePath = 'resources/salad.jpg';
54+
55+
const predictOutput = execSync(
56+
`node ${PREDICT_REGION_TAG}.js ${projectId} ${LOCATION} ${MODEL_ID} ${filePath}`
57+
);
58+
assert.match(predictOutput, /Predicted class name/);
59+
});
60+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2020 Google LLC
2+
//
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+
'use strict';
16+
17+
function main(
18+
projectId = 'YOUR_PROJECT_ID',
19+
location = 'us-central1',
20+
displayName = 'YOUR_DISPLAY_NAME'
21+
) {
22+
// [START automl_vision_object_detection_create_dataset]
23+
/**
24+
* TODO(developer): Uncomment these variables before running the sample.
25+
*/
26+
// const projectId = 'YOUR_PROJECT_ID';
27+
// const location = 'us-central1';
28+
// const displayName = 'YOUR_DISPLAY_NAME';
29+
30+
// Imports the Google Cloud AutoML library
31+
const {AutoMlClient} = require(`@google-cloud/automl`).v1;
32+
33+
// Instantiates a client
34+
const client = new AutoMlClient();
35+
36+
async function createDataset() {
37+
// Construct request
38+
const request = {
39+
parent: client.locationPath(projectId, location),
40+
dataset: {
41+
displayName: displayName,
42+
imageObjectDetectionDatasetMetadata: {},
43+
},
44+
};
45+
46+
// Create dataset
47+
const [operation] = await client.createDataset(request);
48+
49+
// Wait for operation to complete.
50+
const [response] = await operation.promise();
51+
52+
console.log(`Dataset name: ${response.name}`);
53+
console.log(`
54+
Dataset id: ${
55+
response.name
56+
.split('/')
57+
[response.name.split('/').length - 1].split('\n')[0]
58+
}`);
59+
}
60+
61+
createDataset();
62+
// [END automl_vision_object_detection_create_dataset]
63+
}
64+
65+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2020 Google LLC
2+
//
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+
'use strict';
16+
17+
function main(
18+
projectId = 'YOUR_PROJECT_ID',
19+
location = 'us-central1',
20+
datasetId = 'YOUR_DATASET_ID',
21+
displayName = 'YOUR_DISPLAY_NAME'
22+
) {
23+
// [START automl_vision_object_detection_create_model]
24+
/**
25+
* TODO(developer): Uncomment these variables before running the sample.
26+
*/
27+
// const projectId = 'YOUR_PROJECT_ID';
28+
// const location = 'us-central1';
29+
// const dataset_id = 'YOUR_DATASET_ID';
30+
// const displayName = 'YOUR_DISPLAY_NAME';
31+
32+
// Imports the Google Cloud AutoML library
33+
const {AutoMlClient} = require(`@google-cloud/automl`).v1;
34+
35+
// Instantiates a client
36+
const client = new AutoMlClient();
37+
38+
async function createModel() {
39+
// Construct request
40+
const request = {
41+
parent: client.locationPath(projectId, location),
42+
model: {
43+
displayName: displayName,
44+
datasetId: datasetId,
45+
imageObjectDetectionModelMetadata: {},
46+
},
47+
};
48+
49+
// Don't wait for the LRO
50+
const [operation] = await client.createModel(request);
51+
console.log(`Training started... ${operation}`);
52+
console.log(`Training operation name: ${operation.name}`);
53+
}
54+
55+
createModel();
56+
// [END automl_vision_object_detection_create_model]
57+
}
58+
59+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2020 Google LLC
2+
//
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+
'use strict';
16+
17+
function main(
18+
projectId = 'YOUR_PROJECT_ID',
19+
location = 'us-central1',
20+
modelId = 'YOUR_MODEL_ID'
21+
) {
22+
// [START automl_vision_object_detection_deploy_model_node_count]
23+
/**
24+
* TODO(developer): Uncomment these variables before running the sample.
25+
*/
26+
// const projectId = 'YOUR_PROJECT_ID';
27+
// const location = 'us-central1';
28+
// const modelId = 'YOUR_MODEL_ID';
29+
30+
// Imports the Google Cloud AutoML library
31+
const {AutoMlClient} = require(`@google-cloud/automl`).v1;
32+
33+
// Instantiates a client
34+
const client = new AutoMlClient();
35+
36+
async function deployModelWithNodeCount() {
37+
// Construct request
38+
const request = {
39+
name: client.modelPath(projectId, location, modelId),
40+
imageObjectDetectionModelDeploymentMetadata: {
41+
nodeCount: 2,
42+
},
43+
};
44+
45+
const [operation] = await client.deployModel(request);
46+
47+
// Wait for operation to complete.
48+
const [response] = await operation.promise();
49+
console.log(`Model deployment finished. ${response}`);
50+
}
51+
52+
deployModelWithNodeCount();
53+
// [END automl_vision_object_detection_deploy_model_node_count]
54+
}
55+
56+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)