Skip to content

Commit

Permalink
docs(samples): modernize the samples (#838)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Oct 26, 2020
1 parent a2e86d3 commit 2feefa4
Show file tree
Hide file tree
Showing 32 changed files with 359 additions and 208 deletions.
1 change: 1 addition & 0 deletions vision/samples/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
rules:
no-console: off
node/no-unsupported-features/node-builtins: off
5 changes: 5 additions & 0 deletions vision/samples/async-batch-annotate-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,9 @@ function main(
// [END vision_async_batch_annotate_images]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
5 changes: 5 additions & 0 deletions vision/samples/batch-annotate-files-gcs.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,9 @@ function main(
// [END vision_batch_annotate_files_gcs]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
11 changes: 7 additions & 4 deletions vision/samples/batch-annotate-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ function main(fileName = 'path/to/your/file.pdf') {

// Imports the Google Cloud client libraries
const {ImageAnnotatorClient} = require('@google-cloud/vision').v1;
const fs = require('fs');
const {promisify} = require('util');
const readFileAsync = promisify(fs.readFile);
const fs = require('fs').promises;

// Instantiates a client
const client = new ImageAnnotatorClient();
Expand All @@ -38,7 +36,7 @@ function main(fileName = 'path/to/your/file.pdf') {
// https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#inputconfig
const inputConfig = {
mimeType: 'application/pdf',
content: await readFileAsync(fileName),
content: await fs.readFile(fileName),
};

// Set the type of annotation you want to perform on the file
Expand Down Expand Up @@ -99,4 +97,9 @@ function main(fileName = 'path/to/your/file.pdf') {
// [END vision_batch_annotate_files]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
22 changes: 10 additions & 12 deletions vision/samples/detect.v1p3beta1.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

/* eslint-disable */

'use strict';

async function detectHandwritingOCR(fileName) {
Expand Down Expand Up @@ -74,40 +72,40 @@ async function detectHandwritingGCS(uri) {
// [END vision_handwritten_ocr_gcs_beta]
}

require(`yargs`)
require('yargs')
.demand(1)
.command(
`detectHandwriting`,
`Detects handwriting in a local image file.`,
'detectHandwriting',
'Detects handwriting in a local image file.',
{},
opts => detectHandwritingOCR(opts.handwritingSample)
)
.command(
`detectHandwritingGCS`,
`Detects handwriting from Google Cloud Storage Bucket.`,
'detectHandwritingGCS',
'Detects handwriting from Google Cloud Storage Bucket.',
{},
opts => detectHandwritingGCS(opts.handwritingSample)
)
.options({
handwritingSample: {
alias: 'h',
default: `./resources/handwritten.jpg`,
default: './resources/handwritten.jpg',
global: true,
requiresArg: true,
type: 'string',
},
handwritingGcsUri: {
alias: 'u',
default: `gs://cloud-samples-data/vision/handwritten.jpg`,
default: 'gs://cloud-samples-data/vision/handwritten.jpg',
global: true,
requiresArg: true,
type: 'string',
},
})
.example(`node $0 detectHandwriting ./resources/handwritten.jpg`)
.example(`node $0 detectHandwritingGCS gs://my-bucket/image.jpg`)
.example('node $0 detectHandwriting ./resources/handwritten.jpg')
.example('node $0 detectHandwritingGCS gs://my-bucket/image.jpg')
.wrap(120)
.recommendCommands()
.epilogue(`For more information, see https://cloud.google.com/vision/docs`)
.epilogue('For more information, see https://cloud.google.com/vision/docs')
.help()
.strict().argv;
5 changes: 2 additions & 3 deletions vision/samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "nodejs-docs-samples-vision",
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
"author": "Google LLC",
"engines": {
"node": ">=8"
"node": ">=10.17.0"
},
"files": [
"*.js"
Expand All @@ -14,7 +14,6 @@
},
"dependencies": {
"@google-cloud/vision": "^2.1.2",
"mathjs": "^7.0.0",
"natural": "^2.0.0",
"pureimage": "^0.2.1",
"redis": "^3.0.0",
Expand Down
15 changes: 10 additions & 5 deletions vision/samples/productSearch/addProductToProductSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

function main(projectId, location, productId, productSetId) {
// [START vision_product_search_add_product_to_product_set]
async function addProductToProductSet() {
const vision = require('@google-cloud/vision');

const client = new vision.ProductSearchClient();
const vision = require('@google-cloud/vision');
const client = new vision.ProductSearchClient();

async function addProductToProductSet() {
/**
* TODO(developer): Uncomment the following line before running the sample.
*/
Expand All @@ -44,7 +43,13 @@ function main(projectId, location, productId, productSetId) {
await client.addProductToProductSet(request);
console.log('Product added to product set.');
}
// [END vision_product_search_add_product_to_product_set]
addProductToProductSet();
// [END vision_product_search_add_product_to_product_set]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
18 changes: 11 additions & 7 deletions vision/samples/productSearch/createProduct.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ function main(
productCategory
) {
// [START vision_product_search_create_product]
async function createProduct() {
// Imports the Google Cloud client library
const vision = require('@google-cloud/vision');

// Creates a client
const client = new vision.ProductSearchClient();
// Imports the Google Cloud client library
const vision = require('@google-cloud/vision');

// Creates a client
const client = new vision.ProductSearchClient();
async function createProduct() {
/**
* TODO(developer): Uncomment the following line before running the sample.
*/
Expand All @@ -55,7 +54,12 @@ function main(
const [createdProduct] = await client.createProduct(request);
console.log(`Product name: ${createdProduct.name}`);
}
// [END vision_product_search_create_product]
createProduct();
// [END vision_product_search_create_product]
}
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
18 changes: 12 additions & 6 deletions vision/samples/productSearch/createProductSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

function main(projectId, location, productSetId, productSetDisplayName) {
// [START vision_product_search_create_product_set]
async function createProductSet() {
// Imports the Google Cloud client library
const vision = require('@google-cloud/vision');
// Imports the Google Cloud client library
const vision = require('@google-cloud/vision');

// Creates a client
const client = new vision.ProductSearchClient();
// Creates a client
const client = new vision.ProductSearchClient();

async function createProductSet() {
/**
* TODO(developer): Uncomment the following line before running the sample.
*/
Expand All @@ -47,7 +47,13 @@ function main(projectId, location, productSetId, productSetDisplayName) {
const [createdProductSet] = await client.createProductSet(request);
console.log(`Product Set name: ${createdProductSet.name}`);
}
// [END vision_product_search_create_product_set]
createProductSet();
// [END vision_product_search_create_product_set]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
14 changes: 10 additions & 4 deletions vision/samples/productSearch/createReferenceImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

function main(projectId, location, productId, referenceImageId, gcsUri) {
// [START vision_product_search_create_reference_image]
async function createReferenceImage() {
const vision = require('@google-cloud/vision');
const vision = require('@google-cloud/vision');

const client = new vision.ProductSearchClient();
const client = new vision.ProductSearchClient();

async function createReferenceImage() {
/**
* TODO(developer): Uncomment the following line before running the sample.
*/
Expand All @@ -46,7 +46,13 @@ function main(projectId, location, productId, referenceImageId, gcsUri) {
console.log(`response.name: ${response.name}`);
console.log(`response.uri: ${response.uri}`);
}
// [END vision_product_search_create_reference_image]
createReferenceImage();
// [END vision_product_search_create_reference_image]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
18 changes: 12 additions & 6 deletions vision/samples/productSearch/deleteProduct.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

function main(projectId, location, productId) {
// [START vision_product_search_delete_product]
async function deleteProduct() {
// Imports the Google Cloud client library
const vision = require('@google-cloud/vision');
// Imports the Google Cloud client library
const vision = require('@google-cloud/vision');

// Creates a client
const client = new vision.ProductSearchClient();
// Creates a client
const client = new vision.ProductSearchClient();

async function deleteProduct() {
/**
* TODO(developer): Uncomment the following line before running the sample.
*/
Expand All @@ -36,7 +36,13 @@ function main(projectId, location, productId) {
await client.deleteProduct({name: productPath});
console.log('Product deleted.');
}
// [END vision_product_search_delete_product]
deleteProduct();
// [END vision_product_search_delete_product]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
18 changes: 12 additions & 6 deletions vision/samples/productSearch/deleteProductSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

function main(projectId, location, productSetId) {
// [START vision_product_search_delete_product_set]
async function deleteProductSet() {
// Imports the Google Cloud client library
const vision = require('@google-cloud/vision');
// Imports the Google Cloud client library
const vision = require('@google-cloud/vision');

// Creates a client
const client = new vision.ProductSearchClient();
// Creates a client
const client = new vision.ProductSearchClient();

async function deleteProductSet() {
/**
* TODO(developer): Uncomment the following line before running the sample.
*/
Expand All @@ -40,7 +40,13 @@ function main(projectId, location, productSetId) {
await client.deleteProductSet({name: productSetPath});
console.log('Product set deleted.');
}
// [END vision_product_search_delete_product_set]
deleteProductSet();
// [END vision_product_search_delete_product_set]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
14 changes: 10 additions & 4 deletions vision/samples/productSearch/deleteReferenceImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

function main(projectId, location, productId, referenceImageId) {
// [START vision_product_search_delete_reference_image]
async function deleteReferenceImage() {
const vision = require('@google-cloud/vision');
const vision = require('@google-cloud/vision');

const client = new vision.ProductSearchClient();
const client = new vision.ProductSearchClient();

async function deleteReferenceImage() {
/**
* TODO(developer): Uncomment the following line before running the sample.
*/
Expand All @@ -43,7 +43,13 @@ function main(projectId, location, productId, referenceImageId) {
await client.deleteReferenceImage(request);
console.log('Reference image deleted from product.');
}
// [END vision_product_search_delete_reference_image]
deleteReferenceImage();
// [END vision_product_search_delete_reference_image]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
18 changes: 12 additions & 6 deletions vision/samples/productSearch/getProduct.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

function main(projectId, location, productId) {
// [START vision_product_search_get_product]
async function getProduct() {
// Imports the Google Cloud client library
const vision = require('@google-cloud/vision');
// Imports the Google Cloud client library
const vision = require('@google-cloud/vision');

// Creates a client
const client = new vision.ProductSearchClient();
// Creates a client
const client = new vision.ProductSearchClient();

async function getProduct() {
/**
* TODO(developer): Uncomment the following line before running the sample.
*/
Expand All @@ -41,7 +41,13 @@ function main(projectId, location, productId) {
console.log(`Product category: ${product.productCategory}`);
console.log(`Product labels: ${product.productLabels}`);
}
// [END vision_product_search_get_product]
getProduct();
// [END vision_product_search_get_product]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
Loading

0 comments on commit 2feefa4

Please sign in to comment.