Created
May 11, 2019 00:38
-
-
Save cneud/04be29b586f7ecf198def7ba99e2712c to your computer and use it in GitHub Desktop.
Google Cloud Vision OCR Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Copyright 2017 Google Inc. All Rights Reserved. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
"""This application demonstrates how to perform fulltext detection with the | |
Google Cloud Vision API. | |
Example Usage: | |
python gcvocr.py text ./resources/wakeupcat.jpg | |
For more information, the documentation at | |
https://cloud.google.com/vision/docs. | |
""" | |
import argparse | |
import io | |
import re | |
# [START vision_fulltext_detection] | |
def detect_document(path): | |
"""Detects document features in an image.""" | |
from google.cloud import vision | |
client = vision.ImageAnnotatorClient() | |
# [START vision_python_migration_document_text_detection] | |
with io.open(path, 'rb') as image_file: | |
content = image_file.read() | |
image = vision.types.Image(content=content) | |
response = client.document_text_detection(image=image) | |
for page in response.full_text_annotation.pages: | |
for block in page.blocks: | |
print('\nBlock confidence: {}\n'.format(block.confidence)) | |
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)) | |
# [END vision_python_migration_document_text_detection] | |
# [END vision_fulltext_detection] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment