{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "id": "Sq8K13e0F5fx" }, "outputs": [], "source": [ "#The line #!default_exp app is typically used in fastai's nbdev library, which is used for creating Python libraries from Jupyter notebooks.\n", "\n", "#!default_exp app" ] }, { "cell_type": "markdown", "metadata": { "id": "rAQBAIwXGa65" }, "source": [ "#Cap recognizer" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "DgI4y7YPGdEL" }, "outputs": [], "source": [ "!pip install -Uqq fastai gradio nbdev" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'fastai'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[3], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mfastai\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(fastai\u001b[38;5;241m.\u001b[39m__version__)\n", "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'fastai'" ] } ], "source": [ "import fastai\n", "print(fastai.__version__)\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'fastai'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mfastai\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mvision\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mall\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;241m*\u001b[39m\n", "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'fastai'" ] } ], "source": [ "from fastai.vision.all import *" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'fastai'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[10], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mfastai\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mvision\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mall\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m load_learner\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mgradio\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mas\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mgr\u001b[39;00m\n", "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'fastai'" ] } ], "source": [ "from fastai.vision.all import load_learner\n", "import gradio as gr" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "id": "v83ndxlVHoWj" }, "outputs": [ { "ename": "NameError", "evalue": "name 'load_learner' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[11], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m#!export\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[43mload_learner\u001b[49m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodels/cap_recognizer_v0.pkl\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mNameError\u001b[0m: name 'load_learner' is not defined" ] } ], "source": [ "#!export\n", "model = load_learner(\"models/cap_recognizer_v0.pkl\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "5E15Hum7IXkU" }, "outputs": [], "source": [ "#|export\n", "\n", "cap_labels = [\n", " 'balaclava cap',\n", " 'baseball cap',\n", " 'beanie cap',\n", " 'boater hat',\n", " 'bowler hat',\n", " 'bucket hat',\n", " 'cowboy hat',\n", " 'fedora cap',\n", " 'flat cap',\n", " 'ivy cap',\n", " 'kepi cap',\n", " 'newsboy cap',\n", " 'pork pie hat',\n", " 'rasta cap',\n", " 'sun hat',\n", " 'taqiyah cap',\n", " 'top hat',\n", " 'trucker cap',\n", " 'turban cap',\n", " 'visor cap'\n", "]\n", "\n" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "id": "70xb8V6tJQAU" }, "outputs": [], "source": [ "img = PILImage.create(\"test_images/unknown_01.jpg\")\n", "img.thumbnail((128,128))" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 357 }, "id": "cGpRTaZWJhKt", "outputId": "08497a21-13a9-4c76-f1c0-7af5a5df3fa3" }, "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": [ "{'balaclava cap': 0.0001849675754783675,\n", " 'baseball cap': 6.271898018894717e-05,\n", " 'beanie cap': 0.99442058801651,\n", " 'boater hat': 5.3084555418081436e-08,\n", " 'bowler hat': 2.4172715029635583e-07,\n", " 'bucket hat': 1.0174167073273566e-05,\n", " 'cowboy hat': 8.527808148528493e-08,\n", " 'fedora cap': 3.8955821679564906e-08,\n", " 'flat cap': 2.141284539902699e-06,\n", " 'ivy cap': 2.632383711898001e-06,\n", " 'kepi cap': 0.00014104695583228022,\n", " 'newsboy cap': 1.075521777238464e-05,\n", " 'pork pie hat': 3.110138635520343e-08,\n", " 'rasta cap': 3.2799041946418583e-05,\n", " 'sun hat': 1.9477438399917446e-06,\n", " 'taqiyah cap': 8.358875493286178e-05,\n", " 'top hat': 3.8565694637782144e-08,\n", " 'trucker cap': 4.3577329051913694e-05,\n", " 'turban cap': 0.004263813607394695,\n", " 'visor cap': 0.0007387499208562076}" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "recognize_image(img)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ZerTaI5OPCGx", "outputId": "75d2223d-f318-4e32-a3ec-ac3fc6217534" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n", "* Running on public URL: https://29d0fcc78c958c8fd7.gradio.live\n", "\n", "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n" ] }, { "data": { "text/plain": [] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#!export\n", "\n", "image = gr.Image()\n", "label = gr.Label()\n", "examples = [\n", " 'test_images/unknown_00.jpg',\n", " 'test_images/unknown_01.jpg',\n", " 'test_images/unknown_02.jpg',\n", " 'test_images/unknown_03.jpg']\n", "\n", "\n", "demo = gr.Interface(fn=recognize_image, inputs=\"image\", outputs=\"label\", examples = examples)\n", "demo.launch(inline = False, share = True)" ] }, { "cell_type": "markdown", "metadata": { "id": "uEfA2Pjmfl-e" }, "source": [ "#Notebook to python script export\n", "done it manually" ] } ], "metadata": { "colab": { "provenance": [] }, "kernelspec": { "display_name": "Python 3", "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.6" } }, "nbformat": 4, "nbformat_minor": 0 }