Spaces:
Sleeping
Sleeping
Faizan Azizahmed Shaikh
commited on
Commit
·
ac7f210
1
Parent(s):
f1d5817
Upload 2 files
Browse files- app.py +42 -0
- object_detection.ipynb +109 -0
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
# coding: utf-8
|
| 3 |
+
|
| 4 |
+
# In[ ]:
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
# importing required libraries
|
| 8 |
+
from transformers import pipeline
|
| 9 |
+
import gradio as gr
|
| 10 |
+
from PIL import Image, ImageDraw
|
| 11 |
+
|
| 12 |
+
# main function for object detection
|
| 13 |
+
def detector(raw):
|
| 14 |
+
# Resize the image
|
| 15 |
+
WIDTH = 800
|
| 16 |
+
width, height = raw.size
|
| 17 |
+
ratio = float(WIDTH) / float(width)
|
| 18 |
+
new_h = height * ratio
|
| 19 |
+
ip_img = raw.resize((int(WIDTH), int(new_h)), Image.Resampling.LANCZOS)
|
| 20 |
+
|
| 21 |
+
# load the model pipeline and predict
|
| 22 |
+
outs = pipeline(model="hustvl/yolos-tiny")(ip_img)
|
| 23 |
+
|
| 24 |
+
# draw the image on the canvas
|
| 25 |
+
draw = ImageDraw.Draw(ip_img)
|
| 26 |
+
|
| 27 |
+
# draw the boxes with labels
|
| 28 |
+
for object in outs:
|
| 29 |
+
score = f"{object['score']*100:.2f}%"
|
| 30 |
+
label = object['label']
|
| 31 |
+
xmin, ymin, xmax, ymax = object['box'].values()
|
| 32 |
+
draw.rectangle((xmin, ymin, xmax, ymax), outline='red', width=1)
|
| 33 |
+
draw.text((xmin, ymin), f"{label}: {score}", fill="black")
|
| 34 |
+
|
| 35 |
+
return ip_img
|
| 36 |
+
|
| 37 |
+
demo = gr.Interface(fn=detector,
|
| 38 |
+
inputs=gr.Image(type='pil'),
|
| 39 |
+
outputs=gr.Image(type='pil'), allow_flagging=False)
|
| 40 |
+
demo.queue(True)
|
| 41 |
+
demo.launch(debug=True, inline=False, show_api=False, share=False)
|
| 42 |
+
|
object_detection.ipynb
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 5,
|
| 6 |
+
"id": "0db6b22a-5180-45ad-bbcb-aeff29f1af15",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [
|
| 9 |
+
{
|
| 10 |
+
"name": "stderr",
|
| 11 |
+
"output_type": "stream",
|
| 12 |
+
"text": [
|
| 13 |
+
"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",
|
| 14 |
+
" warnings.warn(\n"
|
| 15 |
+
]
|
| 16 |
+
},
|
| 17 |
+
{
|
| 18 |
+
"name": "stdout",
|
| 19 |
+
"output_type": "stream",
|
| 20 |
+
"text": [
|
| 21 |
+
"Running on local URL: http://127.0.0.1:7860\n",
|
| 22 |
+
"\n",
|
| 23 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
| 24 |
+
]
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"name": "stderr",
|
| 28 |
+
"output_type": "stream",
|
| 29 |
+
"text": [
|
| 30 |
+
"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"
|
| 31 |
+
]
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
"name": "stdout",
|
| 35 |
+
"output_type": "stream",
|
| 36 |
+
"text": [
|
| 37 |
+
"Keyboard interruption in main thread... closing server.\n"
|
| 38 |
+
]
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"data": {
|
| 42 |
+
"text/plain": []
|
| 43 |
+
},
|
| 44 |
+
"execution_count": 5,
|
| 45 |
+
"metadata": {},
|
| 46 |
+
"output_type": "execute_result"
|
| 47 |
+
}
|
| 48 |
+
],
|
| 49 |
+
"source": [
|
| 50 |
+
"# importing required libraries\n",
|
| 51 |
+
"from transformers import pipeline\n",
|
| 52 |
+
"import gradio as gr\n",
|
| 53 |
+
"from PIL import Image, ImageDraw\n",
|
| 54 |
+
"\n",
|
| 55 |
+
"# main function for object detection\n",
|
| 56 |
+
"def detector(raw):\n",
|
| 57 |
+
" # Resize the image\n",
|
| 58 |
+
" WIDTH = 800\n",
|
| 59 |
+
" width, height = raw.size\n",
|
| 60 |
+
" ratio = float(WIDTH) / float(width)\n",
|
| 61 |
+
" new_h = height * ratio\n",
|
| 62 |
+
" ip_img = raw.resize((int(WIDTH), int(new_h)), Image.Resampling.LANCZOS)\n",
|
| 63 |
+
"\n",
|
| 64 |
+
" # load the model pipeline and predict\n",
|
| 65 |
+
" outs = pipeline(model=\"hustvl/yolos-tiny\")(ip_img)\n",
|
| 66 |
+
"\n",
|
| 67 |
+
" # draw the image on the canvas\n",
|
| 68 |
+
" draw = ImageDraw.Draw(ip_img)\n",
|
| 69 |
+
"\n",
|
| 70 |
+
" # draw the boxes with labels\n",
|
| 71 |
+
" for object in outs:\n",
|
| 72 |
+
" score = f\"{object['score']*100:.2f}%\"\n",
|
| 73 |
+
" label = object['label']\n",
|
| 74 |
+
" xmin, ymin, xmax, ymax = object['box'].values()\n",
|
| 75 |
+
" draw.rectangle((xmin, ymin, xmax, ymax), outline='red', width=1)\n",
|
| 76 |
+
" draw.text((xmin, ymin), f\"{label}: {score}\", fill=\"black\")\n",
|
| 77 |
+
" \n",
|
| 78 |
+
" return ip_img\n",
|
| 79 |
+
"\n",
|
| 80 |
+
"demo = gr.Interface(fn=detector, \n",
|
| 81 |
+
" inputs=gr.Image(type='pil'),\n",
|
| 82 |
+
" outputs=gr.Image(type='pil'), allow_flagging=False)\n",
|
| 83 |
+
"demo.queue(True)\n",
|
| 84 |
+
"demo.launch(debug=True, inline=False, show_api=False, share=False)"
|
| 85 |
+
]
|
| 86 |
+
}
|
| 87 |
+
],
|
| 88 |
+
"metadata": {
|
| 89 |
+
"kernelspec": {
|
| 90 |
+
"display_name": "Python 3 (ipykernel)",
|
| 91 |
+
"language": "python",
|
| 92 |
+
"name": "python3"
|
| 93 |
+
},
|
| 94 |
+
"language_info": {
|
| 95 |
+
"codemirror_mode": {
|
| 96 |
+
"name": "ipython",
|
| 97 |
+
"version": 3
|
| 98 |
+
},
|
| 99 |
+
"file_extension": ".py",
|
| 100 |
+
"mimetype": "text/x-python",
|
| 101 |
+
"name": "python",
|
| 102 |
+
"nbconvert_exporter": "python",
|
| 103 |
+
"pygments_lexer": "ipython3",
|
| 104 |
+
"version": "3.11.4"
|
| 105 |
+
}
|
| 106 |
+
},
|
| 107 |
+
"nbformat": 4,
|
| 108 |
+
"nbformat_minor": 5
|
| 109 |
+
}
|