Skip to content

Commit e2fa657

Browse files
committed
feat(VR): New param accept_language in detect_faces
1 parent 658b7ad commit e2fa657

File tree

1 file changed

+51
-32
lines changed

1 file changed

+51
-32
lines changed

watson_developer_cloud/visual_recognition_v3.py

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,8 @@ def classify(self,
111111
non-ASCII characters. The service assumes UTF-8 encoding if it encounters
112112
non-ASCII characters.
113113
You can also include an image with the **url** parameter.
114-
:param str accept_language: The language of the output class names. The full set
115-
of languages is supported for the built-in classifier IDs: `default`, `food`, and
116-
`explicit`. The class names of custom classifiers are not translated.
117-
The response might not be in the specified language when the requested language is
118-
not supported or when there is no translation for the class name.
114+
:param str accept_language: The desired language of parts of the response. See the
115+
response for details.
119116
:param str url: The URL of an image to analyze. Must be in .jpg, or .png format.
120117
The minimum recommended pixel density is 32X32 pixels per inch, and the maximum
121118
image size is 10 MB.
@@ -188,6 +185,7 @@ def classify(self,
188185
def detect_faces(self,
189186
images_file=None,
190187
url=None,
188+
accept_language=None,
191189
images_file_content_type=None,
192190
images_filename=None,
193191
**kwargs):
@@ -198,7 +196,7 @@ def detect_faces(self,
198196
to the Face model was removed. The identity information refers to the `name` of
199197
the person, `score`, and `type_hierarchy` knowledge graph. For details about the
200198
enhanced Face model, see the [Release
201-
notes](https://console.bluemix.net/docs/services/visual-recognition/release-notes.html#2april2018).
199+
notes](https://cloud.ibm.com/docs/services/visual-recognition/release-notes.html#2april2018).
202200
Analyze and get data about faces in images. Responses can include estimated age
203201
and gender. This feature uses a built-in model, so no training is necessary. The
204202
Detect faces method does not support general biometric facial recognition.
@@ -217,14 +215,16 @@ def detect_faces(self,
217215
the maximum image size is 10 MB. Redirects are followed, so you can use a
218216
shortened URL.
219217
You can also include images with the **images_file** parameter.
218+
:param str accept_language: The desired language of parts of the response. See the
219+
response for details.
220220
:param str images_file_content_type: The content type of images_file.
221221
:param str images_filename: The filename for images_file.
222222
:param dict headers: A `dict` containing the request headers
223223
:return: A `DetailedResponse` containing the result, headers and HTTP status code.
224224
:rtype: DetailedResponse
225225
"""
226226

227-
headers = {}
227+
headers = {'Accept-Language': accept_language}
228228
if 'headers' in kwargs:
229229
headers.update(kwargs.get('headers'))
230230

@@ -238,7 +238,7 @@ def detect_faces(self,
238238
images_file_content_type or
239239
'application/octet-stream')
240240
if url:
241-
form_data['url'] = (None, url)
241+
form_data['url'] = (None, url, 'text/plain')
242242

243243
url = '/v3/detect_faces'
244244
response = self.request(
@@ -434,7 +434,7 @@ def update_classifier(self,
434434
Update a custom classifier by adding new positive or negative classes (examples)
435435
or by adding new images to existing classes. You must supply at least one set of
436436
positive or negative examples. For details, see [Updating custom
437-
classifiers](https://console.bluemix.net/docs/services/visual-recognition/customizing.html#updating-custom-classifiers).
437+
classifiers](https://cloud.ibm.com/docs/services/visual-recognition/customizing.html#updating-custom-classifiers).
438438
Encode all names in UTF-8 if they contain non-ASCII characters (.zip and image
439439
file names, and classifier and class names). The service assumes UTF-8 encoding if
440440
it encounters non-ASCII characters.
@@ -555,7 +555,7 @@ def delete_user_data(self, customer_id, **kwargs):
555555
You associate a customer ID with data by passing the `X-Watson-Metadata` header
556556
with a request that passes data. For more information about personal data and
557557
customer IDs, see [Information
558-
security](https://console.bluemix.net/docs/services/visual-recognition/information-security.html).
558+
security](https://cloud.ibm.com/docs/services/visual-recognition/information-security.html).
559559
560560
:param str customer_id: The customer ID for which all data is to be deleted.
561561
:param dict headers: A `dict` containing the request headers
@@ -640,23 +640,34 @@ class ClassResult(object):
640640
Result of a class within a classifier.
641641
642642
:attr str class_name: Name of the class.
643+
Class names are translated in the language defined by the **Accept-Language** request
644+
header for the build-in classifier IDs (`default`, `food`, and `explicit`). Class
645+
names of custom classifiers are not translated. The response might not be in the
646+
specified language when the requested language is not supported or when there is no
647+
translation for the class name.
643648
:attr float score: Confidence score for the property in the range of 0 to 1. A higher
644649
score indicates greater likelihood that the class is depicted in the image. The
645650
default threshold for returning scores from a classifier is 0.5.
646-
:attr str type_hierarchy: Knowledge graph of the property. For example,
651+
:attr str type_hierarchy: (optional) Knowledge graph of the property. For example,
647652
`/fruit/pome/apple/eating apple/Granny Smith`. Included only if identified.
648653
"""
649654

650-
def __init__(self, class_name, score, type_hierarchy):
655+
def __init__(self, class_name, score, type_hierarchy=None):
651656
"""
652657
Initialize a ClassResult object.
653658
654659
:param str class_name: Name of the class.
660+
Class names are translated in the language defined by the **Accept-Language**
661+
request header for the build-in classifier IDs (`default`, `food`, and
662+
`explicit`). Class names of custom classifiers are not translated. The response
663+
might not be in the specified language when the requested language is not
664+
supported or when there is no translation for the class name.
655665
:param float score: Confidence score for the property in the range of 0 to 1. A
656666
higher score indicates greater likelihood that the class is depicted in the image.
657667
The default threshold for returning scores from a classifier is 0.5.
658-
:param str type_hierarchy: Knowledge graph of the property. For example,
659-
`/fruit/pome/apple/eating apple/Granny Smith`. Included only if identified.
668+
:param str type_hierarchy: (optional) Knowledge graph of the property. For
669+
example, `/fruit/pome/apple/eating apple/Granny Smith`. Included only if
670+
identified.
660671
"""
661672
self.class_name = class_name
662673
self.score = score
@@ -678,10 +689,6 @@ def _from_dict(cls, _dict):
678689
'Required property \'score\' not present in ClassResult JSON')
679690
if 'type_hierarchy' in _dict:
680691
args['type_hierarchy'] = _dict.get('type_hierarchy')
681-
else:
682-
raise ValueError(
683-
'Required property \'type_hierarchy\' not present in ClassResult JSON'
684-
)
685692
return cls(**args)
686693

687694
def _to_dict(self):
@@ -807,22 +814,29 @@ class ClassifiedImages(object):
807814
"""
808815
Results for all images.
809816
810-
:attr int custom_classes: Number of custom classes identified in the images.
811-
:attr int images_processed: Number of images processed for the API call.
817+
:attr int custom_classes: (optional) Number of custom classes identified in the
818+
images.
819+
:attr int images_processed: (optional) Number of images processed for the API call.
812820
:attr list[ClassifiedImage] images: Classified images.
813821
:attr list[WarningInfo] warnings: (optional) Information about what might cause less
814822
than optimal output. For example, a request sent with a corrupt .zip file and a list
815823
of image URLs will still complete, but does not return the expected output. Not
816824
returned when there is no warning.
817825
"""
818826

819-
def __init__(self, custom_classes, images_processed, images, warnings=None):
827+
def __init__(self,
828+
images,
829+
custom_classes=None,
830+
images_processed=None,
831+
warnings=None):
820832
"""
821833
Initialize a ClassifiedImages object.
822834
823-
:param int custom_classes: Number of custom classes identified in the images.
824-
:param int images_processed: Number of images processed for the API call.
825835
:param list[ClassifiedImage] images: Classified images.
836+
:param int custom_classes: (optional) Number of custom classes identified in the
837+
images.
838+
:param int images_processed: (optional) Number of images processed for the API
839+
call.
826840
:param list[WarningInfo] warnings: (optional) Information about what might cause
827841
less than optimal output. For example, a request sent with a corrupt .zip file and
828842
a list of image URLs will still complete, but does not return the expected output.
@@ -839,16 +853,8 @@ def _from_dict(cls, _dict):
839853
args = {}
840854
if 'custom_classes' in _dict:
841855
args['custom_classes'] = _dict.get('custom_classes')
842-
else:
843-
raise ValueError(
844-
'Required property \'custom_classes\' not present in ClassifiedImages JSON'
845-
)
846856
if 'images_processed' in _dict:
847857
args['images_processed'] = _dict.get('images_processed')
848-
else:
849-
raise ValueError(
850-
'Required property \'images_processed\' not present in ClassifiedImages JSON'
851-
)
852858
if 'images' in _dict:
853859
args['images'] = [
854860
ClassifiedImage._from_dict(x) for x in (_dict.get('images'))
@@ -1443,19 +1449,24 @@ class FaceGender(object):
14431449
Information about the gender of the face.
14441450
14451451
:attr str gender: Gender identified by the face. For example, `MALE` or `FEMALE`.
1452+
:attr str gender_label: The word for "male" or "female" in the language defined by the
1453+
**Accept-Language** request header.
14461454
:attr float score: Confidence score in the range of 0 to 1. A higher score indicates
14471455
greater confidence in the estimated value for the property.
14481456
"""
14491457

1450-
def __init__(self, gender, score):
1458+
def __init__(self, gender, gender_label, score):
14511459
"""
14521460
Initialize a FaceGender object.
14531461
14541462
:param str gender: Gender identified by the face. For example, `MALE` or `FEMALE`.
1463+
:param str gender_label: The word for "male" or "female" in the language defined
1464+
by the **Accept-Language** request header.
14551465
:param float score: Confidence score in the range of 0 to 1. A higher score
14561466
indicates greater confidence in the estimated value for the property.
14571467
"""
14581468
self.gender = gender
1469+
self.gender_label = gender_label
14591470
self.score = score
14601471

14611472
@classmethod
@@ -1467,6 +1478,12 @@ def _from_dict(cls, _dict):
14671478
else:
14681479
raise ValueError(
14691480
'Required property \'gender\' not present in FaceGender JSON')
1481+
if 'gender_label' in _dict:
1482+
args['gender_label'] = _dict.get('gender_label')
1483+
else:
1484+
raise ValueError(
1485+
'Required property \'gender_label\' not present in FaceGender JSON'
1486+
)
14701487
if 'score' in _dict:
14711488
args['score'] = _dict.get('score')
14721489
else:
@@ -1479,6 +1496,8 @@ def _to_dict(self):
14791496
_dict = {}
14801497
if hasattr(self, 'gender') and self.gender is not None:
14811498
_dict['gender'] = self.gender
1499+
if hasattr(self, 'gender_label') and self.gender_label is not None:
1500+
_dict['gender_label'] = self.gender_label
14821501
if hasattr(self, 'score') and self.score is not None:
14831502
_dict['score'] = self.score
14841503
return _dict

0 commit comments

Comments
 (0)