File size: 1,845 Bytes
94475d8
 
1e0acdb
94475d8
 
 
 
 
72fcc90
94475d8
72fcc90
 
 
 
 
 
 
 
 
9745baf
 
72fcc90
 
94475d8
9745baf
94475d8
72fcc90
 
94475d8
 
9745baf
 
 
 
 
72fcc90
 
 
 
 
9745baf
 
72fcc90
 
 
 
94475d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b278901
94475d8
 
 
 
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
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()