|
| 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 v3 Visual Recognition service |
| 16 | +(http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/visual-recognition/) |
| 17 | +""" |
| 18 | +import json |
| 19 | +import mimetypes |
| 20 | + |
| 21 | +from .watson_developer_cloud_service import WatsonDeveloperCloudService |
| 22 | + |
| 23 | + |
| 24 | +class VisualRecognitionV3(WatsonDeveloperCloudService): |
| 25 | + """Client for the Visual Recognition service""" |
| 26 | + |
| 27 | + default_url = 'https://gateway-a.watsonplatform.net/visual-recognition/api' |
| 28 | + latest_version = '2016-05-20' |
| 29 | + |
| 30 | + def __init__(self, version, url=default_url, use_vcap_services=True, api_key=None): |
| 31 | + """ |
| 32 | + Construct an instance. Fetches service parameters from VCAP_SERVICES |
| 33 | + runtime variable for Bluemix, or it defaults to local URLs. |
| 34 | + :param version: specifies the specific version-date of the service to use |
| 35 | + """ |
| 36 | + |
| 37 | + WatsonDeveloperCloudService.__init__( |
| 38 | + self, 'watson_vision_combined', url, None, None, use_vcap_services, api_key) |
| 39 | + self.version = version |
| 40 | + |
| 41 | + def get_classifier(self, classifier_id): |
| 42 | + """ |
| 43 | + Retrieves information about a specific classifier. |
| 44 | + :param classifier_id: The classifier id |
| 45 | + """ |
| 46 | + |
| 47 | + params = {'version': self.version} |
| 48 | + return self.request(method='GET', url='/v3/classifiers/{0}'.format(classifier_id), params=params, |
| 49 | + accept_json=True) |
| 50 | + |
| 51 | + def delete_classifier(self, classifier_id): |
| 52 | + """ |
| 53 | + Deletes a custom classifier with the specified classifier id. |
| 54 | + :param classifier_id: The classifier id |
| 55 | + """ |
| 56 | + |
| 57 | + params = {'version': self.version} |
| 58 | + return self.request(method='DELETE', url='/v3/classifiers/{0}'.format(classifier_id), params=params, |
| 59 | + accept_json=True) |
| 60 | + |
| 61 | + def list_classifiers(self, verbose=False): |
| 62 | + """ |
| 63 | + Returns a list of user-created and built-in classifiers. (May change in the future to only user-created.) |
| 64 | + :param verbose: Specifies whether to return more information about each classifier, such as the author |
| 65 | + """ |
| 66 | + |
| 67 | + params = {'verbose': verbose, 'version': self.version} |
| 68 | + return self.request(method='GET', url='/v3/classifiers', params=params, accept_json=True) |
| 69 | + |
| 70 | + def create_classifier(self, name, **kwargs): |
| 71 | + """ |
| 72 | + Train a new classifier from example images which are uploaded. |
| 73 | + :param name: The desired short name of the new classifier. |
| 74 | + :param <NAME>_positive_examples: Up to 5 zip files of images that depict the subject of the new classifier. |
| 75 | + :param negative_examples: A zip file of images that do not depict the subject of the new classifier. |
| 76 | + :return: |
| 77 | + """ |
| 78 | + |
| 79 | + params = {'version': self.version} |
| 80 | + data = {'name': name} |
| 81 | + # Params sent as url parameters here |
| 82 | + return self.request(method='POST', url='/v3/classifiers', files=kwargs, data=data, params=params, |
| 83 | + accept_json=True) |
| 84 | + |
| 85 | + def _image_call(self, url, images_file=None, images_url=None, params=None): |
| 86 | + if images_file is None and images_url is None: |
| 87 | + raise AssertionError('You must specify either a file or a url') |
| 88 | + |
| 89 | + if images_url: |
| 90 | + params['url'] = images_url |
| 91 | + return self.request(method='GET', url=url, params=params, accept_json=True) |
| 92 | + else: |
| 93 | + filename = images_file.name |
| 94 | + mime_type = mimetypes.guess_type(filename)[0] or 'application/octet-stream' |
| 95 | + return self.request(method='POST', url=url, |
| 96 | + files={'images_file': (filename, images_file, mime_type)}, params=params, |
| 97 | + accept_json=True) |
| 98 | + |
| 99 | + def classify(self, images_file=None, images_url=None, classifier_ids=None, owners=None, threshold=None): |
| 100 | + """ |
| 101 | + Returns a list of classification scores for one or more input images. |
| 102 | + :param images_file: An image file or zip file of image files to analyze. |
| 103 | + :param images_url: The url for an image file or zip file of images to analyze. |
| 104 | + :param classifier_ids: The ids of classifiers to consider. When absent, considers all classifiers. |
| 105 | + :return: |
| 106 | + """ |
| 107 | + |
| 108 | + if isinstance(classifier_ids, list): |
| 109 | + classifier_ids = ','.join(classifier_ids) |
| 110 | + if isinstance(owners, list): |
| 111 | + owners = ','.join(owners) |
| 112 | + |
| 113 | + params = {'version': self.version, 'classifier_ids': classifier_ids, 'owners': owners, 'threshold': threshold} |
| 114 | + return self._image_call('/v3/classify', images_file, images_url, params) |
| 115 | + |
| 116 | + def detect_faces(self, images_file=None, images_url=None): |
| 117 | + """ |
| 118 | + Returns a list of faces detected. This includes identities for famous people. |
| 119 | + :param images_file: An image file or zip file of image files to analyze. |
| 120 | + :param images_url: The url for an image file or zip file of images to analyze. |
| 121 | + :return: |
| 122 | + """ |
| 123 | + |
| 124 | + params = {'version': self.version} |
| 125 | + return self._image_call('/v3/detect_faces', images_file, images_url, params) |
| 126 | + |
| 127 | + def recognize_text(self, images_file=None, images_url=None): |
| 128 | + """ |
| 129 | + Returns a list of recognized text |
| 130 | + :param images_file: An image file or zip file of image files to analyze. |
| 131 | + :param images_url: The url for an image file or zip file of images to analyze. |
| 132 | + :return: |
| 133 | + """ |
| 134 | + |
| 135 | + params = {'version': self.version} |
| 136 | + return self._image_call('/v3/recognize_text', images_file, images_url, params) |
0 commit comments