Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 785b2b7

Browse filesBrowse files
nnegreyandrewsg
authored andcommitted
Move ocr pdf/tiff samples to GA (GoogleCloudPlatform#1522)
* Move ocr pdf/tiff samples to GA * Remove blank spaces and fragment
1 parent b734ad2 commit 785b2b7
Copy full SHA for 785b2b7

File tree

Expand file treeCollapse file tree

4 files changed

+110
-4
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+110
-4
lines changed

‎vision/cloud-client/detect/README.rst

Copy file name to clipboardExpand all lines: vision/cloud-client/detect/README.rst
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ To run this sample:
8181
$ python detect.py
8282
8383
usage: detect.py [-h]
84-
{faces,faces-uri,labels,labels-uri,landmarks,landmarks-uri,text,text-uri,logos,logos-uri,safe-search,safe-search-uri,properties,properties-uri,web,web-uri,web-geo,web-geo-uri,crophints,crophints-uri,document,document-uri}
84+
{faces,faces-uri,labels,labels-uri,landmarks,landmarks-uri,text,text-uri,logos,logos-uri,safe-search,safe-search-uri,properties,properties-uri,web,web-uri,web-geo,web-geo-uri,crophints,crophints-uri,document,document-uri,ocr-uri}
8585
...
8686
8787
This application demonstrates how to perform basic operations with the
@@ -94,12 +94,13 @@ To run this sample:
9494
python detect.py web-uri http://wheresgus.com/dog.JPG
9595
python detect.py web-geo ./resources/city.jpg
9696
python detect.py faces-uri gs://your-bucket/file.jpg
97+
python detect_pdf.py ocr-uri gs://python-docs-samples-tests/HodgeConj.pdf gs://BUCKET_NAME/PREFIX/
9798
9899
For more information, the documentation at
99100
https://cloud.google.com/vision/docs.
100101
101102
positional arguments:
102-
{faces,faces-uri,labels,labels-uri,landmarks,landmarks-uri,text,text-uri,logos,logos-uri,safe-search,safe-search-uri,properties,properties-uri,web,web-uri,web-geo,web-geo-uri,crophints,crophints-uri,document,document-uri}
103+
{faces,faces-uri,labels,labels-uri,landmarks,landmarks-uri,text,text-uri,logos,logos-uri,safe-search,safe-search-uri,properties,properties-uri,web,web-uri,web-geo,web-geo-uri,crophints,crophints-uri,document,document-uri,ocr-uri}
103104
faces Detects faces in an image.
104105
faces-uri Detects faces in the file located in Google Cloud
105106
Storage or the web.
@@ -135,6 +136,7 @@ To run this sample:
135136
document Detects document features in an image.
136137
document-uri Detects document features in the file located in
137138
Google Cloud Storage.
139+
ocr-uri OCR with PDF/TIFF as source files on GCS
138140
139141
optional arguments:
140142
-h, --help show this help message and exit

‎vision/cloud-client/detect/detect.py

Copy file name to clipboardExpand all lines: vision/cloud-client/detect/detect.py
+83-1Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,20 @@
2424
python detect.py web-uri http://wheresgus.com/dog.JPG
2525
python detect.py web-geo ./resources/city.jpg
2626
python detect.py faces-uri gs://your-bucket/file.jpg
27+
python detect_pdf.py ocr-uri gs://python-docs-samples-tests/HodgeConj.pdf \
28+
gs://BUCKET_NAME/PREFIX/
2729
2830
For more information, the documentation at
2931
https://cloud.google.com/vision/docs.
3032
"""
3133

3234
import argparse
3335
import io
36+
import re
3437

38+
from google.cloud import storage
3539
from google.cloud import vision
40+
from google.protobuf import json_format
3641

3742

3843
# [START def_detect_faces]
@@ -636,6 +641,76 @@ def detect_document_uri(uri):
636641
# [END def_detect_document_uri]
637642

638643

644+
# [START vision_async_detect_document_ocr]
645+
def async_detect_document(gcs_source_uri, gcs_destination_uri):
646+
"""OCR with PDF/TIFF as source files on GCS"""
647+
# Supported mime_types are: 'application/pdf' and 'image/tiff'
648+
mime_type = 'application/pdf'
649+
650+
# How many pages should be grouped into each json output file.
651+
batch_size = 2
652+
653+
client = vision.ImageAnnotatorClient()
654+
655+
feature = vision.types.Feature(
656+
type=vision.enums.Feature.Type.DOCUMENT_TEXT_DETECTION)
657+
658+
gcs_source = vision.types.GcsSource(uri=gcs_source_uri)
659+
input_config = vision.types.InputConfig(
660+
gcs_source=gcs_source, mime_type=mime_type)
661+
662+
gcs_destination = vision.types.GcsDestination(uri=gcs_destination_uri)
663+
output_config = vision.types.OutputConfig(
664+
gcs_destination=gcs_destination, batch_size=batch_size)
665+
666+
async_request = vision.types.AsyncAnnotateFileRequest(
667+
features=[feature], input_config=input_config,
668+
output_config=output_config)
669+
670+
operation = client.async_batch_annotate_files(
671+
requests=[async_request])
672+
673+
print('Waiting for the operation to finish.')
674+
operation.result(timeout=180)
675+
676+
# Once the request has completed and the output has been
677+
# written to GCS, we can list all the output files.
678+
storage_client = storage.Client()
679+
680+
match = re.match(r'gs://([^/]+)/(.+)', gcs_destination_uri)
681+
bucket_name = match.group(1)
682+
prefix = match.group(2)
683+
684+
bucket = storage_client.get_bucket(bucket_name=bucket_name)
685+
686+
# List objects with the given prefix.
687+
blob_list = list(bucket.list_blobs(prefix=prefix))
688+
print('Output files:')
689+
for blob in blob_list:
690+
print(blob.name)
691+
692+
# Process the first output file from GCS.
693+
# Since we specified batch_size=2, the first response contains
694+
# the first two pages of the input file.
695+
output = blob_list[0]
696+
697+
json_string = output.download_as_string()
698+
response = json_format.Parse(
699+
json_string, vision.types.AnnotateFileResponse())
700+
701+
# The actual response for the first page of the input file.
702+
first_page_response = response.responses[0]
703+
annotation = first_page_response.full_text_annotation
704+
705+
# Here we print the full text from the first page.
706+
# The response contains more information:
707+
# annotation/pages/blocks/paragraphs/words/symbols
708+
# including confidence scores and bounding boxes
709+
print(u'Full text:\n{}'.format(
710+
annotation.text))
711+
# [END vision_async_detect_document_ocr]
712+
713+
639714
def run_local(args):
640715
if args.command == 'faces':
641716
detect_faces(args.path)
@@ -684,6 +759,8 @@ def run_uri(args):
684759
detect_document_uri(args.uri)
685760
elif args.command == 'web-geo-uri':
686761
web_entities_include_geo_results_uri(args.uri)
762+
elif args.command == 'ocr-uri':
763+
async_detect_document(args.uri, args.destination_uri)
687764

688765

689766
if __name__ == '__main__':
@@ -785,9 +862,14 @@ def run_uri(args):
785862
'document-uri', help=detect_document_uri.__doc__)
786863
document_uri_parser.add_argument('uri')
787864

865+
ocr_uri_parser = subparsers.add_parser(
866+
'ocr-uri', help=async_detect_document.__doc__)
867+
ocr_uri_parser.add_argument('uri')
868+
ocr_uri_parser.add_argument('destination_uri')
869+
788870
args = parser.parse_args()
789871

790-
if ('uri' in args.command):
872+
if 'uri' in args.command:
791873
run_uri(args)
792874
else:
793875
run_local(args)

‎vision/cloud-client/detect/detect_test.py

Copy file name to clipboardExpand all lines: vision/cloud-client/detect/detect_test.py
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@
1414

1515
import os
1616

17+
from google.cloud import storage
18+
1719
import detect
1820

1921
BUCKET = os.environ['CLOUD_STORAGE_BUCKET']
22+
OUTPUT_PREFIX = 'OCR_PDF_TEST_OUTPUT'
23+
GCS_SOURCE_URI = 'gs://{}/HodgeConj.pdf'.format(BUCKET)
24+
GCS_DESTINATION_URI = 'gs://{}/{}/'.format(BUCKET, OUTPUT_PREFIX)
2025

2126

2227
def test_labels(capsys):
@@ -271,3 +276,20 @@ def test_detect_crop_hints_http(capsys):
271276
detect.detect_crop_hints_uri(uri.format(BUCKET))
272277
out, _ = capsys.readouterr()
273278
assert 'bounds: (0,0)' in out
279+
280+
281+
def test_async_detect_document(capsys):
282+
storage_client = storage.Client()
283+
bucket = storage_client.get_bucket(BUCKET)
284+
assert len(list(bucket.list_blobs(prefix=OUTPUT_PREFIX))) == 0
285+
286+
detect.async_detect_document(
287+
gcs_source_uri=GCS_SOURCE_URI,
288+
gcs_destination_uri=GCS_DESTINATION_URI)
289+
out, _ = capsys.readouterr()
290+
291+
assert 'Hodge conjecture' in out
292+
assert len(list(bucket.list_blobs(prefix=OUTPUT_PREFIX))) == 3
293+
294+
for blob in bucket.list_blobs(prefix=OUTPUT_PREFIX):
295+
blob.delete()
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
google-cloud-vision==0.31.0
1+
google-cloud-vision==0.32.0
22
google-cloud-storage==1.6.0

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.