Skip to content

Commit a2011d5

Browse files
Add the ability to specify AnnotateImageRequest items in single-feature methods. (#3554)
1 parent 2cfe5c8 commit a2011d5

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

packages/google-cloud-vision/google/cloud/vision/decorators.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ def _create_single_feature_method(feature, enum):
8585
image (:class:`~.{module}.types.Image`): The image to analyze.
8686
options (:class:`google.gax.CallOptions`): Overrides the
8787
default settings for this call, e.g, timeout, retries, etc.
88+
kwargs (dict): Additional properties to be set on the
89+
:class:`~.{module}.types.AnnotateImageRequest`.
8890
8991
Returns:
9092
:class:`~.{module}.types.AnnotateImageResponse`: The API response.
@@ -94,16 +96,17 @@ def _create_single_feature_method(feature, enum):
9496
feature_value = {'type': enum.__dict__[feature]}
9597

9698
# Define the function to be returned.
97-
def inner(self, image, options=None):
99+
def inner(self, image, options=None, **kwargs):
98100
"""Return a single feature annotation for the given image.
99101
100102
Intended for use with functools.partial, to create the particular
101103
single-feature methods.
102104
"""
103-
request = {
104-
'image': image,
105-
'features': [feature_value],
106-
}
105+
request = dict(
106+
image=image,
107+
features=[feature_value],
108+
**kwargs
109+
)
107110
return self.annotate_image(request, options=options)
108111

109112
# Set the appropriate function metadata.

packages/google-cloud-vision/tests/unit/test_decorators.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,21 @@ class SingleFeatureMethodTests(unittest.TestCase):
5555
def test_runs_generic_single_image(self, ai):
5656
ai.return_value = vision.types.AnnotateImageResponse()
5757

58+
# Prove that other aspects of the AnnotateImageRequest, such as the
59+
# image context, will be preserved.
60+
SENTINEL = object()
61+
5862
# Make a face detection request.
5963
client = vision.ImageAnnotatorClient(
6064
credentials=mock.Mock(spec=Credentials),
6165
)
6266
image = {'source': {'image_uri': 'gs://my-test-bucket/image.jpg'}}
63-
response = client.face_detection(image)
67+
response = client.face_detection(image, image_context=SENTINEL)
68+
assert isinstance(response, vision.types.AnnotateImageResponse)
6469

6570
# Assert that the single-image method was called as expected.
6671
ai.assert_called_once_with({
6772
'features': [{'type': vision.enums.Feature.Type.FACE_DETECTION}],
6873
'image': image,
74+
'image_context': SENTINEL,
6975
}, options=None)

0 commit comments

Comments
 (0)