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

Vision: Add 'parent' argumen to annotation requests; add 'ProductSearchClient.purge_products' method; add 'object_annotations' field to product search results (via synth). #8988

Merged
merged 1 commit into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions vision/google/cloud/vision_v1/gapic/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class Likelihood(enum.IntEnum):

Attributes:
UNKNOWN (int): Unknown likelihood.
VERY_UNLIKELY (int): It is very unlikely that the image belongs to the specified vertical.
UNLIKELY (int): It is unlikely that the image belongs to the specified vertical.
POSSIBLE (int): It is possible that the image belongs to the specified vertical.
LIKELY (int): It is likely that the image belongs to the specified vertical.
VERY_LIKELY (int): It is very likely that the image belongs to the specified vertical.
VERY_UNLIKELY (int): It is very unlikely.
UNLIKELY (int): It is unlikely.
POSSIBLE (int): It is possible.
LIKELY (int): It is likely.
VERY_LIKELY (int): It is very likely.
"""

UNKNOWN = 0
Expand Down
111 changes: 107 additions & 4 deletions vision/google/cloud/vision_v1/gapic/image_annotator_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import google.api_core.gapic_v1.client_info
import google.api_core.gapic_v1.config
import google.api_core.gapic_v1.method
import google.api_core.gapic_v1.routing_header
import google.api_core.grpc_helpers
import google.api_core.operation
from google.api_core import operations_v1
Expand Down Expand Up @@ -190,6 +191,7 @@ def __init__(
def batch_annotate_images(
self,
requests,
parent=None,
retry=google.api_core.gapic_v1.method.DEFAULT,
timeout=google.api_core.gapic_v1.method.DEFAULT,
metadata=None,
Expand All @@ -212,6 +214,16 @@ def batch_annotate_images(

If a dict is provided, it must be of the same form as the protobuf
message :class:`~google.cloud.vision_v1.types.AnnotateImageRequest`
parent (str): Optional. Target project and location to make a call.

Format: ``projects/{project-id}/locations/{location-id}``.

If no parent is specified, a region will be chosen automatically.

Supported location-ids: ``us``: USA country only, ``asia``: East asia
areas, like Japan, Taiwan, ``eu``: The European Union.

Example: ``projects/project-A/locations/eu``.
retry (Optional[google.api_core.retry.Retry]): A retry object used
to retry requests. If ``None`` is specified, requests will
be retried using a default configuration.
Expand Down Expand Up @@ -242,14 +254,30 @@ def batch_annotate_images(
client_info=self._client_info,
)

request = image_annotator_pb2.BatchAnnotateImagesRequest(requests=requests)
request = image_annotator_pb2.BatchAnnotateImagesRequest(
requests=requests, parent=parent
)
if metadata is None:
metadata = []
metadata = list(metadata)
try:
routing_header = [("parent", parent)]
except AttributeError:
pass
else:
routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
routing_header
)
metadata.append(routing_metadata)

return self._inner_api_calls["batch_annotate_images"](
request, retry=retry, timeout=timeout, metadata=metadata
)

def batch_annotate_files(
self,
requests,
parent=None,
retry=google.api_core.gapic_v1.method.DEFAULT,
timeout=google.api_core.gapic_v1.method.DEFAULT,
metadata=None,
Expand Down Expand Up @@ -279,6 +307,16 @@ def batch_annotate_files(

If a dict is provided, it must be of the same form as the protobuf
message :class:`~google.cloud.vision_v1.types.AnnotateFileRequest`
parent (str): Optional. Target project and location to make a call.

Format: ``projects/{project-id}/locations/{location-id}``.

If no parent is specified, a region will be chosen automatically.

Supported location-ids: ``us``: USA country only, ``asia``: East asia
areas, like Japan, Taiwan, ``eu``: The European Union.

Example: ``projects/project-A/locations/eu``.
retry (Optional[google.api_core.retry.Retry]): A retry object used
to retry requests. If ``None`` is specified, requests will
be retried using a default configuration.
Expand Down Expand Up @@ -309,7 +347,22 @@ def batch_annotate_files(
client_info=self._client_info,
)

request = image_annotator_pb2.BatchAnnotateFilesRequest(requests=requests)
request = image_annotator_pb2.BatchAnnotateFilesRequest(
requests=requests, parent=parent
)
if metadata is None:
metadata = []
metadata = list(metadata)
try:
routing_header = [("parent", parent)]
except AttributeError:
pass
else:
routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
routing_header
)
metadata.append(routing_metadata)

return self._inner_api_calls["batch_annotate_files"](
request, retry=retry, timeout=timeout, metadata=metadata
)
Expand All @@ -318,6 +371,7 @@ def async_batch_annotate_images(
self,
requests,
output_config,
parent=None,
retry=google.api_core.gapic_v1.method.DEFAULT,
timeout=google.api_core.gapic_v1.method.DEFAULT,
metadata=None,
Expand Down Expand Up @@ -365,6 +419,16 @@ def async_batch_annotate_images(

If a dict is provided, it must be of the same form as the protobuf
message :class:`~google.cloud.vision_v1.types.OutputConfig`
parent (str): Optional. Target project and location to make a call.

Format: ``projects/{project-id}/locations/{location-id}``.

If no parent is specified, a region will be chosen automatically.

Supported location-ids: ``us``: USA country only, ``asia``: East asia
areas, like Japan, Taiwan, ``eu``: The European Union.

Example: ``projects/project-A/locations/eu``.
retry (Optional[google.api_core.retry.Retry]): A retry object used
to retry requests. If ``None`` is specified, requests will
be retried using a default configuration.
Expand Down Expand Up @@ -398,8 +462,21 @@ def async_batch_annotate_images(
)

request = image_annotator_pb2.AsyncBatchAnnotateImagesRequest(
requests=requests, output_config=output_config
requests=requests, output_config=output_config, parent=parent
)
if metadata is None:
metadata = []
metadata = list(metadata)
try:
routing_header = [("parent", parent)]
except AttributeError:
pass
else:
routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
routing_header
)
metadata.append(routing_metadata)

operation = self._inner_api_calls["async_batch_annotate_images"](
request, retry=retry, timeout=timeout, metadata=metadata
)
Expand All @@ -413,6 +490,7 @@ def async_batch_annotate_images(
def async_batch_annotate_files(
self,
requests,
parent=None,
retry=google.api_core.gapic_v1.method.DEFAULT,
timeout=google.api_core.gapic_v1.method.DEFAULT,
metadata=None,
Expand Down Expand Up @@ -449,6 +527,16 @@ def async_batch_annotate_files(

If a dict is provided, it must be of the same form as the protobuf
message :class:`~google.cloud.vision_v1.types.AsyncAnnotateFileRequest`
parent (str): Optional. Target project and location to make a call.

Format: ``projects/{project-id}/locations/{location-id}``.

If no parent is specified, a region will be chosen automatically.

Supported location-ids: ``us``: USA country only, ``asia``: East asia
areas, like Japan, Taiwan, ``eu``: The European Union.

Example: ``projects/project-A/locations/eu``.
retry (Optional[google.api_core.retry.Retry]): A retry object used
to retry requests. If ``None`` is specified, requests will
be retried using a default configuration.
Expand Down Expand Up @@ -479,7 +567,22 @@ def async_batch_annotate_files(
client_info=self._client_info,
)

request = image_annotator_pb2.AsyncBatchAnnotateFilesRequest(requests=requests)
request = image_annotator_pb2.AsyncBatchAnnotateFilesRequest(
requests=requests, parent=parent
)
if metadata is None:
metadata = []
metadata = list(metadata)
try:
routing_header = [("parent", parent)]
except AttributeError:
pass
else:
routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
routing_header
)
metadata.append(routing_metadata)

operation = self._inner_api_calls["async_batch_annotate_files"](
request, retry=retry, timeout=timeout, metadata=metadata
)
Expand Down
133 changes: 133 additions & 0 deletions vision/google/cloud/vision_v1/gapic/product_search_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from google.api_core import operations_v1
import google.api_core.page_iterator
import google.api_core.path_template
import google.api_core.protobuf_helpers
import grpc

from google.cloud.vision_v1.gapic import enums
Expand Down Expand Up @@ -1886,3 +1887,135 @@ def import_product_sets(
product_search_service_pb2.ImportProductSetsResponse,
metadata_type=product_search_service_pb2.BatchOperationMetadata,
)

def purge_products(
self,
parent,
product_set_purge_config=None,
delete_orphan_products=None,
force=None,
retry=google.api_core.gapic_v1.method.DEFAULT,
timeout=google.api_core.gapic_v1.method.DEFAULT,
metadata=None,
):
"""
Asynchronous API to delete all Products in a ProductSet or all Products
that are in no ProductSet.

