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 ):
@@ -80,37 +41,14 @@ class Client(JSONClient):
8041 ``http`` object is created that is bound to the
8142 ``credentials`` for the current object.
8243 """
44+ _vision_api_internal = None
8345
8446 def __init__ (self , project = None , credentials = None , http = None ):
8547 super (Client , self ).__init__ (
8648 project = project , credentials = credentials , http = http )
8749 self ._connection = Connection (
8850 credentials = self ._credentials , http = self ._http )
8951
90- def annotate (self , image , features ):
91- """Annotate an image to discover it's attributes.
92-
93- :type image: str
94- :param image: A string which can be a URL, a Google Cloud Storage path,
95- or a byte stream of the image.
96-
97- :type features: list of :class:`~google.cloud.vision.feature.Feature`
98- :param features: The type of detection that the Vision API should
99- use to determine image attributes. Pricing is
100- based on the number of Feature Types.
101-
102- See: https://cloud.google.com/vision/docs/pricing
103- :rtype: dict
104- :returns: List of annotations.
105- """
106- request = VisionRequest (image , features )
107-
108- data = {'requests' : [request .as_dict ()]}
109- response = self ._connection .api_request (
110- method = 'POST' , path = '/images:annotate' , data = data )
111-
112- return response ['responses' ][0 ]
113-
11452 def image (self , content = None , filename = None , source_uri = None ):
11553 """Get instance of Image using current client.
11654
@@ -128,3 +66,14 @@ def image(self, content=None, filename=None, source_uri=None):
12866 """
12967 return Image (client = self , content = content , filename = filename ,
13068 source_uri = source_uri )
69+
70+ @property
71+ def _vision_api (self ):
72+ """Proxy method that handles which transport call Vision Annotate.
73+
74+ :rtype: :class:`~google.cloud.vision._rest._HTTPVisionAPI`
75+ :returns: Instance of ``_HTTPVisionAPI`` used to make requests.
76+ """
77+ if self ._vision_api_internal is None :
78+ self ._vision_api_internal = _HTTPVisionAPI (self )
79+ return self ._vision_api_internal
0 commit comments