# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# EDITING INSTRUCTIONS
# This file was generated from the file
# https://github.com/google/googleapis/blob/master/google/cloud/vision/v1/image_annotator.proto,
# and updates to that file get reflected here through a refresh process.
# For the short term, the refresh process will only be runnable by Google engineers.
#
# The only allowed edits are to method and file documentation. A 3-way
# merge preserves those additions if the generated source changes.
"""Accesses the google.cloud.vision.v1 ImageAnnotator API."""
import json
import os
import pkg_resources
import platform
from google.gax import api_callable
from google.gax import config
from google.gax import path_template
import google.gax
from google.cloud.vision.v1 import image_annotator_pb2
[docs]class ImageAnnotatorApi(object):
"""
Service that performs Google Cloud Vision API detection tasks, such as face,
landmark, logo, label, and text detection, over client images, and returns
detected entities from the images.
"""
SERVICE_ADDRESS = 'vision.googleapis.com'
"""The default address of the service."""
DEFAULT_SERVICE_PORT = 443
"""The default port of the service."""
_CODE_GEN_NAME_VERSION = 'gapic/0.1.0'
_GAX_VERSION = pkg_resources.get_distribution('google-gax').version
# The scopes needed to make gRPC calls to all of the methods defined in
# this service
_ALL_SCOPES = ('https://www.googleapis.com/auth/cloud-platform', )
def __init__(self,
service_path=SERVICE_ADDRESS,
port=DEFAULT_SERVICE_PORT,
channel=None,
metadata_transformer=None,
ssl_creds=None,
scopes=None,
client_config=None,
app_name='gax',
app_version=_GAX_VERSION):
"""Constructor.
Args:
service_path (string): The domain name of the API remote host.
port (int): The port on which to connect to the remote host.
channel (:class:`grpc.beta.implementations.Channel`): A ``Channel``
object through which to make calls.
ssl_creds (:class:`grpc.beta.implementations.ClientCredentials`):
A `ClientCredentials` for use with an SSL-enabled channel.
client_config (dict):
A dictionary for call options for each method. See
:func:`google.gax.construct_settings` for the structure of
this data. Falls back to the default config if not specified
or the specified config is missing data points.
metadata_transformer (Callable[[], list]): A function that creates
the metadata for requests.
app_name (string): The codename of the calling service.
app_version (string): The version of the calling service.
Returns:
A ImageAnnotatorApi object.
"""
if scopes is None:
scopes = self._ALL_SCOPES
if client_config is None:
client_config = {}
goog_api_client = '{}/{} {} gax/{} python/{}'.format(
app_name, app_version, self._CODE_GEN_NAME_VERSION,
self._GAX_VERSION, platform.python_version())
metadata = [('x-goog-api-client', goog_api_client)]
default_client_config = json.loads(
pkg_resources.resource_string(
__name__, 'image_annotator_client_config.json').decode())
defaults = api_callable.construct_settings(
'google.cloud.vision.v1.ImageAnnotator',
default_client_config,
client_config,
config.STATUS_CODE_NAMES,
kwargs={'metadata': metadata})
self.stub = config.create_stub(
image_annotator_pb2.beta_create_ImageAnnotator_stub,
service_path,
port,
ssl_creds=ssl_creds,
channel=channel,
metadata_transformer=metadata_transformer,
scopes=scopes)
self._batch_annotate_images = api_callable.create_api_call(
self.stub.BatchAnnotateImages,
settings=defaults['batch_annotate_images'])
# Service calls
[docs] def batch_annotate_images(self, requests, options=None):
"""
Run image detection and annotation for a batch of images.
Example:
>>> from google.cloud.vision.v1.image_annotator_api import ImageAnnotatorApi
>>> from google.cloud.vision.v1 import image_annotator_pb2
>>> api = ImageAnnotatorApi()
>>> requests = []
>>> response = api.batch_annotate_images(requests)
Args:
requests (list[:class:`google.cloud.vision.v1.image_annotator_pb2.AnnotateImageRequest`]): Individual image annotation requests for this batch.
options (:class:`google.gax.CallOptions`): Overrides the default
settings for this call, e.g, timeout, retries etc.
Returns:
A :class:`google.cloud.vision.v1.image_annotator_pb2.BatchAnnotateImagesResponse` instance.
Raises:
:exc:`google.gax.errors.GaxError` if the RPC is aborted.
"""
request = image_annotator_pb2.BatchAnnotateImagesRequest(
requests=requests)
return self._batch_annotate_images(request, options)