google-cloud-vision 0.22.1 → 0.23.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6ae41f26fa335cd0ec9a055716542f405caed2e9
4
- data.tar.gz: 355a0a40b3cb78ff3153c5fee062f9698f354242
3
+ metadata.gz: 5bdbd68499f14f96adeff2ab43a46eb094c2cbc1
4
+ data.tar.gz: 3ac3f507e6960ba31dff0c6723320e49fab0ac6c
5
5
  SHA512:
6
- metadata.gz: 704c57d9fc2b0035f53217d4a81339ad311d5e4733e12d6dee9bc1a0626cebe88d07f2fcda36362234c364d02a28dc080dfe734640b0899b3b00211d458e8107
7
- data.tar.gz: 3e1fdbb422795c61079e55fc0df11b288806fa80ec311d3a6ee67cb5520afd8a528ce91602bf2deb3e4bbcd5f939153700c5590c6337795e094e1256bb6c1513
6
+ metadata.gz: 98d7660ad9d58a22e402df61af630b8014c1c59c27754383c1d40536e9b6c014bedabbef579e29f08c261ac3ad6b18855c6bf1505585fb244637a7469aa63b4f
7
+ data.tar.gz: 2e53399f5efdd0f1712cf6877f7617c97951bade9f4c2fbfa519fb31b4f5759f31cddf2d10c957b84b0d5e13a0a83c93c30acc5171e80aac5eb18020c86b2a5e
@@ -62,6 +62,16 @@ module Google
62
62
  # image = vision.image "path/to/landmark.jpg"
63
63
  # ```
64
64
  #
65
+ # Or any publicly-accessible image HTTP/HTTPS URL:
66
+ #
67
+ # ```ruby
68
+ # require "google/cloud/vision"
69
+ #
70
+ # vision = Google::Cloud::Vision.new
71
+ #
72
+ # image = vision.image "https://www.example.com/images/landmark.jpg"
73
+ # ```
74
+ #
65
75
  # Or, you can initialize the image with a Google Cloud Storage URI:
66
76
  #
67
77
  # ```ruby
@@ -98,7 +108,8 @@ module Google
98
108
  # ```
99
109
  #
100
110
  # To run multiple features on an image in a single request, pass the image
101
- # (or a string file path or Storage URI) to {Vision::Project#annotate}:
111
+ # (or a string file path, publicly-accessible image HTTP/HTTPS URL, or
112
+ # Storage URI) to {Vision::Project#annotate}:
102
113
  #
103
114
  # ```ruby
104
115
  # require "google/cloud/vision"
@@ -159,7 +170,7 @@ module Google
159
170
  # annotations[0].faces.count #=> 1
160
171
  # annotations[0].labels.count #=> 4
161
172
  # annotations[1].landmarks.count #=> 1
162
- # annotations[2].text.words.count #=> 28
173
+ # annotations[2].text.pages.count #=> 1
163
174
  # ```
164
175
  #
165
176
  # The maximum number of results returned when performing face, landmark,
@@ -430,14 +441,130 @@ module Google
430
441
  # # labels = vision.image("path/to/labels.jpg").labels 5
431
442
  #
432
443
  attr_accessor :default_max_labels
