{ "cells": [ { "cell_type": "code", "execution_count": 5, "id": "0db6b22a-5180-45ad-bbcb-aeff29f1af15", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\faiza\\anaconda3\\envs\\hgace\\Lib\\site-packages\\gradio\\interface.py:331: UserWarning: The `allow_flagging` parameter in `Interface` nowtakes a string value ('auto', 'manual', or 'never'), not a boolean. Setting parameter to: 'never'.\n", " warnings.warn(\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7860\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Could not find image processor class in the image processor config or the model config. Loading based on pattern matching with the model's feature extractor configuration.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Keyboard interruption in main thread... closing server.\n" ] }, { "data": { "text/plain": [] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# importing required libraries\n", "from transformers import pipeline\n", "import gradio as gr\n", "from PIL import Image, ImageDraw\n", "\n", "# main function for object detection\n", "def detector(raw):\n", " # Resize the image\n", " WIDTH = 800\n", " width, height = raw.size\n", " ratio = float(WIDTH) / float(width)\n", " new_h = height * ratio\n", " ip_img = raw.resize((int(WIDTH), int(new_h)), Image.Resampling.LANCZOS)\n", "\n", " # load the model pipeline and predict\n", " outs = pipeline(model=\"hustvl/yolos-tiny\")(ip_img)\n", "\n", " # draw the image on the canvas\n", " draw = ImageDraw.Draw(ip_img)\n", "\n", " # draw the boxes with labels\n", " for object in outs:\n", " score = f\"{object['score']*100:.2f}%\"\n", " label = object['label']\n", " xmin, ymin, xmax, ymax = object['box'].values()\n", " draw.rectangle((xmin, ymin, xmax, ymax), outline='red', width=1)\n", " draw.text((xmin, ymin), f\"{label}: {score}\", fill=\"black\")\n", " \n", " return ip_img\n", "\n", "demo = gr.Interface(fn=detector, \n", " inputs=gr.Image(type='pil'),\n", " outputs=gr.Image(type='pil'), allow_flagging=False)\n", "demo.queue(True)\n", "demo.launch(debug=True, inline=False, show_api=False, share=False)" ] } ], "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.11.4" } }, "nbformat": 4, "nbformat_minor": 5 }