Skip to content

Commit

Permalink
Update google cloud vision >= 2.0.0 (#23755)
Browse files Browse the repository at this point in the history
Co-authored-by: Anand Inguva <[email protected]>
  • Loading branch information
AnandInguva and AnandInguva authored Oct 20, 2022
1 parent 3d055da commit a138a4f
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 40 deletions.
51 changes: 29 additions & 22 deletions sdks/python/apache_beam/ml/gcp/visionml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 =
(
Expand Down Expand Up @@ -152,23 +152,27 @@ 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)
else:
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


Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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__()
Expand Down
9 changes: 5 additions & 4 deletions sdks/python/apache_beam/ml/gcp/visionml_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 5 additions & 2 deletions sdks/python/apache_beam/ml/gcp/visionml_test_it.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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))

Expand Down
6 changes: 3 additions & 3 deletions sdks/python/container/py310/base_image_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions sdks/python/container/py37/base_image_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions sdks/python/container/py38/base_image_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions sdks/python/container/py39/base_image_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sdks/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': [
Expand Down

0 comments on commit a138a4f

Please sign in to comment.