google-cloud-vision 0.30.4 → 0.31.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
  SHA256:
3
- metadata.gz: fe3749e507b7805dca06c8a3def8d9a8b9ef304db045cbc3cfafbb909d60198f
4
- data.tar.gz: 4a4249af10a0a2bc4720d1de86d1b5b913f7e0542358cdfd1c2310b4f03f9a20
3
+ metadata.gz: b97dd452706561fd304da05cf2572636618d8695e0d13e9db8fe0e000cd3a876
4
+ data.tar.gz: d1c466ff99ff655bb881d382e444846605a927a5820e2645f2068cd5a8e053b3
5
5
  SHA512:
6
- metadata.gz: 8e40b6316cce217c6d546e269981cf3cbbdb5fe2cf99e14985308370839e90fccb26584ca96993e083935401f037c25bcd2c29afde59f392c8610bdc94814b90
7
- data.tar.gz: 42ea947494ff179667fc0a62d54e49d74558b22417dd793e257bd4f0e3a8a197f6a6e598e0e031d5622242a1646f0bc68a6d3223b458a70c97223077c90bd0fb
6
+ metadata.gz: d12d2ecd9e3c996af934dff0ae9508a377cb373ba92852d2b7c3aff0e6e34039403f0f54c5ba15f05c911ea490c80794aef2d3a7eac70ab5842377bec9567260
7
+ data.tar.gz: ed0e16cb06817bb8cecaf574247e44466db8c4f1f96d2c064ef3bfb17c5e37b6df75310bacebc2f58bc7841fd25bce38f27ce5bd2e6fbe52d2c8a0c22a304cc2
@@ -1,5 +1,9 @@
1
1
  # Release History
2
2
 
3
+ ### 0.31.0 / 2018-09-26
4
+
5
+ * Add Object Localization.
6
+
3
7
  ### 0.30.4 / 2018-09-20
4
8
 
5
9
  * Update documentation.
@@ -142,4 +142,5 @@ Google::Cloud.configure.add_config! :vision do |config|
142
142
  config.add_field! :default_max_labels, 100
143
143
  config.add_field! :default_max_crop_hints, 100
144
144
  config.add_field! :default_max_web, 100
145
+ config.add_field! :default_max_object_localizations, 100
145
146
  end
@@ -442,6 +442,14 @@ module Google
442
442
  def default_max_web
443
443
  configure.default_max_web
444
444
  end
445
+
446
+ def default_max_object_localizations= value
447
+ configure.default_max_object_localizations = value
448
+ end
449
+
450
+ def default_max_object_localizations
451
+ configure.default_max_object_localizations
452
+ end
445
453
  end
446
454
 
447
455
  ##
@@ -116,6 +116,7 @@ module Google
116
116
  # feature. Optional.
117
117
  # @param [Boolean, Integer] web Whether to perform the web annotation
118
118
  # feature. Optional.
119
+ # @param [Boolean, Integer] object_localizations TODO! Optional.
119
120
  #
120
121
  # @return [Annotation, Array<Annotation>] The results for all image
121
122
  # detections, returned as a single {Annotation} instance for one
@@ -145,9 +146,10 @@ module Google
145
146
  def annotate *images, faces: false, landmarks: false, logos: false,
146
147
  labels: false, text: false, document: false,
147
148
  safe_search: false, properties: false, crop_hints: false,
148
- web: false
149
+ web: false, object_localizations: false
149
150
  add_requests(images, faces, landmarks, logos, labels, text, document,
150
- safe_search, properties, crop_hints, web)
151
+ safe_search, properties, crop_hints, web,
152
+ object_localizations)
151
153
  end
152
154
 
153
155
  protected
@@ -158,10 +160,11 @@ module Google
158
160
  end
159
161
 
160
162
  def add_requests images, faces, landmarks, logos, labels, text,
161
- document, safe_search, properties, crop_hints, web
163
+ document, safe_search, properties, crop_hints, web,
164
+ object_localizations
162
165
  features = annotate_features(faces, landmarks, logos, labels, text,
163
166
  document, safe_search, properties,
164
- crop_hints, web)
167
+ crop_hints, web, object_localizations)
165
168
 
166
169
  Array(images).flatten.each do |img|
