/* https://stackoverflow.com/questions/57071788/google-vision-api-text-detection-display-words-by-block https://gist.github.com/UBISOFT-1/f00e4d22790f4af378d70b237fa56ca9 response = client.text_detection(image=image) # The actual response for the first page of the input file. breaks = vision.enums.TextAnnotation.DetectedBreak.BreakType paragraphs = [] lines = [] # extract text by block of detection for page in response.full_text_annotation.pages: for block in page.blocks: for paragraph in block.paragraphs: para = "" line = "" suppose = str(paragraph.bounding_box) suppose = suppose.replace('vertices ','') print(suppose) for word in paragraph.words: for symbol in word.symbols: line += symbol.text if symbol.property.detected_break.type == breaks.SPACE: line += ' ' if symbol.property.detected_break.type == breaks.EOL_SURE_SPACE: line += ' ' lines.append(line) para += line line = '' if symbol.property.detected_break.type == breaks.LINE_BREAK: lines.append(line) para += line line = '' paragraphs.append(para) return "\n".join(paragraphs) https://blog.searce.com/tips-tricks-for-using-google-vision-api-for-text-detection-2d6d1e0c6361 def draw_boxes(image, bounds, color,width=5): draw = ImageDraw.Draw(image) for bound in bounds: draw.line([ bound.vertices[0].x, bound.vertices[0].y, bound.vertices[1].x, bound.vertices[1].y, bound.vertices[2].x, bound.vertices[2].y, bound.vertices[3].x, bound.vertices[3].y, bound.vertices[0].x, bound.vertices[0].y],fill=color, width=width) return image def get_document_bounds(response, feature): for i,page in enumerate(document.pages): for block in page.blocks: if feature==FeatureType.BLOCK: bounds.append(block.bounding_box) for paragraph in block.paragraphs: if feature==FeatureType.PARA: bounds.append(paragraph.bounding_box) for word in paragraph.words: for symbol in word.symbols: if (feature == FeatureType.SYMBOL): bounds.append(symbol.bounding_box) if (feature == FeatureType.WORD): bounds.append(word.bounding_box) return bounds bounds=get_document_bounds(response, FeatureType.WORD) draw_boxes(image,bounds, 'yellow') */