Skip to content

Instantly share code, notes, and snippets.

@dibonetta
Created April 22, 2020 12:30
Show Gist options
  • Save dibonetta/395109546ec00cd2012a9ad14c48c4ab to your computer and use it in GitHub Desktop.
Save dibonetta/395109546ec00cd2012a9ad14c48c4ab to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install --upgrade \"watson-developer-cloud>=2.4.1\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# @hidden_cell\n",
"my_apikey = 'D3qkuAmlYmbCGL0SW0YiWjbp7xGmnSpw2Vut4LeEn3Bo'\n",
"classifier_id = 'firesmokeclassifier_1015612219'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from watson_developer_cloud import VisualRecognitionV3\n",
"visual_recognition = VisualRecognitionV3(\n",
" version='2018-03-19',\n",
" iam_apikey=my_apikey)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import urllib.request\n",
"from matplotlib import pyplot as plt\n",
"from pylab import rcParams\n",
"def plt_image(image_url, size = (5,4)):\n",
" image = cv2.imread(image_url)\n",
" if len(image.shape) == 3:\n",
" image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
" rcParams['figure.figsize'] = size[0], size[1] #set image display size\n",
" plt.axis(\"off\")\n",
" plt.imshow(image, cmap=\"Greys_r\")\n",
" plt.show()\n",
" \n",
"import json\n",
"from pandas.io.json import json_normalize\n",
"def result(testfile):\n",
" with open(testfile, 'rb') as images_file:\n",
" classes = visual_recognition.classify(\n",
" images_file,\n",
" threshold='0.0',\n",
" classifier_ids=classifier_id).get_result()\n",
" json_classes = classes['images'][0]['classifiers'][0]['classes']\n",
" df = json_normalize(json_classes).sort_values('score', ascending=False).reset_index(drop=True)\n",
" return df"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"testfile = 'test_fire.jpg'\n",
"plt_image(testfile) \n",
"result(testfile)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"testfile = 'test_smoke.jpg'\n",
"plt_image(testfile) \n",
"result(testfile)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"testfile = 'test_neutral.jpg'\n",
"plt_image(testfile) \n",
"result(testfile)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"testfile = 'test_smokefire.jpg'\n",
"plt_image(testfile) \n",
"result(testfile)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"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.6.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment