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

Vision support for batch processing part one. #2967

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 8 additions & 8 deletions vision/google/cloud/vision/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ def annotate(self, image, features):
:type features: list
:param features: List of :class:`~google.cloud.vision.feature.Feature`.

:rtype: :class:`~google.cloud.vision.annotations.Annotations`
:returns: Instance of ``Annotations`` with results or ``None``.
:rtype: :class:`~google.cloud.vision.annotations.Annotations` or list
:returns: Instance of ``Annotations`` or list of ``Annotations``.
"""
gapic_features = [_to_gapic_feature(feature) for feature in features]
gapic_image = _to_gapic_image(image)
request = image_annotator_pb2.AnnotateImageRequest(
image=gapic_image, features=gapic_features)
requests = [request]
annotator_client = self._annotator_client
images = annotator_client.batch_annotate_images(requests)
if len(images.responses) == 1:
return Annotations.from_pb(images.responses[0])
elif len(images.responses) > 1:
raise NotImplementedError(
'Multiple image processing is not yet supported.')
responses = annotator_client.batch_annotate_images(requests).responses
if len(responses) == 1:

This comment was marked as spam.

This comment was marked as spam.

return Annotations.from_pb(responses[0])
elif len(responses) > 1:
return [Annotations.from_pb(response)
for response in responses]


def _to_gapic_feature(feature):
Expand Down
16 changes: 8 additions & 8 deletions vision/google/cloud/vision/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ def annotate(self, image, features):
based on the number of Feature Types.

See: https://cloud.google.com/vision/docs/pricing
:rtype: dict
:returns: List of annotations.
:rtype: list or class:`~googe.cloud.vision.annotations.Annotations`
:returns: Instance of ``Annotations`` or List of ``Annotations``.
"""
request = _make_request(image, features)

data = {'requests': [request]}
api_response = self._connection.api_request(
method='POST', path='/images:annotate', data=data)
images = api_response.get('responses')
if len(images) == 1:
return Annotations.from_api_repr(images[0])
elif len(images) > 1:
raise NotImplementedError(
'Multiple image processing is not yet supported.')
responses = api_response.get('responses')
if len(responses) == 1:

This comment was marked as spam.

return Annotations.from_api_repr(responses[0])
elif len(responses) > 1:
return [Annotations.from_api_repr(response)
for response in responses]


def _make_request(image, features):
Expand Down
33 changes: 33 additions & 0 deletions vision/unit_tests/_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,39 @@
}


MULTIPLE_RESPONSE = {
'responses': [
{
'labelAnnotations': [
{
'mid': '/m/0k4j',
'description': 'automobile',
'score': 0.9776855
},
{
'mid': '/m/07yv9',
'description': 'vehicle',
'score': 0.947987
},
{
'mid': '/m/07r04',
'description': 'truck',
'score': 0.88429511
},
],
},
{
'safeSearchAnnotation': {
'adult': 'VERY_UNLIKELY',
'spoof': 'UNLIKELY',
'medical': 'POSSIBLE',
'violence': 'VERY_UNLIKELY'
},
},
],
}


SAFE_SEARCH_DETECTION_RESPONSE = {
'responses': [
{
Expand Down
4 changes: 2 additions & 2 deletions vision/unit_tests/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ def test_annotate_multiple_results(self):
gax_api._annotator_client = mock.Mock(
spec_set=['batch_annotate_images'], **mock_response)
with mock.patch('google.cloud.vision._gax.Annotations'):
with self.assertRaises(NotImplementedError):
gax_api.annotate(image, [feature])
responses = gax_api.annotate(image, [feature])

self.assertEqual(len(responses), 2)

This comment was marked as spam.

gax_api._annotator_client.batch_annotate_images.assert_called()


Expand Down
16 changes: 13 additions & 3 deletions vision/unit_tests/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def test_call_annotate_with_more_than_one_result(self):
from google.cloud.vision.feature import Feature
from google.cloud.vision.feature import FeatureTypes
from google.cloud.vision.image import Image
from google.cloud.vision.likelihood import Likelihood
from unit_tests._fixtures import MULTIPLE_RESPONSE

client = mock.Mock(spec_set=['_connection'])
feature = Feature(FeatureTypes.LABEL_DETECTION, 5)
Expand All @@ -58,9 +60,17 @@ def test_call_annotate_with_more_than_one_result(self):

http_api = self._make_one(client)
http_api._connection = mock.Mock(spec_set=['api_request'])
http_api._connection.api_request.return_value = {'responses': [1, 2]}
with self.assertRaises(NotImplementedError):
http_api.annotate(image, [feature])
http_api._connection.api_request.return_value = MULTIPLE_RESPONSE
responses = http_api.annotate(image, [feature])

self.assertEqual(len(responses), 2)
image_one = responses[0]
image_two = responses[1]
self.assertEqual(len(image_one.labels), 3)
self.assertIsInstance(image_one.safe_searches, tuple)
self.assertEqual(image_two.safe_searches.adult,
Likelihood.VERY_UNLIKELY)
self.assertEqual(len(image_two.labels), 0)


class TestVisionRequest(unittest.TestCase):
Expand Down