444
+
445
+ ##
446
+ # The default max results to return for crop hints detection requests.
447
+ # This is used on {Project#annotate} as well as {Image#crop_hints}.
448
+ #
449
+ # The default value is 100.
450
+ #
451
+ # @example Using the default setting on {Project#annotate}:
452
+ # require "google/cloud/vision"
453
+ #
454
+ # vision = Google::Cloud::Vision.new
455
+ #
456
+ # Google::Cloud::Vision.default_max_crop_hints #=> 100
457
+ #
458
+ # img = "path/to/landmarks.jpg"
459
+ # annotation = vision.annotate img, crop_hints: true
460
+ # # This is the same as calling
461
+ # # annotation = vision.annotate img, crop_hints: 100
462
+ #
463
+ # @example Updating the default setting on {Project#annotate}:
464
+ # require "google/cloud/vision"
465
+ #
466
+ # vision = Google::Cloud::Vision.new
467
+ #
468
+ # # Set a new default
469
+ # Google::Cloud::Vision.default_max_crop_hints = 5
470
+ #
471
+ # img = "path/to/landmarks.jpg"
472
+ # annotation = vision.annotate img, crop_hints: true
473
+ # # This is the same as calling
474
+ # # annotation = vision.annotate img, crop_hints: 5
475
+ #
476
+ #
477
+ # @example Using the default setting on {Image#crop_hints}:
478
+ # require "google/cloud/vision"
479
+ #
480
+ # vision = Google::Cloud::Vision.new
481
+ #
482
+ # Google::Cloud::Vision.default_max_crop_hints #=> 100
483
+ #
484
+ # crop_hints = vision.image("path/to/landmarks.jpg").crop_hints
485
+ # # This is the same as calling
486
+ # # crop_hints = vision.image("path/to/landmarks.jpg").crop_hints 100
487
+ #
488
+ # @example Updating the default setting on {Image#crop_hints}:
489
+ # require "google/cloud/vision"
490
+ #
491
+ # vision = Google::Cloud::Vision.new
492
+ #
493
+ # # Set a new default
494
+ # Google::Cloud::Vision.default_max_crop_hints = 5
495
+ #
496
+ # crop_hints = vision.image("path/to/landmarks.jpg").crop_hints
497
+ # # This is the same as calling
498
+ # # crop_hints = vision.image("path/to/landmarks.jpg").crop_hints 5
499
+ #
500
+ attr_accessor :default_max_crop_hints
501
+
502
+ ##
503
+ # The default max results to return for web detection requests.
504
+ # This is used on {Project#annotate} as well as {Image#web}.
505
+ #
506
+ # The default value is 100.
507
+ #
508
+ # @example Using the default setting on {Project#annotate}:
509
+ # require "google/cloud/vision"
510
+ #
511
+ # vision = Google::Cloud::Vision.new
512
+ #
513
+ # Google::Cloud::Vision.default_max_web #=> 100
514
+ #
515
+ # img = "path/to/landmarks.jpg"
516
+ # annotation = vision.annotate img, web: true
517
+ # # This is the same as calling
518
+ # # annotation = vision.annotate img, web: 100
519
+ #
520
+ # @example Updating the default setting on {Project#annotate}:
521
+ # require "google/cloud/vision"
522
+ #
523
+ # vision = Google::Cloud::Vision.new
524
+ #
525
+ # # Set a new default
526
+ # Google::Cloud::Vision.default_max_web = 5
527
+ #
528
+ # img = "path/to/landmarks.jpg"
529
+ # annotation = vision.annotate img, web: true
530
+ # # This is the same as calling
531
+ # # annotation = vision.annotate img, web: 5
532
+ #
533
+ #
534
+ # @example Using the default setting on {Image#web}:
535
+ # require "google/cloud/vision"
536
+ #
537
+ # vision = Google::Cloud::Vision.new
538
+ #
539
+ # Google::Cloud::Vision.default_max_web #=> 100
540
+ #
541
+ # web = vision.image("path/to/landmarks.jpg").web
542
+ # # This is the same as calling
543
+ # # web = vision.image("path/to/landmarks.jpg").web 100
544
+ #
545
+ # @example Updating the default setting on {Image#web}:
546
+ # require "google/cloud/vision"
547
+ #
548
+ # vision = Google::Cloud::Vision.new
549
+ #
550
+ # # Set a new default
551
+ # Google::Cloud::Vision.default_max_web = 5
552
+ #
553
+ # web = vision.image("path/to/landmarks.jpg").web
554
+ # # This is the same as calling
555
+ # # web = vision.image("path/to/landmarks.jpg").web 5
556
+ #
557
+ attr_accessor :default_max_web
433
558
  end
434
559
 
435
560
  # Set the default values.
436
561
  # Update the comments documentation when these change.
437
- self.default_max_faces = 100
438
- self.default_max_landmarks = 100
439
- self.default_max_logos = 100
440
- self.default_max_labels = 100
562
+ self.default_max_faces = 100
563
+ self.default_max_landmarks = 100
564
+ self.default_max_logos = 100
565
+ self.default_max_labels = 100
566
+ self.default_max_crop_hints = 100
567
+ self.default_max_web = 100
441
568
 
442
569
  ##
443
570
  # Creates a new object for connecting to the Vision service.
@@ -103,12 +103,19 @@ module Google
103
103
  # detection feature. The maximum number of results is configured in
104
104
  # {Google::Cloud::Vision.default_max_labels}, or may be provided here.
105
105
  # Optional.
