@@ -43,22 +43,31 @@ def draw_boxes(image, bounds, color):
43
43
draw = ImageDraw .Draw (image )
44
44
45
45
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
+ )
51
60
return image
52
61
53
62
63
+ # [START vision_document_text_tutorial_detect_bounds]
54
64
def get_document_bounds (image_file , feature ):
55
- # [START vision_document_text_tutorial_detect_bounds]
56
65
"""Returns document bounds given an image."""
57
66
client = vision .ImageAnnotatorClient ()
58
67
59
68
bounds = []
60
69
61
- with io .open (image_file , 'rb' ) as image_file :
70
+ with io .open (image_file , "rb" ) as image_file :
62
71
content = image_file .read ()
63
72
64
73
image = vision .Image (content = content )
@@ -72,43 +81,43 @@ def get_document_bounds(image_file, feature):
72
81
for paragraph in block .paragraphs :
73
82
for word in paragraph .words :
74
83
for symbol in word .symbols :
75
- if ( feature == FeatureType .SYMBOL ) :
84
+ if feature == FeatureType .SYMBOL :
76
85
bounds .append (symbol .bounding_box )
77
86
78
- if ( feature == FeatureType .WORD ) :
87
+ if feature == FeatureType .WORD :
79
88
bounds .append (word .bounding_box )
80
89
81
- if ( feature == FeatureType .PARA ) :
90
+ if feature == FeatureType .PARA :
82
91
bounds .append (paragraph .bounding_box )
83
92
84
- if ( feature == FeatureType .BLOCK ) :
93
+ if feature == FeatureType .BLOCK :
85
94
bounds .append (block .bounding_box )
86
95
87
96
# The list `bounds` contains the coordinates of the bounding boxes.
88
- # [END vision_document_text_tutorial_detect_bounds]
89
97
return bounds
98
+ # [END vision_document_text_tutorial_detect_bounds]
90
99
91
100
92
101
def render_doc_text (filein , fileout ):
93
102
image = Image .open (filein )
94
103
bounds = get_document_bounds (filein , FeatureType .BLOCK )
95
- draw_boxes (image , bounds , ' blue' )
104
+ draw_boxes (image , bounds , " blue" )
96
105
bounds = get_document_bounds (filein , FeatureType .PARA )
97
- draw_boxes (image , bounds , ' red' )
106
+ draw_boxes (image , bounds , " red" )
98
107
bounds = get_document_bounds (filein , FeatureType .WORD )
99
- draw_boxes (image , bounds , ' yellow' )
108
+ draw_boxes (image , bounds , " yellow" )
100
109
101
110
if fileout != 0 :
102
111
image .save (fileout )
103
112
else :
104
113
image .show ()
105
114
106
115
107
- if __name__ == ' __main__' :
116
+ if __name__ == " __main__" :
108
117
# [START vision_document_text_tutorial_run_application]
109
118
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 )
112
121
args = parser .parse_args ()
113
122
114
123
render_doc_text (args .detect_file , args .out_file )
0 commit comments