Ejemplo n.º 1
0
 def test_set_list(self):
     list_ops_response = ops.ListOperationsResponse()
     protobuf.set(list_ops_response, 'operations', [
         {
             'name': 'foo'
         },
         ops.Operation(name='bar'),
     ])
     assert len(list_ops_response.operations) == 2
     for operation in list_ops_response.operations:
         assert isinstance(operation, ops.Operation)
     assert list_ops_response.operations[0].name == 'foo'
     assert list_ops_response.operations[1].name == 'bar'
Ejemplo n.º 2
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]
Ejemplo n.º 3
0
 def test_invalid_object(self):
     obj = object()
     with pytest.raises(TypeError):
         protobuf.set(obj, 'foo', 'bar')
Ejemplo n.º 4
0
 def test_set_nested(self):
     mapping = {}
     protobuf.set(mapping, 'foo.bar', 'baz')
     assert mapping == {'foo': {'bar': 'baz'}}
Ejemplo n.º 5
0
 def test_set_pb2(self):
     operation = ops.Operation()
     protobuf.set(operation, 'name', 'foo')
     assert operation.name == 'foo'
Ejemplo n.º 6
0
 def test_set_dict(self):
     mapping = {}
     protobuf.set(mapping, 'foo', 'bar')
     assert mapping == {'foo': 'bar'}
Ejemplo n.º 7
0
 def test_set_dict_nested_with_dict(self):
     rule = http_pb2.HttpRule()
     pattern = {'kind': 'foo', 'path': 'bar'}
     protobuf.set(rule, 'custom', pattern)
     assert rule.custom.kind == 'foo'
     assert rule.custom.path == 'bar'
Ejemplo n.º 8
0
 def test_set_dict_nested_with_message(self):
     rule = http_pb2.HttpRule()
     pattern = http_pb2.CustomHttpPattern(kind='foo', path='bar')
     protobuf.set(rule, 'custom', pattern)
     assert rule.custom.kind == 'foo'
     assert rule.custom.path == 'bar'