106
- # @param [Boolean] text Whether to perform the text (OCR) feature.
106
+ # @param [Boolean] text Whether to perform the text detection feature
107
+ # (OCR for shorter documents with sparse text). Optional.
108
+ # @param [Boolean] document Whether to perform the document text
109
+ # detection feature (OCR for longer documents with dense text).
107
110
  # Optional.
108
111
  # @param [Boolean] safe_search Whether to perform the safe search
109
112
  # feature. Optional.
110
113
  # @param [Boolean] properties Whether to perform the image properties
111
114
  # feature (currently, the image's dominant colors.) Optional.
115
+ # @param [Boolean, Integer] crop_hints Whether to perform the crop hints
116
+ # feature. Optional.
117
+ # @param [Boolean, Integer] web Whether to perform the web annotation
118
+ # feature. Optional.
112
119
  #
113
120
  # @return [Annotation, Array<Annotation>] The results for all image
114
121
  # detections, returned as a single {Annotation} instance for one
@@ -133,13 +140,14 @@ module Google
133
140
  # annotations[0].faces.count #=> 1
134
141
  # annotations[0].labels.count #=> 4
135
142
  # annotations[1].landmarks.count #=> 1
136
- # annotations[2].text.words.count #=> 28
143
+ # annotations[2].text.pages.count #=> 1
137
144
  #
138
145
  def annotate *images, faces: false, landmarks: false, logos: false,
139
- labels: false, text: false, safe_search: false,
140
- properties: false
141
- add_requests(images, faces, landmarks, logos, labels, text,
142
- safe_search, properties)
146
+ labels: false, text: false, document: false,
147
+ safe_search: false, properties: false, crop_hints: false,
148
+ web: false
149
+ add_requests(images, faces, landmarks, logos, labels, text, document,
150
+ safe_search, properties, crop_hints, web)
143
151
  end
144
152
 
145
153
  protected
@@ -150,9 +158,10 @@ module Google
150
158
  end
151
159
 
152
160
  def add_requests images, faces, landmarks, logos, labels, text,
153
- safe_search, properties
161
+ document, safe_search, properties, crop_hints, web
154
162
  features = annotate_features(faces, landmarks, logos, labels, text,
155
- safe_search, properties)
163
+ document, safe_search, properties,
164
+ crop_hints, web)
156
165
 
157
166
  Array(images).flatten.each do |img|
158
167
  i = image(img)
@@ -164,20 +173,34 @@ module Google
164
173
  end
165
174
  end
166
175
 
167
- def annotate_features faces, landmarks, logos, labels, text,
168
- safe_search, properties
176
+ def annotate_features faces, landmarks, logos, labels, text, document,
177
+ safe_search, properties, crop_hints, web
169
178
  return default_features if default_features?(
170
- faces, landmarks, logos, labels, text, safe_search, properties)
179
+ faces, landmarks, logos, labels, text, document, safe_search,
180
+ properties, crop_hints, web)
171
181
 
172
- faces, landmarks, logos, labels = validate_max_values(
173
- faces, landmarks, logos, labels)
182
+ faces, landmarks, logos, labels, crop_hints, web = validate_max_args(
183
+ faces, landmarks, logos, labels, crop_hints, web)
184
+
185
+ f = value_features faces, landmarks, logos, labels, crop_hints, web
186
+ f + boolean_features(text, document, safe_search, properties)
187
+ end
174
188
 
189
+ def value_features faces, landmarks, logos, labels, crop_hints, web
175
190
  f = []
176
191
  f << feature(:FACE_DETECTION, faces) unless faces.zero?
177
192
  f << feature(:LANDMARK_DETECTION, landmarks) unless landmarks.zero?
178
193
  f << feature(:LOGO_DETECTION, logos) unless logos.zero?
179
194
  f << feature(:LABEL_DETECTION, labels) unless labels.zero?
195
+ f << feature(:CROP_HINTS, crop_hints) unless crop_hints.zero?
196
+ f << feature(:WEB_DETECTION, web) unless web.zero?
197
+ f
198
+ end
199
+
200
+ def boolean_features text, document, safe_search, properties
201
+ f = []
180
202
  f << feature(:TEXT_DETECTION, 1) if text
203
+ f << feature(:DOCUMENT_TEXT_DETECTION, 1) if document
181
204
  f << feature(:SAFE_SEARCH_DETECTION, 1) if safe_search
182
205
  f << feature(:IMAGE_PROPERTIES, 1) if properties
183
206
  f
