Skip to content

Commit

Permalink
Add the ability to specify AnnotateImageRequest items in single-featu…
Browse files Browse the repository at this point in the history
…re methods. (#3554)
  • Loading branch information
lukesneeringer authored Jun 27, 2017
1 parent 2cfe5c8 commit a2011d5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
13 changes: 8 additions & 5 deletions packages/google-cloud-vision/google/cloud/vision/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def _create_single_feature_method(feature, enum):
image (:class:`~.{module}.types.Image`): The image to analyze.
options (:class:`google.gax.CallOptions`): Overrides the
default settings for this call, e.g, timeout, retries, etc.
kwargs (dict): Additional properties to be set on the
:class:`~.{module}.types.AnnotateImageRequest`.
Returns:
:class:`~.{module}.types.AnnotateImageResponse`: The API response.
Expand All @@ -94,16 +96,17 @@ def _create_single_feature_method(feature, enum):
feature_value = {'type': enum.__dict__[feature]}

# Define the function to be returned.
def inner(self, image, options=None):
def inner(self, image, options=None, **kwargs):
"""Return a single feature annotation for the given image.
Intended for use with functools.partial, to create the particular
single-feature methods.
"""
request = {
'image': image,
'features': [feature_value],
}
request = dict(
image=image,
features=[feature_value],
**kwargs
)
return self.annotate_image(request, options=options)

# Set the appropriate function metadata.
Expand Down
8 changes: 7 additions & 1 deletion packages/google-cloud-vision/tests/unit/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,21 @@ class SingleFeatureMethodTests(unittest.TestCase):
def test_runs_generic_single_image(self, ai):
ai.return_value = vision.types.AnnotateImageResponse()

# Prove that other aspects of the AnnotateImageRequest, such as the
# image context, will be preserved.
SENTINEL = object()

# Make a face detection request.
client = vision.ImageAnnotatorClient(
credentials=mock.Mock(spec=Credentials),
)
image = {'source': {'image_uri': 'gs://my-test-bucket/image.jpg'}}
response = client.face_detection(image)
response = client.face_detection(image, image_context=SENTINEL)
assert isinstance(response, vision.types.AnnotateImageResponse)

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

0 comments on commit a2011d5

Please sign in to comment.