167
170
  i = image(img)
@@ -174,38 +177,54 @@ module Google
174
177
  end
175
178
 
176
179
  def annotate_features faces, landmarks, logos, labels, text, document,
177
- safe_search, properties, crop_hints, web
180
+ safe_search, properties, crop_hints, web,
181
+ object_localizations
178
182
  return default_features if default_features?(
179
183
  faces, landmarks, logos, labels, text, document, safe_search,
180
- properties, crop_hints, web
184
+ properties, crop_hints, web, object_localizations
181
185
  )
182
186
 
183
- faces, landmarks, logos, labels, crop_hints, web = validate_max_args(
184
- faces, landmarks, logos, labels, crop_hints, web
185
- )
187
+ faces, landmarks, logos, labels, crop_hints, web, \
188
+ object_localizations = validate_max_args(
189
+ faces, landmarks, logos, labels, crop_hints, web,
190
+ object_localizations
191
+ )
186
192
 
187
- f = value_features faces, landmarks, logos, labels, crop_hints, web
193
+ f = value_features faces, landmarks, logos, labels, crop_hints, web,
194
+ object_localizations
188
195
  f + boolean_features(text, document, safe_search, properties)
189
196
  end
190
197
 
191
- def value_features faces, landmarks, logos, labels, crop_hints, web
192
- f = []
193
- f << feature(:FACE_DETECTION, faces) unless faces.zero?
194
- f << feature(:LANDMARK_DETECTION, landmarks) unless landmarks.zero?
195
- f << feature(:LOGO_DETECTION, logos) unless logos.zero?
196
- f << feature(:LABEL_DETECTION, labels) unless labels.zero?
197
- f << feature(:CROP_HINTS, crop_hints) unless crop_hints.zero?
198
- f << feature(:WEB_DETECTION, web) unless web.zero?
199
- f
198
+ def value_features faces, landmarks, logos, labels, crop_hints, web,
199
+ object_localizations
200
+ [
201
+ value_feature(:FACE_DETECTION, faces),
202
+ value_feature(:LANDMARK_DETECTION, landmarks),
203
+ value_feature(:LOGO_DETECTION, logos),
204
+ value_feature(:LABEL_DETECTION, labels),
205
+ value_feature(:CROP_HINTS, crop_hints),
206
+ value_feature(:WEB_DETECTION, web),
207
+ value_feature(:OBJECT_LOCALIZATION, object_localizations)
208
+ ].compact
209
+ end
210
+
211
+ def value_feature type, value
212
+ return if value.zero?
213
+ feature type, value
200
214
  end
201
215
 
202
216
  def boolean_features text, document, safe_search, properties
203
- f = []
204
- f << feature(:TEXT_DETECTION, 1) if text
205
- f << feature(:DOCUMENT_TEXT_DETECTION, 1) if document
206
- f << feature(:SAFE_SEARCH_DETECTION, 1) if safe_search
207
- f << feature(:IMAGE_PROPERTIES, 1) if properties
208
- f
217
+ [
218
+ boolean_feature(:TEXT_DETECTION, text),
219
+ boolean_feature(:DOCUMENT_TEXT_DETECTION, document),
220
+ boolean_feature(:SAFE_SEARCH_DETECTION, safe_search),
221
+ boolean_feature(:IMAGE_PROPERTIES, properties)
222
+ ].compact
223
+ end
224
+
225
+ def boolean_feature type, value
226
+ return unless value
227
+ feature type, 1
209
228
  end
210
229
 
211
230
  def feature type, max_results
@@ -215,11 +234,12 @@ module Google
215
234
  end
216
235
 
217
236
  def default_features? faces, landmarks, logos, labels, text, document,
218
- safe_search, properties, crop_hints, web
237
+ safe_search, properties, crop_hints, web,
238
+ object_localizations
219
239
  faces == false && landmarks == false && logos == false &&
220
240
  labels == false && text == false && document == false &&
221
241
  safe_search == false && properties == false &&
222
- crop_hints == false && web == false
242
+ crop_hints == false && web == false && object_localizations == false
223
243
  end
224
244
 
225
245
  def default_features
@@ -235,11 +255,14 @@ module Google
235
255
  feature(:SAFE_SEARCH_DETECTION, 1),
