ibm_watson 2.1.3 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,517 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # (C) Copyright IBM Corp. 2018, 2020.
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
- #
17
- # IBM OpenAPI SDK Code Generator Version: 3.38.0-07189efd-20210827-205025
18
- #
19
- # IBM Watson™ Visual Recognition is discontinued. Existing instances are supported
20
- # until 1 December 2021, but as of 7 January 2021, you can't create instances. Any
21
- # instance that is provisioned on 1 December 2021 will be deleted.
22
- # {: deprecated}
23
- #
24
- # The IBM Watson Visual Recognition service uses deep learning algorithms to identify
25
- # scenes and objects in images that you upload to the service. You can create and train a
26
- # custom classifier to identify subjects that suit your needs.
27
-
28
- require "concurrent"
29
- require "erb"
30
- require "json"
31
- require "ibm_cloud_sdk_core"
32
- require_relative "./common.rb"
33
-
34
- module IBMWatson
35
- ##
36
- # The Visual Recognition V3 service.
37
- class VisualRecognitionV3 < IBMCloudSdkCore::BaseService
38
- include Concurrent::Async
39
- DEFAULT_SERVICE_NAME = "visual_recognition"
40
- DEFAULT_SERVICE_URL = "https://api.us-south.visual-recognition.watson.cloud.ibm.com"
41
- attr_accessor :version
42
- ##
43
- # @!method initialize(args)
44
- # Construct a new client for the Visual Recognition service.
45
- #
46
- # @param args [Hash] The args to initialize with
47
- # @option args version [String] Release date of the API version you want to use. Specify dates in YYYY-MM-DD
48
- # format. The current version is `2018-03-19`.
49
- # @option args service_url [String] The base service URL to use when contacting the service.
50
- # The base service_url may differ between IBM Cloud regions.
51
- # @option args authenticator [Object] The Authenticator instance to be configured for this service.
52
- # @option args service_name [String] The name of the service to configure. Will be used as the key to load
53
- # any external configuration, if applicable.
54
- def initialize(args = {})
55
- warn "On 1 December 2021, Visual Recognition will no longer be available. For more information, see https://github.com/watson-developer-cloud/ruby-sdk/tree/master#visual-recognition-deprecation."
56
- @__async_initialized__ = false
57
- defaults = {}
58
- defaults[:service_url] = DEFAULT_SERVICE_URL
59
- defaults[:service_name] = DEFAULT_SERVICE_NAME
60
- defaults[:authenticator] = nil
61
- defaults[:version] = nil
62
- user_service_url = args[:service_url] unless args[:service_url].nil?
63
- args = defaults.merge(args)
64
- @version = args[:version]
65
- raise ArgumentError.new("version must be provided") if @version.nil?
66
-
67
- args[:authenticator] = IBMCloudSdkCore::ConfigBasedAuthenticatorFactory.new.get_authenticator(service_name: args[:service_name]) if args[:authenticator].nil?
68
- super
69
- @service_url = user_service_url unless user_service_url.nil?
70
- end
71
-
72
- #########################
73
- # General
74
- #########################
75
-
76
- ##
77
- # @!method classify(images_file: nil, images_filename: nil, images_file_content_type: nil, url: nil, threshold: nil, owners: nil, classifier_ids: nil, accept_language: nil)
78
- # Classify images.
79
- # Classify images with built-in or custom classifiers.
80
- # @param images_file [File] An image file (.gif, .jpg, .png, .tif) or .zip file with images. Maximum image
81
- # size is 10 MB. Include no more than 20 images and limit the .zip file to 100 MB.
82
- # Encode the image and .zip file names in UTF-8 if they contain non-ASCII
83
- # characters. The service assumes UTF-8 encoding if it encounters non-ASCII
84
- # characters.
85
- #
86
- # You can also include an image with the **url** parameter.
87
- # @param images_filename [String] The filename for images_file.
88
- # @param images_file_content_type [String] The content type of images_file.
89
- # @param url [String] The URL of an image (.gif, .jpg, .png, .tif) to analyze. The minimum recommended
90
- # pixel density is 32X32 pixels, but the service tends to perform better with images
91
- # that are at least 224 x 224 pixels. The maximum image size is 10 MB.
92
- #
93
- # You can also include images with the **images_file** parameter.
94
- # @param threshold [Float] The minimum score a class must have to be displayed in the response. Set the
95
- # threshold to `0.0` to return all identified classes.
96
- # @param owners [Array[String]] The categories of classifiers to apply. The **classifier_ids** parameter overrides
97
- # **owners**, so make sure that **classifier_ids** is empty.
98
- # - Use `IBM` to classify against the `default` general classifier. You get the same
99
- # result if both **classifier_ids** and **owners** parameters are empty.
100
- # - Use `me` to classify against all your custom classifiers. However, for better
101
- # performance use **classifier_ids** to specify the specific custom classifiers to
102
- # apply.
103
- # - Use both `IBM` and `me` to analyze the image against both classifier categories.
104
- # @param classifier_ids [Array[String]] Which classifiers to apply. Overrides the **owners** parameter. You can specify
105
- # both custom and built-in classifier IDs. The built-in `default` classifier is used
106
- # if both **classifier_ids** and **owners** parameters are empty.
107
- #
108
- # The following built-in classifier IDs require no training:
109
- # - `default`: Returns classes from thousands of general tags.
110
- # - `food`: Enhances specificity and accuracy for images of food items.
111
- # - `explicit`: Evaluates whether the image might be pornographic.
112
- # @param accept_language [String] The desired language of parts of the response. See the response for details.
113
- # @return [IBMCloudSdkCore::DetailedResponse] A `IBMCloudSdkCore::DetailedResponse` object representing the response.
114
- def classify(images_file: nil, images_filename: nil, images_file_content_type: nil, url: nil, threshold: nil, owners: nil, classifier_ids: nil, accept_language: nil)
115
- raise ArgumentError.new("version must be provided") if version.nil?
116
-
117
- headers = {
118
- "Accept-Language" => accept_language
119
- }
120
- sdk_headers = Common.new.get_sdk_headers("watson_vision_combined", "V3", "classify")
121
- headers.merge!(sdk_headers)
122
-
123
- params = {
124
- "version" => @version
125
- }
126
-
127
- form_data = {}
128
-
129
- unless images_file.nil?
130
- unless images_file.instance_of?(StringIO) || images_file.instance_of?(File)
131
- images_file = images_file.respond_to?(:to_json) ? StringIO.new(images_file.to_json) : StringIO.new(images_file)
132
- end
133
- images_filename = images_file.path if images_filename.nil? && images_file.respond_to?(:path)
134
- form_data[:images_file] = HTTP::FormData::File.new(images_file, content_type: images_file_content_type.nil? ? "application/octet-stream" : images_file_content_type, filename: images_filename)
135
- end
136
-
137
- classifier_ids *= "," unless classifier_ids.nil?
138
- owners *= "," unless owners.nil?
139
-
140
- form_data[:url] = HTTP::FormData::Part.new(url.to_s, content_type: "text/plain") unless url.nil?
141
-
142
- form_data[:threshold] = HTTP::FormData::Part.new(threshold.to_s, content_type: "application/json") unless threshold.nil?
143
-
144
- form_data[:owners] = HTTP::FormData::Part.new(owners, content_type: "application/json") unless owners.nil?
145
-
146
- form_data[:classifier_ids] = HTTP::FormData::Part.new(classifier_ids, content_type: "application/json") unless classifier_ids.nil?
147
-
148
- method_url = "/v3/classify"
149
-
150
- response = request(
151
- method: "POST",
152
- url: method_url,
153
- headers: headers,
154
- params: params,
155
- form: form_data,
156
- accept_json: true
157
- )
158
- response
159
- end
160
- #########################
161
- # Custom
162
- #########################
163
-
164
- ##
165
- # @!method create_classifier(name:, positive_examples:, negative_examples: nil, negative_examples_filename: nil)
166
- # Create a classifier.
167
- # Train a new multi-faceted classifier on the uploaded image data. Create your
168
- # custom classifier with positive or negative example training images. Include at
169
- # least two sets of examples, either two positive example files or one positive and
170
- # one negative file. You can upload a maximum of 256 MB per call.
171
- #
172
- # **Tips when creating:**
173
- #
174
- # - If you set the **X-Watson-Learning-Opt-Out** header parameter to `true` when you
175
- # create a classifier, the example training images are not stored. Save your
176
- # training images locally. For more information, see [Data
177
- # collection](#data-collection).
178
- #
179
- # - Encode all names in UTF-8 if they contain non-ASCII characters (.zip and image
180
- # file names, and classifier and class names). The service assumes UTF-8 encoding if
181
- # it encounters non-ASCII characters.
182
- # @param name [String] The name of the new classifier. Encode special characters in UTF-8.
183
- # @param positive_examples [File] A .zip file of images that depict the visual subject of a class in the new
184
- # classifier. You can include more than one positive example file in a call.
185
- #
186
- # Specify the parameter name by appending `_positive_examples` to the class name.
187
- # For example, `goldenretriever_positive_examples` creates the class
188
- # **goldenretriever**. The string cannot contain the following characters: ``$ * - {
189
- # } \ | / ' " ` [ ]``.
190
- #
191
- # Include at least 10 images in .jpg or .png format. The minimum recommended image
192
- # resolution is 32X32 pixels. The maximum number of images is 10,000 images or 100
193
- # MB per .zip file.
194
- #
195
- # Encode special characters in the file name in UTF-8.
196
- # @param negative_examples [File] A .zip file of images that do not depict the visual subject of any of the classes
197
- # of the new classifier. Must contain a minimum of 10 images.
198
- #
199
- # Encode special characters in the file name in UTF-8.
200
- # @param negative_examples_filename [String] The filename for negative_examples.
201
- # @return [IBMCloudSdkCore::DetailedResponse] A `IBMCloudSdkCore::DetailedResponse` object representing the response.
202
- def create_classifier(name:, positive_examples:, negative_examples: nil, negative_examples_filename: nil)
203
- raise ArgumentError.new("version must be provided") if version.nil?
204
-
205
- raise ArgumentError.new("name must be provided") if name.nil?
206
-
207
- raise ArgumentError.new("positive_examples must be a hash") unless positive_examples.is_a?(Hash)
208
- raise ArgumentError.new("positive_examples must have at least one hash entry") if positive_examples.empty?
209
-
210
- headers = {
211
- }
212
- sdk_headers = Common.new.get_sdk_headers("watson_vision_combined", "V3", "create_classifier")
213
- headers.merge!(sdk_headers)
214
-
215
- params = {
216
- "version" => @version
217
- }
218
-
219
- form_data = {}
220
-
221
- form_data[:name] = HTTP::FormData::Part.new(name.to_s, content_type: "text/plain")
222
-
223
- positive_examples&.each do |key, value|
224
- part_name = "%s_positive_examples" % key.to_s
225
- unless value.instance_of?(StringIO) || value.instance_of?(File)
226
- value = value.respond_to?(:to_json) ? StringIO.new(value.to_json) : StringIO.new(value)
227
- end
228
- filename = value.path if value.respond_to?(:path)
229
- form_data[part_name.to_sym] = HTTP::FormData::File.new(value, content_type: "application/octet-stream", filename: filename)
230
- end
231
-
232
- unless negative_examples.nil?
233
- unless negative_examples.instance_of?(StringIO) || negative_examples.instance_of?(File)
234
- negative_examples = negative_examples.respond_to?(:to_json) ? StringIO.new(negative_examples.to_json) : StringIO.new(negative_examples)
235
- end
236
- negative_examples_filename = negative_examples.path if negative_examples_filename.nil? && negative_examples.respond_to?(:path)
237
- form_data[:negative_examples] = HTTP::FormData::File.new(negative_examples, content_type: "application/octet-stream", filename: negative_examples_filename)
238
- end
239
-
240
- method_url = "/v3/classifiers"
241
-
242
- response = request(
243
- method: "POST",
244
- url: method_url,
245
- headers: headers,
246
- params: params,
247
- form: form_data,
248
- accept_json: true
249
- )
250
- response
251
- end
252
-
253
- ##
254
- # @!method list_classifiers(verbose: nil)
255
- # Retrieve a list of classifiers.
256
- # @param verbose [Boolean] Specify `true` to return details about the classifiers. Omit this parameter to
257
- # return a brief list of classifiers.
258
- # @return [IBMCloudSdkCore::DetailedResponse] A `IBMCloudSdkCore::DetailedResponse` object representing the response.
259
- def list_classifiers(verbose: nil)
260
- raise ArgumentError.new("version must be provided") if version.nil?
261
-
262
- headers = {
263
- }
264
- sdk_headers = Common.new.get_sdk_headers("watson_vision_combined", "V3", "list_classifiers")
265
- headers.merge!(sdk_headers)
266
-
267
- params = {
268
- "version" => @version,
269
- "verbose" => verbose
270
- }
271
-
272
- method_url = "/v3/classifiers"
273
-
274
- response = request(
275
- method: "GET",
276
- url: method_url,
277
- headers: headers,
278
- params: params,
279
- accept_json: true
280
- )
281
- response
282
- end
283
-
284
- ##
285
- # @!method get_classifier(classifier_id:)
286
- # Retrieve classifier details.
287
- # Retrieve information about a custom classifier.
288
- # @param classifier_id [String] The ID of the classifier.
289
- # @return [IBMCloudSdkCore::DetailedResponse] A `IBMCloudSdkCore::DetailedResponse` object representing the response.
290
- def get_classifier(classifier_id:)
291
- raise ArgumentError.new("version must be provided") if version.nil?
292
-
293
- raise ArgumentError.new("classifier_id must be provided") if classifier_id.nil?
294
-
295
- headers = {
296
- }
297
- sdk_headers = Common.new.get_sdk_headers("watson_vision_combined", "V3", "get_classifier")
298
- headers.merge!(sdk_headers)
299
-
300
- params = {
301
- "version" => @version
302
- }
303
-
304
- method_url = "/v3/classifiers/%s" % [ERB::Util.url_encode(classifier_id)]
305
-
306
- response = request(
307
- method: "GET",
308
- url: method_url,
309
- headers: headers,
310
- params: params,
311
- accept_json: true
312
- )
313
- response
314
- end
315
-
316
- ##
317
- # @!method update_classifier(classifier_id:, positive_examples: nil, negative_examples: nil, negative_examples_filename: nil)
318
- # Update a classifier.
319
- # Update a custom classifier by adding new positive or negative classes or by adding
320
- # new images to existing classes. You must supply at least one set of positive or
321
- # negative examples. For details, see [Updating custom
322
- # classifiers](https://cloud.ibm.com/docs/visual-recognition?topic=visual-recognition-customizing#updating-custom-classifiers).
323
- #
324
- # Encode all names in UTF-8 if they contain non-ASCII characters (.zip and image
325
- # file names, and classifier and class names). The service assumes UTF-8 encoding if
326
- # it encounters non-ASCII characters.
327
- #
328
- # **Tips about retraining:**
329
- #
330
- # - You can't update the classifier if the **X-Watson-Learning-Opt-Out** header
331
- # parameter was set to `true` when the classifier was created. Training images are
332
- # not stored in that case. Instead, create another classifier. For more information,
333
- # see [Data collection](#data-collection).
334
- #
335
- # - Don't make retraining calls on a classifier until the status is ready. When you
336
- # submit retraining requests in parallel, the last request overwrites the previous
337
- # requests. The `retrained` property shows the last time the classifier retraining
338
- # finished.
339
- # @param classifier_id [String] The ID of the classifier.
340
- # @param positive_examples [File] A .zip file of images that depict the visual subject of a class in the classifier.
341
- # The positive examples create or update classes in the classifier. You can include
342
- # more than one positive example file in a call.
343
- #
344
- # Specify the parameter name by appending `_positive_examples` to the class name.
345
- # For example, `goldenretriever_positive_examples` creates the class
346
- # `goldenretriever`. The string cannot contain the following characters: ``$ * - { }
347
- # \ | / ' " ` [ ]``.
348
- #
349
- # Include at least 10 images in .jpg or .png format. The minimum recommended image
350
- # resolution is 32X32 pixels. The maximum number of images is 10,000 images or 100
351
- # MB per .zip file.
352
- #
353
- # Encode special characters in the file name in UTF-8.
354
- # @param negative_examples [File] A .zip file of images that do not depict the visual subject of any of the classes
355
- # of the new classifier. Must contain a minimum of 10 images.
356
- #
357
- # Encode special characters in the file name in UTF-8.
358
- # @param negative_examples_filename [String] The filename for negative_examples.
359
- # @return [IBMCloudSdkCore::DetailedResponse] A `IBMCloudSdkCore::DetailedResponse` object representing the response.
360
- def update_classifier(classifier_id:, positive_examples: nil, negative_examples: nil, negative_examples_filename: nil)
361
- raise ArgumentError.new("version must be provided") if version.nil?
362
-
363
- raise ArgumentError.new("classifier_id must be provided") if classifier_id.nil?
364
-
365
- raise ArgumentError.new("positive_examples must be a hash") unless positive_examples.nil? || positive_examples.is_a?(Hash)
366
-
367
- headers = {
368
- }
369
- sdk_headers = Common.new.get_sdk_headers("watson_vision_combined", "V3", "update_classifier")
370
- headers.merge!(sdk_headers)
371
-
372
- params = {
373
- "version" => @version
374
- }
375
-
376
- form_data = {}
377
-
378
- positive_examples&.each do |key, value|
379
- part_name = "%s_positive_examples" % key.to_s
380
- unless value.instance_of?(StringIO) || value.instance_of?(File)
381
- value = value.respond_to?(:to_json) ? StringIO.new(value.to_json) : StringIO.new(value)
382
- end
383
- filename = value.path if value.respond_to?(:path)
384
- form_data[part_name.to_sym] = HTTP::FormData::File.new(value, content_type: "application/octet-stream", filename: filename)
385
- end
386
-
387
- unless negative_examples.nil?
388
- unless negative_examples.instance_of?(StringIO) || negative_examples.instance_of?(File)
389
- negative_examples = negative_examples.respond_to?(:to_json) ? StringIO.new(negative_examples.to_json) : StringIO.new(negative_examples)
390
- end
391
- negative_examples_filename = negative_examples.path if negative_examples_filename.nil? && negative_examples.respond_to?(:path)
392
- form_data[:negative_examples] = HTTP::FormData::File.new(negative_examples, content_type: "application/octet-stream", filename: negative_examples_filename)
393
- end
394
-
395
- method_url = "/v3/classifiers/%s" % [ERB::Util.url_encode(classifier_id)]
396
-
397
- response = request(
398
- method: "POST",
399
- url: method_url,
400
- headers: headers,
401
- params: params,
402
- form: form_data,
403
- accept_json: true
404
- )
405
- response
406
- end
407
-
408
- ##
409
- # @!method delete_classifier(classifier_id:)
410
- # Delete a classifier.
411
- # @param classifier_id [String] The ID of the classifier.
412
- # @return [nil]
413
- def delete_classifier(classifier_id:)
414
- raise ArgumentError.new("version must be provided") if version.nil?
415
-
416
- raise ArgumentError.new("classifier_id must be provided") if classifier_id.nil?
417
-
418
- headers = {
419
- }
420
- sdk_headers = Common.new.get_sdk_headers("watson_vision_combined", "V3", "delete_classifier")
421
- headers.merge!(sdk_headers)
422
-
423
- params = {
424
- "version" => @version
425
- }
426
-
427
- method_url = "/v3/classifiers/%s" % [ERB::Util.url_encode(classifier_id)]
428
-
429
- request(
430
- method: "DELETE",
431
- url: method_url,
432
- headers: headers,
433
- params: params,
434
- accept_json: true
435
- )
436
- nil
437
- end
438
- #########################
439
- # Core ML
440
- #########################
441
-
442
- ##
443
- # @!method get_core_ml_model(classifier_id:)
444
- # Retrieve a Core ML model of a classifier.
445
- # Download a Core ML model file (.mlmodel) of a custom classifier that returns
446
- # <tt>"core_ml_enabled": true</tt> in the classifier details.
447
- # @param classifier_id [String] The ID of the classifier.
448
- # @return [IBMCloudSdkCore::DetailedResponse] A `IBMCloudSdkCore::DetailedResponse` object representing the response.
449
- def get_core_ml_model(classifier_id:)
450
- raise ArgumentError.new("version must be provided") if version.nil?
451
-
452
- raise ArgumentError.new("classifier_id must be provided") if classifier_id.nil?
453
-
454
- headers = {
455
- }
456
- sdk_headers = Common.new.get_sdk_headers("watson_vision_combined", "V3", "get_core_ml_model")
457
- headers.merge!(sdk_headers)
458
-
459
- params = {
460
- "version" => @version
461
- }
462
-
463
- method_url = "/v3/classifiers/%s/core_ml_model" % [ERB::Util.url_encode(classifier_id)]
464
-
465
- response = request(
466
- method: "GET",
467
- url: method_url,
468
- headers: headers,
469
- params: params,
470
- accept_json: false
471
- )
472
- response
473
- end
474
- #########################
475
- # User data
476
- #########################
477
-
478
- ##
479
- # @!method delete_user_data(customer_id:)
480
- # Delete labeled data.
481
- # Deletes all data associated with a specified customer ID. The method has no effect
482
- # if no data is associated with the customer ID.
483
- #
484
- # You associate a customer ID with data by passing the `X-Watson-Metadata` header
485
- # with a request that passes data. For more information about personal data and
486
- # customer IDs, see [Information
487
- # security](https://cloud.ibm.com/docs/visual-recognition?topic=visual-recognition-information-security).
488
- # @param customer_id [String] The customer ID for which all data is to be deleted.
489
- # @return [nil]
490
- def delete_user_data(customer_id:)
491
- raise ArgumentError.new("version must be provided") if version.nil?
492
-
493
- raise ArgumentError.new("customer_id must be provided") if customer_id.nil?
494
-
495
- headers = {
496
- }
497
- sdk_headers = Common.new.get_sdk_headers("watson_vision_combined", "V3", "delete_user_data")
498
- headers.merge!(sdk_headers)
499
-
500
- params = {
501
- "version" => @version,
502
- "customer_id" => customer_id
503
- }
504
-
505
- method_url = "/v3/user_data"
506
-
507
- request(
508
- method: "DELETE",
509
- url: method_url,
510
- headers: headers,
511
- params: params,
512
- accept_json: true
513
- )
514
- nil
515
- end
516
- end
517
- end