{ "cells": [ { "cell_type": "code", "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "68CgGISA0OQu", "outputId": "5529450e-9f82-479a-b398-9570a0969314" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n" ] } ], "source": [ "from google.colab import drive\n", "drive.mount('/content/drive')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "NN8c3dXt0hfN", "outputId": "83a5e277-e7d3-498a-e080-a573efdb2ce1" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "datasets README.md\t\tsaved_model yolov5-flask-master\n", "Gradio.ipynb requirements.txt\tyolov5\n", "[Errno 2] No such file or directory: 'drive/My Drive/Colab Notebooks/Model Representation/'\n", "/content/drive/My Drive/Colab Notebooks/Model Representation\n" ] } ], "source": [ "!ls \"/content/drive/My Drive/Colab Notebooks/Model Representation/\"\n", "%cd drive/My Drive/Colab Notebooks/Model Representation/" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "vN36F7c50mF4", "outputId": "cffb7408-84e1-41a2-b6dd-b4256c374449" }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'/content/drive/My Drive/Colab Notebooks/Model Representation'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 3 } ], "source": [ "import os\n", "cwd = os.getcwd()\n", "cwd" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "8xT1CQBM1Afx", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "b8593c94-3223-480c-910a-0667a2e019f1" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\u001b[K |████████████████████████████████| 5.6 MB 2.0 MB/s \n", "\u001b[K |████████████████████████████████| 596 kB 65.4 MB/s \n", "\u001b[K |████████████████████████████████| 140 kB 18.1 MB/s \n", "\u001b[K |████████████████████████████████| 84 kB 3.4 MB/s \n", "\u001b[K |████████████████████████████████| 1.1 MB 66.8 MB/s \n", "\u001b[K |████████████████████████████████| 54 kB 2.7 MB/s \n", "\u001b[K |████████████████████████████████| 54 kB 572 kB/s \n", "\u001b[K |████████████████████████████████| 57 kB 4.9 MB/s \n", "\u001b[K |████████████████████████████████| 2.3 MB 46.4 MB/s \n", "\u001b[K |████████████████████████████████| 84 kB 3.0 MB/s \n", "\u001b[K |████████████████████████████████| 212 kB 51.5 MB/s \n", "\u001b[K |████████████████████████████████| 272 kB 58.7 MB/s \n", "\u001b[K |████████████████████████████████| 271 kB 47.1 MB/s \n", "\u001b[K |████████████████████████████████| 144 kB 67.1 MB/s \n", "\u001b[K |████████████████████████████████| 94 kB 3.7 MB/s \n", "\u001b[K |████████████████████████████████| 63 kB 1.5 MB/s \n", "\u001b[K |████████████████████████████████| 80 kB 8.6 MB/s \n", "\u001b[K |████████████████████████████████| 68 kB 2.6 MB/s \n", "\u001b[K |████████████████████████████████| 43 kB 1.5 MB/s \n", "\u001b[K |████████████████████████████████| 856 kB 49.4 MB/s \n", "\u001b[K |████████████████████████████████| 62 kB 838 kB/s \n", "\u001b[K |████████████████████████████████| 4.1 MB 40.7 MB/s \n", "\u001b[?25h Building wheel for ffmpy (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Building wheel for python-multipart (setup.py) ... \u001b[?25l\u001b[?25hdone\n" ] } ], "source": [ "!pip install -qr https://raw.githubusercontent.com/ultralytics/yolov5/master/requirements.txt gradio # install dependencies" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 1000 }, "id": "FwCb9Nmk1n6n", "outputId": "219fa614-9dda-4870-fe54-dfce5b4e4b05" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[31m\u001b[1mrequirements:\u001b[0m PyYAML>=5.3.1 not found and is required by YOLOv5, attempting auto-update...\n", "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", "Requirement already satisfied: PyYAML>=5.3.1 in /usr/local/lib/python3.7/dist-packages (6.0)\n", "\n", "\u001b[31m\u001b[1mrequirements:\u001b[0m torch!=1.12.0,>=1.7.0 not found and is required by YOLOv5, attempting auto-update...\n", "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", "Requirement already satisfied: torch!=1.12.0,>=1.7.0 in /usr/local/lib/python3.7/dist-packages (1.11.0)\n", "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from torch!=1.12.0,>=1.7.0) (4.1.1)\n", "\n", "\u001b[31m\u001b[1mrequirements:\u001b[0m torchvision!=0.13.0,>=0.8.1 not found and is required by YOLOv5, attempting auto-update...\n", "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", "Requirement already satisfied: torchvision!=0.13.0,>=0.8.1 in /usr/local/lib/python3.7/dist-packages (0.12.0)\n", "Requirement already satisfied: torch==1.11.0 in /usr/local/lib/python3.7/dist-packages (from torchvision!=0.13.0,>=0.8.1) (1.11.0)\n", "Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /usr/local/lib/python3.7/dist-packages (from torchvision!=0.13.0,>=0.8.1) (7.1.2)\n", "Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from torchvision!=0.13.0,>=0.8.1) (2.23.0)\n", "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from torchvision!=0.13.0,>=0.8.1) (1.21.6)\n", "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from torchvision!=0.13.0,>=0.8.1) (4.1.1)\n", "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests->torchvision!=0.13.0,>=0.8.1) (3.0.4)\n", "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests->torchvision!=0.13.0,>=0.8.1) (1.24.3)\n", "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests->torchvision!=0.13.0,>=0.8.1) (2022.6.15)\n", "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests->torchvision!=0.13.0,>=0.8.1) (2.10)\n", "\n", "\u001b[31m\u001b[1mrequirements:\u001b[0m 3 packages updated per /root/.cache/torch/hub/ultralytics_yolov5_master/requirements.txt\n", "\u001b[31m\u001b[1mrequirements:\u001b[0m ⚠️ \u001b[1mRestart runtime or rerun command for updates to take effect\u001b[0m\n", "\n", "YOLOv5 🚀 2022-7-14 Python-3.7.13 torch-1.11.0+cu102 CPU\n", "\n", "Fusing layers... \n", "YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients\n", "Adding AutoShape... \n", "/usr/local/lib/python3.7/dist-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n", " warnings.warn(value)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Colab notebook detected. This cell will run indefinitely so that you can see errors and logs. To turn off, set debug=False in launch().\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Connected (version 2.0, client OpenSSH_7.6p1)\n", "Authentication (publickey) successful!\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Running on public URL: https://21879.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": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Keyboard interruption in main thread... closing server.\n" ] }, { "data": { "text/plain": [ "(,\n", " 'http://127.0.0.1:7860/',\n", " 'https://21879.gradio.app')" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import gradio as gr\n", "import torch\n", "from PIL import Image\n", "\n", "# Images\n", "#torch.hub.download_url_to_file('https://github.com/ultralytics/yolov5/raw/master/data/images/zidane.jpg', 'zidane.jpg')\n", "#torch.hub.download_url_to_file('https://github.com/ultralytics/yolov5/raw/master/data/images/bus.jpg', 'bus.jpg')\n", "\n", "# Model\n", "#model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # force_reload=True to update\n", "\n", "model = torch.hub.load(cwd+'/yolov5', 'custom', path=cwd+'/saved_model/yolov5s.pt', source='local') # local model\n", "\n", "\n", "\n", "def yolo(im, size=640):\n", " g = (size / max(im.size)) # gain\n", " im = im.resize((int(x * g) for x in im.size), Image.ANTIALIAS) # resize\n", "\n", " results = model(im) # inference\n", " results.render() # updates results.imgs with boxes and labels\n", " return Image.fromarray(results.imgs[0])\n", "\n", "\n", "inputs = gr.inputs.Image(type='pil', label=\"Original Image\")\n", "outputs = gr.outputs.Image(type=\"pil\", label=\"Output Image\")\n", "\n", "title = \"YOLOv5\"\n", "description = \"YOLOv5 Gradio demo for object detection. Upload an image or click an example image to use.\"\n", "article = \"

YOLOv5 is a family of compound-scaled object detection models trained on the COCO dataset, and includes \" \\\n", " \"simple functionality for Test Time Augmentation (TTA), model ensembling, hyperparameter evolution, \" \\\n", " \"and export to ONNX, CoreML and TFLite. Source code |\" \\\n", " \"iOS App | PyTorch Hub

\"\n", "\n", "examples = [[cwd+'/datasets/zidane.jpg'], [cwd+'/datasets/bus.jpg']]\n", "gr.Interface(yolo, inputs, outputs, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch(\n", " debug=True)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 1000 }, "id": "8kaOPNyz2cEr", "outputId": "4012dc70-c529-47cb-e8c0-ef053f268629" }, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "\u001b[31m\u001b[1mrequirements:\u001b[0m PyYAML>=5.3.1 not found and is required by YOLOv5, attempting auto-update...\n", "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", "Requirement already satisfied: PyYAML>=5.3.1 in /usr/local/lib/python3.7/dist-packages (6.0)\n", "\n", "\u001b[31m\u001b[1mrequirements:\u001b[0m 1 package updated per /content/drive/MyDrive/Colab Notebooks/Model Representation/yolov5/requirements.txt\n", "\u001b[31m\u001b[1mrequirements:\u001b[0m ⚠️ \u001b[1mRestart runtime or rerun command for updates to take effect\u001b[0m\n", "\n", "YOLOv5 🚀 2022-6-24 Python-3.7.13 torch-1.12.0+cu113 CPU\n", "\n", "Fusing layers... \n", "Model summary: 213 layers, 7012822 parameters, 0 gradients\n", "Adding AutoShape... \n", "/usr/local/lib/python3.7/dist-packages/gradio/inputs.py:257: UserWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components\n", " \"Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components\",\n", "/usr/local/lib/python3.7/dist-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n", " warnings.warn(value)\n", "/usr/local/lib/python3.7/dist-packages/gradio/outputs.py:43: UserWarning: Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components\n", " \"Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components\",\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Colab notebook detected. This cell will run indefinitely so that you can see errors and logs. To turn off, set debug=False in launch().\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "Connected (version 2.0, client OpenSSH_7.6p1)\n", "Authentication (publickey) successful!\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Running on public URL: https://24062.gradio.app\n", "\n", "This share link expires in 72 hours. For free permanent hosting, check out Spaces (https://huggingface.co/spaces)\n" ] }, { "output_type": "display_data", "data": { "text/plain": [ "" ], "text/html": [ "
" ] }, "metadata": {} }, { "output_type": "stream", "name": "stderr", "text": [ "Traceback (most recent call last):\n", " File \"/usr/local/lib/python3.7/dist-packages/gradio/routes.py\", line 256, in run_predict\n", " fn_index, raw_input, username, session_state\n", " File \"/usr/local/lib/python3.7/dist-packages/gradio/blocks.py\", line 599, in process_api\n", " predictions, duration = await self.call_function(fn_index, processed_input)\n", " File \"/usr/local/lib/python3.7/dist-packages/gradio/blocks.py\", line 515, in call_function\n", " block_fn.fn, *processed_input, limiter=self.limiter\n", " File \"/usr/local/lib/python3.7/dist-packages/anyio/to_thread.py\", line 32, in run_sync\n", " func, *args, cancellable=cancellable, limiter=limiter\n", " File \"/usr/local/lib/python3.7/dist-packages/anyio/_backends/_asyncio.py\", line 937, in run_sync_in_worker_thread\n", " return await future\n", " File \"/usr/local/lib/python3.7/dist-packages/anyio/_backends/_asyncio.py\", line 867, in run\n", " result = context.run(func, *args)\n", " File \"/usr/local/lib/python3.7/dist-packages/gradio/interface.py\", line 487, in \n", " if len(self.output_components) == 1\n", " File \"/usr/local/lib/python3.7/dist-packages/gradio/interface.py\", line 666, in run_prediction\n", " prediction = self.fn(*processed_input)\n", " File \"\", line 23, in yolo\n", " g = (size / max(im.size)) # gain\n", "AttributeError: 'NoneType' object has no attribute 'size'\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Keyboard interruption in main thread... closing server.\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "(,\n", " 'http://127.0.0.1:7860/',\n", " 'https://24062.gradio.app')" ] }, "metadata": {}, "execution_count": 7 } ], "source": [ "import gradio as gr\n", "import torch\n", "from PIL import Image\n", "\n", "\n", "import os\n", "cwd = os.getcwd()\n", "\n", "# !pip install -qr https://raw.githubusercontent.com/ultralytics/yolov5/master/requirements.txt gradio\n", "\n", "\n", "\n", "# Images\n", "#torch.hub.download_url_to_file('https://github.com/ultralytics/yolov5/raw/master/data/images/zidane.jpg', 'zidane.jpg')\n", "#torch.hub.download_url_to_file('https://github.com/ultralytics/yolov5/raw/master/data/images/bus.jpg', 'bus.jpg')\n", "\n", "# Model\n", "#model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # force_reload=True to update\n", "model = torch.hub.load(cwd+'/yolov5', 'custom', path=cwd+'/saved_model/s1000_best.pt', source='local') # local model\n", "\n", "\n", "def yolo(im, size=640):\n", " g = (size / max(im.size)) # gain\n", " im = im.resize((int(x * g) for x in im.size), Image.ANTIALIAS) # resize\n", "\n", " results = model(im) # inference\n", " results.render() # updates results.imgs with boxes and labels\n", " return Image.fromarray(results.imgs[0])\n", "\n", "\n", "inputs = gr.inputs.Image(type='pil', label=\"Original Image\")\n", "outputs = gr.outputs.Image(type=\"pil\", label=\"Output Image\")\n", "\n", "title = \"S1000 Detection\"\n", "description = \"YOLOv5 Gradio demo for object detection. Upload an image or click an example image to use.\"\n", "article = \"

YOLOv5 is a family of compound-scaled object detection models trained on the COCO dataset, and includes \" \\\n", " \"simple functionality for Test Time Augmentation (TTA), model ensembling, hyperparameter evolution, \" \\\n", " \"and export to ONNX, CoreML and TFLite. Source code |\" \\\n", " \"iOS App | PyTorch Hub

\"\n", "\n", "path_folder = cwd+'/datasets/s1000/'\n", "examples = [[path_folder+'s1000_50_metre.jpg'], [path_folder+'s1000_100_metre.jpg'],[path_folder+'s1000_150_metre.jpg'],[path_folder+'s1000_200_metre.jpg'],[path_folder+'s1000_250_metre.jpg']]\n", "gr.Interface(yolo, inputs, outputs, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch(\n", " debug=True)\n", "\n", "\n", "'''\n", "\n", "\n", "git init\n", "git config user.name bulentsofttech\n", "git config user.email bulent.softtech@gmail.com\n", "git add *\n", "git commit -m \"WriteCommit\"\n", "git push origin master\n", "\n", "\n", "'''" ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "Gradio.ipynb", "provenance": [] }, "kernelspec": { "display_name": "Python 3", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 0 }