Skip to content

Commit 443610f

Browse files
gguussAce Nassri
authored andcommitted
Adds vision 1.1 features (#318)
1 parent b05f5ec commit 443610f

File tree

3 files changed

+284
-2
lines changed

3 files changed

+284
-2
lines changed

vision/samples/detect.js

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,205 @@ function detectSafeSearchGCS (bucketName, fileName) {
374374
// [END vision_safe_search_detection_gcs]
375375
}
376376

377+
function detectCropHints (fileName) {
378+
// [START vision_crop_hint_detection]
379+
380+
// Imports the Google Cloud client library
381+
const Vision = require('@google-cloud/vision');
382+
383+
// Instantiates a client
384+
const vision = Vision();
385+
386+
// The path to the local image file, e.g. "/path/to/image.png"
387+
// const fileName = 'my-file.jpg';
388+
389+
// Find crop hints for the local file
390+
vision.detectCrops(fileName)
391+
.then((data) => {
392+
const cropHints = data[0];
393+
394+
cropHints.forEach((hintBounds, hintIdx) => {
395+
console.log(`Crop Hint ${hintIdx}:`);
396+
hintBounds.forEach((bound, boundIdx) => {
397+
console.log(` Bound ${boundIdx}: (${bound.x}, ${bound.y})`);
398+
});
399+
});
400+
});
401+
// [END vision_crop_hint_detection]
402+
}
403+
404+
function detectCropHintsGCS (bucketName, fileName) {
405+
// [START vision_crop_hint_detection_gcs]
406+
407+
// Imports the Google Cloud client libraries
408+
const Storage = require('@google-cloud/storage');
409+
const Vision = require('@google-cloud/vision');
410+
411+
// Instantiates clients
412+
const storage = Storage();
413+
const vision = Vision();
414+
415+
// The name of the bucket where the file resides, e.g. "my-bucket"
416+
// const bucketName = 'my-bucket';
417+
418+
// The path to the file within the bucket, e.g. "path/to/image.png"
419+
// const fileName = 'my-file.jpg';
420+
421+
// Find crop hints for the remote file
422+
vision.detectCrops(storage.bucket(bucketName).file(fileName))
423+
.then((data) => {
424+
const cropHints = data[0];
425+
426+
cropHints.forEach((hintBounds, hintIdx) => {
427+
console.log(`Crop Hint ${hintIdx}:`);
428+
hintBounds.forEach((bound, boundIdx) => {
429+
console.log(` Bound ${boundIdx}: (${bound.x}, ${bound.y})`);
430+
});
431+
});
432+
});
433+
// [END vision_crop_hint_detection_gcs]
434+
}
435+
436+
function detectWeb (fileName) {
437+
// [START vision_web_detection]
438+
439+
// Imports the Google Cloud client library
440+
const Vision = require('@google-cloud/vision');
441+
442+
// Instantiates a client
443+
const vision = Vision();
444+
445+
// The path to the local image file, e.g. "/path/to/image.png"
446+
// const fileName = 'my-file.jpg';
447+
448+
// Detect similar images on the web to a local file
449+
vision.detectSimilar(fileName)
450+
.then((data) => {
451+
const results = data[1].responses[0].webDetection;
452+
453+
if (results.fullMatchingImages.length > 0) {
454+
console.log(`Full matches found: ${results.fullMatchingImages.length}`);
455+
results.fullMatchingImages.forEach((image) => {
456+
console.log(` URL: ${image.url}`);
457+
console.log(` Score: ${image.score}`);
458+
});
459+
}
460+
461+
if (results.partialMatchingImages.length > 0) {
462+
console.log(`Partial matches found: ${results.partialMatchingImages.length}`);
463+
results.partialMatchingImages.forEach((image) => {
464+
console.log(` URL: ${image.url}`);
465+
console.log(` Score: ${image.score}`);
466+
});
467+
}
468+
469+
if (results.webEntities.length > 0) {
470+
console.log(`Web entities found: ${results.webEntities.length}`);
471+
results.webEntities.forEach((webEntity) => {
472+
console.log(` Description: ${webEntity.description}`);
473+
console.log(` Score: ${webEntity.score}`);
474+
});
475+
}
476+
});
477+
// [END vision_web_detection]
478+
}
479+
480+
function detectWebGCS (bucketName, fileName) {
481+
// [START vision_web_detection_gcs]
482+
483+
// Imports the Google Cloud client libraries
484+
const Storage = require('@google-cloud/storage');
485+
const Vision = require('@google-cloud/vision');
486+
487+
// Instantiates clients
488+
const storage = Storage();
489+
const vision = Vision();
490+
491+
// The name of the bucket where the file resides, e.g. "my-bucket"
492+
// const bucketName = 'my-bucket';
493+
494+
// The path to the file within the bucket, e.g. "path/to/image.png"
495+
// const fileName = 'my-file.jpg';
496+
497+
// Detect similar images on the web to a remote file
498+
vision.detectSimilar(storage.bucket(bucketName).file(fileName))
499+
.then((data) => {
500+
const results = data[1].responses[0].webDetection;
501+
502+
if (results.fullMatchingImages.length > 0) {
503+
console.log(`Full matches found: ${results.fullMatchingImages.length}`);
504+
results.fullMatchingImages.forEach((image) => {
505+
console.log(` URL: ${image.url}`);
506+
console.log(` Score: ${image.score}`);
507+
});
508+
}
509+
510+
if (results.partialMatchingImages.length > 0) {
511+
console.log(`Partial matches found: ${results.partialMatchingImages.length}`);
512+
results.partialMatchingImages.forEach((image) => {
513+
console.log(` URL: ${image.url}`);
514+
console.log(` Score: ${image.score}`);
515+
});
516+
}
517+
518+
if (results.webEntities.length > 0) {
519+
console.log(`Web entities found: ${results.webEntities.length}`);
520+
results.webEntities.forEach((webEntity) => {
521+
console.log(` Description: ${webEntity.description}`);
522+
console.log(` Score: ${webEntity.score}`);
523+
});
524+
}
525+
});
526+
// [END vision_web_detection_gcs]
527+
}
528+
529+
function detectFulltext (fileName) {
530+
// [START vision_fulltext_detection]
531+
532+
// Imports the Google Cloud client library
533+
const Vision = require('@google-cloud/vision');
534+
535+
// Instantiates a client
536+
const vision = Vision();
537+
538+
// The path to the local image file, e.g. "/path/to/image.png"
539+
// const fileName = 'my-file.jpg';
540+
541+
// // Read a local image as a text document
542+
vision.readDocument(fileName)
543+
.then((data) => {
544+
const results = data[1].responses[0].fullTextAnnotation;
545+
console.log(results.text);
546+
});
547+
// [END vision_fulltext_detection]
548+
}
549+
550+
function detectFulltextGCS (bucketName, fileName) {
551+
// [START vision_fulltext_detection_gcs]
552+
553+
// Imports the Google Cloud client libraries
554+
const Storage = require('@google-cloud/storage');
555+
const Vision = require('@google-cloud/vision');
556+
557+
// Instantiates clients
558+
const storage = Storage();
559+
const vision = Vision();
560+
561+
// The name of the bucket where the file resides, e.g. "my-bucket"
562+
// const bucketName = 'my-bucket';
563+
564+
// The path to the file within the bucket, e.g. "path/to/image.png"
565+
// const fileName = 'my-file.jpg';
566+
567+
// Read a remote image as a text document
568+
vision.readDocument(storage.bucket(bucketName).file(fileName))
569+
.then((data) => {
570+
const results = data[1].responses[0].fullTextAnnotation;
571+
console.log(results.text);
572+
});
573+
// [END vision_fulltext_detection_gcs]
574+
}
575+
377576
require(`yargs`)
378577
.demand(1)
379578
.command(
@@ -460,6 +659,42 @@ require(`yargs`)
460659
{},
461660
(opts) => detectSafeSearchGCS(opts.bucket, opts.fileName)
462661
)
662+
.command(
663+
`crops <fileName>`,
664+
`Detects crop hints in a local image file.`,
665+
{},
666+
(opts) => detectCropHints(opts.fileName)
667+
)
668+
.command(
669+
`crops-gcs <bucket> <fileName>`,
670+
`Detects crop hints in an image in Google Cloud Storage.`,
671+
{},
672+
(opts) => detectCropHintsGCS(opts.bucket, opts.fileName)
673+
)
674+
.command(
675+
`web <fileName>`,
676+
`Finds similar photos on the web for a local image file.`,
677+
{},
678+
(opts) => detectWeb(opts.fileName)
679+
)
680+
.command(
681+
`web-gcs <bucket> <fileName>`,
682+
`Finds similar photos on the web for an image in Google Cloud Storage.`,
683+
{},
684+
(opts) => detectWebGCS(opts.bucket, opts.fileName)
685+
)
686+
.command(
687+
`fulltext <fileName>`,
688+
`Extracts full text from a local image file.`,
689+
{},
690+
(opts) => detectFulltext(opts.fileName)
691+
)
692+
.command(
693+
`fulltext-gcs <bucket> <fileName>`,
694+
`Extracts full text from an image in Google Cloud Storage.`,
695+
{},
696+
(opts) => detectFulltextGCS(opts.bucket, opts.fileName)
697+
)
463698
.example(`node $0 faces ./resources/face_no_surprise.jpg`)
464699
.example(`node $0 faces-gcs my-bucket your-image.jpg`)
465700
.example(`node $0 labels ./resources/wakeupcat.jpg`)
@@ -474,6 +709,12 @@ require(`yargs`)
474709
.example(`node $0 properties-gcs my-bucket your-image.jpg`)
475710
.example(`node $0 safe-search ./resources/wakeupcat.jpg`)
476711
.example(`node $0 safe-search-gcs my-bucket your-image.jpg`)
712+
.example(`node $0 crops ./resources/wakeupcat.jpg`)
713+
.example(`node $0 crops-gcs my-bucket your-image.jpg`)
714+
.example(`node $0 web ./resources/wakeupcat.jpg`)
715+
.example(`node $0 web-gcs my-bucket your-image.jpg`)
716+
.example(`node $0 fulltext ./resources/wakeupcat.jpg`)
717+
.example(`node $0 fulltext-gcs my-bucket your-image.jpg`)
477718
.wrap(120)
478719
.recommendCommands()
479720
.epilogue(`For more information, see https://cloud.google.com/vision/docs`)

vision/samples/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"dependencies": {
1111
"@google-cloud/storage": "0.7.0",
12-
"@google-cloud/vision": "0.8.0",
12+
"@google-cloud/vision": "0.9.0",
1313
"async": "2.1.4",
1414
"natural": "0.4.0",
1515
"redis": "2.6.5",

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ const files = [
2929
`landmark.jpg`,
3030
`logos.png`,
3131
`text.jpg`,
32-
`wakeupcat.jpg`
32+
`wakeupcat.jpg`,
33+
`faulkner.jpg`
3334
].map((name) => {
3435
return {
3536
name,
@@ -130,3 +131,43 @@ test(`should detect safe-search in a remote file`, async (t) => {
130131
const output = await runAsync(`${cmd} safe-search-gcs ${bucketName} ${files[4].name}`, cwd);
131132
t.true(output.includes(`Medical:`));
132133
});
134+
135+
test(`should detect crop hints in a local file`, async (t) => {
136+
const output = await runAsync(`${cmd} crops ${files[2].localPath}`, cwd);
137+
t.true(output.includes(`Crop Hint 0:`));
138+
t.true(output.includes(`Bound 2: (280, 43)`));
139+
});
140+
141+
test(`should detect crop hints in a remote file`, async (t) => {
142+
const output = await runAsync(`${cmd} crops-gcs ${bucketName} ${files[2].name}`, cwd);
143+
t.true(output.includes(`Crop Hint 0:`));
144+
t.true(output.includes(`Bound 2: (280, 43)`));
145+
});
146+
147+
test(`should detect similar web images in a local file`, async (t) => {
148+
const output = await runAsync(`${cmd} web ${files[5].localPath}`, cwd);
149+
t.true(output.includes('Full matches found: 5'));
150+
t.true(output.includes('URL: https://cloud.google.com/vision/docs/images/'));
151+
t.true(output.includes('Partial matches found: 5'));
152+
t.true(output.includes('Web entities found: 5'));
153+
t.true(output.includes('Description: Google Cloud Platform'));
154+
});
155+
156+
test(`should detect similar web images in a remote file`, async (t) => {
157+
const output = await runAsync(`${cmd} web-gcs ${bucketName} ${files[5].name}`, cwd);
158+
t.true(output.includes('Full matches found: 5'));
159+
t.true(output.includes('URL: https://cloud.google.com/vision/docs/images/'));
160+
t.true(output.includes('Partial matches found: 5'));
161+
t.true(output.includes('Web entities found: 5'));
162+
t.true(output.includes('Description: Google Cloud Platform'));
163+
});
164+
165+
test(`should read a document from a local file`, async (t) => {
166+
const output = await runAsync(`${cmd} fulltext ${files[2].localPath}`, cwd);
167+
t.true(output.includes('Google Cloud Platform'));
168+
});
169+
170+
test(`should read a document from a remote file`, async (t) => {
171+
const output = await runAsync(`${cmd} fulltext-gcs ${bucketName} ${files[2].name}`, cwd);
172+
t.true(output.includes('Google Cloud Platform'));
173+
});

0 commit comments

Comments
 (0)