aboutsummaryrefslogtreecommitdiff
path: root/.virtual_documents/notebooks/IBM Watson Visual Recognition.ipynb
blob: adeabf5772a58b6903c131d9a8d553dfcc3bf667 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
pip install --upgrade --user "ibm-watson>=4.5.0"


apikey = "<your-apikey>"
version = "2018-03-19"
url = "<your-url>"


import json
from ibm_watson import VisualRecognitionV3
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

authenticator = IAMAuthenticator(apikey)
visual_recognition = VisualRecognitionV3(
    version=version,
    authenticator=authenticator
)

visual_recognition.set_service_url(url)


visual_recognition.set_default_headers({'x-watson-learning-opt-out': "true"})


data = [
{
  "title": "Bear Country, South Dakota",
  "url": "https://example.com/photos/highres/20140717.jpg"
},
{
  "title": "Pactola Lake",
  "url": "https://example.com/photos/highres/20140718.jpg"
},
{
  "title": "Welcome to Utah",
  "url": "https://example.com/photos/highres/20190608_02.jpg"
},
{
  "title": "Honey Badger",
  "url": "https://example.com/photos/highres/20190611_03.jpg"
},
{
  "title": "Grand Canyon Lizard",
  "url": "https://example.com/photos/highres/20190612.jpg"
},
{
  "title": "The Workhouse",
  "url": "https://example.com/photos/highres/20191116_01.jpg"
}
]


from ibm_watson import ApiException

for x in range(len(data)):
    try:
        url = data[x]["url"]
        images_filename = data[x]["title"]
        classes = visual_recognition.classify(
            url=url,
            images_filename=images_filename,
            threshold='0.6',
            owners=["IBM"]).get_result()
        print("-------------------------------------------------------------------------------------------------------------------------------------")
        print("Image Title: ", data[x]["title"], "\n")
        print("Image URL: ", data[x]["url"], "\n")
        classification_results = classes["images"][0]["classifiers"][0]["classes"]
        for result in classification_results:
            print(result["class"], "(", result["score"], ")")
        print("-------------------------------------------------------------------------------------------------------------------------------------")
    except ApiException as ex:
        print("Method failed with status code " + str(ex.code) + ": " + ex.message)