gcloud 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,112 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
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
+ # http://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 Gcloud
17
+ module Vision
18
+ ##
19
+ # # Location
20
+ #
21
+ # A latitude/longitude pair with values conforming to the [WGS84
22
+ # standard](http://www.unoosa.org/pdf/icg/2012/template/WGS_84.pdf).
23
+ #
24
+ # @attr [Float] latitude The degrees latitude conforming to the [WGS84
25
+ # standard](http://www.unoosa.org/pdf/icg/2012/template/WGS_84.pdf).
26
+ # @attr [Float] longitude The degrees longitude conforming to the [WGS84
27
+ # standard](http://www.unoosa.org/pdf/icg/2012/template/WGS_84.pdf).
28
+ #
29
+ # @example
30
+ # require "gcloud"
31
+ #
32
+ # gcloud = Gcloud.new
33
+ # vision = gcloud.vision
34
+ #
35
+ # image = vision.image "path/to/landmark.jpg"
36
+ # entity = image.landmark
37
+ #
38
+ # location = entity.locations.first
39
+ #
40
+ # location.latitude #=> 43.878264
41
+ # location.longitude #=> -103.45700740814209
42
+ #
43
+ class Location
44
+ attr_accessor :latitude, :longitude
45
+
46
+ ##
47
+ # @private Creates a new Location instance.
48
+ def initialize latitude, longitude
49
+ @latitude = latitude
50
+ @longitude = longitude
51
+ end
52
+
53
+ ##
54
+ # Returns the object's property values as an array.
55
+ #
56
+ # @return [Array]
57
+ #
58
+ def to_a
59
+ to_ary
60
+ end
61
+
62
+ ##
63
+ # Returns the object's property values as an array.
64
+ #
65
+ # @return [Array]
66
+ #
67
+ def to_ary
68
+ [latitude, longitude]
69
+ end
70
+
71
+ ##
72
+ # Converts object to a hash. All keys will be symbolized.
73
+ #
74
+ # @return [Hash]
75
+ #
76
+ def to_h
77
+ to_hash
78
+ end
79
+
80
+ ##
81
+ # Converts object to a hash. All keys will be symbolized.
82
+ #
83
+ # @return [Hash]
84
+ #
85
+ def to_hash
86
+ { latitude: latitude, longitude: longitude }
87
+ end
88
+
89
+ # @private
90
+ def to_s
91
+ "(latitude: #{latitude.inspect}, longitude: #{longitude.inspect})"
92
+ end
93
+
94
+ # @private
95
+ def inspect
96
+ "#<#{self.class.name} #{self}>"
97
+ end
98
+
99
+ ##
100
+ # @private New Google API Client LatLng object.
101
+ def to_gapi
102
+ to_hash
103
+ end
104
+
105
+ ##
106
+ # @private New Location from a Google API Client LatLng object.
107
+ def self.from_gapi gapi
108
+ new gapi["latitude"], gapi["longitude"]
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,280 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
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
+ # http://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 "gcloud/gce"
17
+ require "gcloud/vision/connection"
18
+ require "gcloud/vision/credentials"
19
+ require "gcloud/vision/annotate"
20
+ require "gcloud/vision/image"
21
+ require "gcloud/vision/annotation"
22
+ require "gcloud/vision/errors"
23
+
24
+ module Gcloud
25
+ module Vision
26
+ ##
27
+ # # Project
28
+ #
29
+ # Google Cloud Vision allows easy integration of vision detection features
30
+ # within developer applications, including image labeling, face and landmark
31
+ # detection, optical character recognition (OCR), and tagging of explicit
32
+ # content.
33
+ #
34
+ # @example
35
+ # require "gcloud"
36
+ #
37
+ # gcloud = Gcloud.new
38
+ # vision = gcloud.vision
39
+ #
40
+ # image = vision.image "path/to/landmark.jpg"
41
+ #
42
+ # annotation = vision.annotate image, labels: true
43
+ #
44
+ # annotation.labels.map &:description
45
+ # #=> ["stone carving", "ancient history", "statue", "sculpture",
46
+ # #=> "monument", "landmark"]
47
+ #
48
+ # See Gcloud#vision
49
+ class Project
50
+ ##
51
+ # @private The Connection object.
52
+ attr_accessor :connection
53
+
54
+ ##
55
+ # @private Creates a new Project instance.
56
+ def initialize project, credentials
57
+ project = project.to_s # Always cast to a string
58
+ fail ArgumentError, "project is missing" if project.empty?
59
+ @connection = Connection.new project, credentials
60
+ end
61
+
62
+ # The Vision project connected to.
63
+ #
64
+ # @example
65
+ # require "gcloud"
66
+ #
67
+ # gcloud = Gcloud.new "my-todo-project",
68
+ # "/path/to/keyfile.json"
69
+ # vision = gcloud.vision
70
+ #
71
+ # vision.project #=> "my-todo-project"
72
+ #
73
+ def project
74
+ connection.project
75
+ end
76
+
77
+ ##
78
+ # @private Default project.
79
+ def self.default_project
80
+ ENV["VISION_PROJECT"] ||
81
+ ENV["GCLOUD_PROJECT"] ||
82
+ ENV["GOOGLE_CLOUD_PROJECT"] ||
83
+ Gcloud::GCE.project_id
84
+ end
85
+
86
+ ##
87
+ # Returns a new image from the given source.
88
+ #
89
+ # Cloud Vision sets upper limits on file size as well as on the total
90
+ # combined size of all images in a request. Reducing your file size can
91
+ # significantly improve throughput; however, be careful not to reduce
92
+ # image quality in the process. See [Best Practices - Image
93
+ # Sizing](https://cloud.google.com/vision/docs/image-best-practices#image_sizing)
94
+ # for current file size limits.
95
+ #
96
+ # Note that an object in Google Cloud Storage is a single entity;
97
+ # permissions affect only that object. "Directory permissions" do not
98
+ # exist (though default bucket permissions do exist). Make sure the code
99
+ # which performs your request has access to that image.
100
+ #
101
+ # @see https://cloud.google.com/vision/docs/image-best-practices Best
102
+ # Practices
103
+ #
104
+ # @param [String, IO, StringIO, Tempfile, Gcloud::Storage::File] source A
105
+ # string file path or Cloud Storage URI of the form
106
+ # `"gs://bucketname/path/to/image_filename"`; or a File, IO, StringIO,
107
+ # or Tempfile instance; or an instance of Gcloud::Storage::File.
108
+ #
109
+ # @return [Image] An image for the Vision service.
110
+ #
111
+ # @example With a Google Cloud Storage URI:
112
+ # require "gcloud"
113
+ #
114
+ # gcloud = Gcloud.new
115
+ # vision = gcloud.vision
116
+ #
117
+ # image = vision.image "gs://bucket-name/path_to_image_object"
118
+ #
119
+ # @example With a local file path:
120
+ # require "gcloud"
121
+ #
122
+ # gcloud = Gcloud.new
123
+ # vision = gcloud.vision
124
+ #
125
+ # image = vision.image "path/to/landmark.jpg"
126
+ #
127
+ def image source
128
+ return source if source.is_a? Image
129
+ Image.from_source source, self
130
+ end
131
+
132
+ ##
133
+ # Performs detection of Cloud Vision [features](https://cloud.google.com/vision/reference/rest/v1/images/annotate#Feature)
134
+ # on the given image(s). If no options for features are provided, **all**
135
+ # image detection features will be performed, with a default of `10`
136
+ # results for faces, landmarks, logos, and labels. If any feature option
137
+ # is provided, only the specified feature detections will be performed.
138
+ # Please review [Pricing](https://cloud.google.com/vision/docs/pricing)
139
+ # before use, as a separate charge is incurred for each feature performed
140
+ # on an image.
141
+ #
142
+ # Cloud Vision sets upper limits on file size as well as on the total
143
+ # combined size of all images in a request. Reducing your file size can
144
+ # significantly improve throughput; however, be careful not to reduce
145
+ # image quality in the process. See [Best Practices - Image
146
+ # Sizing](https://cloud.google.com/vision/docs/image-best-practices#image_sizing)
147
+ # for current file size limits.
148
+ #
149
+ # @see https://cloud.google.com/vision/docs/requests-and-responses Cloud
150
+ # Vision API Requests and Responses
151
+ # @see https://cloud.google.com/vision/reference/rest/v1/images/annotate#AnnotateImageRequest
152
+ # AnnotateImageRequest
153
+ # @see https://cloud.google.com/vision/docs/pricing Cloud Vision Pricing
154
+ #
155
+ # @param [Image, Object] images The image or images to annotate. This can
156
+ # be an {Image} instance, or any other type that converts to an {Image}.
157
+ # See {#image} for details.
158
+ # @param [Boolean, Integer] faces Whether to perform the facial detection
159
+ # feature. The maximum number of results is configured in
160
+ # {Gcloud::Vision.default_max_faces}, or may be provided here. Optional.
161
+ # @param [Boolean, Integer] landmarks Whether to perform the landmark
162
+ # detection feature. The maximum number of results is configured in
163
+ # {Gcloud::Vision.default_max_landmarks}, or may be provided here.
164
+ # Optional.
165
+ # @param [Boolean, Integer] logos Whether to perform the logo detection
166
+ # feature. The maximum number of results is configured in
167
+ # {Gcloud::Vision.default_max_logos}, or may be provided here. Optional.
168
+ # @param [Boolean, Integer] labels Whether to perform the label detection
169
+ # feature. The maximum number of results is configured in
170
+ # {Gcloud::Vision.default_max_labels}, or may be provided here.
171
+ # Optional.
172
+ # @param [Boolean] text Whether to perform the text (OCR) feature.
173
+ # Optional.
174
+ # @param [Boolean] safe_search Whether to perform the safe search feature.
175
+ # Optional.
176
+ # @param [Boolean] properties Whether to perform the image properties
177
+ # feature (currently, the image's dominant colors.) Optional.
178
+ #
179
+ # @yield [annotate] A block for requests that involve multiple feature
180
+ # configurations. See {Annotate#annotate}.
181
+ # @yieldparam [Annotate] annotate the Annotate object
182
+ #
183
+ # @return [Annotation, Array<Annotation>] The results for all image
184
+ # detections, returned as a single {Annotation} instance for one image,
185
+ # or as an array of {Annotation} instances, one per image, for multiple
186
+ # images.
187
+ #
188
+ # @example With a single image:
189
+ # require "gcloud"
190
+ #
191
+ # gcloud = Gcloud.new
192
+ # vision = gcloud.vision
193
+ #
194
+ # image = vision.image "path/to/landmark.jpg"
195
+ #
196
+ # annotation = vision.annotate image, labels: true
197
+ #
198
+ # annotation.labels.map &:description
199
+ # #=> ["stone carving", "ancient history", "statue", "sculpture",
200
+ # #=> "monument", "landmark"]
201
+ #
202
+ # @example With multiple images:
203
+ # require "gcloud"
204
+ #
205
+ # gcloud = Gcloud.new
206
+ # vision = gcloud.vision
207
+ #
208
+ # face_image = vision.image "path/to/face.jpg"
209
+ # landmark_image = vision.image "path/to/landmark.jpg"
210
+ #
211
+ # annotations = vision.annotate face_image, landmark_image, labels: true
212
+ #
213
+ # annotations[0].labels.count #=> 4
214
+ # annotations[1].labels.count #=> 6
215
+ #
216
+ # @example With multiple images and configurations passed in a block:
217
+ # require "gcloud"
218
+ #
219
+ # gcloud = Gcloud.new
220
+ # vision = gcloud.vision
221
+ #
222
+ # face_image = vision.image "path/to/face.jpg"
223
+ # landmark_image = vision.image "path/to/landmark.jpg"
224
+ # text_image = vision.image "path/to/text.png"
225
+ #
226
+ # annotations = vision.annotate do |annotate|
227
+ # annotate.annotate face_image, faces: true, labels: true
228
+ # annotate.annotate landmark_image, landmarks: true
229
+ # annotate.annotate text_image, text: true
230
+ # end
231
+ #
232
+ # annotations[0].faces.count #=> 1
233
+ # annotations[0].labels.count #=> 4
234
+ # annotations[1].landmarks.count #=> 1
235
+ # annotations[2].text.words.count #=> 28
236
+ #
237
+ # @example Maximum result values can also be provided:
238
+ # require "gcloud"
239
+ #
240
+ # gcloud = Gcloud.new
241
+ # vision = gcloud.vision
242
+ #
243
+ # image = vision.image "path/to/landmark.jpg"
244
+ #
245
+ # annotation = vision.annotate image, labels: 3
246
+ #
247
+ # annotation.labels.map &:description
248
+ # #=> ["stone carving", "ancient history", "statue"]
249
+ #
250
+ def annotate *images, faces: false, landmarks: false, logos: false,
251
+ labels: false, text: false, safe_search: false,
252
+ properties: false
253
+ a = Annotate.new self
254
+ a.annotate(*images, faces: faces, landmarks: landmarks, logos: logos,
255
+ labels: labels, text: text,
256
+ safe_search: safe_search, properties: properties)
257
+
258
+ yield a if block_given?
259
+
260
+ resp = connection.annotate a.requests
261
+ fail ApiError.from_response(resp) unless resp.success?
262
+ annotations = Array(resp.data["responses"]).map do |gapi|
263
+ Annotation.from_gapi gapi
264
+ end
265
+ return annotations.first if annotations.count == 1
266
+ annotations
267
+ end
268
+ alias_method :mark, :annotate
269
+ alias_method :detect, :annotate
270
+
271
+ protected
272
+
273
+ ##
274
+ # Raise an error unless an active connection is available.
275
+ def ensure_connection!
276
+ fail "Must have active connection" unless connection
277
+ end
278
+ end
279
+ end
280
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gcloud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Silvano Luciani
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-05-11 00:00:00.000000000 Z
13
+ date: 2016-05-19 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: grpc
@@ -30,16 +30,16 @@ dependencies:
30
30
  name: google-protobuf
