示例#1
0
    def annotate_image(self,
                       request,
                       *,
                       retry=None,
                       timeout=None,
                       metadata=()):
        """Run image detection and annotation for an image.

        Example:
            >>> from google.cloud.vision_v1 import ImageAnnotatorClient
            >>> client = ImageAnnotatorClient()
            >>> request = {
            ...     'image': {
            ...         'source': {'image_uri': 'https://foo.com/image.jpg'},
            ...     },
            ... }
            >>> response = client.annotate_image(request)

        Args:
            request (:class:`~.vision_v1.AnnotateImageRequest`)
            retry (google.api_core.retry.Retry): Designation of what errors, if any,
                should be retried.
            timeout (float): The timeout for this request.
            metadata (Sequence[Tuple[str, str]]): Strings which should be
                sent along with the request as metadata.

        Returns:
            :class:`~.vision_v1.AnnotateImageResponse` The API response.
        """
        if not isinstance(request, proto.Message):
            # If the image is a file handler, set the content.
            image = protobuf.get(request, "image")
            if not isinstance(image, proto.Message):
                if hasattr(image, "read"):
                    img_bytes = image.read()
                    protobuf.set(request, "image", {})
                    protobuf.set(request, "image.content", img_bytes)
                    image = protobuf.get(request, "image")

                # If a filename is provided, read the file.
                filename = protobuf.get(image, "source.filename", default=None)
                if filename:
                    with open(filename, "rb") as img_file:
                        protobuf.set(request, "image.content", img_file.read())
                        protobuf.set(request, "image.source", None)

        # This method allows features not to be specified, and you get all
        # of them.
        if not isinstance(request, proto.Message):
            protobuf.setdefault(request, "features", self._get_all_features())
        elif len(request.features) == 0:
            request.features = self._get_all_features()
        r = self.batch_annotate_images(requests=[request],
                                       retry=retry,
                                       timeout=timeout,
                                       metadata=metadata)
        return r.responses[0]
