Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import ImageDraw
|
| 4 |
+
|
| 5 |
+
pipe = pipeline("object-detection" , model = "hustvl/yolos-tiny")
|
| 6 |
+
|
| 7 |
+
def detect_objects(image):
|
| 8 |
+
results = pipe(image)
|
| 9 |
+
draw = ImageDraw.Draw(image)
|
| 10 |
+
|
| 11 |
+
for r in results:
|
| 12 |
+
box = r['box']
|
| 13 |
+
draw.rectangle(
|
| 14 |
+
[(box['xmin'], box['ymin']) , (box['xmax'],box['ymax'])], outline = "blue" , width = 4
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
draw.text((box['xmin'], box['ymin']), f"{r['label']} {r['score']:.2f}" , fill = "blue")
|
| 18 |
+
|
| 19 |
+
for r in results:
|
| 20 |
+
print(f"{r['label']} ({r['score']:.2f}) - Box: {r['box']}")
|
| 21 |
+
|
| 22 |
+
return image
|
| 23 |
+
|
| 24 |
+
with gr.Blocks(title = "🎯 Object Detection" , theme = gr.themes.Base()) as demo:
|
| 25 |
+
|
| 26 |
+
gr.Markdown("## 🎯Object Detection")
|
| 27 |
+
|
| 28 |
+
with gr.Row():
|
| 29 |
+
with gr.Column():
|
| 30 |
+
image_input = gr.Image(label = "Upload an Image" , type = "pil")
|
| 31 |
+
submit_btn = gr.Button("🔍 Detect Objects")
|
| 32 |
+
with gr.Column():
|
| 33 |
+
output_image = gr.Image(label= "Result" , type = "pil")
|
| 34 |
+
|
| 35 |
+
submit_btn.click(
|
| 36 |
+
fn = detect_objects ,
|
| 37 |
+
inputs = [input_image] ,
|
| 38 |
+
outputs= [output_image]
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
demo.launch()
|