8000 Merge pull request #2914 from daspecster/vision-add-grpc-conversions · richkadel/google-cloud-python@1a0ce44 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1a0ce44

Browse files
authored
Merge pull request googleapis#2914 from daspecster/vision-add-grpc-conversions
Add Vision Image and Feature gRPC conversion helper functions.
2 parents cc10ac3 + 7cb9e29 commit 1a0ce44

File tree

4 files changed

+120
-8
lines changed

4 files changed

+120
-8
lines changed

vision/google/cloud/vision/_gax.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,55 @@
1414

1515
"""GAX Client for interacting with the Google Cloud Vision API."""
1616

17+
from google.cloud.gapic.vision.v1 import image_annotator_client
18+
from google.cloud.grpc.vision.v1 import image_annotator_pb2
19+
20+
from google.cloud._helpers import _to_bytes
21+
1722

1823
class _GAPICVisionAPI(object):
1924
"""Vision API for interacting with the gRPC version of Vision.
2025
21-
:type client: :class:`~google.cloud.core.client.Client`
26+
:type client: :class:`~google.cloud.vision.client.Client`
2227
:param client: Instance of ``Client`` object.
2328
"""
2429
def __init__(self, client=None):
25-
raise NotImplementedError
30+
self._client = client
31+
self._api = image_annotator_client.ImageAnnotatorClient()
32+
33+
34+
def _to_gapic_feature(feature):
35+
"""Helper function to convert a ``Feature`` to a gRPC ``Feature``.
36+
37+
:type feature: :class:`~google.cloud.vision.feature.Feature`
38+
:param feature: Local ``Feature`` class to be converted to gRPC ``Feature``
39+
instance.
40+
41+
:rtype: :class:`~google.cloud.grpc.vision.v1.image_annotator_pb2.Feature`
42+
:returns: gRPC ``Feature`` converted from
43+
:class:`~google.cloud.vision.feature.Feature`.
44+
"""
45+
return image_annotator_pb2.Feature(
46+
type=getattr(image_annotator_pb2.Feature, feature.feature_type),
47+
max_results=feature.max_results)
48+
49+
50+
def _to_gapic_image(image):
51+
"""Helper function to convert an ``Image`` to a gRPC ``Image``.
52+
53+
:type image: :class:`~google.cloud.vision.image.Image`
54+
:param image: Local ``Image`` class to be converted to gRPC ``Image``.
55+
56+
:rtype: :class:`~google.cloud.grpc.vision.v1.image_annotator_pb2.Image`
57+
:returns: gRPC ``Image`` converted from
58+
:class:`~google.cloud.vision.image.Image`.
59+
"""
60+
if image.content is not None:
61+
return image_annotator_pb2.Image(content=_to_bytes(image.content))
62+
if image.source is not None:
63+
return image_annotator_pb2.Image(
64+
source=image_annotator_pb2.ImageSource(
65+
gcs_image_uri=image.source
66+
),
67+
)
68+
raise ValueError('No image content or source found.')

vision/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
REQUIREMENTS = [
5353
'enum34',
5454
'google-cloud-core >= 0.22.1, < 0.23dev',
55+
'gapic-google-cloud-vision-v1 >= 0.14.0, < 0.15dev',
5556
]
5657

5758
setup(

vision/unit_tests/test__gax.py

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import unittest
1616

17+
import mock
18+
1719

1820
class TestGAXClient(unittest.TestCase):
1921
def _get_target_class(self):
@@ -23,7 +25,62 @@ def _get_target_class(self):
2325
def _make_one(self, *args, **kwargs):
2426
return self._get_target_class()(*args, **kwargs)
2527

26-
def test_gax_not_implemented(self):
28+
def test_ctor(self):
29+
client = mock.Mock()
30+
with mock.patch('google.cloud.vision._gax.image_annotator_client.'
31+
'ImageAnnotatorClient'):
32+
api = self._make_one(client)
33+
self.assertIs(api._client, client)
34+
35+
36+
class TestToGAPICFeature(unittest.TestCase):
37+
def _call_fut(self, feature):
38+
from google.cloud.vision._gax import _to_gapic_feature
39+
return _to_gapic_feature(feature)
40+
41+
def test__to_gapic_feature(self):
42+
from google.cloud.vision.feature import Feature
43+
from google.cloud.vision.feature import FeatureTypes
44+
from google.cloud.grpc.vision.v1 import image_annotator_pb2
45+
46+
feature = Feature(FeatureTypes.LABEL_DETECTION, 5)
47+
feature_pb = self._call_fut(feature)
48+
self.assertIsInstance(feature_pb, image_annotator_pb2.Feature)
49+
self.assertEqual(feature_pb.type, 4)
50+
self.assertEqual(feature_pb.max_results, 5)
51+
52+
53+
class TestToGAPICImage(unittest.TestCase):
54+
def _call_fut(self, image):
55+
from google.cloud.vision._gax import _to_gapic_image
56+
return _to_gapic_image(image)
57+
58+
def test__to_gapic_image_content(self):
59+
import base64
60+
from google.cloud.vision.image import Image
61+
from google.cloud.grpc.vision.v1 import image_annotator_pb2
62+
63+
image_content = b'abc 1 2 3'
64+
b64_content = base64.b64encode(image_content)
65+
client = object()
66+
image = Image(client, content=image_content)
67+
image_pb = self._call_fut(image)
68+
self.assertIsInstance(image_pb, image_annotator_pb2.Image)
69+
self.assertEqual(image_pb.content, b64_content)
70+
71+
def test__to_gapic_image_uri(self):
72+
from google.cloud.vision.image import Image
73+
from google.cloud.grpc.vision.v1 import image_annotator_pb2
74+
75+
image_uri = 'gs://1234/34.jpg'
2776
client = object()
28-
with self.assertRaises(NotImplementedError):
29-
self._make_one(client=client)
77+
image = Image(client, source_uri=image_uri)
78+
image_pb = self._call_fut(image)
79+
self.assertIsInstance(image_pb, image_annotator_pb2.Image)
80+
self.assertEqual(image_pb.source.gcs_image_uri, image_uri)
81+
82+
def test__to_gapic_with_empty_image(self):
83+
image = mock.Mock(
84+
content=None, source=None, spec=['content', 'source'])
85+
with self.assertRaises(ValueError):
86+
self._call_fut(image)

vision/unit_tests/test_client.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,25 @@ def test_annotate_with_preset_api(self):
5555
client._vision_api.annotate()
5656
api.annotate.assert_called_once_with()
5757

58-
def test_gax_not_implemented_from_client(self):
58+
def test_make_gax_client(self):
59+
from google.cloud.vision._gax import _GAPICVisionAPI
60+
5961
credentials = _make_credentials()
6062
client = self._make_one(project=PROJECT, credentials=credentials,
6163
use_gax=None)
6264
client._connection = _Connection()
65+
with mock.patch('google.cloud.vision.client._GAPICVisionAPI',
66+
spec=True):
67+
self.assertIsInstance(client._vision_api, _GAPICVisionAPI)
68+
69+
def test_make_http_client(self):
70+
from google.cloud.vision._http import _HTTPVisionAPI
6371

64-
with self.assertRaises(NotImplementedError):
65-
client._vision_api()
72+
credentials = _make_credentials()
73+
client = self._make_one(project=PROJECT, credentials=credentials,
74+
use_gax=False)
75+
client._connection = _Connection()
76+
self.assertIsInstance(client._vision_api, _HTTPVisionAPI)
6677

6778
def test_face_annotation(self):
6879
from google.cloud.vision.feature import Feature, FeatureTypes

0 commit comments

Comments
 (0)
0