|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2017 Google Inc. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Outlines document text given an image. |
| 18 | +
|
| 19 | +Example: |
| 20 | + python doctext.py resources/text_menu.jpg |
| 21 | +""" |
| 22 | +# [START full_tutorial] |
| 23 | +# [START imports] |
| 24 | +import argparse |
| 25 | +from enum import Enum |
| 26 | +import io |
| 27 | + |
| 28 | +from google.cloud import vision |
| 29 | +from PIL import Image, ImageDraw |
| 30 | +# [END imports] |
| 31 | + |
| 32 | + |
| 33 | +class FeatureType(Enum): |
| 34 | + PAGE = 1 |
| 35 | + BLOCK = 2 |
| 36 | + PARA = 3 |
| 37 | + WORD = 4 |
| 38 | + SYMBOL = 5 |
| 39 | + |
| 40 | + |
| 41 | +def draw_boxes(image, blocks, color): |
| 42 | + """Draw a border around the image using the hints in the vector list.""" |
| 43 | + # [START draw_blocks] |
| 44 | + draw = ImageDraw.Draw(image) |
| 45 | + |
| 46 | + for block in blocks: |
| 47 | + draw.polygon([ |
| 48 | + block.vertices[0].x, block.vertices[0].y, |
| 49 | + block.vertices[1].x, block.vertices[1].y, |
| 50 | + block.vertices[2].x, block.vertices[2].y, |
| 51 | + block.vertices[3].x, block.vertices[3].y], None, color) |
| 52 | + return image |
| 53 | + # [END draw_blocks] |
| 54 | + |
| 55 | + |
| 56 | +def get_document_bounds(image_file, feature): |
| 57 | + # [START detect_bounds] |
| 58 | + """Returns document bounds given an image.""" |
| 59 | + vision_client = vision.Client() |
| 60 | + |
| 61 | + bounds = [] |
| 62 | + |
| 63 | + with io.open(image_file, 'rb') as image_file: |
| 64 | + content = image_file.read() |
| 65 | + |
| 66 | + image = vision_client.image(content=content) |
| 67 | + document = image.detect_full_text() |
| 68 | + |
| 69 | + # Collect specified feature bounds by enumerating all document features |
| 70 | + for page in document.pages: |
| 71 | + for block in page.blocks: |
| 72 | + for paragraph in block.paragraphs: |
| 73 | + for word in paragraph.words: |
| 74 | + for symbol in word.symbols: |
| 75 | + if (feature == FeatureType.SYMBOL): |
| 76 | + bounds.append(symbol.bounding_box) |
| 77 | + |
| 78 | + if (feature == FeatureType.WORD): |
| 79 | + bounds.append(word.bounding_box) |
| 80 | + |
| 81 | + if (feature == FeatureType.PARA): |
| 82 | + bounds.append(paragraph.bounding_box) |
| 83 | + |
| 84 | + if (feature == FeatureType.BLOCK): |
| 85 | + bounds.append(block.bounding_box) |
| 86 | + |
| 87 | + if (feature == FeatureType.PAGE): |
| 88 | + bounds.append(block.bounding_box) |
| 89 | + |
| 90 | + return bounds |
| 91 | + # [END detect_bounds] |
| 92 | + |
| 93 | + |
| 94 | +def render_doc_text(filein, fileout): |
| 95 | + # [START render_doc_text] |
| 96 | + image = Image.open(filein) |
| 97 | + bounds = get_document_bounds(filein, FeatureType.PAGE) |
| 98 | + draw_boxes(image, bounds, 'blue') |
| 99 | + bounds = get_document_bounds(filein, FeatureType.PARA) |
| 100 | + draw_boxes(image, bounds, 'red') |
| 101 | + bounds = get_document_bounds(filein, FeatureType.WORD) |
| 102 | + draw_boxes(image, bounds, 'yellow') |
| 103 | + |
| 104 | + if fileout is not 0: |
| 105 | + image.save(fileout) |
| 106 | + else: |
| 107 | + image.show() |
| 108 | + # [END render_doc_text] |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == '__main__': |
| 112 | + # [START run_crop] |
| 113 | + parser = argparse.ArgumentParser() |
| 114 | + parser.add_argument('detect_file', help='The image for text detection.') |
| 115 | + parser.add_argument('-out_file', help='Optional output file', default=0) |
| 116 | + args = parser.parse_args() |
| 117 | + |
| 118 | + parser = argparse.ArgumentParser() |
| 119 | + render_doc_text(args.detect_file, args.out_file) |
| 120 | + # [END run_crop] |
| 121 | +# [END full_tutorial] |
0 commit comments