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

# 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)

    # Draw boxes
    draw = ImageDraw.Draw(image)
    for det in detections:
        box = det["box"]
        label = f'{det["label"]} ({det["score"]:.2f})'
        print(f"Drawing box: {box} Label: {label}")  # Debug

        # Draw rectangle
        draw.rectangle(
            [(box["xmin"], box["ymin"]), (box["xmax"], box["ymax"])],
            outline="red",
            width=3
        )
        # Optional: draw label
        draw.text((box["xmin"] + 2, box["ymin"] - 10), label, fill="red")

    return image

gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil", label="Upload Astrophotography Image"),
    outputs=gr.Image(type="pil", label="Detected Objects"),
    title="Astrophotography Object Detector",
    allow_flagging="manual",
).launch()