Skip to content

Commit 9095542

Browse files
chore(fixit): vision document text sample clean-up (GoogleCloudPlatform#10060)
* chore(fixit): vision document text sample clean-up * format changes --------- Co-authored-by: Balaji Subramaniam <balajismaniam@google.com>
1 parent 544a25a commit 9095542

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

vision/snippets/document_text/doctext.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
from google.cloud import vision
2828
from PIL import Image, ImageDraw
29+
2930
# [END vision_document_text_tutorial_imports]
3031

3132

@@ -38,7 +39,16 @@ class FeatureType(Enum):
3839

3940

4041
def draw_boxes(image, bounds, color):
41-
"""Draw a border around the image using the hints in the vector list."""
42+
"""Draws a border around the image using the hints in the vector list.
43+
44+
Args:
45+
image: the input image object.
46+
bounds: list of coordinates for the boxes.
47+
color: the color of the box.
48+
49+
Returns:
50+
An image with colored bounds added.
51+
"""
4252
draw = ImageDraw.Draw(image)
4353

4454
for bound in bounds:
@@ -61,7 +71,15 @@ def draw_boxes(image, bounds, color):
6171

6272
# [START vision_document_text_tutorial_detect_bounds]
6373
def get_document_bounds(image_file, feature):
64-
"""Returns document bounds given an image."""
74+
"""Finds the document bounds given an image and feature type.
75+
76+
Args:
77+
image_file: path to the image file.
78+
feature: feature type to detect.
79+
80+
Returns:
81+
List of coordinates for the corresponding feature type.
82+
"""
6583
client = vision.ImageAnnotatorClient()
6684

6785
bounds = []
@@ -94,10 +112,18 @@ def get_document_bounds(image_file, feature):
94112

95113
# The list `bounds` contains the coordinates of the bounding boxes.
96114
return bounds
115+
116+
97117
# [END vision_document_text_tutorial_detect_bounds]
98118

99119

100120
def render_doc_text(filein, fileout):
121+
"""Outlines document features (blocks, paragraphs and words) given an image.
122+
123+
Args:
124+
filein: path to the input image.
125+
fileout: path to the output image.
126+
"""
101127
image = Image.open(filein)
102128
bounds = get_document_bounds(filein, FeatureType.BLOCK)
103129
draw_boxes(image, bounds, "blue")

vision/snippets/document_text/doctext_test.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
import doctext
1818

1919

20-
def test_text(capsys):
20+
def test_text() -> None:
2121
"""Checks the output image for drawing the crop hint is created."""
22-
doctext.render_doc_text('resources/text_menu.jpg', 'output-text.jpg')
23-
out, _ = capsys.readouterr()
24-
assert os.path.isfile('output-text.jpg')
22+
doctext.render_doc_text("resources/text_menu.jpg", "output-text.jpg")
23+
assert os.path.isfile("output-text.jpg")

0 commit comments

Comments
 (0)