Skip to content

Commit

Permalink
Code Samples demonstrating Object Localization & Handwriting OCR (#133)
Browse files Browse the repository at this point in the history
* betaPR

* Fixed Region Tags, automl up next

* fixed linting

* really actually fixed linting

* linter again

* Update .eslintignore

* update eslintignore again

* more linting

* really hate linting

* Update .eslintrc.yml

* That was harsh, lintings okay

* Update detect.v1p3beta1.js

* Update detect.v1p3beta1.js

* Fixed Region Tags

* Updated v1p3beta1.js

* Fixed detect.v1p3beta1

* Fixed test

* removed comment

* removed comment

* Updated URI paths 

I'm sorry, I'm having some issues with git here- my branch has had changes from last comments/edits, I'm sorry about redundancies

* changed to LLC

* Changing google LLC

* disable prettier for lint

* fixed //standard endpoint

* Creates a client fix

* removed JSON stringify

* changed order

* added language hinting

also fixed default handwritingGCSuri

* forgot fs.

* cleaned up spacing between api call and request

* cleaned up image examples
  • Loading branch information
CallistoCF authored and beccasaurus committed Jul 24, 2018
1 parent 901c51e commit 8da00cf
Show file tree
Hide file tree
Showing 5 changed files with 308 additions and 0 deletions.
216 changes: 216 additions & 0 deletions vision/samples/detect.v1p3beta1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
/**
* Copyright 2018, 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.
*/

/* eslint-disable */

'use strict';

function localizeObjects(fileName) {
// [START vision_localize_objects]
// Imports the Google Cloud client libraries
const vision = require('@google-cloud/vision').v1p3beta1;
const fs = require('fs');

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

/**
* TODO(developer): Uncomment the following line before running the sample.
*/
// const fileName = `/path/to/localImage.png`;

const request = {
image: {content: fs.readFileSync(fileName)},
};

client
.objectLocalization(request)
.then(results => {
const objects = results[0].localizedObjectAnnotations;
objects.forEach(object => {
console.log(`Name: ${object.name}`);
console.log(`Confidence: ${object.score}`);
const veritices = object.boundingPoly.normalizedVertices;
veritices.forEach(v => console.log(`x: ${v.x}, y:${v.y}`));
});
})
.catch(err => {
console.error('ERROR:', err);
});
// [END vision_localize_objects]
}

function localizeObjectsGCS(uri) {
// [START vision_localize_objects_uri]
// Imports the Google Cloud client libraries
const vision = require('@google-cloud/vision').v1p3beta1;
const fs = require(`fs`);

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

/**
* TODO(developer): Uncomment the following line before running the sample.
*/
// const uri = `gs://bucket/bucketImage.png`;

client
.objectLocalization(uri)
.then(results => {
const objects = results[0].localizedObjectAnnotations;
objects.forEach(object => {
console.log(`Name: ${object.name}`);
console.log(`Confidence: ${object.score}`);
const veritices = object.boundingPoly.normalizedVertices;
veritices.forEach(v => console.log(`x: ${v.x}, y:${v.y}`));
});
})
.catch(err => {
console.error('ERROR:', err);
});
// [END vision_localize_objects_uri]
}

function detectHandwritingOCR(fileName) {
// [START vision_handwritten_ocr]
// Imports the Google Cloud client libraries
const vision = require('@google-cloud/vision').v1p3beta1;

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

/**
* TODO(developer): Uncomment the following line before running the sample.
*/
// const fileName = `/path/to/localImage.png`;

const request = {
image: {
content: fs.readFileSync(fileName)
},
feature: {
languageHints: ['en-t-i0-handwrit'],
},
};
client
.documentTextDetection(request)
.then(results => {
const fullTextAnnotation = results[0].fullTextAnnotation;
console.log(`Full text: ${fullTextAnnotation.text}`);
})
.catch(err => {
console.error('ERROR:', err);
});
// [END vision_handwritten_ocr]
}

function detectHandwritingGCS(uri) {
// [START vision_handwritten_ocr_uri]
// Imports the Google Cloud client libraries
const vision = require('@google-cloud/vision').v1p3beta1;

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

/**
* TODO(developer): Uncomment the following line before running the sample.
*/
// const uri = `gs://bucket/bucketImage.png`;

const request = {
image: {
content: fs.readFileSync(uri)
},
feature: {
languageHints: ['en-t-i0-handwrit'],
},
};

client
.documentTextDetection(request)
.then(results => {
const fullTextAnnotation = results[0].fullTextAnnotation;
console.log(`Full text: ${fullTextAnnotation.text}`);
})
.catch(err => {
console.error('ERROR:', err);
});
// [END vision_handwritten_ocr_uri]
}

require(`yargs`)
.demand(1)
.command(
`localizeObjects`,
`Detects Objects in a local image file`,
{},
opts => localizeObjects(opts.fileName)
)
.command(
`localizeObjectsGCS`,
`Detects Objects Google Cloud Storage Bucket`,
{},
opts => localizeObjectsGCS(opts.gcsUri)
)
.command(
`detectHandwriting`,
`Detects handwriting in a local image file.`,
{},
opts => detectHandwritingOCR(opts.handwritingSample)
)
.command(
`detectHandwritingGCS`,
`Detects handwriting from Google Cloud Storage Bucket.`,
{},
opts => detectHandwritingGCS(opts.handwritingSample)
)
.options({
fileName: {
alias: 'f',
default: `./resources/duck_and_truck.jpg`,
global: true,
requiresArg: true,
type: 'string',
},
gcsUri: {
alias: 'u',
global: true,
requiresArg: true,
type: 'string',
},
handwritingSample: {
alias: 'h',
default: `./resources/handwritten.jpg`,
global: true,
requiresArg: true,
type: 'string',
},
handwritingGcsUri: {
alias: 'u',
default: `gs://cloud-samples-data/vision/handwritten.jpg`,
global: true,
requiresArg: true,
type: 'string',
},
})
.example(`node $0 localizeObjects -f ./resources/duck_and_truck.jpg`)
.example(`node $0 localizeObjectsGCS 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`)
.help()
.strict().argv;
Binary file added vision/samples/resources/bicycle.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added vision/samples/resources/duck_and_truck.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added vision/samples/resources/handwritten.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
92 changes: 92 additions & 0 deletions vision/samples/system-test/detect.v1p3beta1.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Copyright 2017, 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 path = require(`path`);
const storage = require(`@google-cloud/storage`)();
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);
const uuid = require(`uuid`);

