File size: 1,559 Bytes
34f0c5f
 
b920e42
34f0c5f
 
 
 
5c00058
34f0c5f
 
 
 
 
 
5c00058
34f0c5f
1a8a49f
34f0c5f
5c00058
b920e42
1a8a49f
34f0c5f
 
b920e42
 
1a8a49f
 
 
 
 
 
 
 
 
5c00058
34f0c5f
 
 
 
 
b920e42
34f0c5f
b920e42
34f0c5f
b920e42
34f0c5f
 
 
b920e42
 
 
 
34f0c5f
 
 
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
import gradio as gr
from transformers import pipeline
from PIL import Image, ImageDraw

# Load object detection pipeline
model_pipeline = pipeline(
    task="object-detection", 
    model="bortle/autotrain-ap-obj-detector-2"
)

def predict(image):
    width = 1080
    ratio = width / image.width
    height = int(image.height * ratio)
    image = image.resize((width, height))

    detections = model_pipeline(image, threshold=0.9)

    draw = ImageDraw.Draw(image)
    table_rows = []
    
    for det in detections:
        box = det["box"]
        label = det["label"]
        score = round(det["score"], 4)
        table_rows.append([
            label,
            f"{score:.2%}",
            int(box["xmin"]),
            int(box["ymin"]),
            int(box["xmax"]),
            int(box["ymax"])
        ])
        table_rows.sort(key=lambda x: float(x[1].strip('%')), reverse=True)

        draw.rectangle(
            [(box["xmin"], box["ymin"]), (box["xmax"], box["ymax"])],
            outline="red",
            width=3
        )
        draw.text((box["xmin"] + 4, box["ymin"] - 12), f"{label} ({score:.2f})", fill="red")

    return image, table_rows

# Gradio Interface
gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil", label="Upload Astrophotography Image"),
    outputs=[
        gr.Image(type="pil", label="Detected Objects"),
        gr.Dataframe(headers=["Class", "Confidence", "Xmin", "Ymin", "Xmax", "Ymax"], label="Detections")
    ],
    title="Astrophotography Object Detector",
    allow_flagging="manual",
).launch()