コード例 #1
0
def _to_gapic_image(image):
    """Helper function to convert an ``Image`` to a gRPC ``Image``.

    :type image: :class:`~google.cloud.vision.image.Image`
    :param image: Local ``Image`` class to be converted to gRPC ``Image``.

    :rtype: :class:`~google.cloud.proto.vision.v1.image_annotator_pb2.Image`
    :returns: gRPC ``Image`` converted from
              :class:`~google.cloud.vision.image.Image`.
    """
    if image.content is not None:
        return image_annotator_pb2.Image(content=image.content)
    if image.source is not None:
        if image.source.startswith('gs://'):
            return image_annotator_pb2.Image(
                source=image_annotator_pb2.ImageSource(
                    gcs_image_uri=image.source
                ),
            )
        elif image.source.startswith(('http://', 'https://')):
            return image_annotator_pb2.Image(
                source=image_annotator_pb2.ImageSource(
                    image_uri=image.source
                ),
            )
    raise ValueError('No image content or source found.')
コード例 #2
0
ファイル: test__http.py プロジェクト: qlg/google-cloud-python
    def test_call_annotate_with_pb_requests_results(self):
        from google.cloud.proto.vision.v1 import image_annotator_pb2

        client = mock.Mock(spec_set=['_connection'])

        feature_type = image_annotator_pb2.Feature.CROP_HINTS
        feature = image_annotator_pb2.Feature(type=feature_type, max_results=2)

        image = image_annotator_pb2.Image(content=IMAGE_CONTENT)

        aspect_ratios = [1.3333, 1.7777]
        crop_hints_params = image_annotator_pb2.CropHintsParams(
            aspect_ratios=aspect_ratios)
        image_context = image_annotator_pb2.ImageContext(
            crop_hints_params=crop_hints_params)
        request = image_annotator_pb2.AnnotateImageRequest(
            image=image, features=[feature], image_context=image_context)

        http_api = self._make_one(client)
        http_api._connection = mock.Mock(spec_set=['api_request'])
        http_api._connection.api_request.return_value = {'responses': []}

        responses = http_api.annotate(requests_pb=[request])

        # Establish that one and exactly one api_request call was made.
        self.assertEqual(http_api._connection.api_request.call_count, 1)

        # Establish that the basic keyword arguments look correct.
        call = http_api._connection.api_request.mock_calls[0]
        self.assertEqual(call[2]['method'], 'POST')
        self.assertEqual(call[2]['path'], '/images:annotate')

        # Establish that the responses look correct.
        self.assertEqual(responses, [])
        self.assertEqual(len(responses), 0)
コード例 #3
0
    def test_annotate_with_pb_requests_results(self):
        from google.cloud.proto.vision.v1 import image_annotator_pb2
        from google.cloud.vision.annotations import Annotations

        client = mock.Mock(spec_set=['_credentials'])

        feature_type = image_annotator_pb2.Feature.CROP_HINTS
        feature = image_annotator_pb2.Feature(type=feature_type, max_results=2)

        image_content = b'abc 1 2 3'
        image = image_annotator_pb2.Image(content=image_content)

        aspect_ratios = [1.3333, 1.7777]
        crop_hints_params = image_annotator_pb2.CropHintsParams(
            aspect_ratios=aspect_ratios)
        image_context = image_annotator_pb2.ImageContext(
            crop_hints_params=crop_hints_params)
        request = image_annotator_pb2.AnnotateImageRequest(
            image=image, features=[feature], image_context=image_context)

        with mock.patch('google.cloud.vision._gax.image_annotator_client.'
                        'ImageAnnotatorClient'):
            gax_api = self._make_one(client)

        responses = [
            image_annotator_pb2.AnnotateImageResponse(),
            image_annotator_pb2.AnnotateImageResponse(),
        ]
        response = image_annotator_pb2.BatchAnnotateImagesResponse(
            responses=responses)

        gax_api._annotator_client = mock.Mock(
            spec_set=['batch_annotate_images'])
        gax_api._annotator_client.batch_annotate_images.return_value = response
        responses = gax_api.annotate(requests_pb=[request])

        self.assertEqual(len(responses), 2)
        self.assertIsInstance(responses[0], Annotations)
        self.assertIsInstance(responses[1], Annotations)
        gax_api._annotator_client.batch_annotate_images.assert_called()