|
3 | 3 | import os |
4 | 4 | from os.path import join, dirname |
5 | 5 | from unittest import TestCase |
| 6 | +import json |
6 | 7 |
|
7 | 8 | pytestmark = pytest.mark.skip('Run These Manually, they are destructive') |
8 | | - |
9 | 9 | class IntegrationTestVisualRecognitionV3(TestCase): |
10 | 10 |
|
11 | 11 | def setUp(self): |
12 | 12 | self.visual_recognition = watson_developer_cloud.VisualRecognitionV3('2016-05-20', api_key=os.environ.get( |
13 | | - 'VISUAL_RECOGNITION_API_KEY')) |
| 13 | + 'YOUR API KEY')) |
14 | 14 | self.visual_recognition.set_default_headers({'X-Watson-Learning-Opt-Out': '1', 'X-Watson-Test': '1'}) |
15 | | - self.collections = self.visual_recognition.list_collections() |
16 | | - |
17 | | - collection_json = self.visual_recognition.create_collection(name="test_integration_collection") |
18 | | - self.collection_id = collection_json['collection_id'] |
19 | | - |
20 | | - def tearDown(self): |
21 | | - results = self.visual_recognition.delete_collection(collection_id=self.collection_id) |
22 | | - assert not results |
23 | | - |
24 | | - def test_list_collections(self): |
25 | | - results = self.visual_recognition.list_collections() |
26 | | - assert len(results['collections']) - len(self.collections['collections']) == 1 |
27 | | - |
28 | | - def test_add_image_check_metadata_delete_image(self): |
29 | | - with open(join(dirname(__file__), '../resources/face.jpg'), 'rb') as image_file: |
30 | | - self.visual_recognition.add_image(collection_id=self.collection_id, image_file=image_file, metadata={'name': 'face'}) |
31 | 15 |
|
32 | | - images = self.visual_recognition.list_images(self.collection_id) |
33 | | - assert len(images['images']) == 1 |
| 16 | + def test_classify(self): |
| 17 | + car_path = join(dirname(__file__), '../../resources/cars.zip') |
| 18 | + with open(car_path, 'rb') as images_file: |
| 19 | + parameters = json.dumps({'threshold': 0.1, 'classifier_ids': ['CarsvsTrucks_1479118188', 'default']}) |
| 20 | + car_results = self.visual_recognition.classify(images_file=images_file, |
| 21 | + parameters=parameters) |
| 22 | + assert car_results is not None |
34 | 23 |
|
35 | | - image_id = images['images'][0]['image_id'] |
36 | | - meta = self.visual_recognition.get_image_metadata(collection_id=self.collection_id, image_id=image_id) |
37 | | - assert not meta |
| 24 | + def test_detect_faces(self): |
| 25 | + output = self.visual_recognition.detect_faces(parameters=json.dumps({'url': 'https://www.ibm.com/ibm/ginni/images/ginni_bio_780x981_v4_03162016.jpg'})) |
| 26 | + assert output is not None |
38 | 27 |
|
39 | | - assert meta['name'] == 'face' |
40 | | - assert 'neverland' not in meta |
| 28 | + def test_custom_classifier(self): |
| 29 | + with open(os.path.join(os.path.dirname(__file__), '../../resources/cars.zip'), 'rb') as cars, \ |
| 30 | + open(os.path.join(os.path.dirname(__file__), '../../resources/trucks.zip'), 'rb') as trucks: |
| 31 | + classifier = self.visual_recognition.create_classifier('Cars vs Trucks', cars_positive_examples=cars, negative_examples=trucks) |
41 | 32 |
|
42 | | - self.visual_recognition.set_image_metadata(collection_id=self.collection_id, image_id=image_id, metadata={'location': 'neverland'}) |
43 | | - meta = self.visual_recognition.get_image_metadata(collection_id=self.collection_id, image_id=image_id) |
44 | | - assert not meta |
45 | | - assert 'name' not in meta |
46 | | - assert meta['location'] == 'neverland' |
| 33 | + assert classifier is not None |
47 | 34 |
|
48 | | - self.visual_recognition.delete_image(collection_id=self.collection_id, image_id=image_id) |
| 35 | + classifier_id = classifier['classifier_id'] |
| 36 | + output = self.visual_recognition.get_classifier(classifier_id) |
| 37 | + assert output is not None |
49 | 38 |
|
50 | | - images = self.visual_recognition.list_images(self.collection_id) |
51 | | - assert images['images'] |
| 39 | + classifiers = self.visual_recognition.list_classifiers() |
| 40 | + assert classifiers is not None |
52 | 41 |
|
53 | | - def test_find_similar(self): |
54 | | - with open(join(dirname(__file__), '../resources/face.jpg'), 'rb') as image_file: |
55 | | - results = self.visual_recognition.find_similar(collection_id=self.collection_id, image_file=image_file) |
56 | | - assert results['images_processed'] == 1 |
| 42 | + output = self.visual_recognition.delete_classifier(classifier_id) |
| 43 | + assert output is not None |
0 commit comments