Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
vision: resolve lint issues
  • Loading branch information
texasmichelle committed Jan 21, 2020
commit 36ab341bbaf31294de6e2363e3734f00a8f05ead
110 changes: 62 additions & 48 deletions vision/cloud-client/detect/vision_async_batch_annotate_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,72 +14,86 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# DO NOT EDIT! This is a generated sample ("LongRunningPromise", "vision_async_batch_annotate_images")
# DO NOT EDIT! This is a generated sample
# ("LongRunningPromise", "vision_async_batch_annotate_images")

# To install the latest published package dependency, execute the following:
# pip install google-cloud-vision

# sample-metadata
# title: Async Batch Image Annotation
# description: Perform async batch image annotation
# usage: python3 samples/v1/vision_async_batch_annotate_images.py [--input_image_uri "gs://cloud-samples-data/vision/label/wakeupcat.jpg"] [--output_uri "gs://your-bucket/prefix/"]
import sys
# usage: python3 samples/v1/vision_async_batch_annotate_images.py \
# [--input_image_uri \
# "gs://cloud-samples-data/vision/label/wakeupcat.jpg"] \
# [--output_uri "gs://your-bucket/prefix/"]

# [START vision_async_batch_annotate_images]

from google.cloud import vision_v1
from google.cloud.vision_v1 import enums
import six


def sample_async_batch_annotate_images(input_image_uri, output_uri):
"""Perform async batch image annotation"""
# [START vision_async_batch_annotate_images_core]

client = vision_v1.ImageAnnotatorClient()

# input_image_uri = 'gs://cloud-samples-data/vision/label/wakeupcat.jpg'
# output_uri = 'gs://your-bucket/prefix/'

if isinstance(input_image_uri, six.binary_type):
input_image_uri = input_image_uri.decode('utf-8')
if isinstance(output_uri, six.binary_type):
output_uri = output_uri.decode('utf-8')
source = {'image_uri': input_image_uri}
image = {'source': source}
type_ = enums.Feature.Type.LABEL_DETECTION
features_element = {'type': type_}
type_2 = enums.Feature.Type.IMAGE_PROPERTIES
features_element_2 = {'type': type_2}
features = [features_element, features_element_2]
requests_element = {'image': image, 'features': features}
requests = [requests_element]
gcs_destination = {'uri': output_uri}

# The max number of responses to output in each JSON file
batch_size = 2
output_config = {'gcs_destination': gcs_destination, 'batch_size': batch_size}

operation = client.async_batch_annotate_images(requests, output_config)

print('Waiting for operation to complete...')
response = operation.result()

# The output is written to GCS with the provided output_uri as prefix
gcs_output_uri = response.output_config.gcs_destination.uri
print('Output written to GCS with prefix: {}'.format(gcs_output_uri))

# [END vision_async_batch_annotate_images_core]
"""Perform async batch image annotation"""
# [START vision_async_batch_annotate_images_core]

client = vision_v1.ImageAnnotatorClient()

# input_image_uri = 'gs://cloud-samples-data/vision/label/wakeupcat.jpg'
# output_uri = 'gs://your-bucket/prefix/'

if isinstance(input_image_uri, six.binary_type):
input_image_uri = input_image_uri.decode("utf-8")
if isinstance(output_uri, six.binary_type):
output_uri = output_uri.decode("utf-8")
Comment thread
texasmichelle marked this conversation as resolved.
Outdated
source = {"image_uri": input_image_uri}
image = {"source": source}
type_ = enums.Feature.Type.LABEL_DETECTION
features_element = {"type": type_}
type_2 = enums.Feature.Type.IMAGE_PROPERTIES
features_element_2 = {"type": type_2}
Comment thread
texasmichelle marked this conversation as resolved.
Outdated
features = [features_element, features_element_2]
requests_element = {"image": image, "features": features}
requests = [requests_element]
gcs_destination = {"uri": output_uri}

# The max number of responses to output in each JSON file
batch_size = 2
output_config = {
"gcs_destination": gcs_destination, "batch_size": batch_size}

operation = client.async_batch_annotate_images(requests, output_config)

print("Waiting for operation to complete...")
response = operation.result()

# The output is written to GCS with the provided output_uri as prefix
gcs_output_uri = response.output_config.gcs_destination.uri
print("Output written to GCS with prefix: {}".format(gcs_output_uri))

# [END vision_async_batch_annotate_images_core]


# [END vision_async_batch_annotate_images]


def main():
import argparse
import argparse

parser = argparse.ArgumentParser()
parser.add_argument(
"--input_image_uri",
type=str,
default="gs://cloud-samples-data/vision/label/wakeupcat.jpg",
)
parser.add_argument(
"--output_uri", type=str, default="gs://your-bucket/prefix/")
args = parser.parse_args()

parser = argparse.ArgumentParser()
parser.add_argument('--input_image_uri', type=str, default='gs://cloud-samples-data/vision/label/wakeupcat.jpg')
parser.add_argument('--output_uri', type=str, default='gs://your-bucket/prefix/')
args = parser.parse_args()
sample_async_batch_annotate_images(args.input_image_uri, args.output_uri)