31
31
  requirement: !ruby/object:Gem::Requirement
32
32
  requirements:
33
- - - ~>
33
+ - - '='
34
34
  - !ruby/object:Gem::Version
35
- version: 3.0.0.alpha.5.0.5
35
+ version: 3.0.0.alpha.5.0.5.1
36
36
  type: :runtime
37
37
  prerelease: false
38
38
  version_requirements: !ruby/object:Gem::Requirement
39
39
  requirements:
40
- - - ~>
40
+ - - '='
41
41
  - !ruby/object:Gem::Version
42
- version: 3.0.0.alpha.5.0.5
42
+ version: 3.0.0.alpha.5.0.5.1
43
43
  - !ruby/object:Gem::Dependency
44
44
  name: google-api-client
45
45
  requirement: !ruby/object:Gem::Requirement
@@ -378,6 +378,21 @@ files:
378
378
  - lib/gcloud/translate/translation.rb
379
379
  - lib/gcloud/upload.rb
380
380
  - lib/gcloud/version.rb
381
+ - lib/gcloud/vision.rb
382
+ - lib/gcloud/vision/annotate.rb
383
+ - lib/gcloud/vision/annotation.rb
384
+ - lib/gcloud/vision/annotation/entity.rb
385
+ - lib/gcloud/vision/annotation/face.rb
386
+ - lib/gcloud/vision/annotation/properties.rb
387
+ - lib/gcloud/vision/annotation/safe_search.rb
388
+ - lib/gcloud/vision/annotation/text.rb
389
+ - lib/gcloud/vision/annotation/vertex.rb
390
+ - lib/gcloud/vision/connection.rb
391
+ - lib/gcloud/vision/credentials.rb
392
+ - lib/gcloud/vision/errors.rb
393
+ - lib/gcloud/vision/image.rb
394
+ - lib/gcloud/vision/location.rb
395
+ - lib/gcloud/vision/project.rb
381
396
  - lib/google/api/annotations.rb
382
397
  - lib/google/api/http.rb
383
398
  - lib/google/api/label.rb