コード例 #1
0
    def annotate_image(self, request, options=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], options=options)
        return r.responses[0]
コード例 #2
0
 def test_pb2_truthy(self):
     operation = ops.Operation(name='bar')
     protobuf.setdefault(operation, 'name', 'foo')
     assert operation.name == 'bar'
コード例 #3
0
 def test_pb2_falsy(self):
     operation = ops.Operation()
     protobuf.setdefault(operation, 'name', 'foo')
     assert operation.name == 'foo'
コード例 #4
0
 def test_dict_truthy(self):
     mapping = {'foo': 'bar'}
     protobuf.setdefault(mapping, 'foo', 'baz')
     assert mapping == {'foo': 'bar'}
コード例 #5
0
 def test_dict_falsy(self):
     mapping = {'foo': None}
     protobuf.setdefault(mapping, 'foo', 'bar')
     assert mapping == {'foo': 'bar'}
コード例 #6
0
 def test_dict_unset(self):
     mapping = {}
     protobuf.setdefault(mapping, 'foo', 'bar')
     assert mapping == {'foo': 'bar'}