sample_async_batch_annotate_images(args.input_image_uri, args.output_uri)

if __name__ == '__main__':
main()
if __name__ == "__main__":
main()
119 changes: 67 additions & 52 deletions vision/cloud-client/detect/vision_batch_annotate_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# DO NOT EDIT! This is a generated sample ("Request", "vision_batch_annotate_files")
# DO NOT EDIT! This is a generated sample
# ("Request", "vision_batch_annotate_files")

# To install the latest published package dependency, execute the following:
# pip install google-cloud-vision

# sample-metadata
# title:
# description: Perform batch file annotation
# usage: python3 samples/v1/vision_batch_annotate_files.py [--file_path "resources/kafka.pdf"]
import sys
# usage: python3 samples/v1/vision_batch_annotate_files.py \
# [--file_path "resources/kafka.pdf"]

# [START vision_batch_annotate_files]

Expand All @@ -32,64 +33,78 @@
import io
import six


def sample_batch_annotate_files(file_path):
"""
"""
Perform batch file annotation

Args:
file_path Path to local pdf file, e.g. /path/document.pdf
"""
# [START vision_batch_annotate_files_core]

client = vision_v1.ImageAnnotatorClient()

# file_path = 'resources/kafka.pdf'

if isinstance(file_path, six.binary_type):
file_path = file_path.decode('utf-8')

# Supported mime_type: application/pdf, image/tiff, image/gif
mime_type = 'application/pdf'
with io.open(file_path, 'rb') as f:
content = f.read()
input_config = {'mime_type': mime_type, 'content': content}
type_ = enums.Feature.Type.DOCUMENT_TEXT_DETECTION
features_element = {'type': type_}
features = [features_element]

# The service can process up to 5 pages per document file. Here we specify the
# first, second, and last page of the document to be processed.
pages_element = 1
pages_element_2 = 2
pages_element_3 = -1
pages = [pages_element, pages_element_2, pages_element_3]
requests_element = {'input_config': input_config, 'features': features, 'pages': pages}
requests = [requests_element]

response = client.batch_annotate_files(requests)
for image_response in response.responses[0].responses:
print('Full text: {}'.format(image_response.full_text_annotation.text))
for page in image_response.full_text_annotation.pages:
for block in page.blocks:
print('\nBlock confidence: {}'.format(block.confidence))
for par in block.paragraphs:
print('\tParagraph confidence: {}'.format(par.confidence))
for word in par.words:
print('\t\tWord confidence: {}'.format(word.confidence))
for symbol in word.symbols:
print('\t\t\tSymbol: {}, (confidence: {})'.format(symbol.text, symbol.confidence))

# [END vision_batch_annotate_files_core]
# [START vision_batch_annotate_files_core]

client = vision_v1.ImageAnnotatorClient()

# file_path = 'resources/kafka.pdf'

if isinstance(file_path, six.binary_type):
file_path = file_path.decode("utf-8")

# Supported mime_type: application/pdf, image/tiff, image/gif
mime_type = "application/pdf"
with io.open(file_path, "rb") as f:
content = f.read()
input_config = {"mime_type": mime_type, "content": content}
type_ = enums.Feature.Type.DOCUMENT_TEXT_DETECTION
features_element = {"type": type_}
features = [features_element]

# The service can process up to 5 pages per document file. Here we specify
# the first, second, and last page of the document to be processed.
pages_element = 1
pages_element_2 = 2
pages_element_3 = -1
pages = [pages_element, pages_element_2, pages_element_3]
Comment thread
texasmichelle marked this conversation as resolved.
Outdated
requests_element = {
"input_config": input_config,
"features": features,
"pages": pages,
}
requests = [requests_element]

response = client.batch_annotate_files(requests)
for image_response in response.responses[0].responses:
print("Full text: {}".format(image_response.full_text_annotation.text))
for page in image_response.full_text_annotation.pages:
for block in page.blocks:
print("\nBlock confidence: {}".format(block.confidence))
for par in block.paragraphs:
print("\tParagraph confidence: {}".format(par.confidence))
for word in par.words:
print(
"\t\tWord confidence: {}".format(word.confidence))
for symbol in word.symbols:
print(
"\t\t\tSymbol: {}, (confidence: {})".format(
symbol.text, symbol.confidence
)
)

# [END vision_batch_annotate_files_core]


# [END vision_batch_annotate_files]


def main():
import argparse
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--file_path", type=str, default="resources/kafka.pdf")
args = parser.parse_args()

parser = argparse.ArgumentParser()
parser.add_argument('--file_path', type=str, default='resources/kafka.pdf')
args = parser.parse_args()
sample_batch_annotate_files(args.file_path)

sample_batch_annotate_files(args.file_path)

if __name__ == '__main__':
main()
if __name__ == "__main__":
main()
Loading