google-cloud-vision 0.31.0 → 0.32.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +2 -9
  3. data/LICENSE +1 -1
  4. data/README.md +94 -0
  5. data/lib/google/cloud/vision.rb +200 -535
  6. data/lib/google/cloud/vision/v1.rb +129 -55
  7. data/lib/google/cloud/vision/v1/doc/google/cloud/vision/v1/image_annotator.rb +9 -0
  8. data/lib/google/cloud/vision/v1/doc/google/cloud/vision/v1/product_search.rb +88 -0
  9. data/lib/google/cloud/vision/v1/doc/google/cloud/vision/v1/product_search_service.rb +539 -0
  10. data/lib/google/cloud/vision/v1/doc/google/cloud/vision/v1/text_annotation.rb +3 -3
  11. data/lib/google/cloud/vision/v1/doc/google/cloud/vision/v1/web_detection.rb +2 -1
  12. data/lib/google/cloud/vision/{version.rb → v1/doc/google/protobuf/empty.rb} +13 -6
  13. data/lib/google/cloud/vision/v1/doc/google/protobuf/field_mask.rb +230 -0
  14. data/lib/google/cloud/vision/v1/doc/google/protobuf/timestamp.rb +109 -0
  15. data/lib/google/cloud/vision/v1/geometry_pb.rb +1 -0
  16. data/lib/google/cloud/vision/v1/helpers.rb +972 -0
  17. data/lib/google/cloud/vision/v1/image_annotator_client.rb +4 -4
  18. data/lib/google/cloud/vision/v1/image_annotator_pb.rb +5 -0
  19. data/lib/google/cloud/vision/v1/image_annotator_services_pb.rb +2 -1
  20. data/lib/google/cloud/vision/v1/product_search_client.rb +1337 -0
  21. data/lib/google/cloud/vision/v1/product_search_client_config.json +116 -0
  22. data/lib/google/cloud/vision/v1/product_search_pb.rb +45 -0
  23. data/lib/google/cloud/vision/v1/product_search_service_pb.rb +191 -0
  24. data/lib/google/cloud/vision/v1/product_search_service_services_pb.rb +224 -0
  25. data/lib/google/cloud/vision/v1p3beta1.rb +233 -0
  26. data/lib/google/cloud/vision/v1p3beta1/credentials.rb +42 -0
  27. data/lib/google/cloud/vision/v1p3beta1/doc/google/cloud/vision/v1p3beta1/geometry.rb +72 -0
  28. data/lib/google/cloud/vision/v1p3beta1/doc/google/cloud/vision/v1p3beta1/image_annotator.rb +763 -0
  29. data/lib/google/cloud/vision/v1p3beta1/doc/google/cloud/vision/v1p3beta1/product_search.rb +154 -0
  30. data/lib/google/cloud/vision/v1p3beta1/doc/google/cloud/vision/v1p3beta1/product_search_service.rb +533 -0
  31. data/lib/google/cloud/vision/v1p3beta1/doc/google/cloud/vision/v1p3beta1/text_annotation.rb +254 -0
  32. data/lib/google/cloud/vision/v1p3beta1/doc/google/cloud/vision/v1p3beta1/web_detection.rb +101 -0
  33. data/lib/google/cloud/vision/v1p3beta1/doc/google/longrunning/operations.rb +93 -0
  34. data/lib/google/cloud/vision/v1p3beta1/doc/google/protobuf/any.rb +130 -0
  35. data/lib/google/cloud/vision/v1p3beta1/doc/google/protobuf/empty.rb +29 -0
  36. data/lib/google/cloud/vision/v1p3beta1/doc/google/protobuf/field_mask.rb +230 -0
  37. data/lib/google/cloud/vision/v1p3beta1/doc/google/protobuf/timestamp.rb +109 -0
  38. data/lib/google/cloud/vision/v1p3beta1/doc/google/protobuf/wrappers.rb +90 -0
  39. data/lib/google/cloud/vision/v1p3beta1/doc/google/rpc/status.rb +84 -0
  40. data/lib/google/cloud/vision/v1p3beta1/doc/google/type/color.rb +156 -0
  41. data/lib/google/cloud/vision/v1p3beta1/doc/google/type/latlng.rb +65 -0
  42. data/lib/google/cloud/vision/v1p3beta1/geometry_pb.rb +43 -0
  43. data/lib/google/cloud/vision/v1p3beta1/helpers.rb +972 -0
  44. data/lib/google/cloud/vision/v1p3beta1/image_annotator_client.rb +302 -0
  45. data/lib/google/cloud/vision/v1p3beta1/image_annotator_client_config.json +36 -0
  46. data/lib/google/cloud/vision/v1p3beta1/image_annotator_pb.rb +304 -0
  47. data/lib/google/cloud/vision/v1p3beta1/image_annotator_services_pb.rb +55 -0
  48. data/lib/google/cloud/vision/v1p3beta1/product_search_client.rb +1351 -0
  49. data/lib/google/cloud/vision/v1p3beta1/product_search_client_config.json +116 -0
  50. data/lib/google/cloud/vision/v1p3beta1/product_search_pb.rb +64 -0
  51. data/lib/google/cloud/vision/v1p3beta1/product_search_service_pb.rb +191 -0
  52. data/lib/google/cloud/vision/v1p3beta1/product_search_service_services_pb.rb +222 -0
  53. data/lib/google/cloud/vision/v1p3beta1/text_annotation_pb.rb +94 -0
  54. data/lib/google/cloud/vision/v1p3beta1/web_detection_pb.rb +51 -0
  55. metadata +53 -122
  56. data/AUTHENTICATION.md +0 -179
  57. data/CHANGELOG.md +0 -109
  58. data/CODE_OF_CONDUCT.md +0 -40
  59. data/CONTRIBUTING.md +0 -188
  60. data/LOGGING.md +0 -32
  61. data/OVERVIEW.md +0 -202
  62. data/TROUBLESHOOTING.md +0 -37
  63. data/lib/google-cloud-vision.rb +0 -146
  64. data/lib/google/cloud/vision/annotate.rb +0 -303
  65. data/lib/google/cloud/vision/annotation.rb +0 -553
  66. data/lib/google/cloud/vision/annotation/crop_hint.rb +0 -126
  67. data/lib/google/cloud/vision/annotation/entity.rb +0 -225
  68. data/lib/google/cloud/vision/annotation/face.rb +0 -1497
  69. data/lib/google/cloud/vision/annotation/normalized_vertex.rb +0 -76
  70. data/lib/google/cloud/vision/annotation/object_localization.rb +0 -115
  71. data/lib/google/cloud/vision/annotation/properties.rb +0 -220
  72. data/lib/google/cloud/vision/annotation/safe_search.rb +0 -153
  73. data/lib/google/cloud/vision/annotation/text.rb +0 -1015
  74. data/lib/google/cloud/vision/annotation/vertex.rb +0 -91
  75. data/lib/google/cloud/vision/annotation/web.rb +0 -420
  76. data/lib/google/cloud/vision/credentials.rb +0 -57
  77. data/lib/google/cloud/vision/image.rb +0 -832
  78. data/lib/google/cloud/vision/location.rb +0 -98
  79. data/lib/google/cloud/vision/project.rb +0 -367
  80. data/lib/google/cloud/vision/service.rb +0 -83
