Spaces:
Running
Running
File size: 3,246 Bytes
ac7f210 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
{
"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
}
|