|
1 | 1 | from __future__ import print_function |
2 | 2 | import json |
3 | 3 | from os.path import join, dirname |
4 | | -from watson_developer_cloud import VisualRecognitionV3 |
| 4 | +from watson_developer_cloud import VisualRecognitionV3, WatsonApiException |
5 | 5 |
|
6 | 6 | test_url = 'https://www.ibm.com/ibm/ginni/images' \ |
7 | 7 | '/ginni_bio_780x981_v4_03162016.jpg' |
8 | 8 |
|
9 | 9 | visual_recognition = VisualRecognitionV3('2016-05-20', api_key='YOUR API KEY') |
| 10 | +classifier_id = 'CarsvsTrucksxDO_NOT_DELETE_771019274' |
10 | 11 |
|
11 | 12 | # with open(join(dirname(__file__), '../resources/cars.zip'), 'rb') as cars, \ |
12 | 13 | # open(join(dirname(__file__), '../resources/trucks.zip'), 'rb') as |
|
18 | 19 |
|
19 | 20 | car_path = join(dirname(__file__), '../resources/cars.zip') |
20 | 21 | with open(car_path, 'rb') as images_file: |
21 | | - parameters = json.dumps({'threshold': 0.1, 'classifier_ids': ['CarsvsTrucks_931077904', 'default']}) |
| 22 | + parameters = json.dumps({'threshold': 0.1, 'classifier_ids': [classifier_id, 'default']}) |
22 | 23 | car_results = visual_recognition.classify(images_file=images_file, |
23 | 24 | parameters=parameters) |
24 | 25 | print(json.dumps(car_results, indent=2)) |
25 | 26 |
|
| 27 | +# Example with no deprecated |
| 28 | +try: |
| 29 | + with open(car_path, 'rb') as images_file: |
| 30 | + car_results = visual_recognition.classify( |
| 31 | + images_file=images_file, |
| 32 | + threshold='0.1', |
| 33 | + classifier_ids=[classifier_id, 'default']) |
| 34 | + print(json.dumps(car_results, indent=2)) |
| 35 | +except WatsonApiException as ex: |
| 36 | + print(ex.httpResponse.json()) |
| 37 | + |
26 | 38 | # print(json.dumps(visual_recognition.get_classifier('YOUR CLASSIFIER ID'), |
27 | 39 | # indent=2)) |
28 | 40 |
|
|
53 | 65 | with open(face_path, 'rb') as image_file: |
54 | 66 | face_result = visual_recognition.detect_faces(images_file=image_file) |
55 | 67 | print(json.dumps(face_result, indent=2)) |
| 68 | + |
| 69 | +#Core ml model example |
| 70 | +# model_name = '{0}.mlmodel'.format(classifier_id) |
| 71 | +# core_ml_model = visual_recognition.get_core_ml_model(classifier_id) |
| 72 | +# with open('/tmp/{0}'.format(model_name), 'wb') as fp: |
| 73 | +# fp.write(core_ml_model.content) |
0 commit comments