@@ -43,22 +43,31 @@ def draw_boxes(image, bounds, color):
4343 draw = ImageDraw .Draw (image )
4444
4545 for bound in bounds :
46- draw .polygon ([
47- bound .vertices [0 ].x , bound .vertices [0 ].y ,
48- bound .vertices [1 ].x , bound .vertices [1 ].y ,
49- bound .vertices [2 ].x , bound .vertices [2 ].y ,
50- bound .vertices [3 ].x , bound .vertices [3 ].y ], None , color )
46+ draw .polygon (
47+ [
48+ bound .vertices [0 ].x ,
49+ bound .vertices [0 ].y ,
50+ bound .vertices [1 ].x ,
51+ bound .vertices [1 ].y ,
52+ bound .vertices [2 ].x ,
53+ bound .vertices [2 ].y ,
54+ bound .vertices [3 ].x ,
55+ bound .vertices [3 ].y ,
56+ ],
57+ None ,
58+ color ,
59+ )
5160 return image
5261
5362
63+ # [START vision_document_text_tutorial_detect_bounds]
5464def get_document_bounds (image_file , feature ):
55- # [START vision_document_text_tutorial_detect_bounds]
5665 """Returns document bounds given an image."""
5766 client = vision .ImageAnnotatorClient ()
5867
5968 bounds = []
6069
61- with io .open (image_file , 'rb' ) as image_file :
70+ with io .open (image_file , "rb" ) as image_file :
6271 content = image_file .read ()
6372
6473 image = vision .Image (content = content )
@@ -72,43 +81,43 @@ def get_document_bounds(image_file, feature):
7281 for paragraph in block .paragraphs :
7382 for word in paragraph .words :
7483 for symbol in word .symbols :
75- if ( feature == FeatureType .SYMBOL ) :
84+ if feature == FeatureType .SYMBOL :
7685 bounds .append (symbol .bounding_box )
7786
78- if ( feature == FeatureType .WORD ) :
87+ if feature == FeatureType .WORD :
7988 bounds .append (word .bounding_box )
8089
81- if ( feature == FeatureType .PARA ) :
90+ if feature == FeatureType .PARA :
8291 bounds .append (paragraph .bounding_box )
8392
84- if ( feature == FeatureType .BLOCK ) :
93+ if feature == FeatureType .BLOCK :
8594 bounds .append (block .bounding_box )
8695
8796 # The list `bounds` contains the coordinates of the bounding boxes.
88- # [END vision_document_text_tutorial_detect_bounds]
8997 return bounds
98+ # [END vision_document_text_tutorial_detect_bounds]
9099
91100
92101def render_doc_text (filein , fileout ):
93102 image = Image .open (filein )
94103 bounds = get_document_bounds (filein , FeatureType .BLOCK )
95- draw_boxes (image , bounds , ' blue' )
104+ draw_boxes (image , bounds , " blue" )
96105 bounds = get_document_bounds (filein , FeatureType .PARA )
97- draw_boxes (image , bounds , ' red' )
106+ draw_boxes (image , bounds , " red" )
98107 bounds = get_document_bounds (filein , FeatureType .WORD )
99- draw_boxes (image , bounds , ' yellow' )
108+ draw_boxes (image , bounds , " yellow" )
100109
101110 if fileout != 0 :
102111 image .save (fileout )
103112 else :
104113 image .show ()
105114
106115
107- if __name__ == ' __main__' :
116+ if __name__ == " __main__" :
108117 # [START vision_document_text_tutorial_run_application]
109118 parser = argparse .ArgumentParser ()
110- parser .add_argument (' detect_file' , help = ' The image for text detection.' )
111- parser .add_argument (' -out_file' , help = ' Optional output file' , default = 0 )
119+ parser .add_argument (" detect_file" , help = " The image for text detection." )
120+ parser .add_argument (" -out_file" , help = " Optional output file" , default = 0 )
112121 args = parser .parse_args ()
113122
114123 render_doc_text (args .detect_file , args .out_file )
0 commit comments