in vision/snippets/detect/beta_snippets.py [0:0]
def detect_handwritten_ocr(path):
"""Detects handwritten characters in a local image.
Args:
path: The path to the local file.
"""
from google.cloud import vision_v1p3beta1 as vision
client = vision.ImageAnnotatorClient()
with open(path, "rb") as image_file:
content = image_file.read()
image = vision.Image(content=content)
# Language hint codes for handwritten OCR:
# en-t-i0-handwrit, mul-Latn-t-i0-handwrit
# Note: Use only one language hint code per request for handwritten OCR.
image_context = vision.ImageContext(language_hints=["en-t-i0-handwrit"])
response = client.document_text_detection(image=image, image_context=image_context)
print(f"Full Text: {response.full_text_annotation.text}")
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)
)