def get_image_labels_from_url(image_url, limit=DEFAULT_LIMIT):
    """
    :param image_url: str URL
    :param limit: int Max number of results to return
    :return: list of `google.cloud.vision.entity.EntityAnnotation` instances
    """
    image = Image(get_vision_client(), source_uri=image_url)
    return image.detect_labels(limit=limit)
def get_image_labels_from_bytes(image_bytes, limit=DEFAULT_LIMIT):
    """
    :param image_bytes: str image bytes
    :param limit: int Max number of results to return
    :return: list of `google.cloud.vision.entity.EntityAnnotation` instances
    """
    image = Image(get_vision_client(), content=image_bytes)
    return image.detect_labels(limit=limit)
    def test_annotate_no_results(self):
        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)

        mock_response = {
            'batch_annotate_images.return_value': mock.Mock(responses=[]),
        }

        gax_api._annotator_client = mock.Mock(
            spec_set=['batch_annotate_images'], **mock_response)
        with mock.patch('google.cloud.vision._gax.Annotations'):
            images = ((image, [feature]), )
            response = gax_api.annotate(images)
        self.assertEqual(len(response), 0)
        self.assertIsInstance(response, list)

        gax_api._annotator_client.batch_annotate_images.assert_called()
    def test_call_annotate_with_more_than_one_result(self):
        from google.cloud.vision.feature import Feature
        from google.cloud.vision.feature import FeatureTypes
        from google.cloud.vision.image import Image
        from google.cloud.vision.likelihood import Likelihood
        from unit_tests._fixtures import MULTIPLE_RESPONSE

        client = mock.Mock(spec_set=['_connection'])
        feature = Feature(FeatureTypes.LABEL_DETECTION, 5)
        image_content = b'abc 1 2 3'
        image = Image(client, content=image_content)

        http_api = self._make_one(client)
        http_api._connection = mock.Mock(spec_set=['api_request'])
        http_api._connection.api_request.return_value = MULTIPLE_RESPONSE
        images = ((image, [feature]), )
        responses = http_api.annotate(images)

        self.assertEqual(len(responses), 2)
        image_one = responses[0]
        image_two = responses[1]
        self.assertEqual(len(image_one.labels), 3)
        self.assertIsInstance(image_one.safe_searches, tuple)
        self.assertEqual(image_two.safe_searches.adult,
                         Likelihood.VERY_UNLIKELY)
        self.assertEqual(len(image_two.labels), 0)
Exemple #5
0
    def annotate(self, image, features):
        """Annotate an image to discover it's attributes.

        :type image: str
        :param image: A string which can be a URL, a Google Cloud Storage path,
                      or a byte stream of the image.

        :type features:  list of :class:`google.cloud.vision.feature.Feature`
        :param features: The type of detection that the Vision API should
                         use to determine image attributes. Pricing is
                         based on the number of Feature Types.

                         See: https://cloud.google.com/vision/docs/pricing
        :rtype: dict
        :returns: List of annotations.
        """
        img = Image(image, self)
        request = VisionRequest(img, features)

        data = {'requests': [request.as_dict()]}
        response = self.connection.api_request(method='POST',
                                               path='/images:annotate',
                                               data=data)

        return response['responses'][0]
    def test_call_vision_request_with_list_bad_features(self):
        from google.cloud.vision.image import Image

        client = object()
        image = Image(client, content=IMAGE_CONTENT)
        with self.assertRaises(TypeError):
            self._call_fut(image, ['nonsensefeature'])
    def test_annotation(self):
        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)

        mock_response = {
            'batch_annotate_images.return_value':
            mock.Mock(responses=['mock response data']),
        }

        gax_api._annotator_client = mock.Mock(
            spec_set=['batch_annotate_images'], **mock_response)

        with mock.patch('google.cloud.vision._gax.Annotations') as mock_anno:
            images = ((image, [feature]), )
            gax_api.annotate(images)
            mock_anno.from_pb.assert_called_with('mock response data')
        gax_api._annotator_client.batch_annotate_images.assert_called()
    def test_annotate_multiple_results(self):
        from google.cloud.vision_v1.proto 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()
    def test__to_gapic_invalid_image_uri(self):
        from google.cloud.vision.image import Image

        image_uri = 'ftp://1234/34.jpg'
        client = object()
        image = Image(client, source_uri=image_uri)
        with self.assertRaises(ValueError):
            self._call_fut(image)
    def test__to_gapic_image_uri(self):
        from google.cloud.vision.image import Image
        from google.cloud.vision_v1.proto import image_annotator_pb2

        image_uri = 'http://1234/34.jpg'
        client = object()
        image = Image(client, source_uri=image_uri)
        image_pb = self._call_fut(image)
        self.assertIsInstance(image_pb, image_annotator_pb2.Image)
        self.assertEqual(image_pb.source.image_uri, image_uri)
    def test__to_gapic_image_content(self):
        from google.cloud.vision.image import Image
        from google.cloud.vision_v1.proto import image_annotator_pb2

        image_content = b'abc 1 2 3'
        client = object()
        image = Image(client, content=image_content)
        image_pb = self._call_fut(image)
        self.assertIsInstance(image_pb, image_annotator_pb2.Image)
        self.assertEqual(image_pb.content, image_content)
