def test_annotate_multiple_results(self):
        from google.cloud.grpc.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=[])
        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()
Example #2
0
    def test_it(self):
        from google.cloud.grpc.vision.v1 import image_annotator_pb2

        description = 'testing 1 2 3'
        locale = 'US'
        mid = 'm/w/45342234'
        score = 0.235434231
        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)
Example #3
0
    def test_from_pb(self):
        from google.cloud.vision.likelihood import Likelihood
        from google.cloud.vision.safe_search import SafeSearchAnnotation
        from google.cloud.grpc.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)