Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added two samples for "OCR with PDF/TIFF as source files" #2034

Merged
merged 17 commits into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions vision/cloud-client/detect/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,95 @@ def detect_document_uri(uri):
# [END vision_fulltext_detection_gcs]


# [START vision_fulltext_detection_pdf]
happyhuman marked this conversation as resolved.
Show resolved Hide resolved
def detect_pdf_document(path):
"""Detects document features in a PDF/TIFF/GIF file."""
from google.cloud import vision_v1p4beta1 as vision
client = vision.ImageAnnotatorClient()

with io.open(path, 'rb') as pdf_file:
content = pdf_file.read()

# Can be other like 'image/tiff' or 'image/gif'
happyhuman marked this conversation as resolved.
Show resolved Hide resolved
mime_type = 'application/pdf'
file_bytes_input_config = vision.types.InputConfig(
happyhuman marked this conversation as resolved.
Show resolved Hide resolved
content=content, mime_type=mime_type)

feature = vision.types.Feature(
type=vision.enums.Feature.Type.DOCUMENT_TEXT_DETECTION)
image_context = vision.types.ImageContext()
happyhuman marked this conversation as resolved.
Show resolved Hide resolved
# Annotate the first two pages and the last one (max 5 pages)
# First page starts at 1, and not 0. Last page is -1.
pages = [1, 2, -1]
happyhuman marked this conversation as resolved.
Show resolved Hide resolved

online_one_request = vision.types.AnnotateFileRequest(
input_config=file_bytes_input_config,
features=[feature],
image_context=image_context,
pages=pages)

response = client.batch_annotate_files(requests=[online_one_request])
happyhuman marked this conversation as resolved.
Show resolved Hide resolved

for image_response in response.responses[0].responses:
for page in image_response.full_text_annotation.pages:
for block in page.blocks:
print('\nBlock confidence: {}\n'.format(block.confidence))
for par in block.paragraphs:
print('\tParagraph confidence: {}'.format(par.confidence))
for word in par.words:
symbol_texts = [symbol.text for symbol in word.symbols]
word_text = ''.join(symbol_texts)
print('\t\tWord text: {} (confidence: {})'.format(
word_text, word.confidence))
for symbol in word.symbols:
print('\t\t\tSymbol: {} (confidence: {})'.format(
symbol.text, symbol.confidence))
# [END vision_fulltext_detection_pdf]


# [START vision_fulltext_detection_pdf_gcs]
def detect_pdf_document_from_gcs(gcs_uri):
"""Detects document features in a PDF/TIFF/GIF file."""
from google.cloud import vision_v1p4beta1 as vision
client = vision.ImageAnnotatorClient()

# Can be other like 'image/tiff' or 'image/gif'
mime_type = 'application/pdf'
file_bytes_input_config = vision.types.InputConfig(
gcs_source=vision.types.GcsSource(uri=gcs_uri), mime_type=mime_type)

feature = vision.types.Feature(
type=vision.enums.Feature.Type.DOCUMENT_TEXT_DETECTION)
image_context = vision.types.ImageContext()
# Annotate the first two pages and the last one (max 5 pages)
# First page starts at 1, and not 0. Last page is -1.
pages = [1, 2, -1]

online_one_request = vision.types.AnnotateFileRequest(
input_config=file_bytes_input_config,
features=[feature],
image_context=image_context,
pages=pages)

response = client.batch_annotate_files(requests=[online_one_request])

for image_response in response.responses[0].responses:
for page in image_response.full_text_annotation.pages:
for block in page.blocks:
print('\nBlock confidence: {}\n'.format(block.confidence))
for par in block.paragraphs:
print('\tParagraph confidence: {}'.format(par.confidence))
for word in par.words:
symbol_texts = [symbol.text for symbol in word.symbols]
word_text = ''.join(symbol_texts)
print('\t\tWord text: {} (confidence: {})'.format(
word_text, word.confidence))
for symbol in word.symbols:
print('\t\t\tSymbol: {} (confidence: {})'.format(
symbol.text, symbol.confidence))
# [END vision_fulltext_detection_pdf_gcs]


# [START vision_text_detection_pdf_gcs]
def async_detect_document(gcs_source_uri, gcs_destination_uri):
"""OCR with PDF/TIFF as source files on GCS"""
Expand Down
19 changes: 19 additions & 0 deletions vision/cloud-client/detect/detect_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,25 @@ def test_detect_document_uri(capsys):
assert 'class' in out


def test_detect_pdf_document(capsys):
file_name = os.path.join(
os.path.dirname(__file__),
'resources/metamorphosis.pdf')
detect.detect_pdf_document(file_name)
out, _ = capsys.readouterr()
assert 'Symbol' in out
assert 'Word text' in out


def test_detect_pdf_document_from_gcs(capsys):
gcs_uri = 'gs://{}/vision/document_understanding/metamorphosis.pdf' \
.format(ASSET_BUCKET)
detect.detect_pdf_document_from_gcs(gcs_uri)
out, _ = capsys.readouterr()
assert 'Symbol' in out
assert 'Word text' in out


def test_detect_document_http(capsys):
uri = 'https://storage-download.googleapis.com/{}/vision/text/screen.jpg'
detect.detect_document_uri(uri.format(ASSET_BUCKET))
Expand Down
Binary file not shown.