Spaces:
Build error
Build error
| 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.9) | |
| draw = ImageDraw.Draw(image) | |
| table_rows = [] | |
| for det in detections: | |
| box = det["box"] | |
| label = det["label"] | |
| score = round(det["score"], 4) | |
| table_rows.append([ | |
| label, | |
| f"{score:.2%}", | |
| int(box["xmin"]), | |
| int(box["ymin"]), | |
| int(box["xmax"]), | |
| int(box["ymax"]) | |
| ]) | |
| table_rows.sort(key=lambda x: float(x[1].strip('%')), reverse=True) | |
| 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() | |