Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image, ImageDraw
|
| 4 |
+
|
| 5 |
+
# Load object detection pipeline
|
| 6 |
+
model_pipeline = pipeline(
|
| 7 |
+
task="object-detection",
|
| 8 |
+
model="bortle/autotrain-ap-obj-detector-1"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
def predict(image):
|
| 12 |
+
# Resize the image to width 1080, maintaining aspect ratio
|
| 13 |
+
width = 1080
|
| 14 |
+
ratio = width / image.width
|
| 15 |
+
height = int(image.height * ratio)
|
| 16 |
+
resized_image = image.resize((width, height))
|
| 17 |
+
|
| 18 |
+
# Run object detection
|
| 19 |
+
detections = model_pipeline(resized_image)
|
| 20 |
+
|
| 21 |
+
# Draw detections on image
|
| 22 |
+
draw = ImageDraw.Draw(resized_image)
|
| 23 |
+
for det in detections:
|
| 24 |
+
box = det["box"]
|
| 25 |
+
label = f'{det["label"]}: {det["score"]:.2f}'
|
| 26 |
+
draw.rectangle(
|
| 27 |
+
[(box["xmin"], box["ymin"]), (box["xmax"], box["ymax"])],
|
| 28 |
+
outline="red",
|
| 29 |
+
width=3
|
| 30 |
+
)
|
| 31 |
+
draw.text((box["xmin"], box["ymin"] - 10), label, fill="red")
|
| 32 |
+
|
| 33 |
+
return resized_image
|
| 34 |
+
|
| 35 |
+
# Gradio Interface
|
| 36 |
+
gr.Interface(
|
| 37 |
+
fn=predict,
|
| 38 |
+
inputs=gr.Image(type="pil", label="Upload Astrophotography Image"),
|
| 39 |
+
outputs=gr.Image(type="pil", label="Detected Objects"),
|
| 40 |
+
title="Astrophotography Object Detector",
|
| 41 |
+
allow_flagging="manual",
|
| 42 |
+
).launch()
|