Skip to content

Commit 3892e9a

Browse files
committed
Add generated samples for Vision API
1 parent 9ccb348 commit 3892e9a

File tree

3 files changed

+256
-0
lines changed

3 files changed

+256
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2019 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# DO NOT EDIT! This is a generated sample ("LongRunningPromise", "vision_async_batch_annotate_images")
18+
19+
# To install the latest published package dependency, execute the following:
20+
# pip install google-cloud-vision
21+
22+
import sys
23+
24+
# [START vision_async_batch_annotate_images]
25+
26+
from google.cloud import vision_v1
27+
from google.cloud.vision_v1 import enums
28+
import six
29+
30+
def sample_async_batch_annotate_images(input_image_uri, output_uri):
31+
"""Perform async batch image annotation"""
32+
# [START vision_async_batch_annotate_images_core]
33+
34+
client = vision_v1.ImageAnnotatorClient()
35+
36+
# input_image_uri = 'gs://cloud-samples-data/vision/label/woman.jpg'
37+
# output_uri = 'gs://your-bucket/prefix/'
38+
39+
if isinstance(input_image_uri, six.binary_type):
40+
input_image_uri = input_image_uri.decode('utf-8')
41+
if isinstance(output_uri, six.binary_type):
42+
output_uri = output_uri.decode('utf-8')
43+
source = {'image_uri': input_image_uri}
44+
image = {'source': source}
45+
type_ = enums.Feature.Type.LABEL_DETECTION
46+
features_element = {'type': type_}
47+
type_2 = enums.Feature.Type.TEXT_DETECTION
48+
features_element_2 = {'type': type_2}
49+
type_3 = enums.Feature.Type.IMAGE_PROPERTIES
50+
features_element_3 = {'type': type_3}
51+
features = [features_element, features_element_2, features_element_3]
52+
requests_element = {'image': image, 'features': features}
53+
requests = [requests_element]
54+
gcs_destination = {'uri': output_uri}
55+
56+
# The max number of responses to output in each JSON file
57+
batch_size = 2
58+
output_config = {'gcs_destination': gcs_destination, 'batch_size': batch_size}
59+
60+
operation = client.async_batch_annotate_images(requests, output_config)
61+
62+
print('Waiting for operation to complete...')
63+
response = operation.result()
64+
65+
# The output is written to GCS with the provided output_uri as prefix
66+
gcs_output_uri = response.output_config.gcs_destination.uri
67+
print('Output written to GCS with prefix: {}'.format(gcs_output_uri))
68+
69+
# [END vision_async_batch_annotate_images_core]
70+
# [END vision_async_batch_annotate_images]
71+
72+
def main():
73+
import argparse
74+
75+
parser = argparse.ArgumentParser()
76+
parser.add_argument('--input_image_uri', type=str, default='gs://cloud-samples-data/vision/label/woman.jpg')
77+
parser.add_argument('--output_uri', type=str, default='gs://your-bucket/prefix/')
78+
args = parser.parse_args()
79+
80+
sample_async_batch_annotate_images(args.input_image_uri, args.output_uri)
81+
82+
if __name__ == '__main__':
83+
main()
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2019 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# DO NOT EDIT! This is a generated sample ("Request", "vision_batch_annotate_files")
18+
19+
# To install the latest published package dependency, execute the following:
20+
# pip install google-cloud-vision
21+
22+
import sys
23+
24+
# [START vision_batch_annotate_files]
25+
26+
from google.cloud import vision_v1
27+
from google.cloud.vision_v1 import enums
28+
import io
29+
import six
30+
31+
def sample_batch_annotate_files(file_path):
32+
"""
33+
Perform batch file annotation
34+
35+
Args:
36+
file_path Path to local pdf file, e.g. /path/document.pdf
37+
"""
38+
# [START vision_batch_annotate_files_core]
39+
40+
client = vision_v1.ImageAnnotatorClient()
41+
42+
# file_path = 'resources/kafka.pdf'
43+
44+
if isinstance(file_path, six.binary_type):
45+
file_path = file_path.decode('utf-8')
46+
47+
# Supported mime_type: application/pdf, image/tiff, image/gif
48+
mime_type = 'application/pdf'
49+
with io.open(file_path, 'rb') as f:
50+
content = f.read()
51+
input_config = {'mime_type': mime_type, 'content': content}
52+
type_ = enums.Feature.Type.DOCUMENT_TEXT_DETECTION
53+
features_element = {'type': type_}
54+
features = [features_element]
55+
56+
# The service can process up to 5 pages per document file. Here we specify the
57+
# first, second, and last page of the document to be processed.
58+
pages_element = 1
59+
pages_element_2 = 2
60+
pages_element_3 = -1
61+
pages = [pages_element, pages_element_2, pages_element_3]
62+
requests_element = {'input_config': input_config, 'features': features, 'pages': pages}
63+
requests = [requests_element]
64+
65+
response = client.batch_annotate_files(requests)
66+
for image_response in response.responses[0].responses:
67+
print('Full text: {}'.format(image_response.full_text_annotation.text))
68+
for page in image_response.full_text_annotation.pages:
69+
for block in page.blocks:
70+
print('\nBlock confidence: {}'.format(block.confidence))
71+
for par in block.paragraphs:
72+
print('\tParagraph confidence: {}'.format(par.confidence))
73+
for word in par.words:
74+
print('\t\tWord confidence: {}'.format(word.confidence))
75+
for symbol in word.symbols:
76+
print('\t\t\tSymbol: {}, (confidence: {})'.format(symbol.text, symbol.confidence))
77+
78+
# [END vision_batch_annotate_files_core]
79+
# [END vision_batch_annotate_files]
80+
81+
def main():
82+
import argparse
83+
84+
parser = argparse.ArgumentParser()
85+
parser.add_argument('--file_path', type=str, default='resources/kafka.pdf')
86+
args = parser.parse_args()
87+
88+
sample_batch_annotate_files(args.file_path)
89+
90+
if __name__ == '__main__':
91+
main()
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2019 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# DO NOT EDIT! This is a generated sample ("Request", "vision_batch_annotate_files_gcs")
18+
19+
# To install the latest published package dependency, execute the following:
20+
# pip install google-cloud-vision
21+
22+
import sys
23+
24+
# [START vision_batch_annotate_files_gcs]
25+
26+
from google.cloud import vision_v1
27+
from google.cloud.vision_v1 import enums
28+
import six
29+
30+
def sample_batch_annotate_files(gcs_uri):
31+
"""Perform batch file annotation"""
32+
# [START vision_batch_annotate_files_gcs_core]
33+
34+
client = vision_v1.ImageAnnotatorClient()
35+
36+
# gcs_uri = 'gs://cloud-samples-data/vision/document_understanding/kafka.pdf'
37+
38+
if isinstance(gcs_uri, six.binary_type):
39+
gcs_uri = gcs_uri.decode('utf-8')
40+
gcs_source = {'uri': gcs_uri}
41+
input_config = {'gcs_source': gcs_source}
42+
type_ = enums.Feature.Type.DOCUMENT_TEXT_DETECTION
43+
features_element = {'type': type_}
44+
features = [features_element]
45+
46+
# The service can process up to 5 pages per document file. Here we specify the
47+
# first, second, and last page of the document to be processed.
48+
pages_element = 1
49+
pages_element_2 = 2
50+
pages_element_3 = -1
51+
pages = [pages_element, pages_element_2, pages_element_3]
52+
requests_element = {'input_config': input_config, 'features': features, 'pages': pages}
53+
requests = [requests_element]
54+
55+
response = client.batch_annotate_files(requests)
56+
for image_response in response.responses[0].responses:
57+
print('Full text: {}'.format(image_response.full_text_annotation.text))
58+
for page in image_response.full_text_annotation.pages:
59+
for block in page.blocks:
60+
# The service also returns the bounding boxes for blocks, paragraphs, words, and symbols.
61+
print('\nBlock confidence: {}'.format(block.confidence))
62+
for par in block.paragraphs:
63+
print('\tParagraph confidence: {}'.format(par.confidence))
64+
for word in par.words:
65+
print('\t\tWord confidence: {}'.format(word.confidence))
66+
for symbol in word.symbols:
67+
print('\t\t\tSymbol: {}, (confidence: {})'.format(symbol.text, symbol.confidence))
68+
69+
# [END vision_batch_annotate_files_gcs_core]
70+
# [END vision_batch_annotate_files_gcs]
71+
72+
def main():
73+
import argparse
74+
75+
parser = argparse.ArgumentParser()
76+
parser.add_argument('--gcs_uri', type=str, default='gs://cloud-samples-data/vision/document_understanding/kafka.pdf')
77+
args = parser.parse_args()
78+
79+
sample_batch_annotate_files(args.gcs_uri)
80+
81+
if __name__ == '__main__':
82+
main()

0 commit comments

Comments
 (0)