From a138a4f86f6d70dadb9eaefd16ca92d608ba5c5d Mon Sep 17 00:00:00 2001 From: Anand Inguva <34158215+AnandInguva@users.noreply.github.com> Date: Thu, 20 Oct 2022 18:53:10 -0400 Subject: [PATCH] Update google cloud vision >= 2.0.0 (#23755) Co-authored-by: Anand Inguva --- sdks/python/apache_beam/ml/gcp/visionml.py | 51 +++++++++++-------- .../apache_beam/ml/gcp/visionml_test.py | 9 ++-- .../apache_beam/ml/gcp/visionml_test_it.py | 7 ++- .../py310/base_image_requirements.txt | 6 +-- .../py37/base_image_requirements.txt | 4 +- .../py38/base_image_requirements.txt | 6 +-- .../py39/base_image_requirements.txt | 6 +-- sdks/python/setup.py | 2 +- 8 files changed, 51 insertions(+), 40 deletions(-) diff --git a/sdks/python/apache_beam/ml/gcp/visionml.py b/sdks/python/apache_beam/ml/gcp/visionml.py index 3e556b903c44..dd29dd377388 100644 --- a/sdks/python/apache_beam/ml/gcp/visionml.py +++ b/sdks/python/apache_beam/ml/gcp/visionml.py @@ -80,7 +80,7 @@ def __init__( metadata=None): """ Args: - features: (List[``vision.types.Feature.enums.Feature``]) Required. + features: (List[``vision.Feature``]) Required. The Vision API features to detect retry: (google.api_core.retry.Retry) Optional. A retry object used to retry requests. @@ -107,9 +107,9 @@ def __init__( image_contexts = [(''gs://cloud-samples-data/vision/ocr/sign.jpg'', Union[dict, - ``vision.types.ImageContext()``]), + ``vision.ImageContext()``]), (''gs://cloud-samples-data/vision/ocr/sign.jpg'', Union[dict, - ``vision.types.ImageContext()``]),] + ``vision.ImageContext()``]),] context_side_input = ( @@ -152,9 +152,8 @@ def expand(self, pvalue): client_options=self.client_options, metadata=self.metadata))) - @typehints.with_input_types( - Union[str, bytes], Optional[vision.types.ImageContext]) - @typehints.with_output_types(List[vision.types.AnnotateImageRequest]) + @typehints.with_input_types(Union[str, bytes], Optional[vision.ImageContext]) + @typehints.with_output_types(List[vision.AnnotateImageRequest]) def _create_image_annotation_pairs(self, element, context_side_input): if context_side_input: # If we have a side input image context, use that image_context = context_side_input.get(element) @@ -162,13 +161,18 @@ def _create_image_annotation_pairs(self, element, context_side_input): image_context = None if isinstance(element, str): - image = vision.types.Image( - source=vision.types.ImageSource(image_uri=element)) + + image = vision.Image( + {'source': vision.ImageSource({'image_uri': element})}) + else: # Typehint checks only allows str or bytes - image = vision.types.Image(content=element) + image = vision.Image(content=element) - request = vision.types.AnnotateImageRequest( - image=image, features=self.features, image_context=image_context) + request = vision.AnnotateImageRequest({ + 'image': image, + 'features': self.features, + 'image_context': image_context + }) yield request @@ -181,7 +185,7 @@ class AnnotateImageWithContext(AnnotateImage): Element is a tuple of:: (Union[str, bytes], - Optional[``vision.types.ImageContext``]) + Optional[``vision.ImageContext``]) where the former is either an URI (e.g. a GCS URI) or bytes base64-encoded image data. @@ -197,7 +201,7 @@ def __init__( metadata=None): """ Args: - features: (List[``vision.types.Feature.enums.Feature``]) Required. + features: (List[``vision.Feature``]) Required. The Vision API features to detect retry: (google.api_core.retry.Retry) Optional. A retry object used to retry requests. @@ -244,25 +248,28 @@ def expand(self, pvalue): metadata=self.metadata))) @typehints.with_input_types( - Tuple[Union[str, bytes], Optional[vision.types.ImageContext]]) - @typehints.with_output_types(List[vision.types.AnnotateImageRequest]) + Tuple[Union[str, bytes], Optional[vision.ImageContext]]) + @typehints.with_output_types(List[vision.AnnotateImageRequest]) def _create_image_annotation_pairs(self, element, **kwargs): element, image_context = element # Unpack (image, image_context) tuple if isinstance(element, str): - image = vision.types.Image( - source=vision.types.ImageSource(image_uri=element)) + image = vision.Image( + {'source': vision.ImageSource({'image_uri': element})}) else: # Typehint checks only allows str or bytes - image = vision.types.Image(content=element) + image = vision.Image({"content": element}) - request = vision.types.AnnotateImageRequest( - image=image, features=self.features, image_context=image_context) + request = vision.AnnotateImageRequest({ + 'image': image, + 'features': self.features, + 'image_context': image_context + }) yield request -@typehints.with_input_types(List[vision.types.AnnotateImageRequest]) +@typehints.with_input_types(List[vision.AnnotateImageRequest]) class _ImageAnnotateFn(DoFn): """A DoFn that sends each input element to the GCP Vision API. - Returns ``google.cloud.vision.types.BatchAnnotateImagesResponse``. + Returns ``google.cloud.vision.BatchAnnotateImagesResponse``. """ def __init__(self, features, retry, timeout, client_options, metadata): super().__init__() diff --git a/sdks/python/apache_beam/ml/gcp/visionml_test.py b/sdks/python/apache_beam/ml/gcp/visionml_test.py index f038442468f8..479b3d80e4de 100644 --- a/sdks/python/apache_beam/ml/gcp/visionml_test.py +++ b/sdks/python/apache_beam/ml/gcp/visionml_test.py @@ -45,12 +45,13 @@ def setUp(self): self._mock_client = mock.Mock() self._mock_client.batch_annotate_images.return_value = None - feature_type = vision.enums.Feature.Type.TEXT_DETECTION + feature_type = vision.Feature.Type.TEXT_DETECTION self.features = [ - vision.types.Feature( - type=feature_type, max_results=3, model="builtin/stable") + vision.Feature({ + 'type': feature_type, 'max_results': 3, 'model': "builtin/stable" + }) ] - self.img_ctx = vision.types.ImageContext() + self.img_ctx = vision.ImageContext() self.min_batch_size = 1 self.max_batch_size = 1 diff --git a/sdks/python/apache_beam/ml/gcp/visionml_test_it.py b/sdks/python/apache_beam/ml/gcp/visionml_test_it.py index 4413266dcc5c..ea3fc9768ff5 100644 --- a/sdks/python/apache_beam/ml/gcp/visionml_test_it.py +++ b/sdks/python/apache_beam/ml/gcp/visionml_test_it.py @@ -47,7 +47,8 @@ def test_text_detection_with_language_hint(self): IMAGES_TO_ANNOTATE = [ 'gs://apache-beam-samples/advanced_analytics/vision/sign.jpg' ] - IMAGE_CONTEXT = [vision.types.ImageContext(language_hints=['en'])] + + IMAGE_CONTEXT = [vision.ImageContext({'language_hints': ['en']})] with TestPipeline(is_integration_test=True) as p: contexts = p | 'Create context' >> beam.Create( @@ -57,7 +58,9 @@ def test_text_detection_with_language_hint(self): p | beam.Create(IMAGES_TO_ANNOTATE) | AnnotateImage( - features=[vision.types.Feature(type='TEXT_DETECTION')], + features=[ + vision.Feature({'type_': vision.Feature.Type.TEXT_DETECTION}) + ], context_side_input=beam.pvalue.AsDict(contexts)) | beam.ParDo(extract)) diff --git a/sdks/python/container/py310/base_image_requirements.txt b/sdks/python/container/py310/base_image_requirements.txt index aeb8d0d990ba..651a332f909f 100644 --- a/sdks/python/container/py310/base_image_requirements.txt +++ b/sdks/python/container/py310/base_image_requirements.txt @@ -66,7 +66,7 @@ google-cloud-pubsublite==1.5.0 google-cloud-recommendations-ai==0.7.1 google-cloud-spanner==3.22.2 google-cloud-videointelligence==1.16.3 -google-cloud-vision==1.0.2 +google-cloud-vision==3.1.4 google-crc32c==1.5.0 google-pasta==0.2.0 google-resumable-media==2.4.0 @@ -102,7 +102,7 @@ overrides==6.5.0 packaging==21.3 pandas==1.4.4 parameterized==0.8.1 -pbr==5.10.0 +pbr==5.11.0 pluggy==1.0.0 proto-plus==1.22.1 protobuf==3.19.6 @@ -131,7 +131,7 @@ requests-mock==1.10.0 requests-oauthlib==1.3.1 rsa==4.9 scikit-learn==1.1.2 -scipy==1.9.2 +scipy==1.9.3 six==1.16.0 sortedcontainers==2.4.0 soupsieve==2.3.2.post1 diff --git a/sdks/python/container/py37/base_image_requirements.txt b/sdks/python/container/py37/base_image_requirements.txt index 53041ca821f0..2dbf7d1827db 100644 --- a/sdks/python/container/py37/base_image_requirements.txt +++ b/sdks/python/container/py37/base_image_requirements.txt @@ -70,7 +70,7 @@ google-cloud-recommendations-ai==0.7.1 google-cloud-spanner==3.22.2 google-cloud-storage==2.5.0 google-cloud-videointelligence==1.16.3 -google-cloud-vision==1.0.2 +google-cloud-vision==3.1.4 google-crc32c==1.5.0 google-pasta==0.2.0 google-python-cloud-debugger==3.1 @@ -109,7 +109,7 @@ overrides==6.5.0 packaging==21.3 pandas==1.3.5 parameterized==0.8.1 -pbr==5.10.0 +pbr==5.11.0 pluggy==1.0.0 proto-plus==1.22.1 protobuf==3.19.6 diff --git a/sdks/python/container/py38/base_image_requirements.txt b/sdks/python/container/py38/base_image_requirements.txt index e5f7fec58485..ca45ef30f856 100644 --- a/sdks/python/container/py38/base_image_requirements.txt +++ b/sdks/python/container/py38/base_image_requirements.txt @@ -70,7 +70,7 @@ google-cloud-recommendations-ai==0.7.1 google-cloud-spanner==3.22.2 google-cloud-storage==2.5.0 google-cloud-videointelligence==1.16.3 -google-cloud-vision==1.0.2 +google-cloud-vision==3.1.4 google-crc32c==1.5.0 google-pasta==0.2.0 google-python-cloud-debugger==3.1 @@ -109,7 +109,7 @@ overrides==6.5.0 packaging==21.3 pandas==1.4.4 parameterized==0.8.1 -pbr==5.10.0 +pbr==5.11.0 pluggy==1.0.0 proto-plus==1.22.1 protobuf==3.19.6 @@ -138,7 +138,7 @@ requests-mock==1.10.0 requests-oauthlib==1.3.1 rsa==4.9 scikit-learn==1.1.2 -scipy==1.9.2 +scipy==1.9.3 six==1.16.0 sortedcontainers==2.4.0 soupsieve==2.3.2.post1 diff --git a/sdks/python/container/py39/base_image_requirements.txt b/sdks/python/container/py39/base_image_requirements.txt index bc624e7df12c..f4b9cdca16a5 100644 --- a/sdks/python/container/py39/base_image_requirements.txt +++ b/sdks/python/container/py39/base_image_requirements.txt @@ -70,7 +70,7 @@ google-cloud-recommendations-ai==0.7.1 google-cloud-spanner==3.22.2 google-cloud-storage==2.5.0 google-cloud-videointelligence==1.16.3 -google-cloud-vision==1.0.2 +google-cloud-vision==3.1.4 google-crc32c==1.5.0 google-pasta==0.2.0 google-python-cloud-debugger==3.1 @@ -109,7 +109,7 @@ overrides==6.5.0 packaging==21.3 pandas==1.4.4 parameterized==0.8.1 -pbr==5.10.0 +pbr==5.11.0 pluggy==1.0.0 proto-plus==1.22.1 protobuf==3.19.6 @@ -138,7 +138,7 @@ requests-mock==1.10.0 requests-oauthlib==1.3.1 rsa==4.9 scikit-learn==1.1.2 -scipy==1.9.2 +scipy==1.9.3 six==1.16.0 sortedcontainers==2.4.0 soupsieve==2.3.2.post1 diff --git a/sdks/python/setup.py b/sdks/python/setup.py index c91fb2e71a85..aaa761b14763 100644 --- a/sdks/python/setup.py +++ b/sdks/python/setup.py @@ -314,7 +314,7 @@ def get_portability_package_data(): 'google-cloud-dlp>=3.0.0,<4', 'google-cloud-language>=1.3.0,<2', 'google-cloud-videointelligence>=1.8.0,<2', - 'google-cloud-vision>=0.38.0,<2', + 'google-cloud-vision>=2,<4', 'google-cloud-recommendations-ai>=0.1.0,<0.8.0' ], 'interactive': [