Skip to content

Commit 7ff53aa

Browse files
committed
starting to add Visual Recognition v2 beta
1 parent 73a28fe commit 7ff53aa

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import json
2+
from os.path import join, dirname
3+
from watson_developer_cloud import VisualRecognitionV2Beta as VisualRecognition
4+
5+
6+
visual_recognition = VisualRecognition(version='2015-12-02', username='YOUR SERVICE USERNAME',
7+
password='YOUR SERVICE PASSWORD')
8+
9+
# print(json.dumps(visual_recognition.list_classifiers(), indent=2))
10+
11+
with open(join(dirname(__file__), '../resources/test.jpg'), 'rb') as image_file:
12+
print(json.dumps(visual_recognition.classify(image_file, classifier_ids=['Tiger', 'Cat']), indent=2))

watson_developer_cloud/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@
3636
from .tradeoff_analytics_v1 import TradeoffAnalyticsV1
3737
from .visual_insights_v1_experimental import VisualInsightsV1Experimental
3838
from .visual_recognition_v1_beta import VisualRecognitionV1Beta
39+
from .visual_recognition_v2_beta import VisualRecognitionV2Beta
3940
from .version import __version__
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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

Comments
 (0)