Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,103 +6,130 @@ import matplotlib.pyplot as plt
|
|
| 6 |
import io
|
| 7 |
from PIL import Image
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
kz = 2*regionwidth + 1
|
| 17 |
kx = 2*regionwidth + 1
|
| 18 |
-
bg = uniform_filter(
|
| 19 |
-
out =
|
| 20 |
out = (out - out.min()) / (out.max() - out.min() + 1e-6)
|
| 21 |
-
return out
|
| 22 |
-
|
| 23 |
-
def estimate_medline_intensity(
|
| 24 |
-
H, W =
|
| 25 |
-
p1, p99 = np.percentile(
|
| 26 |
-
norm = np.clip((
|
| 27 |
-
z_low = int(0.30
|
| 28 |
-
z_high
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
sm = gaussian_filter1d(img, sigma=4, axis=0)
|
| 34 |
grad = np.gradient(gaussian_filter1d(sm, sigma=1, axis=0), axis=0)
|
| 35 |
-
|
| 36 |
-
return
|
| 37 |
|
| 38 |
def linesweeter(y):
|
| 39 |
from scipy.signal import savgol_filter
|
| 40 |
-
return savgol_filter(y, 9, 2)
|
| 41 |
|
| 42 |
-
def overlay(
|
| 43 |
-
H, W =
|
| 44 |
x = np.arange(W)
|
| 45 |
fig, ax = plt.subplots(figsize=(8,4))
|
| 46 |
-
ax.imshow(
|
| 47 |
ax.plot(x, curve, 'r-', lw=2)
|
| 48 |
-
ax.set_title(title)
|
| 49 |
-
|
| 50 |
-
buf = io.BytesIO()
|
| 51 |
-
plt.savefig(buf, format="png", bbox_inches="tight")
|
| 52 |
-
buf.seek(0)
|
| 53 |
return Image.open(buf)
|
| 54 |
|
|
|
|
| 55 |
with gr.Blocks(title="OCT Step-by-Step Visual Lab") as demo:
|
| 56 |
gr.Markdown("## 🧠 OCT Step-by-Step Visual Lab — Compare cada etapa lado a lado")
|
| 57 |
|
| 58 |
-
img_state = gr.State()
|
| 59 |
|
| 60 |
with gr.Tab("1) Carregar Imagem"):
|
| 61 |
img_input = gr.Image(label="Imagem OCT", type="numpy")
|
| 62 |
def store(img):
|
| 63 |
-
return img
|
| 64 |
img_input.change(store, inputs=img_input, outputs=img_state)
|
| 65 |
|
| 66 |
with gr.Tab("2) Normalização"):
|
| 67 |
btn_norm = gr.Button("Aplicar splitnormalize (balanced)")
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
with gr.Tab("3) Remove Bias"):
|
| 72 |
btn_bias = gr.Button("Aplicar removebias")
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
with gr.Tab("4) Mediana 5×9"):
|
| 77 |
btn_med = gr.Button("Aplicar filtro mediano (5x9)")
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
with gr.Tab("5) Estimar Medline (IS/OS)"):
|
| 82 |
btn_medline = gr.Button("Calcular Medline")
|
| 83 |
out_medline = gr.Image(label="Visualização")
|
| 84 |
-
def show_medline(
|
| 85 |
-
|
| 86 |
-
|
|
|
|
| 87 |
btn_medline.click(show_medline, inputs=img_state, outputs=out_medline)
|
| 88 |
|
| 89 |
with gr.Tab("6) Detectar RPE"):
|
| 90 |
btn_rpe = gr.Button("Detectar RPE simples")
|
| 91 |
out_rpe = gr.Image(label="Visualização")
|
| 92 |
-
def step_rpe(
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
| 96 |
btn_rpe.click(step_rpe, inputs=img_state, outputs=out_rpe)
|
| 97 |
|
| 98 |
with gr.Tab("7) Suavização da RPE"):
|
| 99 |
btn_smooth = gr.Button("Suavizar RPE (linesweeter)")
|
| 100 |
out_smooth = gr.Image(label="Visualização")
|
| 101 |
-
def step_smooth(
|
| 102 |
-
|
| 103 |
-
|
|
|
|
| 104 |
rpe_s = linesweeter(rpe)
|
| 105 |
-
return overlay(
|
| 106 |
btn_smooth.click(step_smooth, inputs=img_state, outputs=out_smooth)
|
| 107 |
|
| 108 |
-
demo.launch()
|
|
|
|
| 6 |
import io
|
| 7 |
from PIL import Image
|
| 8 |
|
| 9 |
+
# ---------- util ----------
|
| 10 |
+
def to_gray2d(arr):
|
| 11 |
+
arr = np.array(arr, dtype=np.float32)
|
| 12 |
+
if arr.ndim == 3 and arr.shape[2] in (3,4):
|
| 13 |
+
arr = arr[..., :3].mean(axis=2)
|
| 14 |
+
elif arr.ndim > 2:
|
| 15 |
+
arr = np.squeeze(arr)
|
| 16 |
+
if arr.max() > 1.0:
|
| 17 |
+
arr = arr / 255.0
|
| 18 |
+
return arr.astype(np.float32)
|
| 19 |
+
|
| 20 |
+
# ---------- pipeline ----------
|
| 21 |
+
def splitnormalize_balanced(img2d):
|
| 22 |
+
p_lo, p_hi = np.percentile(img2d, [2, 98])
|
| 23 |
+
x = np.clip((img2d - p_lo) / (p_hi - p_lo + 1e-6), 0, 1)
|
| 24 |
+
x = exposure.equalize_adapthist(x, clip_limit=0.003, nbins=256)
|
| 25 |
+
return x.astype(np.float32)
|
| 26 |
+
|
| 27 |
+
def removebias(img2d, regionwidth=10, fraction=0.75):
|
| 28 |
kz = 2*regionwidth + 1
|
| 29 |
kx = 2*regionwidth + 1
|
| 30 |
+
bg = uniform_filter(img2d, size=(kz, kx))
|
| 31 |
+
out = img2d - fraction * bg
|
| 32 |
out = (out - out.min()) / (out.max() - out.min() + 1e-6)
|
| 33 |
+
return out.astype(np.float32)
|
| 34 |
+
|
| 35 |
+
def estimate_medline_intensity(img2d):
|
| 36 |
+
H, W = img2d.shape
|
| 37 |
+
p1, p99 = np.percentile(img2d, [1, 99])
|
| 38 |
+
norm = np.clip((img2d - p1) / (p99 - p1 + 1e-6), 0, 1)
|
| 39 |
+
z_low, z_high = int(0.30*H), int(0.70*H)
|
| 40 |
+
return (np.argmax(norm[z_low:z_high, :], axis=0) + z_low).astype(np.float32)
|
| 41 |
+
|
| 42 |
+
def detect_rpe_simple(img2d, medline):
|
| 43 |
+
H, W = img2d.shape
|
| 44 |
+
sm = gaussian_filter1d(img2d, sigma=4, axis=0)
|
|
|
|
| 45 |
grad = np.gradient(gaussian_filter1d(sm, sigma=1, axis=0), axis=0)
|
| 46 |
+
z0, z1 = int(0.30*H), int(0.85*H)
|
| 47 |
+
return (np.argmax(-grad[z0:z1, :], axis=0) + z0).astype(np.float32)
|
| 48 |
|
| 49 |
def linesweeter(y):
|
| 50 |
from scipy.signal import savgol_filter
|
| 51 |
+
return savgol_filter(y, 9, 2).astype(np.float32)
|
| 52 |
|
| 53 |
+
def overlay(img2d, curve, title=""):
|
| 54 |
+
H, W = img2d.shape
|
| 55 |
x = np.arange(W)
|
| 56 |
fig, ax = plt.subplots(figsize=(8,4))
|
| 57 |
+
ax.imshow(img2d, cmap="gray")
|
| 58 |
ax.plot(x, curve, 'r-', lw=2)
|
| 59 |
+
ax.set_title(title); ax.axis('off')
|
| 60 |
+
buf = io.BytesIO(); plt.savefig(buf, format="png", bbox_inches="tight"); buf.seek(0)
|
|
|
|
|
|
|
|
|
|
| 61 |
return Image.open(buf)
|
| 62 |
|
| 63 |
+
# ---------- UI ----------
|
| 64 |
with gr.Blocks(title="OCT Step-by-Step Visual Lab") as demo:
|
| 65 |
gr.Markdown("## 🧠 OCT Step-by-Step Visual Lab — Compare cada etapa lado a lado")
|
| 66 |
|
| 67 |
+
img_state = gr.State() # guarda a imagem 2D corrente (float [0,1])
|
| 68 |
|
| 69 |
with gr.Tab("1) Carregar Imagem"):
|
| 70 |
img_input = gr.Image(label="Imagem OCT", type="numpy")
|
| 71 |
def store(img):
|
| 72 |
+
return to_gray2d(img) if img is not None else None
|
| 73 |
img_input.change(store, inputs=img_input, outputs=img_state)
|
| 74 |
|
| 75 |
with gr.Tab("2) Normalização"):
|
| 76 |
btn_norm = gr.Button("Aplicar splitnormalize (balanced)")
|
| 77 |
+
before_norm = gr.Image(label="Antes")
|
| 78 |
+
after_norm = gr.Image(label="Depois")
|
| 79 |
+
def do_norm(img2d):
|
| 80 |
+
img2d = to_gray2d(img2d)
|
| 81 |
+
out = splitnormalize_balanced(img2d)
|
| 82 |
+
return img2d, out, out # antes, depois, novo estado
|
| 83 |
+
btn_norm.click(do_norm, inputs=img_state, outputs=[before_norm, after_norm, img_state])
|
| 84 |
|
| 85 |
with gr.Tab("3) Remove Bias"):
|
| 86 |
btn_bias = gr.Button("Aplicar removebias")
|
| 87 |
+
before_b = gr.Image(label="Antes")
|
| 88 |
+
after_b = gr.Image(label="Depois")
|
| 89 |
+
def do_bias(img2d):
|
| 90 |
+
img2d = to_gray2d(img2d)
|
| 91 |
+
out = removebias(img2d)
|
| 92 |
+
return img2d, out, out
|
| 93 |
+
btn_bias.click(do_bias, inputs=img_state, outputs=[before_b, after_b, img_state])
|
| 94 |
|
| 95 |
with gr.Tab("4) Mediana 5×9"):
|
| 96 |
btn_med = gr.Button("Aplicar filtro mediano (5x9)")
|
| 97 |
+
before_m = gr.Image(label="Antes")
|
| 98 |
+
after_m = gr.Image(label="Depois")
|
| 99 |
+
def do_med(img2d):
|
| 100 |
+
img2d = to_gray2d(img2d)
|
| 101 |
+
out = median_filter(img2d, size=(5,9)).astype(np.float32)
|
| 102 |
+
return img2d, out, out
|
| 103 |
+
btn_med.click(do_med, inputs=img_state, outputs=[before_m, after_m, img_state])
|
| 104 |
|
| 105 |
with gr.Tab("5) Estimar Medline (IS/OS)"):
|
| 106 |
btn_medline = gr.Button("Calcular Medline")
|
| 107 |
out_medline = gr.Image(label="Visualização")
|
| 108 |
+
def show_medline(img2d):
|
| 109 |
+
img2d = to_gray2d(img2d)
|
| 110 |
+
med = estimate_medline_intensity(img2d)
|
| 111 |
+
return overlay(img2d, med, "Medline (IS/OS)")
|
| 112 |
btn_medline.click(show_medline, inputs=img_state, outputs=out_medline)
|
| 113 |
|
| 114 |
with gr.Tab("6) Detectar RPE"):
|
| 115 |
btn_rpe = gr.Button("Detectar RPE simples")
|
| 116 |
out_rpe = gr.Image(label="Visualização")
|
| 117 |
+
def step_rpe(img2d):
|
| 118 |
+
img2d = to_gray2d(img2d)
|
| 119 |
+
med = estimate_medline_intensity(img2d)
|
| 120 |
+
rpe = detect_rpe_simple(img2d, med)
|
| 121 |
+
return overlay(img2d, rpe, "RPE simples")
|
| 122 |
btn_rpe.click(step_rpe, inputs=img_state, outputs=out_rpe)
|
| 123 |
|
| 124 |
with gr.Tab("7) Suavização da RPE"):
|
| 125 |
btn_smooth = gr.Button("Suavizar RPE (linesweeter)")
|
| 126 |
out_smooth = gr.Image(label="Visualização")
|
| 127 |
+
def step_smooth(img2d):
|
| 128 |
+
img2d = to_gray2d(img2d)
|
| 129 |
+
med = estimate_medline_intensity(img2d)
|
| 130 |
+
rpe = detect_rpe_simple(img2d, med)
|
| 131 |
rpe_s = linesweeter(rpe)
|
| 132 |
+
return overlay(img2d, rpe_s, "RPE suavizada (linesweeter)")
|
| 133 |
btn_smooth.click(step_smooth, inputs=img_state, outputs=out_smooth)
|
| 134 |
|
| 135 |
+
demo.launch()
|