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]
def test_invalid_object(self): obj = object() with pytest.raises(TypeError): protobuf.get(obj, 'foo', 'bar')
def test_get_pb2_default(self): operation = ops.Operation() assert protobuf.get(operation, 'foo', default='bar') == 'bar'
def test_get_pb2_present(self): operation = ops.Operation(name='foo') assert protobuf.get(operation, 'name') == 'foo'
def test_get_pb2_sentinel(self): operation = ops.Operation() with pytest.raises(KeyError): assert protobuf.get(operation, 'foo')
def test_get_dict_nested_default(self): assert protobuf.get({}, 'foo.baz', default='bacon') == 'bacon' assert protobuf.get({'foo': {}}, 'foo.baz', default='bacon') == 'bacon'
def test_get_dict_nested(self): assert protobuf.get({'foo': {'bar': 'baz'}}, 'foo.bar') == 'baz'
def test_get_dict_default(self): assert protobuf.get({}, 'foo', default='bar') == 'bar'
def test_get_dict_present(self): assert protobuf.get({'foo': 'bar'}, 'foo') == 'bar'
def test_get_dict_sentinel(self): with pytest.raises(KeyError): assert protobuf.get({}, 'foo')