If a Product is a member of the specified ProductSet in addition to
other ProductSets, the Product will still be deleted.

It is recommended to not delete the specified ProductSet until after
this operation has completed. It is also recommended to not add any of
the Products involved in the batch delete to a new ProductSet while this
operation is running because those Products may still end up deleted.

It's not possible to undo the PurgeProducts operation. Therefore, it is
recommended to keep the csv files used in ImportProductSets (if that was
how you originally built the Product Set) before starting PurgeProducts,
in case you need to re-import the data after deletion.

If the plan is to purge all of the Products from a ProductSet and then
re-use the empty ProductSet to re-import new Products into the empty
ProductSet, you must wait until the PurgeProducts operation has finished
for that ProductSet.

The ``google.longrunning.Operation`` API can be used to keep track of
the progress and results of the request. ``Operation.metadata`` contains
``BatchOperationMetadata``. (progress)

Example:
>>> from google.cloud import vision_v1
>>>
>>> client = vision_v1.ProductSearchClient()
>>>
>>> parent = client.location_path('[PROJECT]', '[LOCATION]')
>>>
>>> response = client.purge_products(parent)
>>>
>>> def callback(operation_future):
... # Handle result.
... result = operation_future.result()
>>>
>>> response.add_done_callback(callback)
>>>
>>> # Handle metadata.
>>> metadata = response.metadata()

