From 836841dba83ae4b79009799642a7acdef13ffd48 Mon Sep 17 00:00:00 2001 From: Christian Cleberg Date: Mon, 22 May 2023 15:39:44 -0500 Subject: initial commit --- notebooks/IBM Watson Visual Recognition.ipynb | 209 ++++++++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 notebooks/IBM Watson Visual Recognition.ipynb (limited to 'notebooks/IBM Watson Visual Recognition.ipynb') diff --git a/notebooks/IBM Watson Visual Recognition.ipynb b/notebooks/IBM Watson Visual Recognition.ipynb new file mode 100644 index 0000000..86d5862 --- /dev/null +++ b/notebooks/IBM Watson Visual Recognition.ipynb @@ -0,0 +1,209 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# IBM Watson Visual Recognition\n", + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [], + "source": [ + "pip install --upgrade --user \"ibm-watson>=4.5.0\"" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "apikey = \"\"\n", + "version = \"2018-03-19\"\n", + "url = \"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "import json\n", + "from ibm_watson import VisualRecognitionV3\n", + "from ibm_cloud_sdk_core.authenticators import IAMAuthenticator\n", + "\n", + "authenticator = IAMAuthenticator(apikey)\n", + "visual_recognition = VisualRecognitionV3(\n", + " version=version,\n", + " authenticator=authenticator\n", + ")\n", + "\n", + "visual_recognition.set_service_url(url)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [], + "source": [ + "visual_recognition.set_default_headers({'x-watson-learning-opt-out': \"true\"})" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [], + "source": [ + "data = [\n", + "{\n", + " \"title\": \"Bear Country, South Dakota\",\n", + " \"url\": \"https://example.com/photos/highres/20140717.jpg\"\n", + "},\n", + "{\n", + " \"title\": \"Pactola Lake\",\n", + " \"url\": \"https://example.com/photos/highres/20140718.jpg\"\n", + "},\n", + "{\n", + " \"title\": \"Welcome to Utah\",\n", + " \"url\": \"https://example.com/photos/highres/20190608_02.jpg\"\n", + "},\n", + "{\n", + " \"title\": \"Honey Badger\",\n", + " \"url\": \"https://example.com/photos/highres/20190611_03.jpg\"\n", + "},\n", + "{\n", + " \"title\": \"Grand Canyon Lizard\",\n", + " \"url\": \"https://example.com/photos/highres/20190612.jpg\"\n", + "},\n", + "{\n", + " \"title\": \"The Workhouse\",\n", + " \"url\": \"https://example.com/photos/highres/20191116_01.jpg\"\n", + "}\n", + "]" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-------------------------------------------------------------------------------------------------------------------------------------\n", + "Image Title: Bear Country, South Dakota \n", + "\n", + "brown bear ( 0.944 )\n", + "bear ( 1 )\n", + "carnivore ( 1 )\n", + "mammal ( 1 )\n", + "animal ( 1 )\n", + "Alaskan brown bear ( 0.759 )\n", + "greenishness color ( 0.975 )\n", + "-------------------------------------------------------------------------------------------------------------------------------------\n", + "-------------------------------------------------------------------------------------------------------------------------------------\n", + "Image Title: Pactola Lake \n", + "\n", + "ponderosa pine ( 0.763 )\n", + "pine tree ( 0.867 )\n", + "tree ( 0.867 )\n", + "plant ( 0.867 )\n", + "blue color ( 0.959 )\n", + "-------------------------------------------------------------------------------------------------------------------------------------\n", + "-------------------------------------------------------------------------------------------------------------------------------------\n", + "Image Title: Welcome to Utah \n", + "\n", + "signboard ( 0.953 )\n", + "building ( 0.79 )\n", + "blue color ( 0.822 )\n", + "purplish blue color ( 0.619 )\n", + "-------------------------------------------------------------------------------------------------------------------------------------\n", + "-------------------------------------------------------------------------------------------------------------------------------------\n", + "Image Title: Honey Badger \n", + "\n", + "American badger ( 0.689 )\n", + "carnivore ( 0.689 )\n", + "mammal ( 0.864 )\n", + "animal ( 0.864 )\n", + "armadillo ( 0.618 )\n", + "light brown color ( 0.9 )\n", + "reddish brown color ( 0.751 )\n", + "-------------------------------------------------------------------------------------------------------------------------------------\n", + "-------------------------------------------------------------------------------------------------------------------------------------\n", + "Image Title: Grand Canyon Lizard \n", + "\n", + "western fence lizard ( 0.724 )\n", + "lizard ( 0.93 )\n", + "reptile ( 0.93 )\n", + "animal ( 0.93 )\n", + "ultramarine color ( 0.633 )\n", + "-------------------------------------------------------------------------------------------------------------------------------------\n", + "-------------------------------------------------------------------------------------------------------------------------------------\n", + "Image Title: The Workhouse \n", + "\n", + "castle ( 0.896 )\n", + "fortification ( 0.905 )\n", + "defensive structure ( 0.96 )\n", + "stronghold ( 0.642 )\n", + "building ( 0.799 )\n", + "mound ( 0.793 )\n", + "blue color ( 0.745 )\n", + "-------------------------------------------------------------------------------------------------------------------------------------\n" + ] + } + ], + "source": [ + "from ibm_watson import ApiException\n", + "\n", + "for x in range(len(data)):\n", + " try:\n", + " url = data[x][\"url\"]\n", + " images_filename = data[x][\"title\"]\n", + " classes = visual_recognition.classify(\n", + " url=url,\n", + " images_filename=images_filename,\n", + " threshold='0.6',\n", + " owners=[\"IBM\"]).get_result()\n", + " print(\"-------------------------------------------------------------------------------------------------------------------------------------\")\n", + " print(\"Image Title: \", data[x][\"title\"], \"\\n\")\n", + " print(\"Image URL: \", data[x][\"url\"], \"\\n\")\n", + " classification_results = classes[\"images\"][0][\"classifiers\"][0][\"classes\"]\n", + " for result in classification_results:\n", + " print(result[\"class\"], \"(\", result[\"score\"], \")\")\n", + " print(\"-------------------------------------------------------------------------------------------------------------------------------------\")\n", + " except ApiException as ex:\n", + " print(\"Method failed with status code \" + str(ex.code) + \": \" + ex.message)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} -- cgit v1.2.3-70-g09d2