Skip to content

Commit 8da00cf

Browse files
CallistoCFbeccasaurus
authored andcommitted
Code Samples demonstrating Object Localization & Handwriting OCR (#133)
* 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
1 parent 901c51e commit 8da00cf

File tree

5 files changed

+308
-0
lines changed

5 files changed

+308
-0
lines changed

vision/samples/detect.v1p3beta1.js

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
/**
2+
* Copyright 2018, 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+
/* eslint-disable */
17+
18+
'use strict';
19+
20+
function localizeObjects(fileName) {
21+
// [START vision_localize_objects]
22+
// Imports the Google Cloud client libraries
23+
const vision = require('@google-cloud/vision').v1p3beta1;
24+
const fs = require('fs');
25+
26+
// Creates a client
27+
const client = new vision.ImageAnnotatorClient();
28+
29+
/**
30+
* TODO(developer): Uncomment the following line before running the sample.
31+
*/
32+
// const fileName = `/path/to/localImage.png`;
33+
34+
const request = {
35+
image: {content: fs.readFileSync(fileName)},
36+
};
37+
38+
client
39+
.objectLocalization(request)
40+
.then(results => {
41+
const objects = results[0].localizedObjectAnnotations;
42+
objects.forEach(object => {
43+
console.log(`Name: ${object.name}`);
44+
console.log(`Confidence: ${object.score}`);
45+
const veritices = object.boundingPoly.normalizedVertices;
46+
veritices.forEach(v => console.log(`x: ${v.x}, y:${v.y}`));
47+
});
48+
})
49+
.catch(err => {
50+
console.error('ERROR:', err);
51+
});
52+
// [END vision_localize_objects]
53+
}
54+
55+
function localizeObjectsGCS(uri) {
56+
// [START vision_localize_objects_uri]
57+
// Imports the Google Cloud client libraries
58+
const vision = require('@google-cloud/vision').v1p3beta1;
59+
const fs = require(`fs`);
60+
61+
// Creates a client
62+
const client = new vision.ImageAnnotatorClient();
63+
64+
/**
65+
* TODO(developer): Uncomment the following line before running the sample.
66+
*/
67+
// const uri = `gs://bucket/bucketImage.png`;
68+
69+
client
70+
.objectLocalization(uri)
71+
.then(results => {
72+
const objects = results[0].localizedObjectAnnotations;
73+
objects.forEach(object => {
74+
console.log(`Name: ${object.name}`);
75+
console.log(`Confidence: ${object.score}`);
76+
const veritices = object.boundingPoly.normalizedVertices;
77+
veritices.forEach(v => console.log(`x: ${v.x}, y:${v.y}`));
78+
});
79+
})
80+
.catch(err => {
81+
console.error('ERROR:', err);
82+
});
83+
// [END vision_localize_objects_uri]
84+
}
85+
86+
function detectHandwritingOCR(fileName) {
87+
// [START vision_handwritten_ocr]
88+
// Imports the Google Cloud client libraries
89+
const vision = require('@google-cloud/vision').v1p3beta1;
90+
91+
// Creates a client
92+
const client = new vision.ImageAnnotatorClient();
93+
94+
/**
95+
* TODO(developer): Uncomment the following line before running the sample.
96+
*/
97+
// const fileName = `/path/to/localImage.png`;
98+
99+
const request = {
100+
image: {
101+
content: fs.readFileSync(fileName)
102+
},
103+
feature: {
104+
languageHints: ['en-t-i0-handwrit'],
105+
},
106+
};
107+
client
108+
.documentTextDetection(request)
109+
.then(results => {
110+
const fullTextAnnotation = results[0].fullTextAnnotation;
111+
console.log(`Full text: ${fullTextAnnotation.text}`);
112+
})
113+
.catch(err => {
114+
console.error('ERROR:', err);
115+
});
116+
// [END vision_handwritten_ocr]
117+
}
118+
119+
function detectHandwritingGCS(uri) {
120+
// [START vision_handwritten_ocr_uri]
121+
// Imports the Google Cloud client libraries
122+
const vision = require('@google-cloud/vision').v1p3beta1;
123+
124+
// Creates a client
125+
const client = new vision.ImageAnnotatorClient();
126+
127+
/**
128+
* TODO(developer): Uncomment the following line before running the sample.
129+
*/
130+
// const uri = `gs://bucket/bucketImage.png`;
131+
132+
const request = {
133+
image: {
134+
content: fs.readFileSync(uri)
135+
},
136+
feature: {
137+
languageHints: ['en-t-i0-handwrit'],
138+
},
139+
};
140+
141+
client
142+
.documentTextDetection(request)
143+
.then(results => {
144+
const fullTextAnnotation = results[0].fullTextAnnotation;
145+
console.log(`Full text: ${fullTextAnnotation.text}`);
146+
})
147+
.catch(err => {
148+
console.error('ERROR:', err);
149+
});
150+
// [END vision_handwritten_ocr_uri]
151+
}
152+
153+
require(`yargs`)
154+
.demand(1)
155+
.command(
156+
`localizeObjects`,
157+
`Detects Objects in a local image file`,
158+
{},
159+
opts => localizeObjects(opts.fileName)
160+
)
161+
.command(
162+
`localizeObjectsGCS`,
163+
`Detects Objects Google Cloud Storage Bucket`,
164+
{},
165+
opts => localizeObjectsGCS(opts.gcsUri)
166+
)
167+
.command(
168+
`detectHandwriting`,
169+
`Detects handwriting in a local image file.`,
170+
{},
171+
opts => detectHandwritingOCR(opts.handwritingSample)
172+
)
173+
.command(
174+
`detectHandwritingGCS`,
175+
`Detects handwriting from Google Cloud Storage Bucket.`,
176+
{},
177+
opts => detectHandwritingGCS(opts.handwritingSample)
178+
)
179+
.options({
180+
fileName: {
181+
alias: 'f',
182+
default: `./resources/duck_and_truck.jpg`,
183+
global: true,
184+
requiresArg: true,
185+
type: 'string',
186+
},
187+
gcsUri: {
188+
alias: 'u',
189+
global: true,
190+
requiresArg: true,
191+
type: 'string',
192+
},
193+
handwritingSample: {
194+
alias: 'h',
195+
default: `./resources/handwritten.jpg`,
196+
global: true,
197+
requiresArg: true,
198+
type: 'string',
199+
},
200+
handwritingGcsUri: {
201+
alias: 'u',
202+
default: `gs://cloud-samples-data/vision/handwritten.jpg`,
203+
global: true,
204+
requiresArg: true,
205+
type: 'string',
206+
},
207+
})
208+
.example(`node $0 localizeObjects -f ./resources/duck_and_truck.jpg`)
209+
.example(`node $0 localizeObjectsGCS gs://my-bucket/image.jpg`)
210+
.example(`node $0 detectHandwriting ./resources/handwritten.jpg`)
211+
.example(`node $0 detectHandwritingGCS gs://my-bucket/image.jpg`)
212+
.wrap(120)
213+
.recommendCommands()
214+
.epilogue(`For more information, see https://cloud.google.com/vision/docs`)
215+
.help()
216+
.strict().argv;
304 KB
Loading
46.4 KB
Loading
396 KB
Loading
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* Copyright 2017, 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 path = require(`path`);
19+
const storage = require(`@google-cloud/storage`)();
20+
const test = require(`ava`);
21+
const tools = require(`@google-cloud/nodejs-repo-tools`);
22+
const uuid = require(`uuid`);
23+
24+
const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`;
25+
const cmd = `node detect.v1p3beta1.js`;
26+
const cwd = path.join(__dirname, `..`);
27+
28+
const files = [`duck_and_truck.jpg`, `handwritten.jpg`, `bicycle.jpg`].map(
29+
name => {
30+
return {
31+
name,
32+
localPath: path.resolve(path.join(__dirname, `../resources/${name}`)),
33+
};
34+
}
35+
);
36+
37+
test.before(tools.checkCredentials);
38+
test.before(async () => {
39+
const [bucket] = await storage.createBucket(bucketName);
40+
await Promise.all(files.map(file => bucket.upload(file.localPath)));
41+
});
42+
43+
test.after.always(async () => {
44+
const bucket = storage.bucket(bucketName);
45+
await bucket.deleteFiles({force: true});
46+
await bucket.deleteFiles({force: true}); // Try a second time...
47+
await bucket.delete();
48+
});
49+
50+
test.before(tools.checkCredentials);
51+
52+
test("ObjectLocalizer should detect 'Duck', 'Bird' and 'Toy' in duck_and_truck.jpg", async t => {
53+
const output = await tools.runAsync(
54+
`${cmd} localizeObjects ${files[0]}`,
55+
cwd
56+
);
57+
t.true(
58+
output.includes(`Name: Bird`) &&
59+
output.includes(`Name: Duck`) &&
60+
output.includes(`Name: Toy`)
61+
);
62+
});
63+
64+
test(`ObjectLocalizerGCS should detect 'Bird'in duck_and_truck.jpg in GCS bucket`, async t => {
65+
const output = await tools.runAsync(
66+
`${cmd} localizeObjectsGCS -u gs://${bucketName}/${files[0].name}`,
67+
cwd
68+
);
69+
t.true(
70+
output.includes(`Name: Bird`) &&
71+
output.includes(`Name: Duck`) &&
72+
output.includes(`Name: Toy`)
73+
);
74+
});
75+
76+
test(`should read handwriting in local handwritten.jpg sample`, async t => {
77+
const output = await tools.runAsync(
78+
`${cmd} detectHandwriting ${files[1]}`,
79+
cwd
80+
);
81+
t.true(
82+
output.includes(`hand written message`)
83+
);
84+
});
85+
86+
test(`should read handwriting from handwritten.jpg in GCS bucket`, async t => {
87+
const output = await tools.runAsync(
88+
`${cmd} detectHandwritingGCS gs://${bucketName}/${files[1].name}`,
89+
cwd
90+
);
91+
t.true(output.includes(`hand written message`));
92+
});

0 commit comments

Comments
 (0)