25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

137 lines
4.7 KiB

  1. // Copyright 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // AUTO-GENERATED CODE. DO NOT EDIT.
  15. package vision
  16. import (
  17. "time"
  18. "cloud.google.com/go/internal/version"
  19. gax "github.com/googleapis/gax-go"
  20. "golang.org/x/net/context"
  21. "google.golang.org/api/option"
  22. "google.golang.org/api/transport"
  23. visionpb "google.golang.org/genproto/googleapis/cloud/vision/v1p1beta1"
  24. "google.golang.org/grpc"
  25. "google.golang.org/grpc/codes"
  26. "google.golang.org/grpc/metadata"
  27. )
  28. // ImageAnnotatorCallOptions contains the retry settings for each method of ImageAnnotatorClient.
  29. type ImageAnnotatorCallOptions struct {
  30. BatchAnnotateImages []gax.CallOption
  31. }
  32. func defaultImageAnnotatorClientOptions() []option.ClientOption {
  33. return []option.ClientOption{
  34. option.WithEndpoint("vision.googleapis.com:443"),
  35. option.WithScopes(DefaultAuthScopes()...),
  36. }
  37. }
  38. func defaultImageAnnotatorCallOptions() *ImageAnnotatorCallOptions {
  39. retry := map[[2]string][]gax.CallOption{
  40. {"default", "idempotent"}: {
  41. gax.WithRetry(func() gax.Retryer {
  42. return gax.OnCodes([]codes.Code{
  43. codes.DeadlineExceeded,
  44. codes.Unavailable,
  45. }, gax.Backoff{
  46. Initial: 100 * time.Millisecond,
  47. Max: 60000 * time.Millisecond,
  48. Multiplier: 1.3,
  49. })
  50. }),
  51. },
  52. }
  53. return &ImageAnnotatorCallOptions{
  54. BatchAnnotateImages: retry[[2]string{"default", "idempotent"}],
  55. }
  56. }
  57. // ImageAnnotatorClient is a client for interacting with Google Cloud Vision API.
  58. //
  59. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  60. type ImageAnnotatorClient struct {
  61. // The connection to the service.
  62. conn *grpc.ClientConn
  63. // The gRPC API client.
  64. imageAnnotatorClient visionpb.ImageAnnotatorClient
  65. // The call options for this service.
  66. CallOptions *ImageAnnotatorCallOptions
  67. // The x-goog-* metadata to be sent with each request.
  68. xGoogMetadata metadata.MD
  69. }
  70. // NewImageAnnotatorClient creates a new image annotator client.
  71. //
  72. // Service that performs Google Cloud Vision API detection tasks over client
  73. // images, such as face, landmark, logo, label, and text detection. The
  74. // ImageAnnotator service returns detected entities from the images.
  75. func NewImageAnnotatorClient(ctx context.Context, opts ...option.ClientOption) (*ImageAnnotatorClient, error) {
  76. conn, err := transport.DialGRPC(ctx, append(defaultImageAnnotatorClientOptions(), opts...)...)
  77. if err != nil {
  78. return nil, err
  79. }
  80. c := &ImageAnnotatorClient{
  81. conn: conn,
  82. CallOptions: defaultImageAnnotatorCallOptions(),
  83. imageAnnotatorClient: visionpb.NewImageAnnotatorClient(conn),
  84. }
  85. c.setGoogleClientInfo()
  86. return c, nil
  87. }
  88. // Connection returns the client's connection to the API service.
  89. func (c *ImageAnnotatorClient) Connection() *grpc.ClientConn {
  90. return c.conn
  91. }
  92. // Close closes the connection to the API service. The user should invoke this when
  93. // the client is no longer required.
  94. func (c *ImageAnnotatorClient) Close() error {
  95. return c.conn.Close()
  96. }
  97. // setGoogleClientInfo sets the name and version of the application in
  98. // the `x-goog-api-client` header passed on each request. Intended for
  99. // use by Google-written clients.
  100. func (c *ImageAnnotatorClient) setGoogleClientInfo(keyval ...string) {
  101. kv := append([]string{"gl-go", version.Go()}, keyval...)
  102. kv = append(kv, "gapic", version.Repo, "gax", gax.Version, "grpc", grpc.Version)
  103. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  104. }
  105. // BatchAnnotateImages run image detection and annotation for a batch of images.
  106. func (c *ImageAnnotatorClient) BatchAnnotateImages(ctx context.Context, req *visionpb.BatchAnnotateImagesRequest, opts ...gax.CallOption) (*visionpb.BatchAnnotateImagesResponse, error) {
  107. ctx = insertMetadata(ctx, c.xGoogMetadata)
  108. opts = append(c.CallOptions.BatchAnnotateImages[0:len(c.CallOptions.BatchAnnotateImages):len(c.CallOptions.BatchAnnotateImages)], opts...)
  109. var resp *visionpb.BatchAnnotateImagesResponse
  110. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  111. var err error
  112. resp, err = c.imageAnnotatorClient.BatchAnnotateImages(ctx, req, settings.GRPC...)
  113. return err
  114. }, opts...)
  115. if err != nil {
  116. return nil, err
  117. }
  118. return resp, nil
  119. }