{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "d2c2b738", "metadata": {}, "outputs": [], "source": [ "from fastai.vision.all import *\n", "import gradio as gr" ] }, { "cell_type": "code", "execution_count": 2, "id": "b92d24f3", "metadata": {}, "outputs": [], "source": [ "# search_terms = ('hard hat','construction gloves','safety glasses','construction boots', 'screw driver','hammer','f150')\n" ] }, { "cell_type": "code", "execution_count": 9, "id": "34ebdca4", "metadata": {}, "outputs": [], "source": [ "path = Path('construction_photos')" ] }, { "cell_type": "code", "execution_count": 12, "id": "04347f65", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['construction boots', 'construction gloves', 'f150', 'hammer', 'hard hat', 'safety glasses', 'screw driver']" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dls = DataBlock(\n", " blocks=(ImageBlock,CategoryBlock),\n", " getters=None,\n", " n_inp=None,\n", " item_tfms=[Resize(224,method='squish')],\n", " get_items=get_image_files,\n", " splitter=RandomSplitter(seed=42),\n", " get_y=parent_label,\n", ").dataloaders('../python_clean_code_book/construction_photos/',bs=128)\n", "# ).dataloaders()\n", "#['construction boots', 'construction gloves', 'f150', 'hammer', 'hard hat', 'safety glasses', 'screw driver']\n", "dls.vocab" ] }, { "cell_type": "code", "execution_count": 5, "id": "85098e65", "metadata": {}, "outputs": [], "source": [ "learn = load_learner('construction_things.pkl')" ] }, { "cell_type": "code", "execution_count": 13, "id": "a6ed198b", "metadata": {}, "outputs": [], "source": [ "def classify_image(img):\n", " pred,idx,probs = learn.predict(img)\n", " return dict(zip(['construction boots', 'construction gloves', 'f150', 'hammer', 'hard hat', 'safety glasses', 'screw driver'],map(float,probs)))\n", "# classify_image(get_image_files(path)[100])" ] }, { "cell_type": "code", "execution_count": 14, "id": "dccf379e", "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": [ "{'construction boots': 5.033571142121218e-07,\n", " 'construction gloves': 1.9164571085639182e-07,\n", " 'f150': 0.9999986886978149,\n", " 'hammer': 2.487358869984746e-07,\n", " 'hard hat': 9.779357412753598e-08,\n", " 'safety glasses': 2.88559984795711e-08,\n", " 'screw driver': 1.963149287576016e-07}" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classify_image('../../ebs1/python_clean_code_book/construction_photos/f150/00000000.jpg')" ] }, { "cell_type": "code", "execution_count": 15, "id": "b2f582a8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7861/\n", "Running on public URL: https://54519.gradio.app\n", "\n", "This share link expires in 72 hours. For free permanent hosting, check out Spaces (https://huggingface.co/spaces)\n" ] }, { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "(,\n", " 'http://127.0.0.1:7861/',\n", " 'https://54519.gradio.app')" ] }, "execution_count": 15, "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" }, { "name": "stderr", "output_type": "stream", "text": [ "Traceback (most recent call last):\n", " File \"/root/miniconda/lib/python3.9/site-packages/gradio/routes.py\", line 269, in predict\n", " output = await run_in_threadpool(app.launchable.process_api, body, username)\n", " File \"/root/miniconda/lib/python3.9/site-packages/starlette/concurrency.py\", line 45, in run_in_threadpool\n", " return await anyio.to_thread.run_sync(func, *args)\n", " File \"/root/miniconda/lib/python3.9/site-packages/anyio/to_thread.py\", line 28, in run_sync\n", " return await get_asynclib().run_sync_in_worker_thread(func, *args, cancellable=cancellable,\n", " File \"/root/miniconda/lib/python3.9/site-packages/anyio/_backends/_asyncio.py\", line 818, in run_sync_in_worker_thread\n", " return await future\n", " File \"/root/miniconda/lib/python3.9/site-packages/anyio/_backends/_asyncio.py\", line 754, in run\n", " result = context.run(func, *args)\n", " File \"/root/miniconda/lib/python3.9/site-packages/gradio/interface.py\", line 573, in process_api\n", " prediction, durations = self.process(raw_input)\n", " File \"/root/miniconda/lib/python3.9/site-packages/gradio/interface.py\", line 615, in process\n", " predictions, durations = self.run_prediction(\n", " File \"/root/miniconda/lib/python3.9/site-packages/gradio/interface.py\", line 531, in run_prediction\n", " prediction = predict_fn(*processed_input)\n", " File \"/tmp/ipykernel_11528/2341249033.py\", line 3, in classify_image\n", " return dict(zip(dls.vocab,map(float,probs)))\n", "NameError: name 'dls' is not defined\n" ] } ], "source": [ "image = gr.inputs.Image(shape=(192,192))\n", "label = gr.outputs.Label()\n", "interface = gr.Interface(fn=classify_image,inputs=image,outputs=label)\n", "interface.launch(inline=True,share=True)\n", "# interface.launch(inline=False)" ] }, { "cell_type": "code", "execution_count": null, "id": "4b39ddcb", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }