Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to specify AnnotateImageRequest items in single-feature methods. #3554

Merged
merged 2 commits into from
Jun 27, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions 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 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()

This comment was marked as spam.


# 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)