コード例 #1
0
    def test_annotate_multiple_results(self):
        from google.cloud.proto.vision.v1 import image_annotator_pb2
        from google.cloud.vision.annotations import Annotations
        from google.cloud.vision.feature import Feature
        from google.cloud.vision.feature import FeatureTypes
        from google.cloud.vision.image import Image

        client = mock.Mock(spec_set=['_credentials'])
        feature = Feature(FeatureTypes.LABEL_DETECTION, 5)
        image_content = b'abc 1 2 3'
        image = Image(client, content=image_content)
        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
        images = ((image, [feature]),)
        responses = gax_api.annotate(images)

        self.assertEqual(len(responses), 2)
        self.assertIsInstance(responses[0], Annotations)
        self.assertIsInstance(responses[1], Annotations)
        gax_api._annotator_client.batch_annotate_images.assert_called()
コード例 #2
0
    def test_it(self):
        from google.cloud.proto.vision.v1 import image_annotator_pb2

        description = 'testing 1 2 3'
        locale = 'US'
        mid = 'm/w/45342234'
        score = 0.390625
        entity_annotation = _make_pb_entity()

        image_response = image_annotator_pb2.AnnotateImageResponse(
            label_annotations=[entity_annotation])

        annotations = self._call_fut(image_response)
        self.assertEqual(len(annotations['labels']), 1)
        entity = annotations['labels'][0]

        self.assertEqual(entity.description, description)
        self.assertEqual(entity.mid, mid)
        self.assertEqual(entity.locale, locale)
        self.assertEqual(entity.score, score)
        self.assertEqual(len(entity.bounds.vertices), 1)
        self.assertEqual(entity.bounds.vertices[0].x_coordinate, 1)
        self.assertEqual(entity.bounds.vertices[0].y_coordinate, 2)
        self.assertEqual(len(entity.locations), 1)
        self.assertEqual(entity.locations[0].latitude, 1.0)
        self.assertEqual(entity.locations[0].longitude, 2.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()
コード例 #4
0
    def test_from_pb(self):
        from google.cloud.vision.likelihood import Likelihood
        from google.cloud.vision.safe_search import SafeSearchAnnotation
        from google.cloud.proto.vision.v1 import image_annotator_pb2

        image_response = image_annotator_pb2.AnnotateImageResponse()
        annotations = self._make_one().from_pb(image_response)
        self.assertEqual(annotations.labels, [])
        self.assertEqual(annotations.logos, [])
        self.assertEqual(annotations.faces, [])
        self.assertEqual(annotations.landmarks, [])
        self.assertEqual(annotations.texts, [])
        self.assertIsNone(annotations.properties)

        self.assertIsInstance(annotations.safe_searches, SafeSearchAnnotation)
        safe_search = annotations.safe_searches
        unknown = Likelihood.UNKNOWN
        self.assertIs(safe_search.adult, unknown)
        self.assertIs(safe_search.spoof, unknown)
        self.assertIs(safe_search.medical, unknown)
        self.assertIs(safe_search.violence, unknown)