示例#2
0
    def annotate_image(self, request, retry=None, timeout=None):
        """Run image detection and annotation for an image.

        Example:
            >>> from google.cloud.vision_v1 import ImageAnnotatorClient
            >>> client = ImageAnnotatorClient()
            >>> request = {
            ...     'image': {
            ...         'source': {'image_uri': 'https://foo.com/image.jpg'},
            ...     },
            ... }
            >>> response = client.annotate_image(request)

        Args:
            request (:class:`~.vision_v1.types.AnnotateImageRequest`)
            retry (Optional[google.api_core.retry.Retry]):  A retry object used
                to retry requests. If ``None`` is specified, requests will not
                be retried.
            timeout (Optional[float]): The amount of time, in seconds, to wait
                for the request to complete. Note that if ``retry`` is
                specified, the timeout applies to each individual attempt.

        Returns:
            :class:`~.vision_v1.types.AnnotateImageResponse` The API response.
        """
        # If the image is a file handler, set the content.
        image = protobuf.get(request, 'image')
        if hasattr(image, 'read'):
            img_bytes = image.read()
            protobuf.set(request, 'image', {})
            protobuf.set(request, 'image.content', img_bytes)
            image = protobuf.get(request, 'image')

        # If a filename is provided, read the file.
        filename = protobuf.get(image, 'source.filename', default=None)
        if filename:
            with io.open(filename, 'rb') as img_file:
                protobuf.set(request, 'image.content', img_file.read())
                protobuf.set(request, 'image.source', None)

        # This method allows features not to be specified, and you get all
        # of them.
        protobuf.setdefault(request, 'features', self._get_all_features())
        r = self.batch_annotate_images([request], retry=retry, timeout=timeout)
        return r.responses[0]
    def annotate_image(self, request, retry=None, timeout=None):
        """Run image detection and annotation for an image.

        Example:
            >>> from google.cloud.vision_v1 import ImageAnnotatorClient
            >>> client = ImageAnnotatorClient()
            >>> request = {
            ...     'image': {
            ...         'source': {'image_uri': 'https://foo.com/image.jpg'},
            ...     },
            ... }
            >>> response = client.annotate_image(request)

        Args:
            request (:class:`~.vision_v1.types.AnnotateImageRequest`)
            retry (Optional[google.api_core.retry.Retry]):  A retry object used
                to retry requests. If ``None`` is specified, requests will not
                be retried.
            timeout (Optional[float]): The amount of time, in seconds, to wait
                for the request to complete. Note that if ``retry`` is
                specified, the timeout applies to each individual attempt.

        Returns:
            :class:`~.vision_v1.types.AnnotateImageResponse` The API response.
        """
        # If the image is a file handler, set the content.
        image = protobuf.get(request, "image")
        if hasattr(image, "read"):
            img_bytes = image.read()
            protobuf.set(request, "image", {})
            protobuf.set(request, "image.content", img_bytes)
            image = protobuf.get(request, "image")

        # If a filename is provided, read the file.
        filename = protobuf.get(image, "source.filename", default=None)
        if filename:
            with io.open(filename, "rb") as img_file:
                protobuf.set(request, "image.content", img_file.read())
                protobuf.set(request, "image.source", None)

        # This method allows features not to be specified, and you get all
        # of them.
        protobuf.setdefault(request, "features", self._get_all_features())
        r = self.batch_annotate_images([request], retry=retry, timeout=timeout)
        return r.responses[0]
    def annotate_image(self, request, retry=None, timeout=None):
        """Run image detection and annotation for an image.

        Example:
            >>> from google.cloud.vision_v1 import ImageAnnotatorClient
            >>> client = ImageAnnotatorClient()
            >>> request = {
            ...     'image': {
            ...         'source': {'image_uri': 'https://foo.com/image.jpg'},
            ...     },
            ... }
            >>> response = client.annotate_image(request)

        Args:
            request (:class:`~.vision_v1.types.AnnotateImageRequest`)
            options (:class:`google.gax.CallOptions`): Overrides the default
                settings for this call, e.g, timeout, retries, etc.

        Returns:
            :class:`~.vision_v1.types.AnnotateImageResponse` The API response.
        """
        # If the image is a file handler, set the content.
        image = protobuf.get(request, 'image')
        if hasattr(image, 'read'):
            img_bytes = image.read()
            protobuf.set(request, 'image', {})
            protobuf.set(request, 'image.content', img_bytes)
            image = protobuf.get(request, 'image')

        # If a filename is provided, read the file.
        filename = protobuf.get(image, 'source.filename', default=None)
        if filename:
            with io.open(filename, 'rb') as img_file:
                protobuf.set(request, 'image.content', img_file.read())
                protobuf.set(request, 'image.source', None)

        # This method allows features not to be specified, and you get all
        # of them.
        protobuf.setdefault(request, 'features', self._get_all_features())
        r = self.batch_annotate_images([request], retry=retry, timeout=timeout)
        return r.responses[0]
示例#5
0
    def annotate_image(self, request, retry=None, timeout=None):
        """Run image detection and annotation for an image.

        Example:
            >>> from google.cloud.vision_v1 import ImageAnnotatorClient
            >>> client = ImageAnnotatorClient()
            >>> request = {
            ...     'image': {
            ...         'source': {'image_uri': 'https://foo.com/image.jpg'},
            ...     },
            ... }
            >>> response = client.annotate_image(request)

        Args:
            request (:class:`~.vision_v1.types.AnnotateImageRequest`)
            options (:class:`google.gax.CallOptions`): Overrides the default
                settings for this call, e.g, timeout, retries, etc.

        Returns:
            :class:`~.vision_v1.types.AnnotateImageResponse` The API response.
        """
        # If the image is a file handler, set the content.
        image = protobuf.get(request, 'image')
        if hasattr(image, 'read'):
            img_bytes = image.read()
            protobuf.set(request, 'image', {})
            protobuf.set(request, 'image.content', img_bytes)
            image = protobuf.get(request, 'image')

        # If a filename is provided, read the file.
        filename = protobuf.get(image, 'source.filename', default=None)
        if filename:
            with io.open(filename, 'rb') as img_file:
                protobuf.set(request, 'image.content', img_file.read())
                protobuf.set(request, 'image.source', None)

        # This method allows features not to be specified, and you get all
        # of them.
        protobuf.setdefault(request, 'features', self._get_all_features())
        r = self.batch_annotate_images([request], retry=retry, timeout=timeout)
        return r.responses[0]