Args:
parent (str): The project and location in which the Products should be deleted.

Format is ``projects/PROJECT_ID/locations/LOC_ID``.
product_set_purge_config (Union[dict, ~google.cloud.vision_v1.types.ProductSetPurgeConfig]): Specify which ProductSet contains the Products to be deleted.

If a dict is provided, it must be of the same form as the protobuf
message :class:`~google.cloud.vision_v1.types.ProductSetPurgeConfig`
delete_orphan_products (bool): If delete\_orphan\_products is true, all Products that are not in any
ProductSet will be deleted.
force (bool): The default value is false. Override this value to true to actually perform
the purge.
retry (Optional[google.api_core.retry.Retry]): A retry object used
to retry requests. If ``None`` is specified, requests will
be retried using a default configuration.
timeout (Optional[float]): The amount of time, in seconds, to wait
for the request to complete. Note that if ``retry`` is
specified, the timeout applies to each individual attempt.
metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata
that is provided to the method.

Returns:
A :class:`~google.cloud.vision_v1.types._OperationFuture` instance.

Raises:
google.api_core.exceptions.GoogleAPICallError: If the request
failed for any reason.
google.api_core.exceptions.RetryError: If the request failed due
to a retryable error and retry attempts failed.
ValueError: If the parameters are invalid.
"""
# Wrap the transport method to add retry and timeout logic.
if "purge_products" not in self._inner_api_calls:
self._inner_api_calls[
"purge_products"
] = google.api_core.gapic_v1.method.wrap_method(
self.transport.purge_products,
default_retry=self._method_configs["PurgeProducts"].retry,
default_timeout=self._method_configs["PurgeProducts"].timeout,
client_info=self._client_info,
)

# Sanity check: We have some fields which are mutually exclusive;
# raise ValueError if more than one is sent.
google.api_core.protobuf_helpers.check_oneof(
product_set_purge_config=product_set_purge_config,
delete_orphan_products=delete_orphan_products,
)

request = product_search_service_pb2.PurgeProductsRequest(
parent=parent,
product_set_purge_config=product_set_purge_config,
delete_orphan_products=delete_orphan_products,
force=force,
)
if metadata is None:
metadata = []
metadata = list(metadata)
try:
routing_header = [("parent", parent)]
except AttributeError:
pass
else:
routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
routing_header
)
metadata.append(routing_metadata)

operation = self._inner_api_calls["purge_products"](
request, retry=retry, timeout=timeout, metadata=metadata
)
return google.api_core.operation.from_gapic(
operation,
self.transport._operations_client,
empty_pb2.Empty,
metadata_type=product_search_service_pb2.BatchOperationMetadata,
)
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@
"retry_codes_name": "non_idempotent",
"retry_params_name": "default",
},
"PurgeProducts": {
"timeout_millis": 60000,
"retry_codes_name": "non_idempotent",
"retry_params_name": "default",
},
},
}
}
Expand Down
Loading