-
Notifications
You must be signed in to change notification settings - Fork 6.6k
vision: move published samples into master #2743
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
Merged
texasmichelle
merged 18 commits into
GoogleCloudPlatform:master
from
texasmichelle:generated_live
Jan 28, 2020
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
705b704
Add generated samples for Vision API
dizcology aaca667
Add generated sample
dizcology 36ab341
vision: resolve lint issues
texasmichelle 88b647a
vision: add test for async_batch_annotate_images
texasmichelle 2b5a3fe
vision: add test for batch_annotate_files_gcs
texasmichelle e24b100
vision: add test for batch_annotate_files
texasmichelle 4e6f203
fix: pesky lint issues
texasmichelle b75926e
Merge branch 'master' into generated_live
texasmichelle bed231d
Address review cleanup
texasmichelle 7610cd1
Merge branch 'master' into generated_live
texasmichelle c28e115
Remove CLI
texasmichelle 53e0cb4
Merge branch 'master' into generated_live
texasmichelle f2abf4e
Add test fixtures
texasmichelle 7921c89
Remove unused region tag and comments
texasmichelle da9b401
Merge branch 'master' into generated_live
texasmichelle 3e66d5d
Merge branch 'master' into generated_live
texasmichelle 4b4838c
Shorten docstring.
texasmichelle 98395be
Merge branch 'master' into generated_live
texasmichelle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
vision/cloud-client/detect/vision_async_batch_annotate_images.py
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# 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 | ||
# | ||
# https://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. | ||
|
||
# [START vision_async_batch_annotate_images] | ||
|
||
from google.cloud import vision_v1 | ||
from google.cloud.vision_v1 import enums | ||
|
||
|
||
def sample_async_batch_annotate_images( | ||
input_image_uri="gs://cloud-samples-data/vision/label/wakeupcat.jpg", | ||
output_uri="gs://your-bucket/prefix/", | ||
): | ||
"""Perform async batch image annotation.""" | ||
client = vision_v1.ImageAnnotatorClient() | ||
|
||
source = {"image_uri": input_image_uri} | ||
image = {"source": source} | ||
features = [ | ||
{"type": enums.Feature.Type.LABEL_DETECTION}, | ||
{"type": enums.Feature.Type.IMAGE_PROPERTIES}, | ||
] | ||
requests = [{"image": image, "features": features}] | ||
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] |
62 changes: 62 additions & 0 deletions
62
vision/cloud-client/detect/vision_async_batch_annotate_images_test.py
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Copyright 2020 Google | ||
# | ||
# 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. | ||
|
||
import os | ||
import uuid | ||
|
||
from google.cloud import storage | ||
import pytest | ||
|
||
import vision_async_batch_annotate_images | ||
|
||
RESOURCES = os.path.join(os.path.dirname(__file__), "resources") | ||
GCS_ROOT = "gs://cloud-samples-data/vision/" | ||
|
||
BUCKET = os.environ["CLOUD_STORAGE_BUCKET"] | ||
OUTPUT_PREFIX = "TEST_OUTPUT_{}".format(uuid.uuid4()) | ||
GCS_DESTINATION_URI = "gs://{}/{}/".format(BUCKET, OUTPUT_PREFIX) | ||
|
||
|
||
@pytest.fixture() | ||
def storage_client(): | ||
yield storage.Client() | ||
|
||
|
||
@pytest.fixture() | ||
def bucket(storage_client): | ||
bucket = storage_client.get_bucket(BUCKET) | ||
|
||
try: | ||
for blob in bucket.list_blobs(prefix=OUTPUT_PREFIX): | ||
blob.delete() | ||
except Exception: | ||
pass | ||
|
||
yield bucket | ||
|
||
for blob in bucket.list_blobs(prefix=OUTPUT_PREFIX): | ||
blob.delete() | ||
|
||
|
||
def test_sample_asyn_batch_annotate_images(storage_client, bucket, capsys): | ||
input_image_uri = os.path.join(GCS_ROOT, "label/wakeupcat.jpg") | ||
|
||
vision_async_batch_annotate_images.sample_async_batch_annotate_images( | ||
input_image_uri=input_image_uri, output_uri=GCS_DESTINATION_URI | ||
) | ||
|
||
out, _ = capsys.readouterr() | ||
|
||
assert "Output written to GCS" in out | ||
assert len(list(bucket.list_blobs(prefix=OUTPUT_PREFIX))) > 0 |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# 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 | ||
# | ||
# https://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. | ||
|
||
# [START vision_batch_annotate_files] | ||
|
||
from google.cloud import vision_v1 | ||
from google.cloud.vision_v1 import enums | ||
import io | ||
|
||
|
||
def sample_batch_annotate_files(file_path="path/to/your/document.pdf"): | ||
"""Perform batch file annotation.""" | ||
client = vision_v1.ImageAnnotatorClient() | ||
|
||
# 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} | ||
features = [{"type": enums.Feature.Type.DOCUMENT_TEXT_DETECTION}] | ||
|
||
# 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 = [1, 2, -1] | ||
requests = [{"input_config": input_config, "features": features, "pages": pages}] | ||
|
||
response = client.batch_annotate_files(requests) | ||
for image_response in response.responses[0].responses: | ||
print(u"Full text: {}".format(image_response.full_text_annotation.text)) | ||
for page in image_response.full_text_annotation.pages: | ||
for block in page.blocks: | ||
print(u"\nBlock confidence: {}".format(block.confidence)) | ||
for par in block.paragraphs: | ||
print(u"\tParagraph confidence: {}".format(par.confidence)) | ||
for word in par.words: | ||
print(u"\t\tWord confidence: {}".format(word.confidence)) | ||
for symbol in word.symbols: | ||
print( | ||
u"\t\t\tSymbol: {}, (confidence: {})".format( | ||
symbol.text, symbol.confidence | ||
) | ||
) | ||
|
||
|
||
# [END vision_batch_annotate_files] |
57 changes: 57 additions & 0 deletions
57
vision/cloud-client/detect/vision_batch_annotate_files_gcs.py
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# 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 | ||
# | ||
# https://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. | ||
|
||
# [START vision_batch_annotate_files_gcs] | ||
|
||
from google.cloud import vision_v1 | ||
from google.cloud.vision_v1 import enums | ||
|
||
|
||
def sample_batch_annotate_files( | ||
storage_uri="gs://cloud-samples-data/vision/document_understanding/kafka.pdf", | ||
): | ||
"""Perform batch file annotation.""" | ||
mime_type = "application/pdf" | ||
|
||
client = vision_v1.ImageAnnotatorClient() | ||
|
||
gcs_source = {"uri": storage_uri} | ||
input_config = {"gcs_source": gcs_source, "mime_type": mime_type} | ||
features = [{"type": enums.Feature.Type.DOCUMENT_TEXT_DETECTION}] | ||
|
||
# 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 = [1, 2, -1] | ||
requests = [{"input_config": input_config, "features": features, "pages": pages}] | ||
|
||
response = client.batch_annotate_files(requests) | ||
for image_response in response.responses[0].responses: | ||
print(u"Full text: {}".format(image_response.full_text_annotation.text)) | ||
for page in image_response.full_text_annotation.pages: | ||
for block in page.blocks: | ||
print(u"\nBlock confidence: {}".format(block.confidence)) | ||
for par in block.paragraphs: | ||
print(u"\tParagraph confidence: {}".format(par.confidence)) | ||
for word in par.words: | ||
print(u"\t\tWord confidence: {}".format(word.confidence)) | ||
for symbol in word.symbols: | ||
print( | ||
u"\t\t\tSymbol: {}, (confidence: {})".format( | ||
symbol.text, symbol.confidence | ||
) | ||
) | ||
|
||
|
||
# [END vision_batch_annotate_files_gcs] |
30 changes: 30 additions & 0 deletions
30
vision/cloud-client/detect/vision_batch_annotate_files_gcs_test.py
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright 2020 Google | ||
# | ||
# 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. | ||
|
||
import os | ||
|
||
import vision_batch_annotate_files_gcs | ||
|
||
GCS_ROOT = "gs://cloud-samples-data/vision/" | ||
|
||
|
||
def test_sample_batch_annotate_files_gcs(capsys): | ||
storage_uri = os.path.join(GCS_ROOT, "document_understanding/kafka.pdf") | ||
|
||
vision_batch_annotate_files_gcs.sample_batch_annotate_files(storage_uri=storage_uri) | ||
|
||
out, _ = capsys.readouterr() | ||
|
||
assert "Full text" in out | ||
assert "Block confidence" in out |
30 changes: 30 additions & 0 deletions
30
vision/cloud-client/detect/vision_batch_annotate_files_test.py
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright 2020 Google | ||
# | ||
# 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. | ||
|
||
import os | ||
|
||
import vision_batch_annotate_files | ||
|
||
RESOURCES = os.path.join(os.path.dirname(__file__), "resources") | ||
|
||
|
||
def test_sample_batch_annotate_files(capsys): | ||
file_path = os.path.join(RESOURCES, "kafka.pdf") | ||
|
||
vision_batch_annotate_files.sample_batch_annotate_files(file_path=file_path) | ||
|
||
out, _ = capsys.readouterr() | ||
|
||
assert "Full text" in out | ||
assert "Block confidence" in out |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.