google-cloud-vision 0.31.0 → 0.32.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,1015 +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/annotation/vertex"
17
-
18
- module Google
19
- module Cloud
20
- module Vision
21
- class Annotation
22
- ##
23
- # # Text
24
- #
25
- # The results from either the `TEXT_DETECTION` feature (OCR for shorter
26
- # documents with sparse text) or the `DOCUMENT_TEXT_DETECTION` feature
27
- # (OCR for longer documents with dense text). Optional. Contains
28
- # structured representations of OCR extracted text, as well as
29
- # the entire UTF-8 text as a string.
30
- #
31
- # @example
32
- # require "google/cloud/vision"
33
- #
34
- # vision = Google::Cloud::Vision.new
35
- #
36
- # image = vision.image "path/to/text.png"
37
- #
38
- # text = image.text
39
- #
40
- # text.text
41
- # # "Google Cloud Client for Ruby an idiomatic, intuitive... "
42
- #
43
- # text.words[0].text #=> "Google"
44
- # text.words[0].bounds.count #=> 4
45
- # vertex = text.words[0].bounds.first
46
- # vertex.x #=> 13
47
- # vertex.y #=> 8
48
- #
49
- # # Use `pages` to access a full structural representation
50
- # text.pages[0].blocks[0].paragraphs[0].words[0].symbols[0].text
51
- # #=> "G"
52
- #
53
- class Text
54
- ##
55
- # @private The EntityAnnotation GRPC object.
56
- attr_accessor :grpc
57
-
58
- ##
59
- # @private Creates a new Text instance.
60
- def initialize
61
- @grpc = nil
62
- @words = []
63
- end
64
-
65
- ##
66
- # The text detected in an image.
67
- #
68
- # @return [String] The entire text including newline characters.
69
- #
70
- def text
71
- @grpc.description
72
- end
73
-
74
- ##
75
- # The language code detected for `text`.
76
- #
77
- # @return [String] The [ISO
78
- # 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)
79
- # language code.
80
- #
81
- def locale
82
- @grpc.locale
83
- end
84
-
85
- ##
86
- # The bounds for the detected text in the image.
87
- #
88
- # @return [Array<Vertex>]
89
- #
90
- def bounds
91
- return [] unless @grpc.bounding_poly
92
- @bounds ||= Array(@grpc.bounding_poly.vertices).map do |v|
93
- Vertex.from_grpc v
94
- end
95
- end
96
-
97
- ##
98
- # Each word in the detected text, with the bounds for each word.
99
- #
100
- # @return [Array<Word>]
101
- #
102
- def words
103
- @words
104
- end
105
-
106
- ##
107
- # Each page in the detected text, with the metadata for each page.
108
- # Contains a structured representation of OCR extracted text.
109
- # The hierarchy of an OCR extracted text structure is like this:
110
- # Page -> Block -> Paragraph -> Word -> Symbol
111
- # Each structural component, starting from Page, may further have its
112
- # own properties. Properties describe detected languages, breaks etc..
113
- #
114
- # @return [Array<Page>]
115
- #
116
- def pages
117
- @pages
118
- end
119
-
120
- ##
121
- # Deeply converts object to a hash. All keys will be symbolized.
122
- #
123
- # @return [Hash]
124
- #
125
- def to_h
126
- { text: text, locale: locale, bounds: bounds.map(&:to_h),
127
- words: words.map(&:to_h), pages: pages.map(&:to_h) }
128
- end
129
-
130
- # @private
131
- def to_s
132
- to_str
133
- end
134
-
135
- # @private
136
- def to_str
137
- text
138
- end
139
-
140
- # @private
141
- def inspect
142
- format "#<Text text: %s, locale: %s, bounds: %i, words: %i," \
143
- " pages: %i>", text.inspect, locale.inspect, bounds.count,
144
- words.count, pages.count
145
- end
146
-
147
- ##
148
- # @private Create a new Annotation::Text by merging two GRPC models of
149
- # text representation.
150
- def self.from_grpc grpc_text_annotations, grpc_full_text_annotation
151
- text, *words = Array grpc_text_annotations
152
- return nil if text.nil?
153
-
154
- words = words.map { |w| Word.from_grpc w }
155
-
156
- # Since text is taken from grpc_text_annotations, do not use text
157
- # from grpc_full_text_annotation in this merged model.
158
- # Instead, just take the pages.
159
- pages = grpc_full_text_annotation.pages.map { |p| Page.from_grpc p }
160
- new.tap do |t|
161
- t.instance_variable_set :@grpc, text
162
- t.instance_variable_set :@words, words
163
- t.instance_variable_set :@pages, pages
164
- end
165
- end
166
-
167
- ##
168
- # # Word
169
- #
170
- # A word within a detected text (OCR). See {Text#words}.
171
- #
172
- # @example
173
- # require "google/cloud/vision"
174
- #
175
- # vision = Google::Cloud::Vision.new
176
- #
177
- # image = vision.image "path/to/text.png"
178
- # text = image.text
179
- #
180
- # words = text.words
181
- # words.count #=> 28
182
- #
183
- # word = words.first
184
- # word.text #=> "Google"
185
- # word.bounds.count #=> 4
186
- # vertex = word.bounds.first
187
- # vertex.x #=> 13
188
- # vertex.y #=> 8
189
- #
190
- class Word
191
- ##
192
- # @private The EntityAnnotation GRPC object.
193
- attr_accessor :grpc
194
-
195
- ##
196
- # @private Creates a new Word instance.
197
- def initialize
198
- @grpc = nil
199
- end
200
-
201
- ##
202
- # The text of the word.
203
- #
204
- # @return [String]
205
- #
206
- def text
207
- @grpc.description
208
- end
209
-
210
- ##
211
- # The bounds of the word within the detected text.
212
- #
213
- # @return [Array<Vertex>]
214
- #
215
- def bounds
216
- return [] unless @grpc.bounding_poly
217
- @bounds ||= Array(@grpc.bounding_poly.vertices).map do |v|
218
- Vertex.from_grpc v
219
- end
220
- end
221
-
222
- ##
223
- # Deeply converts object to a hash. All keys will be symbolized.
224
- #
225
- # @return [Hash]
226
- #
227
- def to_h
228
- { text: text, bounds: bounds.map(&:to_h) }
229
- end
230
-
231
- # @private
232
- def to_s
233
- to_str
234
- end
235
-
236
- # @private
237
- def to_str
238
- text
239
- end
240
-
241
- # @private
242
- def inspect
243
- format "#<Word text: %s, bounds: %i>", text.inspect, bounds.count
244
- end
245
-
246
- ##
247
- # @private New Annotation::Text::Word from a GRPC
248
- # object.
249
- def self.from_grpc grpc
250
- new.tap { |w| w.instance_variable_set :@grpc, grpc }
251
- end
252
- end
253
-
254
- ##
255
- # # Page
256
- #
257
- # A page within a detected text (OCR). See {Text#pages}.
258
- #
259
- # @example
260
- # require "google/cloud/vision"
261
- #
262
- # vision = Google::Cloud::Vision.new
263
- #
264
- # image = vision.image "path/to/text.png"
265
- #
266
- # text = image.text
267
- #
268
- # page = text.pages.first
269
- #
270
- # page.languages.first.code #=> "en"
271
- # page.wont_be :prefix_break?
272
- # page.width #=> 400
273
- # page.height #=> 80
274
- # page.blocks.count #=> 1
275
- #
276
- class Page
277
- ##
278
- # @private The EntityAnnotation GRPC object.
279
- attr_accessor :grpc
280
-
281
- ##
282
- # @private Creates a new Page instance.
283
- def initialize
284
- @grpc = nil
285
- end
286
-
287
- ##
288
- # A list of detected languages together with confidence.
289
- #
290
- # @return [Array<Language>]
291
- #
292
- def languages
293
- return [] if @grpc.property.nil?
294
- @languages ||= Array(@grpc.property.detected_languages).map do |l|
295
- Language.from_grpc l
296
- end
297
- end
298
-
299
- ##
300
- # The type of a detected break at the start or end of the page.
301
- #
302
- # @return [::Symbol]
303
- #
304
- def break_type
305
- return nil if @grpc.property.nil?
306
- @grpc.property.detected_break &&
307
- @grpc.property.detected_break.type.to_sym
308
- end
309
-
310
- ##
311
- # True if a detected break prepends the page.
312
- #
313
- # @return [Boolean]
314
- #
315
- def prefix_break?
316
- return nil if @grpc.property.nil?
317
- @grpc.property.detected_break &&
318
- @grpc.property.detected_break.is_prefix
319
- end
320
-
321
- ##
322
- # Page width in pixels.
323
- #
324
- # @return [Integer]
325
- #
326
- def width
327
- @grpc.width
328
- end
329
-
330
- ##
331
- # Page height in pixels.
332
- #
333
- # @return [Integer]
334
- #
335
- def height
336
- @grpc.height
337
- end
338
-
339
- ##
340
- # List of blocks of text, images etc on this page.
341
- #
342
- # @return [Array<Block>]
343
- #
344
- def blocks
345
- @blocks ||= Array(@grpc.blocks).map do |b|
346
- Block.from_grpc b
347
- end
348
- end
349
-
350
- ##
351
- # Deeply converts object to a hash. All keys will be symbolized.
352
- #
353
- # @return [Hash]
354
- #
355
- def to_h
356
- { languages: languages.map(&:to_h), break_type: break_type,
357
- prefix_break: prefix_break?, width: width, height: height,
358
- blocks: blocks.map(&:to_h) }
359
- end
360
-
361
- # @private
362
- def to_s
363
- tmplt = "languages: %s, break_type: %s, prefix_break: %s," \
364
- " width: %s, height: %s, blocks: %i"
365
- format tmplt, languages.inspect, break_type, prefix_break?, width,
366
- height, blocks.count
367
- end
368
-
369
- # @private
370
- def inspect
371
- "#<#{self.class.name} #{self}>"
372
- end
373
-
374
- ##
375
- # @private New Annotation::Text::Page from a GRPC
376
- # object.
377
- def self.from_grpc grpc
378
- new.tap { |w| w.instance_variable_set :@grpc, grpc }
379
- end
380
-
381
- ##
382
- # # Block
383
- #
384
- # A logical element on the page. See {Page}.
385
- #
386
- # @example
387
- # require "google/cloud/vision"
388
- #
389
- # vision = Google::Cloud::Vision.new
390
- #
391
- # image = vision.image "path/to/text.png"
392
- # text = image.text
393
- #
394
- # block = text.pages[0].blocks.first
395
- #
396
- # block.languages.first.code #=> "en"
397
- # block.bounds.count #=> 4
398
- # block.paragraphs.count #=> 1
399
- #
400
- class Block
401
- ##
402
- # @private The EntityAnnotation GRPC object.
403
- attr_accessor :grpc
404
-
405
- ##
406
- # @private Creates a new Block instance.
407
- def initialize
408
- @grpc = nil
409
- end
410
-
411
- ##
412
- # A list of detected languages together with confidence.
413
- #
414
- # @return [Array<Language>]
415
- #
416
- def languages
417
- return [] if @grpc.property.nil?
418
- detected_languages = @grpc.property.detected_languages
419
- @languages ||= Array(detected_languages).map do |l|
420
- Language.from_grpc l
421
- end
422
- end
423
-
424
- ##
425
- # The type of a detected break at the start or end of the page.
426
- #
427
- # @return [::Symbol]
428
- #
429
- def break_type
430
- return nil if @grpc.property.nil?
431
- @grpc.property.detected_break &&
432
- @grpc.property.detected_break.type.to_sym
433
- end
434
-
435
- ##
436
- # True if a detected break prepends the page.
437
- #
438
- # @return [Boolean]
439
- #
440
- def prefix_break?
441
- return nil if @grpc.property.nil?
442
- @grpc.property.detected_break &&
443
- @grpc.property.detected_break.is_prefix
444
- end
445
-
446
- ##
447
- # The bounding box for the block.
448
- # The vertices are in the order of top-left, top-right,
449
- # bottom-right, bottom-left. When a rotation of the bounding box
450
- # is detected the rotation is represented as around the top-left
451
- # corner as defined when the text is read in the 'natural'
452
- # orientation.
453
- # For example:
454
- # * when the text is horizontal it might look like:
455
- # 0----1
456
- # | |
457
- # 3----2
458
- # * when rotated 180 degrees around the top-left corner it
459
- # becomes:
460
- # 2----3
461
- # | |
462
- # 1----0
463
- # and the vertice order will still be (0, 1, 2, 3).
464
- #
465
- # @return [Array<Vertex>]
466
- #
467
- def bounds
468
- return [] unless @grpc.bounding_box
469
- @bounds ||= Array(@grpc.bounding_box.vertices).map do |v|
470
- Vertex.from_grpc v
471
- end
472
- end
473
-
474
- ##
475
- # List of paragraphs in this block (if this block is of type
476
- # text).
477
- #
478
- # @return [Array<Paragraph>]
479
- #
480
- def paragraphs
481
- @paragraphs ||= Array(@grpc.paragraphs).map do |b|
482
- Paragraph.from_grpc b
483
- end
484
- end
485
-
486
- ##
487
- # Detected block type (text, image etc) for the block.
488
- #
489
- # @return [::Symbol]
490
- #
491
- def block_type
492
- @grpc.block_type.to_sym
493
- end
494
-
495
- ##
496
- # Deeply converts object to a hash. All keys will be symbolized.
497
- #
498
- # @return [Hash]
499
- #
500
- def to_h
501
- { languages: languages.map(&:to_h), break_type: break_type,
502
- prefix_break: prefix_break?, bounds: bounds.map(&:to_h),
503
- paragraphs: paragraphs.map(&:to_h), block_type: block_type }
504
- end
505
-
506
- # @private
507
- def to_s
508
- tmplt = "languages: %s, break_type: %s, prefix_break: %s," \
509
- " bounds: %i, paragraphs: %i, block_type: %s"
510
- format tmplt, languages.inspect, break_type, prefix_break?,
511
- bounds.count, paragraphs.count, block_type
512
- end
513
-
514
- # @private
515
- def inspect
516
- "#<#{self.class.name} #{self}>"
517
- end
518
-
519
- ##
520
- # @private New Annotation::Text::Page::Block from a GRPC
521
- # object.
522
- def self.from_grpc grpc
523
- new.tap { |w| w.instance_variable_set :@grpc, grpc }
524
- end
525
- end
526
-
527
- ##
528
- # # Paragraph
529
- #
530
- # Structural unit of text representing a number of words in certain
531
- # order. See {Block}.
532
- #
533
- # @example
534
- # require "google/cloud/vision"
535
- #
536
- # vision = Google::Cloud::Vision.new
537
- #
538
- # image = vision.image "path/to/text.png"
539
- # text = image.text
540
- #
541
- # paragraph = text.pages[0].blocks[0].paragraphs.first
542
- #
543
- # paragraph.languages.first.code #=> "en"
544
- # paragraph.bounds.count #=> 4
545
- # paragraph.words.count #=> 10
546
- #
547
- class Paragraph
548
- ##
549
- # @private The EntityAnnotation GRPC object.
550
- attr_accessor :grpc
551
-
552
- ##
553
- # @private Creates a new Paragraph instance.
554
- def initialize
555
- @grpc = nil
556
- end
557
-
558
- ##
559
- # A list of detected languages together with confidence.
560
- #
561
- # @return [Array<Language>]
562
- #
563
- def languages
564
- return [] if @grpc.property.nil?
565
- detected_languages = @grpc.property.detected_languages
566
- @languages ||= Array(detected_languages).map do |l|
567
- Language.from_grpc l
568
- end
569
- end
570
-
571
- ##
572
- # The type of a detected break at the start or end of the page.
573
- #
574
- # @return [::Symbol]
575
- #
576
- def break_type
577
- return nil if @grpc.property.nil?
578
- @grpc.property.detected_break &&
579
- @grpc.property.detected_break.type.to_sym
580
- end
581
-
582
- ##
583
- # True if a detected break prepends the page.
584
- #
585
- # @return [Boolean]
586
- #
587
- def prefix_break?
588
- return nil if @grpc.property.nil?
589
- @grpc.property.detected_break &&
590
- @grpc.property.detected_break.is_prefix
591
- end
592
-
593
- ##
594
- # The bounding box for the paragraph.
595
- # The vertices are in the order of top-left, top-right,
596
- # bottom-right, bottom-left. When a rotation of the bounding box
597
- # is detected the rotation is represented as around the top-left
598
- # corner as defined when the text is read in the 'natural'
599
- # orientation.
600
- # For example:
601
- # * when the text is horizontal it might look like:
602
- # 0----1
603
- # | |
604
- # 3----2
605
- # * when rotated 180 degrees around the top-left corner it
606
- # becomes:
607
- # 2----3
608
- # | |
609
- # 1----0
610
- # and the vertice order will still be (0, 1, 2, 3).
611
- #
612
- # @return [Array<Vertex>]
613
- #
614
- def bounds
615
- return [] unless @grpc.bounding_box
616
- @bounds ||= Array(@grpc.bounding_box.vertices).map do |v|
617
- Vertex.from_grpc v
618
- end
619
- end
620
-
621
- ##
622
- # List of words in this paragraph.
623
- #
624
- # @return [Array<Word>]
625
- #
626
- def words
627
- @words ||= Array(@grpc.words).map do |b|
628
- Word.from_grpc b
629
- end
630
- end
631
-
632
- ##
633
- # Deeply converts object to a hash. All keys will be symbolized.
634
- #
635
- # @return [Hash]
636
- #
637
- def to_h
638
- { languages: languages.map(&:to_h), break_type: break_type,
639
- prefix_break: prefix_break?, bounds: bounds.map(&:to_h),
640
- words: words.map(&:to_h) }
641
- end
642
-
643
- # @private
644
- def to_s
645
- tmplt = "languages: %s, break_type: %s, prefix_break: %s," \
646
- " bounds: %i, words: %i"
647
- format tmplt, languages.inspect, break_type, prefix_break?,
648
- bounds.count, words.count
649
- end
650
-
651
- # @private
652
- def inspect
653
- "#<#{self.class.name} #{self}>"
654
- end
655
-
656
- ##
657
- # @private New Annotation::Text::Page::Paragraph from a GRPC
658
- # object.
659
- def self.from_grpc grpc
660
- new.tap { |w| w.instance_variable_set :@grpc, grpc }
661
- end
662
- end
663
-
664
- ##
665
- # # Word
666
- #
667
- # A word representation. See {Paragraph}.
668
- #
669
- # @example
670
- # require "google/cloud/vision"
671
- #
672
- # vision = Google::Cloud::Vision.new
673
- #
674
- # image = vision.image "path/to/text.png"
675
- # text = image.text
676
- #
677
- # word = text.pages[0].blocks[0].paragraphs[0].words.first
678
- #
679
- # word.languages.first.code #=> "en"
680
- # word.bounds.count #=> 4
681
- # word.symbols.count #=> 6
682
- #
683
- class Word
684
- ##
685
- # @private The EntityAnnotation GRPC object.
686
- attr_accessor :grpc
687
-
688
- ##
689
- # @private Creates a new Word instance.
690
- def initialize
691
- @grpc = nil
692
- end
693
-
694
- ##
695
- # A list of detected languages together with confidence.
696
- #
697
- # @return [Array<Language>]
698
- #
699
- def languages
700
- return [] if @grpc.property.nil?
701
- detected_languages = @grpc.property.detected_languages
702
- @languages ||= Array(detected_languages).map do |l|
703
- Language.from_grpc l
704
- end
705
- end
706
-
707
- ##
708
- # The type of a detected break at the start or end of the page.
709
- #
710
- # @return [::Symbol]
711
- #
712
- def break_type
713
- return nil if @grpc.property.nil?
714
- @grpc.property.detected_break &&
715
- @grpc.property.detected_break.type.to_sym
716
- end
717
-
718
- ##
719
- # True if a detected break prepends the page.
720
- #
721
- # @return [Boolean]
722
- #
723
- def prefix_break?
724
- return nil if @grpc.property.nil?
725
- @grpc.property.detected_break &&
726
- @grpc.property.detected_break.is_prefix
727
- end
728
-
729
- ##
730
- # The bounding box for the word.
731
- # The vertices are in the order of top-left, top-right,
732
- # bottom-right, bottom-left. When a rotation of the bounding box
733
- # is detected the rotation is represented as around the top-left
734
- # corner as defined when the text is read in the 'natural'
735
- # orientation.
736
- # For example:
737
- # * when the text is horizontal it might look like:
738
- # 0----1
739
- # | |
740
- # 3----2
741
- # * when rotated 180 degrees around the top-left corner it
742
- # becomes:
743
- # 2----3
744
- # | |
745
- # 1----0
746
- # and the vertice order will still be (0, 1, 2, 3).
747
- #
748
- # @return [Array<Vertex>]
749
- #
750
- def bounds
751
- return [] unless @grpc.bounding_box
752
- @bounds ||= Array(@grpc.bounding_box.vertices).map do |v|
753
- Vertex.from_grpc v
754
- end
755
- end
756
-
757
- ##
758
- # List of symbols in the word. The order of the symbols follows
759
- # the natural reading order.
760
- #
761
- # @return [Array<Symbol>]
762
- #
763
- def symbols
764
- @symbols ||= Array(@grpc.symbols).map do |b|
765
- Symbol.from_grpc b
766
- end
767
- end
768
-
769
- ##
770
- # Deeply converts object to a hash. All keys will be symbolized.
771
- #
772
- # @return [Hash]
773
- #
774
- def to_h
775
- { languages: languages.map(&:to_h), break_type: break_type,
776
- prefix_break: prefix_break?, bounds: bounds.map(&:to_h),
777
- symbols: symbols.map(&:to_h) }
778
- end
779
-
780
- # @private
781
- def to_s
782
- tmplt = "languages: %s, break_type: %s, prefix_break: %s," \
783
- " bounds: %i, symbols: %i"
784
- format tmplt, languages.inspect, break_type, prefix_break?,
785
- bounds.count, symbols.count
786
- end
787
-
788
- # @private
789
- def inspect
790
- "#<#{self.class.name} #{self}>"
791
- end
792
-
793
- ##
794
- # @private New Annotation::Text::Page::Word from a GRPC
795
- # object.
796
- def self.from_grpc grpc
797
- new.tap { |w| w.instance_variable_set :@grpc, grpc }
798
- end
799
- end
800
-
801
- ##
802
- # # Symbol
803
- #
804
- # A word representation. See {Paragraph}.
805
- #
806
- # @example
807
- # require "google/cloud/vision"
808
- #
809
- # vision = Google::Cloud::Vision.new
810
- #
811
- # image = vision.image "path/to/text.png"
812
- # text = image.text
813
- # page = text.pages.first
814
- #
815
- # symbol = page.blocks[0].paragraphs[0].words[0].symbols[0]
816
- #
817
- # symbol.languages.first.code #=> "en"
818
- # symbol.bounds.count #=> 4
819
- # symbol.text #=> "G"
820
- #
821
- class Symbol
822
- ##
823
- # @private The EntityAnnotation GRPC object.
824
- attr_accessor :grpc
825
-
826
- ##
827
- # @private Creates a new Symbol instance.
828
- def initialize
829
- @grpc = nil
830
- end
831
-
832
- ##
833
- # A list of detected languages together with confidence.
834
- #
835
- # @return [Array<Language>]
836
- #
837
- def languages
838
- return [] if @grpc.property.nil?
839
- detected_languages = @grpc.property.detected_languages
840
- @languages ||= Array(detected_languages).map do |l|
841
- Language.from_grpc l
842
- end
843
- end
844
-
845
- ##
846
- # The type of a detected break at the start or end of the page.
847
- #
848
- # @return [::Symbol]
849
- #
850
- def break_type
851
- return nil if @grpc.property.nil?
852
- @grpc.property.detected_break &&
853
- @grpc.property.detected_break.type.to_sym
854
- end
855
-
856
- ##
857
- # True if a detected break prepends the page.
858
- #
859
- # @return [Boolean]
860
- #
861
- def prefix_break?
862
- return nil if @grpc.property.nil?
863
- @grpc.property.detected_break &&
864
- @grpc.property.detected_break.is_prefix
865
- end
866
-
867
- ##
868
- # The bounding box for the symbol.
869
- # The vertices are in the order of top-left, top-right,
870
- # bottom-right, bottom-left. When a rotation of the bounding box
871
- # is detected the rotation is represented as around the top-left
872
- # corner as defined when the text is read in the 'natural'
873
- # orientation.
874
- # For example:
875
- # * when the text is horizontal it might look like:
876
- # 0----1
877
- # | |
878
- # 3----2
879
- # * when rotated 180 degrees around the top-left corner it
880
- # becomes:
881
- # 2----3
882
- # | |
883
- # 1----0
884
- # and the vertice order will still be (0, 1, 2, 3).
885
- #
886
- # @return [Array<Vertex>]
887
- #
888
- def bounds
889
- return [] unless @grpc.bounding_box
890
- @bounds ||= Array(@grpc.bounding_box.vertices).map do |v|
891
- Vertex.from_grpc v
892
- end
893
- end
894
-
895
- ##
896
- # The actual UTF-8 representation of the symbol.
897
- #
898
- # @return [String]
899
- #
900
- def text
901
- @grpc.text
902
- end
903
-
904
- ##
905
- # Deeply converts object to a hash. All keys will be symbolized.
906
- #
907
- # @return [Hash]
908
- #
909
- def to_h
910
- { languages: languages.map(&:to_h), break_type: break_type,
911
- prefix_break: prefix_break?, bounds: bounds.map(&:to_h),
912
- text: text }
913
- end
914
-
915
- # @private
916
- def to_s
917
- tmplt = "languages: %s, break_type: %s, prefix_break: %s," \
918
- " bounds: %i, text: %s"
919
- format tmplt, languages.inspect, break_type, prefix_break?,
920
- bounds.count, text
921
- end
922
-
923
- # @private
924
- def inspect
925
- "#<#{self.class.name} #{self}>"
926
- end
927
-
928
- ##
929
- # @private New Annotation::Text::Page::Symbol from a GRPC
930
- # object.
931
- def self.from_grpc grpc
932
- new.tap { |w| w.instance_variable_set :@grpc, grpc }
933
- end
934
- end
935
-
936
- ##
937
- # # Language
938
- #
939
- # A language within a detected text (OCR). See {Text#pages}.
940
- #
941
- # @example
942
- # require "google/cloud/vision"
943
- #
944
- # vision = Google::Cloud::Vision.new
945
- #
946
- # image = vision.image "path/to/text.png"
947
- # text = image.text
948
- # page = text.pages.first
949
- #
950
- # language = page.languages.first
951
- # language.code #=> "en"
952
- #
953
- class Language
954
- ##
955
- # @private The EntityAnnotation GRPC object.
956
- attr_accessor :grpc
957
-
958
- ##
959
- # @private Creates a new Language instance.
960
- def initialize
961
- @grpc = nil
962
- end
963
-
964
- ##
965
- # The language code detected for a structural component.
966
- #
967
- # @return [String] The [ISO
968
- # 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)
969
- # language code.
970
- #
971
- def code
972
- @grpc.language_code
973
- end
974
-
975
- ##
976
- # Confidence of detected language.
977
- #
978
- # @return [Float] A value in the range [0,1].
979
- #
980
- def confidence
981
- @grpc.confidence
982
- end
983
-
984
- ##
985
- # Deeply converts object to a hash. All keys will be symbolized.
986
- #
987
- # @return [Hash]
988
- #
989
- def to_h
990
- { code: code, confidence: confidence }
991
- end
992
-
993
- # @private
994
- def to_s
995
- format "code: %s, confidence: %s", code, confidence
996
- end
997
-
998
- # @private
999
- def inspect
1000
- "#<#{self.class.name} #{self}>"
1001
- end
1002
-
1003
- ##
1004
- # @private New Annotation::Text::Page::Language from a GRPC
1005
- # object.
1006
- def self.from_grpc grpc
1007
- new.tap { |w| w.instance_variable_set :@grpc, grpc }
1008
- end
1009
- end
1010
- end
1011
- end
1012
- end
1013
- end
1014
- end
1015
- end