1414
1515"""Client for interacting with the Google Cloud Vision API."""
1616
17-
1817from google .cloud .client import JSONClient
1918from google .cloud .vision .connection import Connection
20- from google .cloud .vision .feature import Feature
2119from google .cloud .vision .image import Image
22-
23-
24- class VisionRequest (object ):
25- """Request container with image and features information to annotate.
26-
27- :type features: list of :class:`~gcoud.vision.feature.Feature`.
28- :param features: The features that dictate which annotations to run.
29-
30- :type image: bytes
31- :param image: Either Google Cloud Storage URI or raw byte stream of image.
32- """
33- def __init__ (self , image , features ):
34- self ._features = []
35- self ._image = image
36-
37- if isinstance (features , list ):
38- self ._features .extend (features )
39- elif isinstance (features , Feature ):
40- self ._features .append (features )
41- else :
42- raise TypeError ('Feature or list of Feature classes are required.' )
43-
44- def as_dict (self ):
45- """Dictionary representation of Image."""
46- return {
47- 'image' : self .image .as_dict (),
48- 'features' : [feature .as_dict () for feature in self .features ]
49- }
50-
51- @property
52- def features (self ):
53- """List of Feature objects."""
54- return self ._features
55-
56- @property
57- def image (self ):
58- """Image object containing image content."""
59- return self ._image
20+ from google .cloud .vision ._http import _HTTPVisionAPI
6021
6122
6223class Client (JSONClient ):
@@ -81,30 +42,7 @@ class Client(JSONClient):
8142 """
8243
8344 _connection_class = Connection
84-
85- def annotate (self , image , features ):
86- """Annotate an image to discover it's attributes.
87-
88- :type image: str
89- :param image: A string which can be a URL, a Google Cloud Storage path,
90- or a byte stream of the image.
91-
92- :type features: list of :class:`~google.cloud.vision.feature.Feature`
93- :param features: The type of detection that the Vision API should
94- use to determine image attributes. Pricing is
95- based on the number of Feature Types.
96-
97- See: https://cloud.google.com/vision/docs/pricing
98- :rtype: dict
99- :returns: List of annotations.
100- """
101- request = VisionRequest (image , features )
102-
103- data = {'requests' : [request .as_dict ()]}
104- response = self ._connection .api_request (
105- method = 'POST' , path = '/images:annotate' , data = data )
106-
107- return response ['responses' ][0 ]
45+ _vision_api_internal = None
10846
10947 def image (self , content = None , filename = None , source_uri = None ):
11048 """Get instance of Image using current client.
@@ -123,3 +61,14 @@ def image(self, content=None, filename=None, source_uri=None):
12361 """
12462 return Image (client = self , content = content , filename = filename ,
12563 source_uri = source_uri )
64+
65+ @property
66+ def _vision_api (self ):
67+ """Proxy method that handles which transport call Vision Annotate.
68+
69+ :rtype: :class:`~google.cloud.vision._rest._HTTPVisionAPI`
70+ :returns: Instance of ``_HTTPVisionAPI`` used to make requests.
71+ """
72+ if self ._vision_api_internal is None :
73+ self ._vision_api_internal = _HTTPVisionAPI (self )
74+ return self ._vision_api_internal
0 commit comments