예제 #1
0
    def batch_annotate_images(
        self,
        requests,
        retry=google.api_core.gapic_v1.method.DEFAULT,
        timeout=google.api_core.gapic_v1.method.DEFAULT,
        metadata=None,
    ):
        """
        Run image detection and annotation for a batch of images.

        Example:
            >>> from google.cloud import vision_v1
            >>>
            >>> client = vision_v1.ImageAnnotatorClient()
            >>>
            >>> # TODO: Initialize `requests`:
            >>> requests = []
            >>>
            >>> response = client.batch_annotate_images(requests)

        Args:
            requests (list[Union[dict, ~google.cloud.vision_v1.types.AnnotateImageRequest]]): Individual image annotation requests for this batch.

                If a dict is provided, it must be of the same form as the protobuf
                message :class:`~google.cloud.vision_v1.types.AnnotateImageRequest`
            retry (Optional[google.api_core.retry.Retry]):  A retry object used
                to retry requests. If ``None`` is specified, requests will not
                be retried.
            timeout (Optional[float]): The amount of time, in seconds, to wait
                for the request to complete. Note that if ``retry`` is
                specified, the timeout applies to each individual attempt.
            metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata
                that is provided to the method.

        Returns:
            A :class:`~google.cloud.vision_v1.types.BatchAnnotateImagesResponse` instance.

        Raises:
            google.api_core.exceptions.GoogleAPICallError: If the request
                    failed for any reason.
            google.api_core.exceptions.RetryError: If the request failed due
                    to a retryable error and retry attempts failed.
            ValueError: If the parameters are invalid.
        """
        # Wrap the transport method to add retry and timeout logic.
        if "batch_annotate_images" not in self._inner_api_calls:
            self._inner_api_calls[
                "batch_annotate_images"] = google.api_core.gapic_v1.method.wrap_method(
                    self.transport.batch_annotate_images,
                    default_retry=self._method_configs["BatchAnnotateImages"].
                    retry,
                    default_timeout=self.
                    _method_configs["BatchAnnotateImages"].timeout,
                    client_info=self._client_info,
                )

        request = image_annotator_pb2.BatchAnnotateImagesRequest(
            requests=requests)
        return self._inner_api_calls["batch_annotate_images"](
            request, retry=retry, timeout=timeout, metadata=metadata)
    def test_batch_annotate_images(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = image_annotator_pb2.BatchAnnotateImagesResponse(
            **expected_response
        )

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = vision_v1.ImageAnnotatorClient()

        # Setup Request
        requests = []

        response = client.batch_annotate_images(requests)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = image_annotator_pb2.BatchAnnotateImagesRequest(
            requests=requests
        )
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def batch_annotate_images(self, requests, options=None):
        """
        Run image detection and annotation for a batch of images.

        Example:
            >>> from google.cloud import vision_v1
            >>>
            >>> client = vision_v1.ImageAnnotatorClient()
            >>>
            >>> requests = []
            >>>
            >>> response = client.batch_annotate_images(requests)

        Args:
            requests (list[Union[dict, ~google.cloud.vision_v1.types.AnnotateImageRequest]]): Individual image annotation requests for this batch.
                If a dict is provided, it must be of the same form as the protobuf
                message :class:`~google.cloud.vision_v1.types.AnnotateImageRequest`
            options (~google.gax.CallOptions): Overrides the default
                settings for this call, e.g, timeout, retries etc.

        Returns:
            A :class:`~google.cloud.vision_v1.types.BatchAnnotateImagesResponse` instance.

        Raises:
            :exc:`google.gax.errors.GaxError` if the RPC is aborted.
            :exc:`ValueError` if the parameters are invalid.
        """
        request = image_annotator_pb2.BatchAnnotateImagesRequest(
            requests=requests)
        return self._batch_annotate_images(request, options)
    def test_batch_annotate_images(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = vision_v1.ImageAnnotatorClient()

        # Mock request
        requests = []

        # Mock response
        expected_response = {}
        expected_response = image_annotator_pb2.BatchAnnotateImagesResponse(
            **expected_response)
        grpc_stub.BatchAnnotateImages.return_value = expected_response

        response = client.batch_annotate_images(requests)
        self.assertEqual(expected_response, response)

        grpc_stub.BatchAnnotateImages.assert_called_once()
        args, kwargs = grpc_stub.BatchAnnotateImages.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = image_annotator_pb2.BatchAnnotateImagesRequest(
            requests=requests)
        self.assertEqual(expected_request, actual_request)
예제 #5
0
    def batch_annotate_images(self,
                              requests,
                              retry=google.api_core.gapic_v1.method.DEFAULT,
                              timeout=google.api_core.gapic_v1.method.DEFAULT):
        """
        Run image detection and annotation for a batch of images.

        Example:
            >>> from google.cloud import vision_v1
            >>>
            >>> client = vision_v1.ImageAnnotatorClient()
            >>>
            >>> requests = []
            >>>
            >>> response = client.batch_annotate_images(requests)

        Args:
            requests (list[Union[dict, ~google.cloud.vision_v1.types.AnnotateImageRequest]]): Individual image annotation requests for this batch.
                If a dict is provided, it must be of the same form as the protobuf
                message :class:`~google.cloud.vision_v1.types.AnnotateImageRequest`
            retry (Optional[google.api_core.retry.Retry]):  A retry object used
                to retry requests. If ``None`` is specified, requests will not
                be retried.
            timeout (Optional[float]): The amount of time, in seconds, to wait
                for the request to complete. Note that if ``retry`` is
                specified, the timeout applies to each individual attempt.

        Returns:
            A :class:`~google.cloud.vision_v1.types.BatchAnnotateImagesResponse` instance.

        Raises:
            google.api_core.exceptions.GoogleAPICallError: If the request
                    failed for any reason.
            google.api_core.exceptions.RetryError: If the request failed due
                    to a retryable error and retry attempts failed.
            ValueError: If the parameters are invalid.
        """
        request = image_annotator_pb2.BatchAnnotateImagesRequest(
            requests=requests)
        return self._batch_annotate_images(request,
                                           retry=retry,
                                           timeout=timeout)
    def test_batch_annotate_images(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = image_annotator_pb2.BatchAnnotateImagesResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = vision_v1.ImageAnnotatorClient(channel=channel)

        # Setup Request
        requests = []

        response = client.batch_annotate_images(requests)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = image_annotator_pb2.BatchAnnotateImagesRequest(
            requests=requests)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
예제 #7
0
    def batch_annotate_images(
        self,
        requests,
        parent=None,
        retry=google.api_core.gapic_v1.method.DEFAULT,
        timeout=google.api_core.gapic_v1.method.DEFAULT,
        metadata=None,
    ):
        """
        Run image detection and annotation for a batch of images.

        Example:
            >>> from google.cloud import vision_v1
            >>>
            >>> client = vision_v1.ImageAnnotatorClient()
            >>>
            >>> # TODO: Initialize `requests`:
            >>> requests = []
            >>>
            >>> response = client.batch_annotate_images(requests)

        Args:
            requests (list[Union[dict, ~google.cloud.vision_v1.types.AnnotateImageRequest]]): Required. Individual image annotation requests for this batch.

                If a dict is provided, it must be of the same form as the protobuf
                message :class:`~google.cloud.vision_v1.types.AnnotateImageRequest`
            parent (str): Optional. Target project and location to make a call.

                Format: ``projects/{project-id}/locations/{location-id}``.

                If no parent is specified, a region will be chosen automatically.

                Supported location-ids: ``us``: USA country only, ``asia``: East asia
                areas, like Japan, Taiwan, ``eu``: The European Union.

                Example: ``projects/project-A/locations/eu``.
            retry (Optional[google.api_core.retry.Retry]):  A retry object used
                to retry requests. If ``None`` is specified, requests will
                be retried using a default configuration.
            timeout (Optional[float]): The amount of time, in seconds, to wait
                for the request to complete. Note that if ``retry`` is
                specified, the timeout applies to each individual attempt.
            metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata
                that is provided to the method.

        Returns:
            A :class:`~google.cloud.vision_v1.types.BatchAnnotateImagesResponse` instance.

        Raises:
            google.api_core.exceptions.GoogleAPICallError: If the request
                    failed for any reason.
            google.api_core.exceptions.RetryError: If the request failed due
                    to a retryable error and retry attempts failed.
            ValueError: If the parameters are invalid.
        """
        # Wrap the transport method to add retry and timeout logic.
        if "batch_annotate_images" not in self._inner_api_calls:
            self._inner_api_calls[
                "batch_annotate_images"] = google.api_core.gapic_v1.method.wrap_method(
                    self.transport.batch_annotate_images,
                    default_retry=self._method_configs["BatchAnnotateImages"].
                    retry,
                    default_timeout=self.
                    _method_configs["BatchAnnotateImages"].timeout,
                    client_info=self._client_info,
                )

        request = image_annotator_pb2.BatchAnnotateImagesRequest(
            requests=requests, parent=parent)
        if metadata is None:
            metadata = []
        metadata = list(metadata)
        try:
            routing_header = [("parent", parent)]
        except AttributeError:
            pass
        else:
            routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
                routing_header)
            metadata.append(routing_metadata)

        return self._inner_api_calls["batch_annotate_images"](
            request, retry=retry, timeout=timeout, metadata=metadata)