|
| 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 VisualRecognitionV2Beta(WatsonDeveloperCloudService): |
| 23 | + |
| 24 | + """Client for the Visual Recognition service""" |
| 25 | + |
| 26 | + default_url = 'https://gateway.watsonplatform.net/visual-recognition-beta/api' |
| 27 | + latest_version = '2015-12-02' |
| 28 | + |
| 29 | + def __init__(self, version, url=default_url, username=None, password=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, username, password, use_vcap_services) |
| 38 | + self.version = version |
| 39 | + |
| 40 | + def list_classifiers(self, verbose=False): |
| 41 | + """ |
| 42 | + Returns a list of user-created and built-in classifiers. (May change in the future to only user-created.) |
| 43 | + :param verbose: Specifies whether to return more information about each classifier, such as the author |
| 44 | + """ |
| 45 | + |
| 46 | + params = {'verbose': verbose, 'version': self.version} |
| 47 | + return self.request(method='GET', url='/v2/classifiers', params=params, accept_json=True) |
| 48 | + |
| 49 | + def classify(self, images_file, classifier_ids=None): |
| 50 | + |
| 51 | + if isinstance(classifier_ids, list): |
| 52 | + classifier_ids = json.dumps(classifier_ids) |
| 53 | + if classifier_ids: |
| 54 | + classifier_ids = '{"classifier_ids": ' + classifier_ids + '}' |
| 55 | + |
| 56 | + params = {'version': self.version} |
| 57 | + data = {'classifier_ids': classifier_ids} |
| 58 | + # Params sent as url parameters here |
| 59 | + return self.request(method='POST', url='/v2/classify', files={'images_file': images_file}, data=data, |
| 60 | + params=params, accept_json=True) |
0 commit comments