-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-gcp.py
80 lines (56 loc) · 2.43 KB
/
test-gcp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Imports the Google Cloud client library
from google.cloud import vision
# client = vision.ImageAnnotatorClient()
# file_url = "data/IAM_lines/a01/a01-000u/a01-000u-00.png"
# image = vision.Image()
# image.source.image_uri = file_uri
def run_quickstart() -> vision.EntityAnnotation:
"""Provides a quick start example for Cloud Vision."""
# Instantiates a client
client = vision.ImageAnnotatorClient()
# The URI of the image file to annotate
# file_uri = "gs://cloud-samples-data/vision/label/wakeupcat.jpg"
file_uri = "data/IAM_lines/a01/a01-000u/a01-000u-00.png"
image = vision.Image()
image.source.image_uri = file_uri
# Performs label detection on the image file
response = client.label_detection(image=image)
labels = response.label_annotations
print("Labels:")
for label in labels:
print(label.description)
return labels
# run_quickstart()
#################################################
def detect_document(path):
"""Detects document features in an image."""
client = vision.ImageAnnotatorClient()
with open(path, "rb") as image_file:
content = image_file.read()
image = vision.Image(content=content)
response = client.document_text_detection(image=image)
for page in response.full_text_annotation.pages:
for block in page.blocks:
print(f"\nBlock confidence: {block.confidence}\n")
for paragraph in block.paragraphs:
print("Paragraph confidence: {}".format(paragraph.confidence))
for word in paragraph.words:
word_text = "".join([symbol.text for symbol in word.symbols])
print(
"Word text: {} (confidence: {})".format(
word_text, word.confidence
)
)
for symbol in word.symbols:
print(
"\tSymbol: {} (confidence: {})".format(
symbol.text, symbol.confidence
)
)
if response.error.message:
raise Exception(
"{}\nFor more info on error messages, check: "
"https://cloud.google.com/apis/design/errors".format(response.error.message)
)
# detect_document("data/IAM_lines/a01/a01-000u/a01-000u-00.png")
detect_document("attachments/eddy-pencil-handwriting.jpeg")