Exemple #12
0
    def test__to_gapic_image_content(self):
        import base64
        from google.cloud.vision.image import Image
        from google.cloud.grpc.vision.v1 import image_annotator_pb2

        image_content = b'abc 1 2 3'
        b64_content = base64.b64encode(image_content)
        client = object()
        image = Image(client, content=image_content)
        image_pb = self._call_fut(image)
        self.assertIsInstance(image_pb, image_annotator_pb2.Image)
        self.assertEqual(image_pb.content, b64_content)
    def image(self, content=None, source_uri=None):
        """Get instance of Image using current client.

        :type content: bytes
        :param content: Byte stream of an image.

        :type source_uri: str
        :param source_uri: Google Cloud Storage URI of image.

        :rtype: :class:`~google.cloud.vision.image.Image`
        :returns: Image instance with the current client attached.
        """
        return Image(client=self, content=content, source_uri=source_uri)
    def test_call_annotate_with_no_results(self):
        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=['_connection'])
        feature = Feature(FeatureTypes.LABEL_DETECTION, 5)
        image_content = b'abc 1 2 3'
        image = Image(client, content=image_content)

        http_api = self._make_one(client)
        http_api._connection = mock.Mock(spec_set=['api_request'])
        http_api._connection.api_request.return_value = {'responses': []}
        self.assertIsNone(http_api.annotate(image, [feature]))
    def test_call_vision_request(self):
        from google.cloud.vision.feature import Feature
        from google.cloud.vision.feature import FeatureTypes
        from google.cloud.vision.image import Image

        client = object()
        image = Image(client, content=IMAGE_CONTENT)
        feature = Feature(feature_type=FeatureTypes.FACE_DETECTION,
                          max_results=3)
        request = self._call_fut(image, feature)
        self.assertEqual(request['image'].get('content'), B64_IMAGE_CONTENT)
        features = request['features']
        self.assertEqual(len(features), 1)
        feature = features[0]
        self.assertEqual(feature['type'], FeatureTypes.FACE_DETECTION)
        self.assertEqual(feature['maxResults'], 3)
    def test_ctor(self):
        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=[])
        image = Image(client, source_uri='gs://images/imageone.jpg')
        face_feature = Feature(FeatureTypes.FACE_DETECTION, 5)
        logo_feature = Feature(FeatureTypes.LOGO_DETECTION, 3)

        batch = self._make_one(client)
        batch.add_image(image, [logo_feature, face_feature])
        self.assertEqual(len(batch.images), 1)
        self.assertEqual(len(batch.images[0]), 2)
        self.assertIsInstance(batch.images[0][0], Image)
        self.assertEqual(len(batch.images[0][1]), 2)
        self.assertIsInstance(batch.images[0][1][0], Feature)
        self.assertIsInstance(batch.images[0][1][1], Feature)
Exemple #17
0
    def test_annotate_multiple_results(self):
        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)

        mock_response = {
            'batch_annotate_images.return_value': mock.Mock(responses=[1, 2]),
        }

        gax_api._annotator_client = mock.Mock(
            spec_set=['batch_annotate_images'], **mock_response)
        with mock.patch('google.cloud.vision._gax.Annotations'):
            with self.assertRaises(NotImplementedError):
                gax_api.annotate(image, [feature])

        gax_api._annotator_client.batch_annotate_images.assert_called()
Exemple #18
0
        features.append(
            Feature(feature_type=FeatureTypes.SAFE_SEARCH_DETECTION,
                    max_results=limit))
        features.append(
            Feature(feature_type=FeatureTypes.LOGO_DETECTION,
                    max_results=limit))
        features.append(
            Feature(feature_type=FeatureTypes.DOCUMENT_TEXT_DETECTION,
                    max_results=limit))
        imagebatch = images[start:start + imagesperbatch]
        for image in imagebatch:
            path = image[1]
            if path is None:
                continue
            if 'http' in path or path.startswith('gs:'):
                image = Image(source_uri=path, client=client)
                vision_client.add_image(image=image, features=features)
            else:
                with io.open(path, 'rb') as image_file:
                    content = image_file.read()

                image = Image(content=content, client=client)
                vision_client.add_image(image=image, features=features)

        count = 0
        results = vision_client.detect()
        for image in imagebatch:
            if image[1] is None:
                imgfeats[image[0]] = None
                continue
            # print 'Extracting for',count+1