@@ -1,57 +0,0 @@
1
- # Copyright 2016 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
-
15
-
16
- require "googleauth"
17
-
18
- module Google
19
- module Cloud
20
- module Vision
21
- ##
22
- # # Credentials
23
- #
24
- # Represents the authentication and authorization used to connect to the
25
- # Vision API.
26
- #
27
- # @example
28
- # require "google/cloud/vision"
29
- #
30
- # keyfile = "/path/to/keyfile.json"
31
- # creds = Google::Cloud::Vision::Credentials.new keyfile
32
- #
33
- # vision = Google::Cloud::Vision.new(
34
- # project_id: "my-project",
35
- # credentials: creds
36
- # )
37
- #
38
- # vision.project_id #=> "my-project"
39
- #
40
- class Credentials < Google::Auth::Credentials
41
- SCOPE = ["https://www.googleapis.com/auth/cloud-platform"].freeze
42
- PATH_ENV_VARS = %w[VISION_CREDENTIALS
43
- VISION_KEYFILE
44
- GOOGLE_CLOUD_CREDENTIALS
45
- GOOGLE_CLOUD_KEYFILE
46
- GCLOUD_KEYFILE].freeze
47
- JSON_ENV_VARS = %w[VISION_CREDENTIALS_JSON
48
- VISION_KEYFILE_JSON
49
- GOOGLE_CLOUD_CREDENTIALS_JSON
50
- GOOGLE_CLOUD_KEYFILE_JSON
51
- GCLOUD_KEYFILE_JSON].freeze
52
- DEFAULT_PATHS = \
53
- ["~/.config/gcloud/application_default_credentials.json"].freeze
54
- end
55
- end
56
- end
57
- end
@@ -1,832 +0,0 @@
1
- # Copyright 2016 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
-
15
-
16
- require "google/cloud/vision/location"
17
- require "stringio"
18
- require "base64"
19
-
20
- module Google
21
- module Cloud
22
- module Vision
23
- ##
24
- # # Image
25
- #
26
- # Represents an image for the Vision service.
27
- #
28
- # An Image instance can be created from a string file path, publicly-
29
- # accessible image HTTP/HTTPS URL, or Cloud Storage URI of the form
30
- # `"gs://bucketname/path/to/image_filename"`; or a File, IO, StringIO, or
31
- # Tempfile instance; or an instance of Google::Cloud::Storage::File.
32
- #
33
- # See {Project#image}.
34
- #
35
- # The Cloud Vision API supports a variety of image file formats, including
36
- # JPEG, PNG8, PNG24, Animated GIF (first frame only), and RAW. See [Best
37
- # Practices - Image
38
- # Types](https://cloud.google.com/vision/docs/best-practices#image_types)
39
- # for the list of formats. Be aware that Cloud Vision sets upper limits on
40
- # file size as well as the total combined size of all images in a request.
41
- # Reducing your file size can significantly improve throughput; however,
42
- # be careful not to reduce image quality in the process. See [Best
43
- # Practices - Image
44
- # Sizing](https://cloud.google.com/vision/docs/best-practices#image_sizing)
45
- # for current file size limits.
46
- #
47
- # @see https://cloud.google.com/vision/docs/best-practices Best
48
- # Practices
49
- #
50
- # @example
51
- # require "google/cloud/vision"
52
- #
53
- # vision = Google::Cloud::Vision.new
54
- #
55
- # image = vision.image "path/to/text.png"
56
- #
57
- # image.context.languages = ["en"]
58
- #
59
- # text = image.text
60
- # text.pages.count #=> 1
61
- #
62
- class Image
63
- # Returns the image context for the image, which accepts metadata values
64
- # such as location and language hints.
65
- # @return [Context] The context instance for the image.
66
- attr_reader :context
67
-
68
- ##
69
- # @private Creates a new Image instance.
70
- def initialize
71
- @io = nil
72
- @url = nil
73
- @vision = nil
74
- @context = Context.new
75
- end
76
-
77
- ##
78
- # @private Whether the Image has content.
79
- #
80
- def io?
81
- !@io.nil?
82
- end
83
-
84
- ##
85
- # @private Whether the Image is a URL.
86
- #
87
- def url?
88
- !@url.nil?
89
- end
90
-
91
- ##
92
- # Performs the `FACE_DETECTION` feature on the image.
93
- #
94
- # @see https://cloud.google.com/vision/docs/pricing Cloud Vision Pricing
95
- #
96
- # @param [Integer] max_results The maximum number of results. The
97
- # default is {Google::Cloud::Vision.default_max_faces}. Optional.
98
- #
99
- # @return [Array<Annotation::Face>] The results of face detection.
100
- #
101
- # @example
102
- # require "google/cloud/vision"
103
- #
104
- # vision = Google::Cloud::Vision.new
105
- # image = vision.image "path/to/face.jpg"
106
- #
107
- # faces = image.faces
108
- #
109
- # face = faces.first
110
- # face.bounds.face.count #=> 4
111
- # vertex = face.bounds.face.first
112
- # vertex.x #=> 28
113
- # vertex.y #=> 40
114
- #
115
- def faces max_results = Vision.default_max_faces
116
- ensure_vision!
117
- annotation = @vision.mark self, faces: max_results
118
- annotation.faces
119
- end
120
-
121
- ##
122
- # Performs the `FACE_DETECTION` feature on the image and returns only
123
- # the first result.
124
- #
125
- # @return [Annotation::Face] The first result of face detection.
126
- #
127
- def face
128
- faces(1).first
129
- end
130
-
131
- ##
132
- # Performs the `LANDMARK_DETECTION` feature on the image.
133
- #
134
- # @see https://cloud.google.com/vision/docs/pricing Cloud Vision Pricing
135
- #
136
- # @param [Integer] max_results The maximum number of results. The
137
- # default is {Google::Cloud::Vision.default_max_landmarks}. Optional.
138
- #
139
- # @return [Array<Annotation::Entity>] The results of landmark detection.
140
- #
141
- # @example
142
- # require "google/cloud/vision"
143
- #
144
- # vision = Google::Cloud::Vision.new
145
- # image = vision.image "path/to/landmark.jpg"
146
- #
147
- # landmarks = image.landmarks
148
- #
149
- # landmark = landmarks.first
150
- # landmark.score #=> 0.9191226363182068
151
- # landmark.description #=> "Mount Rushmore"
152
- # landmark.mid #=> "/m/019dvv"
153
- #
154
- def landmarks max_results = Vision.default_max_landmarks
155
- ensure_vision!
156
- annotation = @vision.mark self, landmarks: max_results
157
- annotation.landmarks
158
- end
159
-
160
- ##
161
- # Performs the `LANDMARK_DETECTION` feature on the image and returns
162
- # only the first result.
163
- #
164
- # @return [Annotation::Entity] The first result of landmark detection.
165
- #
166
- def landmark
167
- landmarks(1).first
168
- end
169
-
170
- ##
171
- # Performs the `LOGO_DETECTION` feature on the image.
172
- #
173
- # @see https://cloud.google.com/vision/docs/pricing Cloud Vision Pricing
174
- #
175
- # @param [Integer] max_results The maximum number of results. The
176
- # default is {Google::Cloud::Vision.default_max_logos}. Optional.
177
- #
178
- # @return [Array<Annotation::Entity>] The results of logo detection.
179
- #
180
- # @example
181
- # require "google/cloud/vision"
182
- #
183
- # vision = Google::Cloud::Vision.new
184
- # image = vision.image "path/to/logo.jpg"
185
- #
186
- # logos = image.logos
187
- #
188
- # logo = logos.first
189
- # logo.score #=> 0.7005731463432312
190
- # logo.description #=> "Google"
191
- # logo.mid #=> "/m/0b34hf"
192
- #
193
- def logos max_results = Vision.default_max_logos
194
- ensure_vision!
195
- annotation = @vision.mark self, logos: max_results
196
- annotation.logos
197
- end
198
-
199
- ##
200
- # Performs the `LOGO_DETECTION` feature on the image and returns only
201
- # the first result.
202
- #
203
- # @return [Annotation::Entity] The first result of logo detection.
204
- #
205
- def logo
206
- logos(1).first
207
- end
208
-
209
- ##
210
- # Performs the `LABEL_DETECTION` feature on the image.
211
- #
212
- # @see https://cloud.google.com/vision/docs/pricing Cloud Vision Pricing
213
- #
214
- # @param [Integer] max_results The maximum number of results. The
215
- # default is {Google::Cloud::Vision.default_max_labels}. Optional.
216
- #
217
- # @return [Array<Annotation::Entity>] The results of label detection.
218
- #
219
- # @example
220
- # require "google/cloud/vision"
221
- #
222
- # vision = Google::Cloud::Vision.new
223
- # image = vision.image "path/to/landmark.jpg"
224
- #
225
- # labels = image.labels
226
- #
227
- # labels.count #=> 4
228
- # label = labels.first
229
- # label.score #=> 0.9481348991394043
230
- # label.description #=> "stone carving"
231
- # label.mid #=> "/m/02wtjj"
232
- #
233
- def labels max_results = Vision.default_max_labels
234
- ensure_vision!
235
- annotation = @vision.mark self, labels: max_results
236
- annotation.labels
237
- end
238
-
239
- ##
240
- # Performs the `LABEL_DETECTION` feature on the image and returns only
241
- # the first result.
242
- #
243
- # @return [Annotation::Entity] The first result of label detection.
244
- #
245
- def label
246
- labels(1).first
247
- end
248
-
249
- ##
250
- # Performs the `TEXT_DETECTION` feature (OCR for shorter documents with
251
- # sparse text) on the image.
252
- #
253
- # @see https://cloud.google.com/vision/docs/pricing Cloud Vision Pricing
254
- #
255
- # @return [Annotation::Text] The results of text (OCR) detection.
256
- #
257
- # @example
258
- # require "google/cloud/vision"
259
- #
260
- # vision = Google::Cloud::Vision.new
261
- # image = vision.image "path/to/text.png"
262
- #
263
- # text = image.text
264
- #
265
- # text.text
266
- # # "Google Cloud Client for Ruby an idiomatic, intuitive... "
267
- #
268
- # text.locale #=> "en"
269
- # text.words.count #=> 28
270
- # text.words[0].text #=> "Google"
271
- # text.words[0].bounds.count #=> 4
272
- # vertex = text.words[0].bounds.first
273
- # vertex.x #=> 13
274
- # vertex.y #=> 8
275
- #
276
- # # Use `pages` to access a full structural representation
277
- # page = text.pages.first
278
- # page.blocks[0].paragraphs[0].words[0].symbols[0].text #=> "G"
279
- #
280
- #
281
- def text
282
- ensure_vision!
283
- annotation = @vision.mark self, text: true
284
- annotation.text
285
- end
286
-
287
- ##
288
- # Performs the `DOCUMENT_TEXT_DETECTION` feature (OCR for longer
289
- # documents with dense text) on the image.
290
- #
291
- # @see https://cloud.google.com/vision/docs/pricing Cloud Vision Pricing
292
- #
293
- # @return [Annotation::Text] The results of document text (OCR)
294
- # detection.
295
- #
296
- # @example
297
- # require "google/cloud/vision"
298
- #
299
- # vision = Google::Cloud::Vision.new
300
- # image = vision.image "path/to/text.png"
301
- #
302
- # text = image.document
303
- #
304
- # text.text
305
- # # "Google Cloud Client for Ruby an idiomatic, intuitive... "
306
- #
307
- # text.words[0].text #=> "Google"
308
- # text.words[0].bounds.count #=> 4
309
- # vertex = text.words[0].bounds.first
310
- # vertex.x #=> 13
311
- # vertex.y #=> 8
312
- #
313
- # # Use `pages` to access a full structural representation
314
- # page = text.pages.first
315
- # page.blocks[0].paragraphs[0].words[0].symbols[0].text #=> "G"
316
- #
317
- def document
318
- ensure_vision!
319
- annotation = @vision.mark self, document: true
320
- annotation.text
321
- end
322
-
323
- ##
324
- # Performs the `SAFE_SEARCH_DETECTION` feature on the image.
325
- #
326
- # @see https://cloud.google.com/vision/docs/pricing Cloud Vision Pricing
327
- #
328
- # @return [Annotation::SafeSearch] The results of safe search detection.
329
- #
330
- # @example
331
- # require "google/cloud/vision"
332
- #
333
- # vision = Google::Cloud::Vision.new
334
- # image = vision.image "path/to/face.jpg"
335
- #
336
- # safe_search = image.safe_search
337
- #
338
- # safe_search.spoof? #=> false
339
- # safe_search.spoof #=> :VERY_UNLIKELY
340
- #
341
- def safe_search
342
- ensure_vision!
343
- annotation = @vision.mark self, safe_search: true
344
- annotation.safe_search
345
- end
346
-
347
- ##
348
- # Performs the `IMAGE_PROPERTIES` feature on the image.
349
- #
350
- # @see https://cloud.google.com/vision/docs/pricing Cloud Vision Pricing
351
- #
352
- # @return [Annotation::Properties] The results of image properties
353
- # detection.
354
- #
355
- # @example
356
- # require "google/cloud/vision"
357
- #
358
- # vision = Google::Cloud::Vision.new
359
- # image = vision.image "path/to/logo.jpg"
360
- #
361
- # properties = image.properties
362
- #
363
- # properties.colors.count #=> 10
364
- # color = properties.colors.first
365
- # color.red #=> 247.0
366
- # color.green #=> 236.0
367
- # color.blue #=> 20.0
368
- #
369
- def properties
370
- ensure_vision!
371
- annotation = @vision.mark self, properties: true
372
- annotation.properties
373
- end
374
-
375
- ##
376
- # Performs the `CROP_HINTS` feature on the image.
377
- #
378
- # @see https://cloud.google.com/vision/docs/pricing Cloud Vision Pricing
379
- #
380
- # @return [Array<Annotation::CropHint>] The results of crop hints
381
- # detection.
382
- #
383
- # @example
384
- # require "google/cloud/vision"
385
- #
386
- # vision = Google::Cloud::Vision.new
387
- # image = vision.image "path/to/face.jpg"
388
- #
389
- # crop_hints = image.crop_hints
390
- # crop_hints.count #=> 1
391
- # crop_hint = crop_hints.first
392
- #
393
- # crop_hint.bounds.count #=> 4
394
- # crop_hint.confidence #=> 1.0
395
- # crop_hint.importance_fraction #=> 1.0399999618530273
396
- #
397
- def crop_hints max_results = Vision.default_max_crop_hints
398
- ensure_vision!
399
- annotation = @vision.mark self, crop_hints: max_results
400
- annotation.crop_hints
401
- end
402
-
403
- ##
404
- # Performs the `WEB_ANNOTATION` feature on the image.
405
- #
406
- # @see https://cloud.google.com/vision/docs/pricing Cloud Vision Pricing
407
- #
408
- # @return [Annotation::Web] The results of web detection.
409
- #
410
- # @example
411
- # require "google/cloud/vision"
412
- #
413
- # vision = Google::Cloud::Vision.new
414
- # image = vision.image "path/to/face.jpg"
415
- #
416
- # web = image.web
417
- #
418
- # entity = web.entities.first
419
- # entity.entity_id #=> "/m/019dvv"
420
- # entity.score #=> 107.34591674804688
421
- # entity.description #=> "Mount Rushmore National Memorial"
422
- #
423
- # full_matching_image = web.full_matching_images.first
424
- # full_matching_image.url #=> "http://example.com/images/123.jpg"
425
- # full_matching_image.score #=> 0.10226666927337646
426
- #
427
- # page_with_matching_images = web.pages_with_matching_images.first
428
- # page_with_matching_images.url #=> "http://example.com/posts/123"
429
- # page_with_matching_images.score #=> 8.114753723144531
430
- #
431
- def web max_results = Vision.default_max_web
432
- ensure_vision!
433
- annotation = @vision.mark self, web: max_results
434
- annotation.web
435
- end
436
-
437
- ##
438
- # Performs the `OBJECT_LOCALIZATION` feature on the image.
439
- #
440
- # @see https://cloud.google.com/vision/docs/pricing Cloud Vision Pricing
441
- #
442
- # @return [Array<Annotation::ObjectLocalization>] The results of object
443
- # localizations detection.
444
- #
445
- # @example
446
- # require "google/cloud/vision"
447
- #
448
- # vision = Google::Cloud::Vision.new
449
- # image = vision.image "path/to/bicycle.jpg"
450
- #
451
- # object_localizations = image.object_localizations
452
- # object_localizations.count #=> 6
453
- # object_localization = object_localizations.first
454
- #
455
- # object_localization.name #=> "Bicycle wheel"
456
- # object_localization.bounds.count #=> 4
457
- #
458
- def object_localizations max_results = \
459
- Vision.default_max_object_localizations
460
- ensure_vision!
461
- annotation = @vision.mark self, object_localizations: max_results
462
- annotation.object_localizations
463
- end
464
-
465
- ##
466
- # Performs detection of Cloud Vision
467
- # [features](https://cloud.google.com/vision/reference/rest/v1/images/annotate#Feature)
468
- # on the image. If no options for features are provided, **all** image
469
- # detection features will be performed, with a default of `100` results
470
- # for faces, landmarks, logos, labels, crop_hints, and web. If any
471
- # feature option is provided, only the specified feature detections will
472
- # be performed. Please review
473
- # [Pricing](https://cloud.google.com/vision/docs/pricing) before use, as
474
- # a separate charge is incurred for each feature performed on an image.
475
- #
476
- # @see https://cloud.google.com/vision/docs/requests-and-responses Cloud
477
- # Vision API Requests and Responses
478
- # @see https://cloud.google.com/vision/reference/rest/v1/images/annotate#AnnotateImageRequest
479
- # AnnotateImageRequest
480
- # @see https://cloud.google.com/vision/docs/pricing Cloud Vision Pricing
481
- #
482
- # @param [Boolean, Integer] faces Whether to perform the facial
483
- # detection feature. The maximum number of results is configured in
484
- # {Google::Cloud::Vision.default_max_faces}, or may be provided here.
485
- # Optional.
486
- # @param [Boolean, Integer] landmarks Whether to perform the landmark
487
- # detection feature. The maximum number of results is configured in
488
- # {Google::Cloud::Vision.default_max_landmarks}, or may be provided
489
- # here. Optional.
490
- # @param [Boolean, Integer] logos Whether to perform the logo detection
491
- # feature. The maximum number of results is configured in
492
- # {Google::Cloud::Vision.default_max_logos}, or may be provided here.
493
- # Optional.
494
- # @param [Boolean, Integer] labels Whether to perform the label
495
- # detection feature. The maximum number of results is configured in
496
- # {Google::Cloud::Vision.default_max_labels}, or may be provided here.
497
- # Optional.
498
- # @param [Boolean] text Whether to perform the text detection feature
499
- # (OCR for shorter documents with sparse text). Optional.
500
- # @param [Boolean] document Whether to perform the document text
501
- # detection feature (OCR for longer documents with dense text).
502
- # Optional.
503
- # @param [Boolean] safe_search Whether to perform the safe search
504
- # feature. Optional.
505
- # @param [Boolean] properties Whether to perform the image properties
506
- # feature (currently, the image's dominant colors.) Optional.
507
- # @param [Boolean, Integer] crop_hints Whether to perform the crop hints
508
- # feature. Optional.
509
- # @param [Boolean, Integer] web Whether to perform the web annotation
510
- # feature. Optional.
511
- # @param [Boolean, Integer] object_localizations Whether to perform the
512
- # object localizations feature. Optional.
513
- #
514
- # @return [Annotation] The results for all image detections, returned as
515
- # a single {Annotation} instance.
516
- #
517
- # @example
518
- # require "google/cloud/vision"
519
- #
520
- # vision = Google::Cloud::Vision.new
521
- #
522
- # image = vision.image "path/to/landmark.jpg"
523
- #
524
- # annotation = image.annotate labels: true, landmarks: true
525
- #
526
- # annotation.labels.map &:description
527
- # # ["stone carving", "ancient history", "statue", "sculpture",
528
- # # "monument", "landmark"]
529
- # annotation.landmarks.count #=> 1
530
- #
531
- # @example Maximum result values can also be provided:
532
- # require "google/cloud/vision"
533
- #
534
- # vision = Google::Cloud::Vision.new
535
- #
536
- # image = vision.image "path/to/landmark.jpg"
537
- #
538
- # annotation = image.annotate labels: 3, landmarks: 3
539
- #
540
- # annotation.labels.map &:description
541
- # # ["stone carving", "ancient history", "statue"]
542
- # annotation.landmarks.count #=> 1
543
- #
544
- def annotate faces: false, landmarks: false, logos: false,
545
- labels: false, text: false, document: false,
546
- safe_search: false, properties: false, crop_hints: false,
547
- web: false, object_localizations: false
548
- @vision.annotate(self, faces: faces, landmarks: landmarks,
549
- logos: logos, labels: labels, text: text,
550
- document: document, safe_search: safe_search,
551
- properties: properties, crop_hints: crop_hints,
552
- web: web,
553
- object_localizations: object_localizations)
554
- end
555
- alias mark annotate
556
- alias detect annotate
557
-
558
- # @private
559
- def to_s
560
- @to_s ||= begin
561
- if io?
562
- @io.rewind
563
- "(#{@io.read(16)}...)"
564
- else
565
- "(#{@url})"
566
- end
567
- end
568
- end
569
-
570
- # @private
571
- def inspect
572
- "#<#{self.class.name} #{self}>"
573
- end
574
-
575
- ##
576
- # @private The GRPC object for the Image.
577
- def to_grpc
578
- if io?
579
- @io.rewind
580
- Google::Cloud::Vision::V1::Image.new content: @io.read
581
- elsif url?
582
- Google::Cloud::Vision::V1::Image.new(
583
- source: Google::Cloud::Vision::V1::ImageSource.new(
584
- image_uri: @url
585
- )
586
- )
587
- else
588
- raise ArgumentError, "Unable to use Image with Vision service."
589
- end
590
- end
591
-
592
- ##
593
- # @private New Image from a source object.
594
- def self.from_source source, vision = nil
595
- if source.respond_to?(:read) && source.respond_to?(:rewind)
596
- return from_io(source, vision)
597
- end
598
- # Convert Storage::File objects to the URL
599
- source = source.to_gs_url if source.respond_to? :to_gs_url
600
- # Everything should be a string from now on
601
- source = String source
602
- # Create an Image from a HTTP/HTTPS URL or Google Storage URL.
603
- return from_url(source, vision) if url? source
604
- # Create an image from a file on the filesystem
605
- if File.file? source
606
- unless File.readable? source
607
- raise ArgumentError, "Cannot read #{source}"
608
- end
609
- return from_io(File.open(source, "rb"), vision)
610
- end
611
- raise ArgumentError, "Unable to convert #{source} to an Image"
612
- end
613
-
614
- ##
615
- # @private New Image from an IO object.
616
- def self.from_io io, vision
617
- if !io.respond_to?(:read) && !io.respond_to?(:rewind)
618
- raise ArgumentError, "Cannot create an Image without an IO object"
619
- end
620
- new.tap do |i|
621
- i.instance_variable_set :@io, io
622
- i.instance_variable_set :@vision, vision
623
- end
624
- end
625
-
626
- ##
627
- # @private New Image from a HTTP/HTTPS URL or Google Storage URL.
628
- def self.from_url url, vision
629
- url = String url
630
- unless url? url
631
- raise ArgumentError, "Cannot create an Image without a URL"
632
- end
633
- new.tap do |i|
634
- i.instance_variable_set :@url, url
635
- i.instance_variable_set :@vision, vision
636
- end
637
- end
638
-
639
- ##
640
- # @private
641
- def self.url? url
642
- regex = %r{\A(http|https|gs):\/\/}
643
- !regex.match(url).nil?
644
- end
645
-
646
- protected
647
-
648
- ##
649
- # Raise an error unless an active vision project object is available.
650
- def ensure_vision!
651
- raise "Must have active connection" unless @vision
652
- end
653
- end
654
-
655
- class Image
656
- ##
657
- # # Image::Context
658
- #
659
- # Represents an image context.
660
- #
661
- # @attr [Array<String>] languages A list of [ISO 639-1 language
662
- # codes](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)
663
- # to use for text (OCR) detection. In most cases, an empty value
664
- # will yield the best results as it will allow text detection to
665
- # automatically detect the text language. For languages based on the
666
- # latin alphabet a hint is not needed. In rare cases, when the
667
- # language of the text in the image is known in advance, setting
668
- # this hint will help get better results (although it will hurt a
669
- # great deal if the hint is wrong). For use with {Image#text}.
670
- # @attr [Array<Float>] aspect_ratios Aspect ratios in floats,
671
- # representing the ratio of the width to the height of the image. For
672
- # example, if the desired aspect ratio is 4/3, the corresponding float
673
- # value should be 1.33333. If not specified, the best possible crop
674
- # is returned. The number of provided aspect ratios is limited to a
675
- # maximum of 16; any aspect ratios provided after the 16th are
676
- # ignored. For use with {Image#crop_hints}.
677
- #
678
- # @example
679
- # require "google/cloud/vision"
680
- #
681
- # vision = Google::Cloud::Vision.new
682
- #
683
- # image = vision.image "path/to/landmark.jpg"
684
- # image.context.area.min = { longitude: -122.0862462,
685
- # latitude: 37.4220041 }
686
- # image.context.area.max = { longitude: -122.0762462,
687
- # latitude: 37.4320041 }
688
- #
689
- class Context
690
- ##
691
- # Returns a lat/long rectangle that specifies the location of the
692
- # image.
693
- # @return [Area] The lat/long pairs for `latLongRect`.
694
- attr_reader :area
695
-
696
- attr_accessor :languages, :aspect_ratios
697
-
698
- ##
699
- # @private Creates a new Context instance.
700
- def initialize
701
- @area = Area.new
702
- @languages = []
703
- @aspect_ratios = []
704
- end
705
-
706
- ##
707
- # Returns `true` if either `min` or `max` are not populated.
708
- #
709
- # @return [Boolean]
710
- #
711
- def empty?
712
- area.empty? && languages.empty? && aspect_ratios.empty?
713
- end
714
-
715
- ##
716
- # @private
717
- def to_grpc
718
- return nil if empty?
719
-
720
- args = {}
721
- args[:lat_long_rect] = area.to_grpc unless area.empty?
722
- args[:language_hints] = languages unless languages.empty?
723
- unless aspect_ratios.empty?
724
- crop_params = Google::Cloud::Vision::V1::CropHintsParams.new(
725
- aspect_ratios: aspect_ratios
726
- )
727
- args[:crop_hints_params] = crop_params
728
- end
729
- Google::Cloud::Vision::V1::ImageContext.new args
730
- end
731
-
732
- ##
733
- # # Image::Context::Area
734
- #
735
- # A Lat/long rectangle that specifies the location of the image.
736
- #
737
- # @example
738
- # require "google/cloud/vision"
739
- #
740
- # vision = Google::Cloud::Vision.new
741
- #
742
- # image = vision.image "path/to/landmark.jpg"
743
- #
744
- # image.context.area.min = { longitude: -122.0862462,
745
- # latitude: 37.4220041 }
746
- # image.context.area.max = { longitude: -122.0762462,
747
- # latitude: 37.4320041 }
748
- #
749
- # entity = image.landmark
750
- #
751
- class Area
752
- # Returns the min lat/long pair.
753
- # @return [Location]
754
- attr_reader :min
755
-
756
- # Returns the max lat/long pair.
757
- # @return [Location]
758
- attr_reader :max
759
-
760
- ##
761
- # @private Creates a new Area instance.
762
- def initialize
763
- @min = Location.new nil, nil
764
- @max = Location.new nil, nil
765
- end
766
-
767
- ##
768
- # Sets the min lat/long pair for the area.
769
- #
770
- # @param [Hash(Symbol => Float)] location A Hash containing the keys
771
- # `:latitude` and `:longitude` with corresponding values
772
- # conforming to the [WGS84
773
- # standard](http://www.unoosa.org/pdf/icg/2012/template/WGS_84.pdf).
774
- def min= location
775
- if location.respond_to?(:to_h) &&
776
- location.to_h.keys.sort == %i[latitude longitude]
777
- @min = Location.new(location.to_h[:latitude],
778
- location.to_h[:longitude])
779
- return
780
- end
781
- raise ArgumentError, "Must pass a proper location value."
782
- end
783
-
784
- ##
785
- # Sets the max lat/long pair for the area.
786
- #
787
- # @param [Hash(Symbol => Float)] location A Hash containing the keys
788
- # `:latitude` and `:longitude` with corresponding values
789
- # conforming to the [WGS84
790
- # standard](http://www.unoosa.org/pdf/icg/2012/template/WGS_84.pdf).
791
- def max= location
792
- if location.respond_to?(:to_h) &&
793
- location.to_h.keys.sort == %i[latitude longitude]
794
- @max = Location.new(location.to_h[:latitude],
795
- location.to_h[:longitude])
796
- return
797
- end
798
- raise ArgumentError, "Must pass a proper location value."
799
- end
800
-
801
- ##
802
- # Returns `true` if either `min` or `max` are not populated.
803
- #
804
- # @return [Boolean]
805
- #
806
- def empty?
807
- min.to_h.values.reject(&:nil?).empty? ||
808
- max.to_h.values.reject(&:nil?).empty?
809
- end
810
-
811
- ##
812
- # Deeply converts object to a hash. All keys will be symbolized.
813
- #
814
- # @return [Hash]
815
- #
816
- def to_h
817
- { min_lat_lng: min.to_h, max_lat_lng: max.to_h }
818
- end
819
-
820
- def to_grpc
821
- return nil if empty?
822
- Google::Cloud::Vision::V1::LatLongRect.new(
823
- min_lat_lng: min.to_grpc,
824
- max_lat_lng: max.to_grpc
825
- )
826
- end
827
- end
828
- end
829
- end
830
- end
831
- end
832
- end