2626import argparse
2727
2828from google .cloud import vision
29+
2930# [END vision_web_detection_tutorial_imports]
3031
3132
32- def annotate (path ):
33- """Returns web annotations given the path to an image."""
33+ def annotate (path : str ) -> vision .WebDetection :
34+ """Returns web annotations given the path to an image.
35+
36+ Args:
37+ path: path to the input image.
38+
39+ Returns:
40+ An WebDetection object with relevant information of the
41+ image from the internet (i.e., the annotations).
42+ """
3443 # [START vision_web_detection_tutorial_annotate]
3544 client = vision .ImageAnnotatorClient ()
3645
37- if path .startswith (' http' ) or path .startswith (' gs:' ):
46+ if path .startswith (" http" ) or path .startswith (" gs:" ):
3847 image = vision .Image ()
3948 image .source .image_uri = path
4049
4150 else :
42- with open (path , 'rb' ) as image_file :
51+ with open (path , "rb" ) as image_file :
4352 content = image_file .read ()
4453
4554 image = vision .Image (content = content )
@@ -50,48 +59,58 @@ def annotate(path):
5059 return web_detection
5160
5261
53- def report (annotations ):
54- """Prints detected features in the provided web annotations."""
62+ def report (annotations : vision .WebDetection ) -> None :
63+ """Prints detected features in the provided web annotations.
64+
65+ Args:
66+ annotations: The web annotations (WebDetection object) from which
67+ the features should be parsed and printed.
68+ """
5569 # [START vision_web_detection_tutorial_print_annotations]
5670 if annotations .pages_with_matching_images :
57- print ('\n {} Pages with matching images retrieved' .format (
58- len (annotations .pages_with_matching_images )))
71+ print (
72+ f"\n { len (annotations .pages_with_matching_images )} Pages with matching images retrieved"
73+ )
5974
6075 for page in annotations .pages_with_matching_images :
61- print (f' Url : { page .url } ' )
76+ print (f" Url : { page .url } " )
6277
6378 if annotations .full_matching_images :
64- print ('\n {} Full Matches found: ' .format (
65- len (annotations .full_matching_images )))
79+ print (
80+ f"\n { len (annotations .full_matching_images )} Full Matches found: "
81+ )
6682
6783 for image in annotations .full_matching_images :
68- print (f' Url : { image .url } ' )
84+ print (f" Url : { image .url } " )
6985
7086 if annotations .partial_matching_images :
71- print ('\n {} Partial Matches found: ' .format (
72- len (annotations .partial_matching_images )))
87+ print (
88+ f"\n { len (annotations .partial_matching_images )} Partial Matches found: "
89+ )
7390
7491 for image in annotations .partial_matching_images :
75- print (f' Url : { image .url } ' )
92+ print (f" Url : { image .url } " )
7693
7794 if annotations .web_entities :
78- print ('\n {} Web entities found: ' .format (
79- len (annotations .web_entities )))
95+ print (f"\n { len (annotations .web_entities )} Web entities found: " )
8096
8197 for entity in annotations .web_entities :
82- print (f' Score : { entity .score } ' )
83- print (f' Description: { entity .description } ' )
98+ print (f" Score : { entity .score } " )
99+ print (f" Description: { entity .description } " )
84100 # [END vision_web_detection_tutorial_print_annotations]
85101
86102
87- if __name__ == ' __main__' :
103+ if __name__ == " __main__" :
88104 # [START vision_web_detection_tutorial_run_application]
89105 parser = argparse .ArgumentParser (
90106 description = __doc__ ,
91- formatter_class = argparse .RawDescriptionHelpFormatter )
92- path_help = str ('The image to detect, can be web URI, '
93- 'Google Cloud Storage, or path to local file.' )
94- parser .add_argument ('image_url' , help = path_help )
107+ formatter_class = argparse .RawDescriptionHelpFormatter ,
108+ )
109+ path_help = str (
110+ "The image to detect, can be web URI, "
111+ "Google Cloud Storage, or path to local file."
112+ )
113+ parser .add_argument ("image_url" , help = path_help )
95114 args = parser .parse_args ()
96115
97116 report (annotate (args .image_url ))
0 commit comments