const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`;
const cmd = `node detect.v1p3beta1.js`;
const cwd = path.join(__dirname, `..`);

const files = [`duck_and_truck.jpg`, `handwritten.jpg`, `bicycle.jpg`].map(
name => {
return {
name,
localPath: path.resolve(path.join(__dirname, `../resources/${name}`)),
};
}
);

test.before(tools.checkCredentials);
test.before(async () => {
const [bucket] = await storage.createBucket(bucketName);
await Promise.all(files.map(file => bucket.upload(file.localPath)));
});

test.after.always(async () => {
const bucket = storage.bucket(bucketName);
await bucket.deleteFiles({force: true});
await bucket.deleteFiles({force: true}); // Try a second time...
await bucket.delete();
});

test.before(tools.checkCredentials);

test("ObjectLocalizer should detect 'Duck', 'Bird' and 'Toy' in duck_and_truck.jpg", async t => {
const output = await tools.runAsync(
`${cmd} localizeObjects ${files[0]}`,
cwd
);
t.true(
output.includes(`Name: Bird`) &&
output.includes(`Name: Duck`) &&
output.includes(`Name: Toy`)
);
});

test(`ObjectLocalizerGCS should detect 'Bird'in duck_and_truck.jpg in GCS bucket`, async t => {
const output = await tools.runAsync(
`${cmd} localizeObjectsGCS -u gs://${bucketName}/${files[0].name}`,
cwd
);
t.true(
output.includes(`Name: Bird`) &&
output.includes(`Name: Duck`) &&
output.includes(`Name: Toy`)
);
});

test(`should read handwriting in local handwritten.jpg sample`, async t => {
const output = await tools.runAsync(
`${cmd} detectHandwriting ${files[1]}`,
cwd
);
t.true(
output.includes(`hand written message`)
);
});

test(`should read handwriting from handwritten.jpg in GCS bucket`, async t => {
const output = await tools.runAsync(
`${cmd} detectHandwritingGCS gs://${bucketName}/${files[1].name}`,
cwd
);
t.true(output.includes(`hand written message`));
});

0 comments on commit 8da00cf

Please sign in to comment.