|
1 | 1 | from .visual_recognition_v3 import VisualRecognitionV3 |
| 2 | +from os.path import basename |
2 | 3 |
|
3 | 4 | class VisualRecognitionV3Adapter(VisualRecognitionV3): |
4 | 5 | def create_classifier(self, |
@@ -59,3 +60,103 @@ def update_classifier(self, |
59 | 60 | files=kwargs, |
60 | 61 | accept_json=True) |
61 | 62 | return response |
| 63 | + |
| 64 | + ######################### |
| 65 | + # General |
| 66 | + ######################### |
| 67 | + |
| 68 | + def classify(self, |
| 69 | + images_file=None, |
| 70 | + accept_language=None, |
| 71 | + url=None, |
| 72 | + threshold=None, |
| 73 | + owners=None, |
| 74 | + classifier_ids=None, |
| 75 | + images_file_content_type=None, |
| 76 | + images_filename=None, |
| 77 | + **kwargs): |
| 78 | + """ |
| 79 | + Classify images. |
| 80 | +
|
| 81 | + Classify images with built-in or custom classifiers. |
| 82 | +
|
| 83 | + :param file images_file: An image file (.jpg, .png) or .zip file with images. |
| 84 | + Maximum image size is 10 MB. Include no more than 20 images and limit the .zip |
| 85 | + file to 100 MB. Encode the image and .zip file names in UTF-8 if they contain |
| 86 | + non-ASCII characters. The service assumes UTF-8 encoding if it encounters |
| 87 | + non-ASCII characters. |
| 88 | + You can also include an image with the **url** parameter. |
| 89 | + :param str accept_language: The language of the output class names. The full set |
| 90 | + of languages is supported for the built-in classifier IDs: `default`, `food`, and |
| 91 | + `explicit`. The class names of custom classifiers are not translated. |
| 92 | + The response might not be in the specified language when the requested language is |
| 93 | + not supported or when there is no translation for the class name. |
| 94 | + :param str url: The URL of an image to analyze. Must be in .jpg, or .png format. |
| 95 | + The minimum recommended pixel density is 32X32 pixels per inch, and the maximum |
| 96 | + image size is 10 MB. |
| 97 | + You can also include images with the **images_file** parameter. |
| 98 | + :param float threshold: The minimum score a class must have to be displayed in the |
| 99 | + response. Set the threshold to `0.0` to ignore the classification score and return |
| 100 | + all values. |
| 101 | + :param list[str] owners: The categories of classifiers to apply. Use `IBM` to |
| 102 | + classify against the `default` general classifier, and use `me` to classify |
| 103 | + against your custom classifiers. To analyze the image against both classifier |
| 104 | + categories, set the value to both `IBM` and `me`. |
| 105 | + The built-in `default` classifier is used if both **classifier_ids** and |
| 106 | + **owners** parameters are empty. |
| 107 | + The **classifier_ids** parameter overrides **owners**, so make sure that |
| 108 | + **classifier_ids** is empty. |
| 109 | + :param list[str] classifier_ids: Which classifiers to apply. Overrides the |
| 110 | + **owners** parameter. You can specify both custom and built-in classifier IDs. The |
| 111 | + built-in `default` classifier is used if both **classifier_ids** and **owners** |
| 112 | + parameters are empty. |
| 113 | + The following built-in classifier IDs require no training: |
| 114 | + - `default`: Returns classes from thousands of general tags. |
| 115 | + - `food`: Enhances specificity and accuracy for images of food items. |
| 116 | + - `explicit`: Evaluates whether the image might be pornographic. |
| 117 | + :param str images_file_content_type: The content type of images_file. |
| 118 | + :param str images_filename: The filename for images_file. |
| 119 | + :param dict headers: A `dict` containing the request headers |
| 120 | + :return: A `DetailedResponse` containing the result, headers and HTTP status code. |
| 121 | + :rtype: DetailedResponse |
| 122 | + """ |
| 123 | + headers = {'Accept-Language': accept_language} |
| 124 | + if 'headers' in kwargs: |
| 125 | + headers.update(kwargs.get('headers')) |
| 126 | + params = {'version': self.version} |
| 127 | + images_file_tuple = None |
| 128 | + if images_file: |
| 129 | + if not images_filename and hasattr(images_file, 'name'): |
| 130 | + images_filename = images_file.name |
| 131 | + images_filename = basename(images_filename) |
| 132 | + mime_type = images_file_content_type or 'application/octet-stream' |
| 133 | + images_file_tuple = (images_filename, images_file, mime_type) |
| 134 | + url_tuple = None |
| 135 | + if url: |
| 136 | + url_tuple = (None, url, 'text/plain') |
| 137 | + threshold_tuple = None |
| 138 | + if threshold: |
| 139 | + threshold_tuple = (None, threshold, 'application/json') |
| 140 | + owners_tuple = None |
| 141 | + if owners: |
| 142 | + owners = self._convert_list(owners) |
| 143 | + owners_tuple = (None, owners, 'application/json') |
| 144 | + classifier_ids_tuple = None |
| 145 | + if classifier_ids: |
| 146 | + classifier_ids = self._convert_list(classifier_ids) |
| 147 | + classifier_ids_tuple = (None, classifier_ids, 'application/json') |
| 148 | + url = '/v3/classify' |
| 149 | + response = self.request( |
| 150 | + method='POST', |
| 151 | + url=url, |
| 152 | + headers=headers, |
| 153 | + params=params, |
| 154 | + files={ |
| 155 | + 'images_file': images_file_tuple, |
| 156 | + 'url': url_tuple, |
| 157 | + 'threshold': threshold_tuple, |
| 158 | + 'owners': owners_tuple, |
| 159 | + 'classifier_ids': classifier_ids_tuple |
| 160 | + }, |
| 161 | + accept_json=True) |
| 162 | + return response |
0 commit comments