Skip to content

Commit 5ba7569

Browse files
committed
Visual Recognition v2
1 parent 7ff53aa commit 5ba7569

File tree

5 files changed

+56
-1
lines changed

5 files changed

+56
-1
lines changed

examples/visual_recognition_v2_beta.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,20 @@
66
visual_recognition = VisualRecognition(version='2015-12-02', username='YOUR SERVICE USERNAME',
77
password='YOUR SERVICE PASSWORD')
88

9-
# print(json.dumps(visual_recognition.list_classifiers(), indent=2))
9+
# with open(join(dirname(__file__), '../resources/cars.zip'), 'rb') as cars, \
10+
# open(join(dirname(__file__), '../resources/trucks.zip'), 'rb') as trucks:
11+
# print(json.dumps(visual_recognition.create_classifier('Cars vs Trucks', positive_examples=cars,
12+
# negative_examples=trucks), indent=2))
13+
14+
# with open(join(dirname(__file__), '../resources/car.jpg'), 'rb') as image_file:
15+
# print(json.dumps(visual_recognition.classify(image_file), indent=2))
16+
17+
# print(json.dumps(visual_recognition.get_classifier(classifier_id='Tiger'), indent=2))
18+
19+
# The service currently has a bug where even successful deletions return a 404
20+
# print(json.dumps(visual_recognition.delete_classifier(classifier_id='YOUR CLASSIFIER ID'), indent=2))
21+
22+
print(json.dumps(visual_recognition.list_classifiers(), indent=2))
1023

1124
with open(join(dirname(__file__), '../resources/test.jpg'), 'rb') as image_file:
1225
print(json.dumps(visual_recognition.classify(image_file, classifier_ids=['Tiger', 'Cat']), indent=2))

resources/car.jpg

44.6 KB
Loading

resources/cars.zip

1.39 MB
Binary file not shown.

resources/trucks.zip

1.29 MB
Binary file not shown.

watson_developer_cloud/visual_recognition_v2_beta.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ def __init__(self, version, url=default_url, username=None, password=None, use_v
3737
self, 'visual_recognition', url, username, password, use_vcap_services)
3838
self.version = version
3939

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/{}'.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/{}'.format(classifier_id), params=params,
58+
accept_json=True)
59+
4060
def list_classifiers(self, verbose=False):
4161
"""
4262
Returns a list of user-created and built-in classifiers. (May change in the future to only user-created.)
@@ -46,7 +66,29 @@ def list_classifiers(self, verbose=False):
4666
params = {'verbose': verbose, 'version': self.version}
4767
return self.request(method='GET', url='/v2/classifiers', params=params, accept_json=True)
4868

69+
def create_classifier(self, name, positive_examples, negative_examples):
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='/v2/classifiers', files={'positive_examples': positive_examples,
82+
'negative_examples': negative_examples},
83+
data=data, params=params, accept_json=True)
84+
4985
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+
"""
5092

5193
if isinstance(classifier_ids, list):
5294
classifier_ids = json.dumps(classifier_ids)

0 commit comments

Comments
 (0)