236
256
  feature(:IMAGE_PROPERTIES, 1),
237
257
  feature(:CROP_HINTS, Google::Cloud::Vision.default_max_crop_hints),
238
- feature(:WEB_DETECTION, Google::Cloud::Vision.default_max_web)
258
+ feature(:WEB_DETECTION, Google::Cloud::Vision.default_max_web),
259
+ feature(:OBJECT_LOCALIZATION,
260
+ Google::Cloud::Vision.default_max_object_localizations)
239
261
  ]
240
262
  end
241
263
 
242
- def validate_max_args faces, landmarks, logos, labels, crop_hints, web
264
+ def validate_max_args faces, landmarks, logos, labels, crop_hints, web,
265
+ object_localizations
243
266
  faces = validate_max_value(
244
267
  faces, Google::Cloud::Vision.default_max_faces
245
268
  )
@@ -258,7 +281,15 @@ module Google
258
281
  web = validate_max_value(
259
282
  web, Google::Cloud::Vision.default_max_web
260
283
  )
261
- [faces, landmarks, logos, labels, crop_hints, web]
284
+ object_localizations = validate_max_value(
285
+ object_localizations,
286
+ Google::Cloud::Vision.default_max_object_localizations
287
+ )
288
+
289
+ [
290
+ faces, landmarks, logos, labels, crop_hints, web,
291
+ object_localizations
292
+ ]
262
293
  end
263
294
 
264
295
  def validate_max_value value, default_value
@@ -20,6 +20,7 @@ require "google/cloud/vision/annotation/safe_search"
20
20
  require "google/cloud/vision/annotation/properties"
21
21
  require "google/cloud/vision/annotation/crop_hint"
22
22
  require "google/cloud/vision/annotation/web"
23
+ require "google/cloud/vision/annotation/object_localization"
23
24
 
24
25
  module Google
25
26
  module Cloud
@@ -475,6 +476,44 @@ module Google
475
476
  !web.nil?
476
477
  end
477
478
 
479
+ ##
480
+ # The results of object localizations detection.
481
+ #
482
+ # @return [Array<ObjectLocalization>]
483
+ #
484
+ # @example
485
+ # require "google/cloud/vision"
486
+ #
487
+ # vision = Google::Cloud::Vision.new
488
+ # image = vision.image "path/to/face.jpg"
489
+ #
490
+ # annotation = vision.annotate image, object_localizations: true
491
+ # object_localizations = annotation.object_localizations
492
+ #
493
+ def object_localizations
494
+ @ol ||= @grpc.localized_object_annotations.map do |ol|
495
+ ObjectLocalization.from_grpc ol
496
+ end
497
+ end
498
+
499
+ ##
500
+ # Whether there is a result for object localizations detection.
501
+ #
502
+ # @return [Boolean]
503
+ #
504
+ # @example
505
+ # require "google/cloud/vision"
506
+ #
507
+ # vision = Google::Cloud::Vision.new
508
+ # image = vision.image "path/to/face.jpg"
509
+ #
510
+ # annotation = vision.annotate image, object_localizations: true
511
+ # annotation.object_localizations? #=> true
512
+ #
513
+ def object_localizations?
514
+ !object_localizations.empty?
515
+ end
516
+
478
517
  ##
479
518
  # Deeply converts object to a hash. All keys will be symbolized.
480
519
  #
@@ -485,16 +524,17 @@ module Google
485
524
  logos: logos.map(&:to_h), labels: labels.map(&:to_h),
486
525
  text: text.to_h, safe_search: safe_search.to_h,
487
526
  properties: properties.to_h, crop_hints: crop_hints.map(&:to_h),
488
- web: web.to_h }
527
+ web: web.to_h, object_localizations: object_localizations.to_h }
489
528
  end
490
529
 
491
530
  # @private
492
531
  def to_s
493
532
  tmplt = "(faces: %i, landmarks: %i, logos: %i, labels: %i," \
494
533
  " text: %s, safe_search: %s, properties: %s," \
495
- " crop_hints: %s, web: %s)"
534
+ " crop_hints: %s, web: %s, object_localizations: %i)"
496
535
  format tmplt, faces.count, landmarks.count, logos.count, labels.count,
