Skip to content

Commit 827807b

Browse files
chore(fixit): clean up vision web samples (GoogleCloudPlatform#10035)
* chore(fixit): clean up vision web samples * Added type hints and used f-strings --------- Co-authored-by: Balaji Subramaniam <balajismaniam@google.com>
1 parent b0e6ee6 commit 827807b

File tree

2 files changed

+49
-31
lines changed

2 files changed

+49
-31
lines changed

vision/snippets/web/web_detect.py

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,29 @@
2626
import argparse
2727

2828
from google.cloud import vision
29+
2930
# [END vision_web_detection_tutorial_imports]
3031

3132

32-
def annotate(path):
33-
"""Returns web annotations given the path to an image."""
33+
def annotate(path: str) -> vision.WebDetection:
34+
"""Returns web annotations given the path to an image.
35+
36+
Args:
37+
path: path to the input image.
38+
39+
Returns:
40+
An WebDetection object with relevant information of the
41+
image from the internet (i.e., the annotations).
42+
"""
3443
# [START vision_web_detection_tutorial_annotate]
3544
client = vision.ImageAnnotatorClient()
3645

37-
if path.startswith('http') or path.startswith('gs:'):
46+
if path.startswith("http") or path.startswith("gs:"):
3847
image = vision.Image()
3948
image.source.image_uri = path
4049

4150
else:
42-
with open(path, 'rb') as image_file:
51+
with open(path, "rb") as image_file:
4352
content = image_file.read()
4453

4554
image = vision.Image(content=content)
@@ -50,48 +59,58 @@ def annotate(path):
5059
return web_detection
5160

5261

53-
def report(annotations):
54-
"""Prints detected features in the provided web annotations."""
62+
def report(annotations: vision.WebDetection) -> None:
63+
"""Prints detected features in the provided web annotations.
64+
65+
Args:
66+
annotations: The web annotations (WebDetection object) from which
67+
the features should be parsed and printed.
68+
"""
5569
# [START vision_web_detection_tutorial_print_annotations]
5670
if annotations.pages_with_matching_images:
57-
print('\n{} Pages with matching images retrieved'.format(
58-
len(annotations.pages_with_matching_images)))
71+
print(
72+
f"\n{len(annotations.pages_with_matching_images)} Pages with matching images retrieved"
73+
)
5974

6075
for page in annotations.pages_with_matching_images:
61-
print(f'Url : {page.url}')
76+
print(f"Url : {page.url}")
6277

6378
if annotations.full_matching_images:
64-
print('\n{} Full Matches found: '.format(
65-
len(annotations.full_matching_images)))
79+
print(
80+
f"\n{len(annotations.full_matching_images)} Full Matches found: "
81+
)
6682

6783
for image in annotations.full_matching_images:
68-
print(f'Url : {image.url}')
84+
print(f"Url : {image.url}")
6985

7086
if annotations.partial_matching_images:
71-
print('\n{} Partial Matches found: '.format(
72-
len(annotations.partial_matching_images)))
87+
print(
88+
f"\n{len(annotations.partial_matching_images)} Partial Matches found: "
89+
)
7390

7491
for image in annotations.partial_matching_images:
75-
print(f'Url : {image.url}')
92+
print(f"Url : {image.url}")
7693

7794
if annotations.web_entities:
78-
print('\n{} Web entities found: '.format(
79-
len(annotations.web_entities)))
95+
print(f"\n{len(annotations.web_entities)} Web entities found: ")
8096

8197
for entity in annotations.web_entities:
82-
print(f'Score : {entity.score}')
83-
print(f'Description: {entity.description}')
98+
print(f"Score : {entity.score}")
99+
print(f"Description: {entity.description}")
84100
# [END vision_web_detection_tutorial_print_annotations]
85101

86102

87-
if __name__ == '__main__':
103+
if __name__ == "__main__":
88104
# [START vision_web_detection_tutorial_run_application]
89105
parser = argparse.ArgumentParser(
90106
description=__doc__,
91-
formatter_class=argparse.RawDescriptionHelpFormatter)
92-
path_help = str('The image to detect, can be web URI, '
93-
'Google Cloud Storage, or path to local file.')
94-
parser.add_argument('image_url', help=path_help)
107+
formatter_class=argparse.RawDescriptionHelpFormatter,
108+
)
109+
path_help = str(
110+
"The image to detect, can be web URI, "
111+
"Google Cloud Storage, or path to local file."
112+
)
113+
parser.add_argument("image_url", help=path_help)
95114
args = parser.parse_args()
96115

97116
report(annotate(args.image_url))

vision/snippets/web/web_detect_test.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@
2020

2121

2222
@pytest.mark.flaky(max_runs=3, min_passes=1)
23-
def test_detect_file(capsys):
24-
file_name = ('../detect/resources/landmark.jpg')
23+
def test_detect_file(capsys) -> None:
24+
file_name = "../detect/resources/landmark.jpg"
2525
web_detect.report(web_detect.annotate(file_name))
2626
out, _ = capsys.readouterr()
27-
assert 'description' in out.lower()
27+
assert "description" in out.lower()
2828

2929

3030
@pytest.mark.flaky(max_runs=3, min_passes=1)
31-
def test_detect_web_gsuri(capsys):
32-
file_name = ('gs://{}/vision/landmark/pofa.jpg'.format(
33-
ASSET_BUCKET))
31+
def test_detect_web_gsuri(capsys) -> None:
32+
file_name = "gs://{}/vision/landmark/pofa.jpg".format(ASSET_BUCKET)
3433
web_detect.report(web_detect.annotate(file_name))
3534
out, _ = capsys.readouterr()
36-
assert 'description:' in out.lower()
35+
assert "description:" in out.lower()

0 commit comments

Comments
 (0)