@@ -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+
377576require ( `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` )
0 commit comments