497
- text?, safe_search?, properties?, crop_hints?, web?
536
+ text?, safe_search?, properties?, crop_hints?, web?,
537
+ object_localizations.count
498
538
  end
499
539
 
500
540
  # @private
@@ -0,0 +1,76 @@
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
+ module Google
17
+ module Cloud
18
+ module Vision
19
+ class Annotation
20
+ ##
21
+ # # NormalizedVertex
22
+ #
23
+ # A normalized vertex in a set of bounding polygon vertices.
24
+ #
25
+ # @attr_reader [Float] x The X coordinate.
26
+ # @attr_reader [Float] y The Y coordinate.
27
+ #
28
+ class NormalizedVertex
29
+ attr_reader :x, :y
30
+
31
+ ##
32
+ # @private Creates a new NormalizedVertex instance.
33
+ def initialize x, y
34
+ @x = x
35
+ @y = y
36
+ end
37
+
38
+ ##
39
+ # Returns the object's property values as an array.
40
+ #
41
+ # @return [Array]
42
+ #
43
+ def to_a
44
+ [x, y]
45
+ end
46
+
47
+ ##
48
+ # Converts object to a hash. All keys will be symbolized.
49
+ #
50
+ # @return [Hash]
51
+ #
52
+ def to_h
53
+ { x: x, y: y }
54
+ end
55
+
56
+ # @private
57
+ def to_s
58
+ "(x: #{x.inspect}, y: #{y.inspect})"
59
+ end
60
+
61
+ # @private
62
+ def inspect
63
+ "#<NormalizedVertex #{self}>"
64
+ end
65
+
66
+ ##
67
+ # @private New Annotation::Entity::Bounds::Vertex from a Google API
68
+ # Client object.
69
+ def self.from_grpc grpc
70
+ new grpc.x, grpc.y
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,115 @@
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
+
15
+
16
+ require "google/cloud/vision/annotation/normalized_vertex"
17
+
18
+ module Google
19
+ module Cloud
20
+ module Vision
21
+ class Annotation
22
+ class ObjectLocalization
23
+ ##
24
+ # @private The LocalizedObjectAnnotation GRPC object.
25
+ attr_accessor :grpc
26
+
27
+ ##
28
+ # @private Creates a new Web instance.
29
+ def initialize
30
+ @grpc = nil
31
+ end
32
+
33
+ ##
34
+ # Opaque entity ID. Should align with {Entity#mid}.
35
+ #
36
+ # @return [String] The opaque entity ID.
37
+ #
38
+ def mid
39
+ @grpc.mid
40
+ end
41
+
42
+ ##
43
+ # The BCP-47 language code, such as "en-US" or "sr-Latn".
44
+ #
45
+ # @see http://www.unicode.org/reports/tr35/#Unicode_locale_identifier
46
+ #
47
+ # @return [String] The language code.
48
+ #
49
+ def code
50
+ @grpc.language_code
51
+ end
52
+
53
+ ##
54
+ # Object name, expressed in its {#code} language.
55
+ #
56
+ # @return [String] The object name.
57
+ #
58
+ def name
59
+ @grpc.name
60
+ end
61
+
62
+ ##
63
+ # The score of the result. Range [0, 1].
64
+ #
65
+ # @return [Float] A value in the range [0, 1].
66
+ #
67
+ def score
68
+ @grpc.score
69
+ end
70
+
71
+ ##
72
+ # Image region to which this entity belongs.
73
+ #
74
+ # @return [Array<NormalizedVertex>] An array of normalized vertices.
75
+ #
76
+ def bounds
77
+ return [] unless @grpc.bounding_poly
78
+ @bounds ||= @grpc.bounding_poly.normalized_vertices.map do |v|
79
+ NormalizedVertex.from_grpc v
80
+ end
81
+ end
82
+
83
+ ##
84
+ # Deeply converts object to a hash. All keys will be symbolized.
85
+ #
86
+ # @return [Hash]
87
+ #
88
+ def to_h
89
+ { mid: mid, code: code, name: name, score: score,
90
+ bounds: bounds.map(&:to_h) }
91
+ end
92
+
93
+ # @private
94
+ def to_s
95
+ tmplt = "mid: %s, code: %s, name: %s, score: %s, " \
96
+ " bounds: %i"
97
+ format tmplt, mid.inspect, code.inspect, name.inspect,
98
+ score.inspect, bounds.count
99
+ end
100
+
101
+ # @private
102
+ def inspect
103
+ "#<#{self.class.name} #{self}>"
104
+ end
105
+
106
+ ##
107
+ # @private New Annotation::Face from a GRPC object.
108
+ def self.from_grpc grpc
109
+ new.tap { |ol| ol.instance_variable_set :@grpc, grpc }
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
@@ -434,6 +434,34 @@ module Google
434
434
  annotation.web