@@ -188,11 +211,12 @@ module Google
188
211
  type: type, max_results: max_results)
189
212
  end
190
213
 
191
- def default_features? faces, landmarks, logos, labels, text,
192
- safe_search, properties
214
+ def default_features? faces, landmarks, logos, labels, text, document,
215
+ safe_search, properties, crop_hints, web
193
216
  faces == false && landmarks == false && logos == false &&
194
- labels == false && text == false && safe_search == false &&
195
- properties == false
217
+ labels == false && text == false && document == false &&
218
+ safe_search == false && properties == false &&
219
+ crop_hints == false && web == false
196
220
  end
197
221
 
198
222
  def default_features
@@ -204,21 +228,28 @@ module Google
204
228
  feature(:LABEL_DETECTION,
205
229
  Google::Cloud::Vision.default_max_labels),
206
230
  feature(:TEXT_DETECTION, 1),
231
+ feature(:DOCUMENT_TEXT_DETECTION, 1),
207
232
  feature(:SAFE_SEARCH_DETECTION, 1),
208
- feature(:IMAGE_PROPERTIES, 1)
233
+ feature(:IMAGE_PROPERTIES, 1),
234
+ feature(:CROP_HINTS, Google::Cloud::Vision.default_max_crop_hints),
235
+ feature(:WEB_DETECTION, Google::Cloud::Vision.default_max_web)
209
236
  ]
210
237
  end
211
238
 
