borakol's picture
Update app.py
9745baf verified
import gradio as gr
from transformers import pipeline
from PIL import ImageDraw , ImageFont
pipe = pipeline("object-detection" , model = "hustvl/yolos-tiny")
def detect_objects(image):
results = pipe(image)
draw = ImageDraw.Draw(image)
try:
font = ImageFont.truetype("arial.ttf", 18)
except:
font = ImageFont.load_default()
for r in results:
box = r["box"]
label = f"{r['label']} {r['score']:.2f}"
# Stil ayarları
color = "green"
box_width = 4
text_padding = 3
# Bounding box
draw.rectangle(
[(box["xmin"], box["ymin"]), (box["xmax"], box["ymax"])],
outline=color, width=box_width
)
# Yazı ölçümü
bbox = draw.textbbox((0, 0), label, font=font)
text_w, text_h = bbox[2] - bbox[0], bbox[3] - bbox[1]
# Yazı arka planı
text_bg = [
(box["xmin"], box["ymin"] - text_h - text_padding),
(box["xmin"] + text_w + text_padding * 2, box["ymin"])
]
draw.rectangle(text_bg, fill=color)
# Yazı
draw.text(
(box["xmin"] + text_padding, box["ymin"] - text_h - text_padding),
label, fill="white", font=font
)
return image
with gr.Blocks(title = "🎯 Object Detection" , theme = gr.themes.Base()) as demo:
gr.Markdown("## 🎯Object Detection")
with gr.Row():
with gr.Column():
image_input = gr.Image(label = "Upload an Image" , type = "pil")
submit_btn = gr.Button("🔍 Detect Objects")
with gr.Column():
output_image = gr.Image(label= "Result" , type = "pil")
submit_btn.click(
fn = detect_objects ,
inputs = [image_input] ,
outputs= [output_image]
)
demo.launch()