coufon commented on a change in pull request #4791: [AIRFLOW-3908] Add more
Google Cloud Vision operators
URL: https://github.com/apache/airflow/pull/4791#discussion_r261438146
##########
File path: airflow/contrib/operators/gcp_vision_operator.py
##########
@@ -671,3 +672,284 @@ def execute(self, context):
timeout=self.timeout,
metadata=self.metadata,
)
+
+
+class CloudVisionAnnotateImageOperator(BaseOperator):
+ """
+ Run image detection and annotation for an image.
+
+ .. seealso::
+ For more information on how to use this operator, take a look at the
guide:
+ :ref:`howto/operator:CloudVisionAnnotateImageOperator`
+
+ :param request: (Required) Individual file annotation requests.
+ If a dict is provided, it must be of the same form as the protobuf
+ message class:`google.cloud.vision_v1.types.AnnotateImageRequest`
+ :type request: dict or google.cloud.vision_v1.types.AnnotateImageRequest
+ :param retry: (Optional) A retry object used to retry requests. If `None`
is
+ specified, requests will not be retried.
+ :type retry: google.api_core.retry.Retry
+ :param timeout: (Optional) 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.
+ :type timeout: float
+ :param gcp_conn_id: (Optional) The connection ID used to connect to Google
Cloud Platform.
+ :type gcp_conn_id: str
+ """
+
+ # [START vision_annotate_image_template_fields]
+ template_fields = ('request', 'gcp_conn_id')
+ # [END vision_annotate_image_template_fields]
+
+ @apply_defaults
+ def __init__(
+ self, request, retry=None, timeout=None,
gcp_conn_id='google_cloud_default', *args, **kwargs
+ ):
+ super(CloudVisionAnnotateImageOperator, self).__init__(*args, **kwargs)
+ self.request = request
+ self.retry = retry
+ self.timeout = timeout
+ self.gcp_conn_id = gcp_conn_id
+
+ def execute(self, context):
+ hook = CloudVisionHook(gcp_conn_id=self.gcp_conn_id)
+ return hook.annotate_image(request=self.request, retry=self.retry,
timeout=self.timeout)
+
+
+class CloudVisionReferenceImageCreateOperator(BaseOperator):
+ """
+ Creates and returns a new ReferenceImage ID resource.
+
+ .. seealso::
+ For more information on how to use this operator, take a look at the
guide:
+ :ref:`howto/operator:CloudVisionReferenceImageCreateOperator`
+
+ :param location: (Required) The region where the Product is located. Valid
regions (as of 2019-02-05) are:
+ us-east1, us-west1, europe-west1, asia-east1
+ :type location: str
+ :param reference_image: (Required) The reference image to create. If an
image ID is specified, it is
+ ignored.
+ If a dict is provided, it must be of the same form as the protobuf
message
+ :class:`google.cloud.vision_v1.types.ReferenceImage`
+ :type reference_image: dict or google.cloud.vision_v1.types.ReferenceImage
+ :param reference_image_id: (Optional) A user-supplied resource id for the
ReferenceImage to be added.
+ If set, the server will attempt to use this value as the resource id.
If it is already in use, an
+ error is returned with code ALREADY_EXISTS. Must be at most 128
characters long. It cannot contain
+ the character `/`.
+ :type reference_image_id: str
+ :param product_id: (Optional) The resource id of this Product.
+ :type product_id: str
+ :param project_id: (Optional) The project in which the Product is located.
If set to None or
+ missing, the default project_id from the GCP connection is used.
+ :type project_id: str
+ :param retry: (Optional) A retry object used to retry requests. If `None`
is
+ specified, requests will not be retried.
+ :type retry: google.api_core.retry.Retry
+ :param timeout: (Optional) 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.
+ :type timeout: float
+ :param metadata: (Optional) Additional metadata that is provided to the
method.
+ :type metadata: sequence[tuple[str, str]]
+ :param gcp_conn_id: (Optional) The connection ID used to connect to Google
Cloud Platform.
+ :type gcp_conn_id: str
+ """
+
+ # [START vision_reference_image_create_template_fields]
+ template_fields = ('location', 'product_id', 'reference_image_id',
'project_id', 'gcp_conn_id')
Review comment:
There may be a need to be able to render reference_image's URL using
template, then we can use "task_instance.xcom_pull(...)". It would be useful if
this reference image is created by a precedent task.
For example:
reference_image = ReferenceImage(uri=GCP_VISION_REFERENCE_IMAGE_URL)
The URL "GCP_VISION_REFERENCE_IMAGE_URL" may be given by a precedent task.
There may be some complexity when for reference_image that is a
"google.cloud.vision_v1.types.ReferenceImage" instead of a dict.
It is similar for CloudVisionAnnotateImageOperator that the input image URL
may be generated by another task, and it already supports image URL template
there.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services