Spaces:
Build error
Build error
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
DEBRI-X Gradio Space Demo
|
| 3 |
+
Drop this file + yolo_best.pt into a Hugging Face Space (SDK=gradio).
|
| 4 |
+
requirements.txt: ultralytics opencv-python-headless huggingface_hub gradio
|
| 5 |
+
"""
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import numpy as np, cv2
|
| 8 |
+
from PIL import Image
|
| 9 |
+
|
| 10 |
+
MODEL_REPO = "Premchan369/debrex-space-debris-capture"
|
| 11 |
+
try:
|
| 12 |
+
from huggingface_hub import hf_hub_download
|
| 13 |
+
from ultralytics import YOLO
|
| 14 |
+
wt = hf_hub_download(MODEL_REPO, "yolo_best.pt")
|
| 15 |
+
yolo = YOLO(wt)
|
| 16 |
+
LOADED = True
|
| 17 |
+
except Exception as e:
|
| 18 |
+
print(f"Model load failed: {e}")
|
| 19 |
+
LOADED = False
|
| 20 |
+
|
| 21 |
+
def detect(image, conf=0.25):
|
| 22 |
+
if image is None:
|
| 23 |
+
return None, "Please upload an image."
|
| 24 |
+
img_bgr = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 25 |
+
if not LOADED:
|
| 26 |
+
H, W = img_bgr.shape[:2]
|
| 27 |
+
cv2.rectangle(img_bgr, (W//4, H//4), (3*W//4, 3*H//4), (0,200,100), 2)
|
| 28 |
+
return Image.fromarray(cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)), "Demo mode."
|
| 29 |
+
yolo.conf = conf
|
| 30 |
+
res = yolo(img_bgr, verbose=False)
|
| 31 |
+
ann = res[0].plot()
|
| 32 |
+
n = len(res[0].boxes)
|
| 33 |
+
cfs = [f"{c:.2f}" for c in res[0].boxes.conf.cpu().numpy()]
|
| 34 |
+
info = f"Objects: {n}\nConf: {', '.join(cfs) or 'none'}\nModel: YOLOv8n (DEBRI-X)"
|
| 35 |
+
return Image.fromarray(cv2.cvtColor(ann, cv2.COLOR_BGR2RGB)), info
|
| 36 |
+
|
| 37 |
+
with gr.Blocks(title="DEBRI-X Debris Detection") as demo:
|
| 38 |
+
gr.Markdown("# 🛰️ DEBRI-X — Space Debris Detection")
|
| 39 |
+
with gr.Row():
|
| 40 |
+
with gr.Column():
|
| 41 |
+
inp = gr.Image(type="pil", label="Input")
|
| 42 |
+
conf = gr.Slider(0.05, 0.95, 0.25, label="Confidence")
|
| 43 |
+
btn = gr.Button("Detect", variant="primary")
|
| 44 |
+
with gr.Column():
|
| 45 |
+
out_img = gr.Image(type="pil", label="Result")
|
| 46 |
+
out_info = gr.Textbox(label="Info", lines=4)
|
| 47 |
+
btn.click(detect, [inp, conf], [out_img, out_info])
|
| 48 |
+
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
demo.launch()
|