def test_call_annotate_with_pb_requests_results(self): from google.cloud.vision_v1.proto 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)
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
def test_annotate_with_pb_requests_results(self): from google.cloud.vision_v1.proto 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) for annotation in responses: self.assertIsInstance(annotation, Annotations) gax_api._annotator_client.batch_annotate_images.assert_called()
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
def annotate(self, images=None, requests_pb=None): """Annotate images through GAX. :type images: list :param images: List containing pairs of :class:`~google.cloud.vision.image.Image` and :class:`~google.cloud.vision.feature.Feature`. e.g. [(image, [feature_one, feature_two]),] :type requests_pb: list :param requests_pb: List of :class:`google.cloud.vision_v1.proto.\ image_annotator_pb2.AnnotateImageRequest` :rtype: list :returns: List of :class:`~google.cloud.vision.annotations.Annotations`. """ if any([images, requests_pb]) is False: return [] if requests_pb is None: requests = [] for image, features in images: gapic_features = [ _to_gapic_feature(feature) for feature in features ] gapic_image = _to_gapic_image(image) request = image_annotator_pb2.AnnotateImageRequest( image=gapic_image, features=gapic_features) requests.append(request) else: requests = requests_pb annotator_client = self._annotator_client responses = annotator_client.batch_annotate_images(requests).responses return [Annotations.from_pb(response) for response in responses]
def makeReqListImage(imageStr): im_obj = pvv.Image(content = imageStr) return pvv.AnnotateImageRequest(image = im_obj, features = [{"type": "TEXT_DETECTION"}])