Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
# app.py — Gradio app for panoramic radiograph segmentation (Detectron2)
|
|
|
|
| 2 |
import os, json, time, cv2, numpy as np, torch, gradio as gr
|
| 3 |
from detectron2.config import get_cfg
|
| 4 |
from detectron2.engine import DefaultPredictor
|
|
@@ -6,9 +7,9 @@ from detectron2.data import MetadataCatalog
|
|
| 6 |
from detectron2.utils.visualizer import Visualizer, ColorMode
|
| 7 |
|
| 8 |
# --- Local artifacts (uploaded to this Space) ---
|
| 9 |
-
LOAD_DIR
|
| 10 |
-
WEIGHTS
|
| 11 |
-
CFG_PATH
|
| 12 |
CLASSES_PATH = f"{LOAD_DIR}/classes.json"
|
| 13 |
|
| 14 |
# --- Build cfg & load model ---
|
|
@@ -42,16 +43,16 @@ def segment(rgb: np.ndarray):
|
|
| 42 |
else:
|
| 43 |
rgb_small = rgb
|
| 44 |
|
| 45 |
-
|
|
|
|
| 46 |
inst = outputs["instances"].to("cpu")
|
| 47 |
|
| 48 |
vis = Visualizer(rgb_small, metadata=meta, scale=1.0, instance_mode=ColorMode.IMAGE_BW)
|
| 49 |
-
|
| 50 |
-
overlay_rgb = vis.get_image()
|
| 51 |
|
| 52 |
dets = []
|
| 53 |
boxes_small = inst.pred_boxes.tensor.numpy().tolist() if inst.has("pred_boxes") else []
|
| 54 |
-
scores
|
| 55 |
classes_idx = inst.pred_classes.numpy().tolist() if inst.has("pred_classes") else []
|
| 56 |
inv = (1.0/scale) if scale != 1.0 else 1.0
|
| 57 |
for b, s, c in zip(boxes_small, scores, classes_idx):
|
|
@@ -71,7 +72,8 @@ with gr.Blocks(title="Panoramic Radiograph Segmentation") as demo:
|
|
| 71 |
with gr.Row():
|
| 72 |
img_out = gr.Image(label="Overlay")
|
| 73 |
json_out = gr.JSON(label="Detections (boxes in original image coords)")
|
|
|
|
| 74 |
gr.Button("Run").click(segment, inputs=img_in, outputs=[img_out, json_out], api_name="/predict")
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
| 77 |
-
demo.launch()
|
|
|
|
| 1 |
# app.py — Gradio app for panoramic radiograph segmentation (Detectron2)
|
| 2 |
+
|
| 3 |
import os, json, time, cv2, numpy as np, torch, gradio as gr
|
| 4 |
from detectron2.config import get_cfg
|
| 5 |
from detectron2.engine import DefaultPredictor
|
|
|
|
| 7 |
from detectron2.utils.visualizer import Visualizer, ColorMode
|
| 8 |
|
| 9 |
# --- Local artifacts (uploaded to this Space) ---
|
| 10 |
+
LOAD_DIR = "./artifacts"
|
| 11 |
+
WEIGHTS = f"{LOAD_DIR}/model_final.pth"
|
| 12 |
+
CFG_PATH = f"{LOAD_DIR}/config.yaml"
|
| 13 |
CLASSES_PATH = f"{LOAD_DIR}/classes.json"
|
| 14 |
|
| 15 |
# --- Build cfg & load model ---
|
|
|
|
| 43 |
else:
|
| 44 |
rgb_small = rgb
|
| 45 |
|
| 46 |
+
# Detectron2 expects BGR; our array is RGB
|
| 47 |
+
outputs = predictor(rgb_small[:, :, ::-1])
|
| 48 |
inst = outputs["instances"].to("cpu")
|
| 49 |
|
| 50 |
vis = Visualizer(rgb_small, metadata=meta, scale=1.0, instance_mode=ColorMode.IMAGE_BW)
|
| 51 |
+
overlay_rgb = vis.draw_instance_predictions(inst).get_image()
|
|
|
|
| 52 |
|
| 53 |
dets = []
|
| 54 |
boxes_small = inst.pred_boxes.tensor.numpy().tolist() if inst.has("pred_boxes") else []
|
| 55 |
+
scores = inst.scores.numpy().tolist() if inst.has("scores") else []
|
| 56 |
classes_idx = inst.pred_classes.numpy().tolist() if inst.has("pred_classes") else []
|
| 57 |
inv = (1.0/scale) if scale != 1.0 else 1.0
|
| 58 |
for b, s, c in zip(boxes_small, scores, classes_idx):
|
|
|
|
| 72 |
with gr.Row():
|
| 73 |
img_out = gr.Image(label="Overlay")
|
| 74 |
json_out = gr.JSON(label="Detections (boxes in original image coords)")
|
| 75 |
+
# Keep a stable API route for future programmatic calls
|
| 76 |
gr.Button("Run").click(segment, inputs=img_in, outputs=[img_out, json_out], api_name="/predict")
|
| 77 |
|
| 78 |
if __name__ == "__main__":
|
| 79 |
+
demo.launch() # no share=True on Spaces
|