Spaces:
Sleeping
Sleeping
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() | |