212
- def validate_max_values faces, landmarks, logos, labels
213
- faces = validate_max_value(
239
+ def validate_max_args faces, landmarks, logos, labels, crop_hints, web
240
+ faces = validate_max_value(
214
241
  faces, Google::Cloud::Vision.default_max_faces)
215
- landmarks = validate_max_value(
242
+ landmarks = validate_max_value(
216
243
  landmarks, Google::Cloud::Vision.default_max_landmarks)
217
- logos = validate_max_value(
244
+ logos = validate_max_value(
218
245
  logos, Google::Cloud::Vision.default_max_logos)
219
- labels = validate_max_value(
246
+ labels = validate_max_value(
220
247
  labels, Google::Cloud::Vision.default_max_labels)
221
- [faces, landmarks, logos, labels]
248
+ crop_hints = validate_max_value(
249
+ crop_hints, Google::Cloud::Vision.default_max_crop_hints)
250
+ web = validate_max_value(
251
+ web, Google::Cloud::Vision.default_max_web)
252
+ [faces, landmarks, logos, labels, crop_hints, web]
222
253
  end
223
254
 
224
255
  def validate_max_value value, default_value
@@ -18,6 +18,8 @@ require "google/cloud/vision/annotation/entity"
18
18
  require "google/cloud/vision/annotation/text"
19
19
  require "google/cloud/vision/annotation/safe_search"
20
20
  require "google/cloud/vision/annotation/properties"
21
+ require "google/cloud/vision/annotation/crop_hint"
22
+ require "google/cloud/vision/annotation/web"
21
23
 
22
24
  module Google
23
25
  module Cloud
@@ -105,7 +107,7 @@ module Google
105
107
  # annotation.face? #=> true
106
108
  #
107
109
  def face?
108
- faces.count > 0
110
+ faces.any?
109
111
  end
110
112
 
111
113
  ##
@@ -163,7 +165,7 @@ module Google
163
165
  # annotation.landmark? #=> true
164
166
  #
165
167
  def landmark?
166
- landmarks.count > 0
168
+ landmarks.any?
167
169
  end
168
170
 
169
171
  ##
@@ -221,7 +223,7 @@ module Google
221
223
  # annotation.logo? #=> true
222
224
  #
223
225
  def logo?
224
- logos.count > 0
226
+ logos.any?
225
227
  end
226
228
 
227
229
  ##
@@ -279,7 +281,7 @@ module Google
279
281
  # annotation.label? #=> true
280
282
  #
281
283
  def label?
282
- labels.count > 0
284
+ labels.any?
283
285
  end
284
286
 
285
287
  ##
@@ -297,7 +299,8 @@ module Google
297
299
  # text = annotation.text
298
300
  #
299
301
  def text
300
- @text ||= Text.from_grpc(@grpc.text_annotations)
302
+ @text ||= \
303
+ Text.from_grpc(@grpc.text_annotations, @grpc.full_text_annotation)
301
304
  end
302
305
 
303
306
  ##
@@ -321,7 +324,7 @@ module Google
321
324
  ##
322
325
  # The results of safe_search detection.
323
326
  #
324
- # @return [SafeSearch]
327
+ # @return [SafeSearch, nil]
325
328
  #
326
329
  # @example
327
330
  # require "google/cloud/vision"
@@ -359,7 +362,7 @@ module Google
359
362
  ##
360
363
  # The results of properties detection.
361
364
  #
362
- # @return [Properties]
365
+ # @return [Properties, nil]
363
366
  #
364
367
  # @example
365
368
  # require "google/cloud/vision"
@@ -394,6 +397,83 @@ module Google
394
397
  !properties.nil?
395
398
  end
396
399
 
400
+ ##
401
+ # The results of crop hints detection.
402
+ #
403
+ # @return [Array<CropHint>]
404
+ #
405
+ # @example
406
+ # require "google/cloud/vision"
407
+ #
408
+ # vision = Google::Cloud::Vision.new
409
+ # image = vision.image "path/to/face.jpg"
410
+ #
411
+ # annotation = vision.annotate image, crop_hints: true
412
+ # crop_hints = annotation.crop_hints
413
+ #
414
+ def crop_hints
415
+ return [] unless @grpc.crop_hints_annotation
416
+ grpc_crop_hints = @grpc.crop_hints_annotation.crop_hints
417
+ @crop_hints ||= Array(grpc_crop_hints).map do |ch|
418
+ CropHint.from_grpc ch
419
+ end
420
+ end
421
+
422
+ ##
423
+ # Whether there is a result for crop hints detection.
424
+ #
425
+ # @return [Boolean]
426
+ #
427
+ # @example
428
+ # require "google/cloud/vision"
429
+ #
430
+ # vision = Google::Cloud::Vision.new
431
+ # image = vision.image "path/to/face.jpg"
432
+ #
433
+ # annotation = vision.annotate image, crop_hints: true
434
+ # annotation.crop_hints? #=> true
435
+ #
436
+ def crop_hints?
437
+ crop_hints.any?
438
+ end
439
+
440
+ ##
441
+ # The results of web detection.
442
+ #
443
+ # @return [Web]
444
+ #
445
+ # @example
446
+ # require "google/cloud/vision"
447
+ #
448
+ # vision = Google::Cloud::Vision.new
449
+ # image = vision.image "path/to/face.jpg"
450
+ #
451
+ # annotation = vision.annotate image, web: true
452
+ # web = annotation.web
453
+ #
454
+ def web
455
+ return nil unless @grpc.web_detection
456
+ @web ||= Web.from_grpc(@grpc.web_detection)
457
+ end
458
+
459
+ ##
460
+ # Whether there is a result for web detection.
461
+ #
462
+ # @return [Boolean]
463
+ #
464
+ # @example
465
+ # require "google/cloud/vision"
466
+ #
467
+ # vision = Google::Cloud::Vision.new
468
+ # image = vision.image "path/to/face.jpg"
469
+ #
470
+ # annotation = vision.annotate image, web: true
471
+ # annotation.web? #=> true
472
+ #
473
+ def web?
474
+ !web.nil?
475
+ end
476
+
397
477
  ##
398
478
  # Deeply converts object to a hash. All keys will be symbolized.
399
479
  #
@@ -402,16 +482,18 @@ module Google
402
482
  def to_h
403
483
  { faces: faces.map(&:to_h), landmarks: landmarks.map(&:to_h),
404
484
  logos: logos.map(&:to_h), labels: labels.map(&:to_h),
405
- text: text.map(&:to_h), safe_search: safe_search.to_h,
406
- properties: properties.to_h }
485
+ text: text.to_h, safe_search: safe_search.to_h,
486
+ properties: properties.to_h, crop_hints: crop_hints.map(&:to_h),
487
+ web: web.to_h }
407
488
  end
408
489
 
409
490
  # @private
410
491
  def to_s
411
492
  tmplt = "(faces: %i, landmarks: %i, logos: %i, labels: %i," \
412
- " text: %s, safe_search: %s, properties: %s)"
493
+ " text: %s, safe_search: %s, properties: %s," \
494
+ " crop_hints: %s, web: %s)"
413
495
  format tmplt, faces.count, landmarks.count, logos.count, labels.count,
414
- text?, safe_search?, properties?
496
+ text?, safe_search?, properties?, crop_hints?, web?
415
497
  end
416
498
 
417
499
  # @private