File size: 4,249 Bytes
04217e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
app_core/visualization.py
Convert model outputs to display-ready images for Gradio.
All functions return uint8 numpy arrays [H, W, 3] or PIL Images.
Gradio Image accepts both.
"""

import numpy as np
from PIL import Image, ImageDraw, ImageFont
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from typing import Optional


def mask_to_rgb(
    binary_mask: np.ndarray,
    color: tuple = (45, 180, 150),   # teal accent
) -> np.ndarray:
    """
    Convert binary float mask [H,W] to coloured uint8 [H,W,3].
    Positive pixels are teal, background is near-black.
    """
    h, w   = binary_mask.shape
    canvas = np.zeros((h, w, 3), dtype=np.uint8)
    canvas[binary_mask > 0.5] = color
    canvas[binary_mask <= 0.5] = (15, 20, 25)
    return canvas


def prob_to_heatmap(prob_map: np.ndarray) -> np.ndarray:
    """
    Convert probability map [H,W] to coloured heatmap uint8 [H,W,3].
    Uses 'viridis' colormap for a clean academic look.
    """
    normed  = np.clip(prob_map, 0, 1)
    colored = (cm.viridis(normed)[:, :, :3] * 255).astype(np.uint8)
    return colored


def make_overlay(
    img_np: np.ndarray,
    prob_map: np.ndarray,
    alpha: float = 0.45,
    threshold: float = 0.5,
) -> np.ndarray:
    """
    Overlay probability map on CT image with a teal/hot colourmap.

    Args:
        img_np   : float32 [H,W] normalised CT image
        prob_map : float32 [H,W] sigmoid probabilities
        alpha    : overlay opacity
        threshold: draw contour at this level

    Returns:
        uint8 [H,W,3]
    """
    # Base CT as RGB
    ct_rgb = np.stack([img_np, img_np, img_np], axis=2)
    ct_rgb = (ct_rgb * 255).clip(0, 255).astype(np.uint8)

    # Coloured probability overlay
    heat   = (cm.plasma(np.clip(prob_map, 0, 1))[:, :, :3] * 255).astype(np.uint8)

    # Blend only where probability is meaningful
    mask_a = (prob_map > 0.2).astype(np.float32)
    blend  = (ct_rgb.astype(np.float32) * (1 - alpha * mask_a[:,:,None]) +
              heat.astype(np.float32)   * (alpha * mask_a[:,:,None]))
    result = blend.clip(0, 255).astype(np.uint8)

    # Draw contour at threshold using matplotlib
    try:
        fig, ax = plt.subplots(1, 1, figsize=(256/96, 256/96), dpi=96)
        ax.imshow(result)
        ax.contour(prob_map, levels=[threshold],
                   colors=["#00e5cc"], linewidths=1.5)
        ax.axis("off")
        fig.tight_layout(pad=0)

        # Render to numpy array
        fig.canvas.draw()
        w_px, h_px = fig.canvas.get_width_height()
        buf = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
        buf = buf.reshape(h_px, w_px, 3)
        plt.close(fig)

        # Resize back to 256×256 if needed
        if buf.shape[:2] != (256, 256):
            pil = Image.fromarray(buf).resize((256, 256), Image.BILINEAR)
            buf = np.array(pil)
        return buf

    except Exception:
        # Fallback if matplotlib rendering fails
        plt.close("all")
        return result


def add_caption(img: np.ndarray, caption: str,
                font_size: int = 14) -> np.ndarray:
    """Add a text caption band at the bottom of an image."""
    pil     = Image.fromarray(img)
    band_h  = font_size + 10
    canvas  = Image.new("RGB", (pil.width, pil.height + band_h), (18, 24, 32))
    canvas.paste(pil, (0, 0))
    draw    = ImageDraw.Draw(canvas)
    draw.text((6, pil.height + 4), caption, fill=(180, 200, 210))
    return np.array(canvas)


def build_comparison_row(
    display_img: np.ndarray,
    results: dict,
) -> list:
    """
    Build a list of (image, caption) tuples for Gradio Gallery.
    One entry per model: original, mask, overlay.
    """
    gallery = []
    gallery.append((display_img, "Input CT Slice"))

    for key, result in results.items():
        if result["status"] == "ok":
            mask_img    = result["binary_mask"]
            overlay_img = result["overlay"]
        else:
            h, w = display_img.shape[:2]
            mask_img    = np.zeros((h, w, 3), dtype=np.uint8)
            overlay_img = display_img.copy()

        gallery.append((mask_img,    f"{key} — Mask"))
        gallery.append((overlay_img, f"{key} — Overlay"))

    return gallery