435
435
  end
436
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
+
437
465
  ##
438
466
  # Performs detection of Cloud Vision
439
467
  # [features](https://cloud.google.com/vision/reference/rest/v1/images/annotate#Feature)
@@ -480,6 +508,8 @@ module Google
480
508
  # feature. Optional.
481
509
  # @param [Boolean, Integer] web Whether to perform the web annotation
482
510
  # feature. Optional.
511
+ # @param [Boolean, Integer] object_localizations Whether to perform the
512
+ # object localizations feature. Optional.
483
513
  #
484
514
  # @return [Annotation] The results for all image detections, returned as
485
515
  # a single {Annotation} instance.
@@ -514,12 +544,13 @@ module Google
514
544
  def annotate faces: false, landmarks: false, logos: false,
515
545
  labels: false, text: false, document: false,
516
546
  safe_search: false, properties: false, crop_hints: false,
517
- web: false
547
+ web: false, object_localizations: false
518
548
  @vision.annotate(self, faces: faces, landmarks: landmarks,
519
549
  logos: logos, labels: labels, text: text,
520
550
  document: document, safe_search: safe_search,
521
551
  properties: properties, crop_hints: crop_hints,
522
- web: web)
552
+ web: web,
553
+ object_localizations: object_localizations)
523
554
  end
524
555
  alias mark annotate
525
556
  alias detect annotate
@@ -186,6 +186,8 @@ module Google
186
186
  # feature. Optional.
187
187
  # @param [Boolean, Integer] web Whether to perform the web annotation
188
188
  # feature. Optional.
189
+ # @param [Boolean, Integer] object_localizations Whether to perform the
190
+ # object localizations feature. Optional.
189
191
  #
190
192
  # @yield [annotate] A block for requests that involve multiple feature
191
193
  # configurations. See {Annotate#annotate}.
@@ -257,12 +259,13 @@ module Google
257
259
  def annotate *images, faces: false, landmarks: false, logos: false,
258
260
  labels: false, text: false, document: false,
259
261
  safe_search: false, properties: false, crop_hints: false,
260
- web: false
262
+ web: false, object_localizations: false
261
263
  a = Annotate.new self
262
264
  a.annotate(*images, faces: faces, landmarks: landmarks, logos: logos,
263
265
  labels: labels, text: text, document: document,
264
266
  safe_search: safe_search, properties: properties,
265
- crop_hints: crop_hints, web: web)
267
+ crop_hints: crop_hints, web: web,
268
+ object_localizations: object_localizations)
266
269
 
267
270
  yield a if block_given?
268
271
 
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module Vision
19
- VERSION = "0.30.4".freeze
19
+ VERSION = "0.31.0".freeze
20
20
  end
21
21
  end
22
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-cloud-vision
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.30.4
4
+ version: 0.31.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Moore
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-09-21 00:00:00.000000000 Z
12
+ date: 2018-09-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: google-cloud-core
@@ -203,6 +203,8 @@ files:
203
203
  - lib/google/cloud/vision/annotation/crop_hint.rb
204
204
  - lib/google/cloud/vision/annotation/entity.rb
205
205
  - lib/google/cloud/vision/annotation/face.rb
206
+ - lib/google/cloud/vision/annotation/normalized_vertex.rb
207
+ - lib/google/cloud/vision/annotation/object_localization.rb
206
208
  - lib/google/cloud/vision/annotation/properties.rb
207
209
  - lib/google/cloud/vision/annotation/safe_search.rb
208
210
  - lib/google/cloud/vision/annotation/text.rb