Skip to content

Commit 87e1d12

Browse files
nirupa-kumarbeccasaurus
authored andcommitted
Vision GA – vision_localize_objects & vision_localize_object_gcs (#200)
* Vision-GA - vision_localize_objects & vision_localize_object_gcs * Vision-GA - README file updates * Vision-GA - Fixes after review * Vision-GA - Fixes after review
1 parent 0d12ae8 commit 87e1d12

File tree

4 files changed

+103
-116
lines changed

4 files changed

+103
-116
lines changed

vision/samples/detect.js

+78
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,70 @@ function detectPdfText(bucketName, fileName) {
831831
// [END vision_text_detection_pdf_gcs]
832832
}
833833

834+
function localizeObjects(fileName) {
835+
// [START vision_localize_objects]
836+
// Imports the Google Cloud client libraries
837+
const vision = require('@google-cloud/vision');
838+
const fs = require('fs');
839+
840+
// Creates a client
841+
const client = new vision.ImageAnnotatorClient();
842+
843+
/**
844+
* TODO(developer): Uncomment the following line before running the sample.
845+
*/
846+
// const fileName = `/path/to/localImage.png`;
847+
const request = {
848+
image: {content: fs.readFileSync(fileName)},
849+
};
850+
851+
client
852+
.objectLocalization(request)
853+
.then(results => {
854+
const objects = results[0].localizedObjectAnnotations;
855+
objects.forEach(object => {
856+
console.log(`Name: ${object.name}`);
857+
console.log(`Confidence: ${object.score}`);
858+
const vertices = object.boundingPoly.normalizedVertices;
859+
vertices.forEach(v => console.log(`x: ${v.x}, y:${v.y}`));
860+
});
861+
})
862+
.catch(err => {
863+
console.error('ERROR:', err);
864+
});
865+
// [END vision_localize_objects]
866+
}
867+
868+
function localizeObjectsGCS(gcsUri) {
869+
// [START vision_localize_objects_gcs]
870+
// Imports the Google Cloud client libraries
871+
const vision = require('@google-cloud/vision');
872+
873+
// Creates a client
874+
const client = new vision.ImageAnnotatorClient();
875+
876+
/**
877+
* TODO(developer): Uncomment the following line before running the sample.
878+
*/
879+
// const gcsUri = `gs://bucket/bucketImage.png`;
880+
881+
client
882+
.objectLocalization(gcsUri)
883+
.then(results => {
884+
const objects = results[0].localizedObjectAnnotations;
885+
objects.forEach(object => {
886+
console.log(`Name: ${object.name}`);
887+
console.log(`Confidence: ${object.score}`);
888+
const veritices = object.boundingPoly.normalizedVertices;
889+
veritices.forEach(v => console.log(`x: ${v.x}, y:${v.y}`));
890+
});
891+
})
892+
.catch(err => {
893+
console.error('ERROR:', err);
894+
});
895+
// [END vision_localize_objects_gcs]
896+
}
897+
834898
require(`yargs`) // eslint-disable-line
835899
.demand(1)
836900
.command(
@@ -968,6 +1032,18 @@ require(`yargs`) // eslint-disable-line
9681032
{},
9691033
opts => detectPdfText(opts.bucketName, opts.fileName)
9701034
)
1035+
.command(
1036+
`localize-objects <fileName>`,
1037+
`Detects Objects in a local image file`,
1038+
{},
1039+
opts => localizeObjects(opts.fileName)
1040+
)
1041+
.command(
1042+
`localize-objects-gcs <gcsUri>`,
1043+
`Detects Objects Google Cloud Storage Bucket`,
1044+
{},
1045+
opts => localizeObjectsGCS(opts.gcsUri)
1046+
)
9711047
.example(`node $0 faces ./resources/face_no_surprise.jpg`)
9721048
.example(`node $0 faces-gcs my-bucket your-image.jpg`)
9731049
.example(`node $0 labels ./resources/wakeupcat.jpg`)
@@ -991,6 +1067,8 @@ require(`yargs`) // eslint-disable-line
9911067
.example(`node $0 fulltext ./resources/wakeupcat.jpg`)
9921068
.example(`node $0 fulltext-gcs my-bucket your-image.jpg`)
9931069
.example(`node $0 pdf my-bucket my-pdf.pdf`)
1070+
.example(`node $0 localize-objects ./resources/duck_and_truck.jpg`)
1071+
.example(`node $0 localize-objects-gcs gs://bucket/bucketImage.png`)
9941072
.wrap(120)
9951073
.recommendCommands()
9961074
.epilogue(`For more information, see https://cloud.google.com/vision/docs`)

vision/samples/detect.v1p3beta1.js

-92
Original file line numberDiff line numberDiff line change
@@ -17,71 +17,6 @@
1717

1818
'use strict';
1919

20-
function localizeObjects(fileName) {
21-
// [START vision_localize_objects_beta]
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_beta]
53-
}
54-
55-
function localizeObjectsGCS(uri) {
56-
// [START vision_localize_objects_gcs_beta]
57-
// Imports the Google Cloud client libraries
58-
const vision = require('@google-cloud/vision').v1p3beta1;
59-
60-
// Creates a client
61-
const client = new vision.ImageAnnotatorClient();
62-
63-
/**
64-
* TODO(developer): Uncomment the following line before running the sample.
65-
*/
66-
// const uri = `gs://bucket/bucketImage.png`;
67-
68-
client
69-
.objectLocalization(uri)
70-
.then(results => {
71-
const objects = results[0].localizedObjectAnnotations;
72-
objects.forEach(object => {
73-
console.log(`Name: ${object.name}`);
74-
console.log(`Confidence: ${object.score}`);
75-
const veritices = object.boundingPoly.normalizedVertices;
76-
veritices.forEach(v => console.log(`x: ${v.x}, y:${v.y}`));
77-
});
78-
})
79-
.catch(err => {
80-
console.error('ERROR:', err);
81-
});
82-
// [END vision_localize_objects_gcs_beta]
83-
}
84-
8520
function detectHandwritingOCR(fileName) {
8621
// [START vision_handwritten_ocr_beta]
8722
// Imports the Google Cloud client libraries
@@ -153,18 +88,6 @@ function detectHandwritingGCS(uri) {
15388

15489
require(`yargs`)
15590
.demand(1)
156-
.command(
157-
`localizeObjects`,
158-
`Detects Objects in a local image file`,
159-
{},
160-
opts => localizeObjects(opts.fileName)
161-
)
162-
.command(
163-
`localizeObjectsGCS`,
164-
`Detects Objects Google Cloud Storage Bucket`,
165-
{},
166-
opts => localizeObjectsGCS(opts.gcsUri)
167-
)
16891
.command(
16992
`detectHandwriting`,
17093
`Detects handwriting in a local image file.`,
@@ -178,19 +101,6 @@ require(`yargs`)
178101
opts => detectHandwritingGCS(opts.handwritingSample)
179102
)
180103
.options({
181-
fileName: {
182-
alias: 'f',
183-
default: `./resources/duck_and_truck.jpg`,
184-
global: true,
185-
requiresArg: true,
186-
type: 'string',
187-
},
188-
gcsUri: {
189-
alias: 'u',
190-
global: true,
191-
requiresArg: true,
192-
type: 'string',
193-
},
194104
handwritingSample: {
195105
alias: 'h',
196106
default: `./resources/handwritten.jpg`,
@@ -206,8 +116,6 @@ require(`yargs`)
206116
type: 'string',
207117
},
208118
})
209-
.example(`node $0 localizeObjects -f ./resources/duck_and_truck.jpg`)
210-
.example(`node $0 localizeObjectsGCS gs://my-bucket/image.jpg`)
211119
.example(`node $0 detectHandwriting ./resources/handwritten.jpg`)
212120
.example(`node $0 detectHandwritingGCS gs://my-bucket/image.jpg`)
213121
.wrap(120)

vision/samples/system-test/detect.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const files = [
3737
`faulkner.jpg`,
3838
`city.jpg`,
3939
'pdf-ocr.pdf',
40+
'duck_and_truck.jpg',
4041
].map(name => {
4142
return {
4243
name,
@@ -297,3 +298,27 @@ test(`should extract text from pdf file`, async t => {
297298
);
298299
t.true(output.includes('pdf-ocr.pdf.json'));
299300
});
301+
302+
test(`should detect objects in a local file`, async t => {
303+
const output = await tools.runAsync(
304+
`${cmd} localize-objects ${files[8].localPath}`,
305+
cwd
306+
);
307+
t.true(
308+
output.includes(`Name: Bird`) &&
309+
output.includes(`Name: Duck`) &&
310+
output.includes(`Name: Toy`)
311+
);
312+
});
313+
314+
test(`should detect objects in a remote file`, async t => {
315+
const output = await tools.runAsync(
316+
`${cmd} localize-objects-gcs gs://${bucketName}/${files[8].name}`,
317+
cwd
318+
);
319+
t.true(
320+
output.includes(`Name: Bird`) &&
321+
output.includes(`Name: Duck`) &&
322+
output.includes(`Name: Toy`)
323+
);
324+
});

vision/samples/system-test/detect.v1p3beta1.test.js

-24
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,6 @@ test.after.always(async () => {
5050

5151
test.before(tools.checkCredentials);
5252

53-
test("ObjectLocalizer should detect 'Duck', 'Bird' and 'Toy' in duck_and_truck.jpg", async t => {
54-
const output = await tools.runAsync(
55-
`${cmd} localizeObjects ${files[0]}`,
56-
cwd
57-
);
58-
t.true(
59-
output.includes(`Name: Bird`) &&
60-
output.includes(`Name: Duck`) &&
61-
output.includes(`Name: Toy`)
62-
);
63-
});
64-
65-
test(`ObjectLocalizerGCS should detect 'Bird'in duck_and_truck.jpg in GCS bucket`, async t => {
66-
const output = await tools.runAsync(
67-
`${cmd} localizeObjectsGCS -u gs://${bucketName}/${files[0].name}`,
68-
cwd
69-
);
70-
t.true(
71-
output.includes(`Name: Bird`) &&
72-
output.includes(`Name: Duck`) &&
73-
output.includes(`Name: Toy`)
74-
);
75-
});
76-
7753
test(`should read handwriting in local handwritten.jpg sample`, async t => {
7854
const output = await tools.runAsync(
7955
`${cmd} detectHandwriting ${files[1]}`,

0 commit comments

Comments
 (0)