|
| 1 | +# Copyright 2015 IBM 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 | +The v1 Visual Recognition service |
| 16 | +(https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/visual-recognition.html) |
| 17 | +""" |
| 18 | +import json |
| 19 | +from .watson_developer_cloud_service import WatsonDeveloperCloudService |
| 20 | + |
| 21 | + |
| 22 | +class VisualRecognitionV3(WatsonDeveloperCloudService): |
| 23 | + |
| 24 | + """Client for the Visual Recognition service""" |
| 25 | + |
| 26 | + default_url = 'https://gateway-a.watsonplatform.net/visual-recognition/api' |
| 27 | + latest_version = '2016-05-19' |
| 28 | + |
| 29 | + def __init__(self, version, url=default_url, api_key=None, use_vcap_services=True): |
| 30 | + """ |
| 31 | + Construct an instance. Fetches service parameters from VCAP_SERVICES |
| 32 | + runtime variable for Bluemix, or it defaults to local URLs. |
| 33 | + :param version: specifies the specific version-date of the service to use |
| 34 | + """ |
| 35 | + |
| 36 | + WatsonDeveloperCloudService.__init__( |
| 37 | + self, 'visual_recognition', url, api_key, use_vcap_services) |
| 38 | + self.version = version |
| 39 | + |
| 40 | + def get_classifier(self, classifier_id): |
| 41 | + """ |
| 42 | + Retrieves information about a specific classifier. |
| 43 | + :param classifier_id: The classifier id |
| 44 | + """ |
| 45 | + |
| 46 | + params = {'version': self.version} |
| 47 | + return self.request(method='GET', url='/v2/classifiers/{0}'.format(classifier_id), params=params, |
| 48 | + accept_json=True) |
| 49 | + |
| 50 | + def delete_classifier(self, classifier_id): |
| 51 | + """ |
| 52 | + Deletes a custom classifier with the specified classifier id. |
| 53 | + :param classifier_id: The classifier id |
| 54 | + """ |
| 55 | + |
| 56 | + params = {'version': self.version} |
| 57 | + return self.request(method='DELETE', url='/v2/classifiers/{0}'.format(classifier_id), params=params, |
| 58 | + accept_json=True) |
| 59 | + |
| 60 | + def list_classifiers(self, verbose=False): |
| 61 | + """ |
| 62 | + Returns a list of user-created and built-in classifiers. (May change in the future to only user-created.) |
| 63 | + :param verbose: Specifies whether to return more information about each classifier, such as the author |
| 64 | + """ |
| 65 | + |
| 66 | + params = {'verbose': verbose, 'version': self.version} |
| 67 | + return self.request(method='GET', url='/v2/classifiers', params=params, accept_json=True) |
| 68 | + |
| 69 | + def create_classifier(self, name, positive_examples, negative_examples, **kwargs): |
| 70 | + """ |
| 71 | + Train a new classifier from example images which are uploaded. |
| 72 | + :param name: The desired short name of the new classifier. |
| 73 | + :param positive_examples: A zip file of images that depict the subject of the new classifier. |
| 74 | + :param negative_examples: A zip file of images that do not depict the subject of the new classifier. |
| 75 | + :return: |
| 76 | + """ |
| 77 | + |
| 78 | + params = {'version': self.version} |
| 79 | + data = {'name': name} |
| 80 | + # Params sent as url parameters here |
| 81 | + return self.request(method='POST', url='/v3/classifiers', files={'positive_examples': positive_examples, |
| 82 | + 'negative_examples': negative_examples}, |
| 83 | + data=data, params=params, accept_json=True) |
| 84 | + |
| 85 | + def classify(self, images_file, classifier_ids=None): |
| 86 | + """ |
| 87 | + Returns a list of classification scores for one or more input images. |
| 88 | + :param images_file: An image file or zip file of image files to analyze. |
| 89 | + :param classifier_ids: The ids of classifiers to consider. When absence, considers all classifiers. |
| 90 | + :return: |
| 91 | + """ |
| 92 | + |
| 93 | + if isinstance(classifier_ids, list): |
| 94 | + classifier_ids = json.dumps(classifier_ids) |
| 95 | + if classifier_ids: |
| 96 | + classifier_ids = '{"classifier_ids": ' + classifier_ids + '}' |
| 97 | + |
| 98 | + params = {'version': self.version} |
| 99 | + data = {'classifier_ids': classifier_ids} |
| 100 | + # Params sent as url parameters here |
| 101 | + return self.request(method='POST', url='/v3/classify', files={'images_file': images_file}, data=data, |
| 102 | + params=params, accept_json=True) |
0 commit comments