示例#6
0
def test_setdefault_pb2_truthy():
    operation = operations_pb2.Operation(name='bar')
    protobuf_helpers.setdefault(operation, 'name', 'foo')
    assert operation.name == 'bar'
示例#7
0
def test_setdefault_pb2_falsy():
    operation = operations_pb2.Operation()
    protobuf_helpers.setdefault(operation, 'name', 'foo')
    assert operation.name == 'foo'
示例#8
0
def test_setdefault_dict_truthy():
    mapping = {'foo': 'bar'}
    protobuf_helpers.setdefault(mapping, 'foo', 'baz')
    assert mapping == {'foo': 'bar'}
示例#9
0
def test_setdefault_dict_falsy():
    mapping = {'foo': None}
    protobuf_helpers.setdefault(mapping, 'foo', 'bar')
    assert mapping == {'foo': 'bar'}
def test_setdefault_pb2_truthy():
    operation = operations_pb2.Operation(name="bar")
    protobuf_helpers.setdefault(operation, "name", "foo")
    assert operation.name == "bar"
def test_setdefault_pb2_truthy():
    operation = operations_pb2.Operation(name='bar')
    protobuf_helpers.setdefault(operation, 'name', 'foo')
    assert operation.name == 'bar'
def test_setdefault_dict_truthy():
    mapping = {'foo': 'bar'}
    protobuf_helpers.setdefault(mapping, 'foo', 'baz')
    assert mapping == {'foo': 'bar'}
def test_setdefault_dict_truthy():
    mapping = {"foo": "bar"}
    protobuf_helpers.setdefault(mapping, "foo", "baz")
    assert mapping == {"foo": "bar"}
def test_setdefault_dict_falsy():
    mapping = {"foo": None}
    protobuf_helpers.setdefault(mapping, "foo", "bar")
    assert mapping == {"foo": "bar"}
def test_setdefault_dict_unset():
    mapping = {}
    protobuf_helpers.setdefault(mapping, "foo", "bar")
    assert mapping == {"foo": "bar"}
def test_setdefault_dict_falsy():
    mapping = {"foo": None}
    protobuf_helpers.setdefault(mapping, "foo", "bar")
    assert mapping == {"foo": "bar"}
def test_setdefault_dict_truthy():
    mapping = {"foo": "bar"}
    protobuf_helpers.setdefault(mapping, "foo", "baz")
    assert mapping == {"foo": "bar"}
def test_setdefault_dict_unset():
    mapping = {}
    protobuf_helpers.setdefault(mapping, 'foo', 'bar')
    assert mapping == {'foo': 'bar'}
def test_setdefault_dict_unset():
    mapping = {}
    protobuf_helpers.setdefault(mapping, "foo", "bar")
    assert mapping == {"foo": "bar"}
def test_setdefault_dict_falsy():
    mapping = {'foo': None}
    protobuf_helpers.setdefault(mapping, 'foo', 'bar')
    assert mapping == {'foo': 'bar'}
def test_setdefault_pb2_truthy():
    operation = operations_pb2.Operation(name="bar")
    protobuf_helpers.setdefault(operation, "name", "foo")
    assert operation.name == "bar"
def test_setdefault_pb2_falsy():
    operation = operations_pb2.Operation()
    protobuf_helpers.setdefault(operation, 'name', 'foo')
    assert operation.name == 'foo'
示例#23
0
def test_setdefault_dict_unset():
    mapping = {}
    protobuf_helpers.setdefault(mapping, 'foo', 'bar')
    assert mapping == {'foo': 'bar'}
def test_setdefault_pb2_falsy():
    operation = operations_pb2.Operation()
    protobuf_helpers.setdefault(operation, "name", "foo")
    assert operation.name == "foo"
def test_setdefault_pb2_falsy():
    operation = operations_pb2.Operation()
    protobuf_helpers.setdefault(operation, "name", "foo")
    assert operation.name == "foo"