示例#1
0
    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)
示例#2
0
    def detect_crop_hints(self, aspect_ratios=None, limit=10):
        """Detect crop hints in image.

        :type aspect_ratios: list
        :param aspect_ratios: (Optional) List of floats i.e. 4/3 == 1.33333. A
                              maximum of 16 aspect ratios can be given.

        :type limit: int
        :param limit: (Optional) The number of crop hints to detect.

        :rtype: list
        :returns: List of :class:`~google.cloud.vision.crop_hint.CropHints`.
        """
        feature_type = image_annotator_pb2.Feature.CROP_HINTS
        feature = image_annotator_pb2.Feature(type=feature_type,
                                              max_results=limit)
        image = _to_gapic_image(self)
        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)

        annotations = self._detect_annotation_from_pb([request])
        return annotations[0].crop_hints
示例#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()
示例#4
0
    def detect_full_text(self, language_hints=None, limit=10):
        """Detect a full document's text.

        :type language_hints: list
        :param language_hints: (Optional) A list of BCP-47 language codes. See
                               https://cloud.google.com/vision/docs/languages

        :type limit: int
        :param limit: (Optional) The number of documents to detect.

        :rtype: list
        :returns: List of :class:`~google.cloud.vision.text.TextAnnotation`.
        """
        feature_type = image_annotator_pb2.Feature.DOCUMENT_TEXT_DETECTION
        feature = image_annotator_pb2.Feature(type=feature_type,
                                              max_results=limit)
        image = _to_gapic_image(self)
        image_context = image_annotator_pb2.ImageContext(
            language_hints=language_hints)
        request = image_annotator_pb2.AnnotateImageRequest(
            image=image, features=[feature], image_context=image_context)
        annotations = self._detect_annotation_from_pb([request])
        return annotations[0].full_texts