Spaces:
Sleeping
Sleeping
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-1" | |
) | |
def predict(image): | |
# Resize the image to width 1080, maintaining aspect ratio | |
width = 1080 | |
ratio = width / image.width | |
height = int(image.height * ratio) | |
resized_image = image.resize((width, height)) | |
# Run object detection | |
detections = model_pipeline(resized_image) | |
# Draw detections on image | |
draw = ImageDraw.Draw(resized_image) | |
for det in detections: | |
box = det["box"] | |
label = f'{det["label"]}: {det["score"]:.2f}' | |
draw.rectangle( | |
[(box["xmin"], box["ymin"]), (box["xmax"], box["ymax"])], | |
outline="red", | |
width=3 | |
) | |
draw.text((box["xmin"], box["ymin"] - 10), label, fill="red") | |
return resized_image | |
# Gradio Interface | |
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() | |