{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from fastai.vision.all import *\n", "import gradio as gr\n", "import pathlib\n", "pathlib.PosixPath = pathlib.WindowsPath" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def get_x(r): return path/'train'/r['fname']\n", "def get_y(r): return r['labels'].split(' ')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "learn = load_learner(\"export.pkl\")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "labels = learn.dls.vocab\n", "def infer(img):\n", " img = PILImage.create(img)\n", " _pred, _pred_w_idx, probs = learn.predict(img)\n", " # gradio doesn't support tensors, so converting to float\n", " labels_probs = {labels[i]: float(probs[i]) for i, _ in enumerate(labels)}\n", " return labels_probs" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "{'aeroplane': 0.00040097636519931257,\n", " 'bicycle': 0.003079020883888006,\n", " 'bird': 0.007594174239784479,\n", " 'boat': 0.0019870696123689413,\n", " 'bottle': 0.0077136121690273285,\n", " 'bus': 0.0002228342927992344,\n", " 'car': 0.002796210814267397,\n", " 'cat': 0.011337166652083397,\n", " 'chair': 0.027877626940608025,\n", " 'cow': 0.0004105104599148035,\n", " 'diningtable': 0.0014102141140028834,\n", " 'dog': 0.9442074298858643,\n", " 'horse': 0.00014107774768490344,\n", " 'motorbike': 0.0004384420462884009,\n", " 'person': 0.9861327409744263,\n", " 'pottedplant': 0.002472719643265009,\n", " 'sheep': 0.015349175781011581,\n", " 'sofa': 0.008290301077067852,\n", " 'train': 0.0043441057205200195,\n", " 'tvmonitor': 0.007222974672913551}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "infer(\"000001.jpg\")" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\Users\\Loc\\anaconda3\\lib\\site-packages\\gradio\\inputs.py:256: UserWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components\n", " warnings.warn(\n", "c:\\Users\\Loc\\anaconda3\\lib\\site-packages\\gradio\\deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n", " warnings.warn(value)\n", "c:\\Users\\Loc\\anaconda3\\lib\\site-packages\\gradio\\outputs.py:196: UserWarning: Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components\n", " warnings.warn(\n", "c:\\Users\\Loc\\anaconda3\\lib\\site-packages\\gradio\\deprecation.py:40: UserWarning: The 'type' parameter has been deprecated. Use the Number component instead.\n", " warnings.warn(value)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7861\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/plain": [ "(, 'http://127.0.0.1:7861/', None)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# get the inputs\n", "inputs = gr.inputs.Image(shape=(192, 192))\n", "\n", "# the app outputs two segmented images\n", "outputs = gr.outputs.Label(num_top_classes=3)\n", "\n", "EXAMPLES_PATH = Path('./examples')\n", "examples = [f'{EXAMPLES_PATH}/{f.name}' for f in EXAMPLES_PATH.iterdir()]\n", "\n", "# it's good practice to pass examples, description and a title to guide users\n", "title = 'Multiple Object Detector'\n", "description = 'This app detects objects that appear in the image'\n", "article = \"Author: Archie Tram. \"\n", "intf = gr.Interface(fn=infer, inputs=inputs, outputs=outputs, examples=examples, title=title, description=description, article=article)\n", "intf.launch(inline=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3.8.5 ('base')", "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" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "8390c3163ce2a2b50e270659ce53992de79de8e8bac3e92aa142b72a0dae5de6" } } }, "nbformat": 4, "nbformat_minor": 2 }