ap-obj-detect / app.py
ppicazo's picture
Update app.py
b920e42 verified
raw
history blame
1.54 kB
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.1)
draw = ImageDraw.Draw(image)
table_rows = []
for det in detections:
box = det["box"]
label = det["label"]
score = round(det["score"], 4)
table_rows.append({
"Class": label,
"Confidence": f"{score:.2%}",
"Xmin": int(box["xmin"]),
"Ymin": int(box["ymin"]),
"Xmax": int(box["xmax"]),
"Ymax": int(box["ymax"]),
})
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()