cmc/data-science
Personal data science learning files.
clone: git clone https://gitbay.org/cmc/data-science.git
main: notebooks/IBM Watson Visual Recognition.ipynb · raw
1{
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# IBM Watson Visual Recognition\n",
8 "Create an account on [IBM Watson Studio](https://www.ibm.com/cloud/watson-studio) and add the [Watson Visual Recognition](https://www.ibm.com/cloud/watson-visual-recognition) service to your free account."
9 ]
10 },
11 {
12 "cell_type": "code",
13 "execution_count": 22,
14 "metadata": {},
15 "outputs": [],
16 "source": [
17 "pip install --upgrade --user \"ibm-watson>=4.5.0\""
18 ]
19 },
20 {
21 "cell_type": "code",
22 "execution_count": 23,
23 "metadata": {},
24 "outputs": [],
25 "source": [
26 "apikey = \"<your-apikey>\"\n",
27 "version = \"2018-03-19\"\n",
28 "url = \"<your-url>\""
29 ]
30 },
31 {
32 "cell_type": "code",
33 "execution_count": 24,
34 "metadata": {},
35 "outputs": [],
36 "source": [
37 "import json\n",
38 "from ibm_watson import VisualRecognitionV3\n",
39 "from ibm_cloud_sdk_core.authenticators import IAMAuthenticator\n",
40 "\n",
41 "authenticator = IAMAuthenticator(apikey)\n",
42 "visual_recognition = VisualRecognitionV3(\n",
43 " version=version,\n",
44 " authenticator=authenticator\n",
45 ")\n",
46 "\n",
47 "visual_recognition.set_service_url(url)"
48 ]
49 },
50 {
51 "cell_type": "code",
52 "execution_count": 25,
53 "metadata": {},
54 "outputs": [],
55 "source": [
56 "visual_recognition.set_default_headers({'x-watson-learning-opt-out': \"true\"})"
57 ]
58 },
59 {
60 "cell_type": "code",
61 "execution_count": 60,
62 "metadata": {},
63 "outputs": [],
64 "source": [
65 "data = [\n",
66 "{\n",
67 " \"title\": \"Bear Country, South Dakota\",\n",
68 " \"url\": \"https://example.com/photos/highres/20140717.jpg\"\n",
69 "},\n",
70 "{\n",
71 " \"title\": \"Pactola Lake\",\n",
72 " \"url\": \"https://example.com/photos/highres/20140718.jpg\"\n",
73 "},\n",
74 "{\n",
75 " \"title\": \"Welcome to Utah\",\n",
76 " \"url\": \"https://example.com/photos/highres/20190608_02.jpg\"\n",
77 "},\n",
78 "{\n",
79 " \"title\": \"Honey Badger\",\n",
80 " \"url\": \"https://example.com/photos/highres/20190611_03.jpg\"\n",
81 "},\n",
82 "{\n",
83 " \"title\": \"Grand Canyon Lizard\",\n",
84 " \"url\": \"https://example.com/photos/highres/20190612.jpg\"\n",
85 "},\n",
86 "{\n",
87 " \"title\": \"The Workhouse\",\n",
88 " \"url\": \"https://example.com/photos/highres/20191116_01.jpg\"\n",
89 "}\n",
90 "]"
91 ]
92 },
93 {
94 "cell_type": "code",
95 "execution_count": 59,
96 "metadata": {},
97 "outputs": [
98 {
99 "name": "stdout",
100 "output_type": "stream",
101 "text": [
102 "-------------------------------------------------------------------------------------------------------------------------------------\n",
103 "Image Title: Bear Country, South Dakota \n",
104 "\n",
105 "brown bear ( 0.944 )\n",
106 "bear ( 1 )\n",
107 "carnivore ( 1 )\n",
108 "mammal ( 1 )\n",
109 "animal ( 1 )\n",
110 "Alaskan brown bear ( 0.759 )\n",
111 "greenishness color ( 0.975 )\n",
112 "-------------------------------------------------------------------------------------------------------------------------------------\n",
113 "-------------------------------------------------------------------------------------------------------------------------------------\n",
114 "Image Title: Pactola Lake \n",
115 "\n",
116 "ponderosa pine ( 0.763 )\n",
117 "pine tree ( 0.867 )\n",
118 "tree ( 0.867 )\n",
119 "plant ( 0.867 )\n",
120 "blue color ( 0.959 )\n",
121 "-------------------------------------------------------------------------------------------------------------------------------------\n",
122 "-------------------------------------------------------------------------------------------------------------------------------------\n",
123 "Image Title: Welcome to Utah \n",
124 "\n",
125 "signboard ( 0.953 )\n",
126 "building ( 0.79 )\n",
127 "blue color ( 0.822 )\n",
128 "purplish blue color ( 0.619 )\n",
129 "-------------------------------------------------------------------------------------------------------------------------------------\n",
130 "-------------------------------------------------------------------------------------------------------------------------------------\n",
131 "Image Title: Honey Badger \n",
132 "\n",
133 "American badger ( 0.689 )\n",
134 "carnivore ( 0.689 )\n",
135 "mammal ( 0.864 )\n",
136 "animal ( 0.864 )\n",
137 "armadillo ( 0.618 )\n",
138 "light brown color ( 0.9 )\n",
139 "reddish brown color ( 0.751 )\n",
140 "-------------------------------------------------------------------------------------------------------------------------------------\n",
141 "-------------------------------------------------------------------------------------------------------------------------------------\n",
142 "Image Title: Grand Canyon Lizard \n",
143 "\n",
144 "western fence lizard ( 0.724 )\n",
145 "lizard ( 0.93 )\n",
146 "reptile ( 0.93 )\n",
147 "animal ( 0.93 )\n",
148 "ultramarine color ( 0.633 )\n",
149 "-------------------------------------------------------------------------------------------------------------------------------------\n",
150 "-------------------------------------------------------------------------------------------------------------------------------------\n",
151 "Image Title: The Workhouse \n",
152 "\n",
153 "castle ( 0.896 )\n",
154 "fortification ( 0.905 )\n",
155 "defensive structure ( 0.96 )\n",
156 "stronghold ( 0.642 )\n",
157 "building ( 0.799 )\n",
158 "mound ( 0.793 )\n",
159 "blue color ( 0.745 )\n",
160 "-------------------------------------------------------------------------------------------------------------------------------------\n"
161 ]
162 }
163 ],
164 "source": [
165 "from ibm_watson import ApiException\n",
166 "\n",
167 "for x in range(len(data)):\n",
168 " try:\n",
169 " url = data[x][\"url\"]\n",
170 " images_filename = data[x][\"title\"]\n",
171 " classes = visual_recognition.classify(\n",
172 " url=url,\n",
173 " images_filename=images_filename,\n",
174 " threshold='0.6',\n",
175 " owners=[\"IBM\"]).get_result()\n",
176 " print(\"-------------------------------------------------------------------------------------------------------------------------------------\")\n",
177 " print(\"Image Title: \", data[x][\"title\"], \"\\n\")\n",
178 " print(\"Image URL: \", data[x][\"url\"], \"\\n\")\n",
179 " classification_results = classes[\"images\"][0][\"classifiers\"][0][\"classes\"]\n",
180 " for result in classification_results:\n",
181 " print(result[\"class\"], \"(\", result[\"score\"], \")\")\n",
182 " print(\"-------------------------------------------------------------------------------------------------------------------------------------\")\n",
183 " except ApiException as ex:\n",
184 " print(\"Method failed with status code \" + str(ex.code) + \": \" + ex.message)"
185 ]
186 }
187 ],
188 "metadata": {
189 "kernelspec": {
190 "display_name": "Python 3 (ipykernel)",
191 "language": "python",
192 "name": "python3"
193 },
194 "language_info": {
195 "codemirror_mode": {
196 "name": "ipython",
197 "version": 3
198 },
199 "file_extension": ".py",
200 "mimetype": "text/x-python",
201 "name": "python",
202 "nbconvert_exporter": "python",
203 "pygments_lexer": "ipython3",
204 "version": "3.11.5"
205 }
206 },
207 "nbformat": 4,
208 "nbformat_minor": 4
209}