|
| 1 | +# Copyright 2017, Google Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# EDITING INSTRUCTIONS |
| 16 | +# This file was generated from the file |
| 17 | +# https://github.com/google/googleapis/blob/master/google/cloud/vision/v1/image_annotator.proto, |
| 18 | +# and updates to that file get reflected here through a refresh process. |
| 19 | +# For the short term, the refresh process will only be runnable by Google engineers. |
| 20 | +# |
| 21 | +# The only allowed edits are to method and file documentation. A 3-way |
| 22 | +# merge preserves those additions if the generated source changes. |
| 23 | +"""Accesses the google.cloud.vision.v1 ImageAnnotator API.""" |
| 24 | + |
| 25 | +import collections |
| 26 | +import json |
| 27 | +import os |
| 28 | +import pkg_resources |
| 29 | +import platform |
| 30 | + |
| 31 | +from google.gax import api_callable |
| 32 | +from google.gax import config |
| 33 | +from google.gax import path_template |
| 34 | +import google.gax |
| 35 | + |
| 36 | +from google.cloud.gapic.vision.v1 import enums |
| 37 | +from google.cloud.proto.vision.v1 import image_annotator_pb2 |
| 38 | + |
| 39 | + |
| 40 | +class ImageAnnotatorClient(object): |
| 41 | + """Service that performs Google Cloud Vision API detection tasks over |
| 42 | + client images, such as face, landmark, logo, label, and text detection. The |
| 43 | + ImageAnnotator service returns detected entities from the images. |
| 44 | + """ |
| 45 | + |
| 46 | + SERVICE_ADDRESS = 'vision.googleapis.com' |
| 47 | + """The default address of the service.""" |
| 48 | + |
| 49 | + DEFAULT_SERVICE_PORT = 443 |
| 50 | + """The default port of the service.""" |
| 51 | + |
| 52 | + # The scopes needed to make gRPC calls to all of the methods defined in |
| 53 | + # this service |
| 54 | + _ALL_SCOPES = ('https://www.googleapis.com/auth/cloud-platform', ) |
| 55 | + |
| 56 | + def __init__(self, |
| 57 | + service_path=SERVICE_ADDRESS, |
| 58 | + port=DEFAULT_SERVICE_PORT, |
| 59 | + channel=None, |
| 60 | + credentials=None, |
| 61 | + ssl_credentials=None, |
| 62 | + scopes=None, |
| 63 | + client_config=None, |
| 64 | + app_name=None, |
| 65 | + app_version='', |
| 66 | + lib_name=None, |
| 67 | + lib_version='', |
| 68 | + metrics_headers=()): |
| 69 | + """Constructor. |
| 70 | +
|
| 71 | + Args: |
| 72 | + service_path (string): The domain name of the API remote host. |
| 73 | + port (int): The port on which to connect to the remote host. |
| 74 | + channel (:class:`grpc.Channel`): A ``Channel`` instance through |
| 75 | + which to make calls. |
| 76 | + credentials (object): The authorization credentials to attach to |
| 77 | + requests. These credentials identify this application to the |
| 78 | + service. |
| 79 | + ssl_credentials (:class:`grpc.ChannelCredentials`): A |
| 80 | + ``ChannelCredentials`` instance for use with an SSL-enabled |
| 81 | + channel. |
| 82 | + scopes (list[string]): A list of OAuth2 scopes to attach to requests. |
| 83 | + client_config (dict): |
| 84 | + A dictionary for call options for each method. See |
| 85 | + :func:`google.gax.construct_settings` for the structure of |
| 86 | + this data. Falls back to the default config if not specified |
| 87 | + or the specified config is missing data points. |
| 88 | + app_name (string): The name of the application calling |
| 89 | + the service. Recommended for analytics purposes. |
| 90 | + app_version (string): The version of the application calling |
| 91 | + the service. Recommended for analytics purposes. |
| 92 | + lib_name (string): The API library software used for calling |
| 93 | + the service. (Unless you are writing an API client itself, |
| 94 | + leave this as default.) |
| 95 | + lib_version (string): The API library software version used |
| 96 | + for calling the service. (Unless you are writing an API client |
| 97 | + itself, leave this as default.) |
| 98 | + metrics_headers (dict): A dictionary of values for tracking |
| 99 | + client library metrics. Ultimately serializes to a string |
| 100 | + (e.g. 'foo/1.2.3 bar/3.14.1'). This argument should be |
| 101 | + considered private. |
| 102 | +
|
| 103 | + Returns: |
| 104 | + A ImageAnnotatorClient object. |
| 105 | + """ |
| 106 | + # Unless the calling application specifically requested |
| 107 | + # OAuth scopes, request everything. |
| 108 | + if scopes is None: |
| 109 | + scopes = self._ALL_SCOPES |
| 110 | + |
| 111 | + # Initialize an empty client config, if none is set. |
| 112 | + if client_config is None: |
| 113 | + client_config = {} |
| 114 | + |
| 115 | + # Initialize metrics_headers as an ordered dictionary |
| 116 | + # (cuts down on cardinality of the resulting string slightly). |
| 117 | + metrics_headers = collections.OrderedDict(metrics_headers) |
| 118 | + metrics_headers['gl-python'] = platform.python_version() |
| 119 | + |
| 120 | + # The library may or may not be set, depending on what is |
| 121 | + # calling this client. Newer client libraries set the library name |
| 122 | + # and version. |
| 123 | + if lib_name: |
| 124 | + metrics_headers[lib_name] = lib_version |
| 125 | + |
| 126 | + # Finally, track the GAPIC package version. |
| 127 | + metrics_headers['gapic'] = pkg_resources.get_distribution( |
| 128 | + 'google-cloud-vision', ).version |
| 129 | + |
| 130 | + # Load the configuration defaults. |
| 131 | + default_client_config = json.loads( |
| 132 | + pkg_resources.resource_string( |
| 133 | + __name__, 'image_annotator_client_config.json').decode()) |
| 134 | + defaults = api_callable.construct_settings( |
| 135 | + 'google.cloud.vision.v1.ImageAnnotator', |
| 136 | + default_client_config, |
| 137 | + client_config, |
| 138 | + config.STATUS_CODE_NAMES, |
| 139 | + metrics_headers=metrics_headers, ) |
| 140 | + self.image_annotator_stub = config.create_stub( |
| 141 | + image_annotator_pb2.ImageAnnotatorStub, |
| 142 | + channel=channel, |
| 143 | + service_path=service_path, |
| 144 | + service_port=port, |
| 145 | + credentials=credentials, |
| 146 | + scopes=scopes, |
| 147 | + ssl_credentials=ssl_credentials) |
| 148 | + |
| 149 | + self._batch_annotate_images = api_callable.create_api_call( |
| 150 | + self.image_annotator_stub.BatchAnnotateImages, |
| 151 | + settings=defaults['batch_annotate_images']) |
| 152 | + |
| 153 | + # Service calls |
| 154 | + def batch_annotate_images(self, requests, options=None): |
| 155 | + """ |
| 156 | + Run image detection and annotation for a batch of images. |
| 157 | +
|
| 158 | + Example: |
| 159 | + >>> from google.cloud.gapic.vision.v1 import image_annotator_client |
| 160 | + >>> client = image_annotator_client.ImageAnnotatorClient() |
| 161 | + >>> requests = [] |
| 162 | + >>> response = client.batch_annotate_images(requests) |
| 163 | +
|
| 164 | + Args: |
| 165 | + requests (list[:class:`google.cloud.proto.vision.v1.image_annotator_pb2.AnnotateImageRequest`]): Individual image annotation requests for this batch. |
| 166 | + options (:class:`google.gax.CallOptions`): Overrides the default |
| 167 | + settings for this call, e.g, timeout, retries etc. |
| 168 | +
|
| 169 | + Returns: |
| 170 | + A :class:`google.cloud.proto.vision.v1.image_annotator_pb2.BatchAnnotateImagesResponse` instance. |
| 171 | +
|
| 172 | + Raises: |
| 173 | + :exc:`google.gax.errors.GaxError` if the RPC is aborted. |
| 174 | + :exc:`ValueError` if the parameters are invalid. |
| 175 | + """ |
| 176 | + # Create the request object. |
| 177 | + request = image_annotator_pb2.BatchAnnotateImagesRequest( |
| 178 | + requests=requests) |
| 179 | + return self._batch_annotate_images(request, options) |
0 commit comments