Skip to content
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
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
24 changes: 12 additions & 12 deletions sdks/python/container/py310/base_image_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# https://s.apache.org/beam-python-dev-wiki
# Reach out to a committer if you need help.

absl-py==1.2.0
absl-py==1.3.0
astunparse==1.6.3
attrs==22.1.0
beautifulsoup4==4.11.1
Expand All @@ -48,9 +48,9 @@ freezegun==1.2.2
future==0.18.2
gast==0.4.0
google-api-core==2.10.2
google-api-python-client==2.64.0
google-api-python-client==2.65.0
google-apitools==0.5.31
google-auth==2.12.0
google-auth==2.13.0
google-auth-httplib2==0.1.0
google-auth-oauthlib==0.4.6
google-cloud-bigquery==3.3.5
Expand All @@ -61,25 +61,25 @@ google-cloud-datastore==1.15.5
google-cloud-dlp==3.9.2
google-cloud-language==1.3.2
google-cloud-profiler==3.1.0
google-cloud-pubsub==2.13.9
google-cloud-pubsub==2.13.10
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
googleapis-common-protos==1.56.4
greenlet==1.1.3.post0
grpc-google-iam-v1==0.12.4
grpcio==1.49.1
grpcio==1.50.0
grpcio-status==1.48.2
guppy3==3.1.2
h5py==3.7.0
hdfs==2.7.0
httplib2==0.20.4
hypothesis==6.56.2
hypothesis==6.56.3
idna==3.4
iniconfig==1.1.1
joblib==1.2.0
Expand All @@ -94,15 +94,15 @@ nltk==3.7
nose==1.3.7
numpy==1.22.4
oauth2client==4.1.3
oauthlib==3.2.1
oauthlib==3.2.2
objsize==0.5.2
opt-einsum==3.3.0
orjson==3.8.0
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 All @@ -123,19 +123,19 @@ pytest-timeout==2.1.0
pytest-xdist==2.5.0
python-dateutil==2.8.2
python-snappy==0.6.1
pytz==2022.4
pytz==2022.5
PyYAML==6.0
regex==2022.9.13
requests==2.28.1
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
SQLAlchemy==1.4.41
SQLAlchemy==1.4.42
sqlparse==0.4.3
tenacity==5.1.5
tensorboard==2.10.1
Expand Down
22 changes: 11 additions & 11 deletions sdks/python/container/py37/base_image_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# https://s.apache.org/beam-python-dev-wiki
# Reach out to a committer if you need help.

absl-py==1.2.0
absl-py==1.3.0
astunparse==1.6.3
attrs==22.1.0
beautifulsoup4==4.11.1
Expand Down Expand Up @@ -50,9 +50,9 @@ freezegun==1.2.2
future==0.18.2
gast==0.4.0
google-api-core==2.10.2
google-api-python-client==2.64.0
google-api-python-client==2.65.0
google-apitools==0.5.31
google-auth==2.12.0
google-auth==2.13.0
google-auth-httplib2==0.1.0
google-auth-oauthlib==0.4.6
google-cloud-bigquery==3.3.5
Expand All @@ -64,27 +64,27 @@ google-cloud-dlp==3.9.2
google-cloud-firestore==2.7.2
google-cloud-language==1.3.2
google-cloud-profiler==3.1.0
google-cloud-pubsub==2.13.9
google-cloud-pubsub==2.13.10
google-cloud-pubsublite==1.5.0
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
google-resumable-media==2.4.0
googleapis-common-protos==1.56.4
greenlet==1.1.3.post0
grpc-google-iam-v1==0.12.4
grpcio==1.49.1
grpcio==1.50.0
grpcio-status==1.48.2
guppy3==3.1.2
h5py==3.7.0
hdfs==2.7.0
httplib2==0.20.4
hypothesis==6.56.2
hypothesis==6.56.3
idna==3.4
importlib-metadata==5.0.0
iniconfig==1.1.1
Expand All @@ -101,15 +101,15 @@ nltk==3.7
nose==1.3.7
numpy==1.21.6
oauth2client==4.1.3
oauthlib==3.2.1
oauthlib==3.2.2
objsize==0.5.2
opt-einsum==3.3.0
orjson==3.8.0
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 All @@ -130,7 +130,7 @@ pytest-timeout==2.1.0
pytest-xdist==2.5.0
python-dateutil==2.8.2
python-snappy==0.6.1
pytz==2022.4
pytz==2022.5
PyYAML==6.0
regex==2022.9.13
requests==2.28.1
Expand All @@ -142,7 +142,7 @@ scipy==1.7.3
six==1.16.0
sortedcontainers==2.4.0
soupsieve==2.3.2.post1
SQLAlchemy==1.4.41
SQLAlchemy==1.4.42
sqlparse==0.4.3
tenacity==5.1.5
tensorboard==2.10.1
Expand Down
Loading