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image import zoom
|
5 |
+
import torch
|
6 |
+
from core.models.dani_model import dani_model
|
7 |
+
import numpy as np
|
8 |
+
from PIL import Image
|
9 |
+
import base64
|
10 |
+
import time
|
11 |
+
|
12 |
+
|
13 |
+
# Funzione per convertire un'immagine in base64
|
14 |
+
def image_to_base64(image_path):
|
15 |
+
with open(image_path, "rb") as img_file:
|
16 |
+
return base64.b64encode(img_file.read()).decode()
|
17 |
+
|
18 |
+
|
19 |
+
st.markdown("""
|
20 |
+
<style>
|
21 |
+
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
|
22 |
+
/* Apply the font to everything */
|
23 |
+
html, body, [class*="st"] {
|
24 |
+
font-family: 'Roboto', sans-serif;
|
25 |
+
}
|
26 |
+
</style>
|
27 |
+
""", unsafe_allow_html=True)
|
28 |
+
|
29 |
+
|
30 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
31 |
+
|
32 |
+
# Dati di esempio predefiniti
|
33 |
+
esempi = {
|
34 |
+
"Frontal ➔ Lateral": {'Frontal': 'FtoL.png', 'Lateral': 'LfromF.png'},
|
35 |
+
"Frontal ➔ Report": {'Frontal': '31d9847f-987fcf63-704f7496-d2b21eb8-63cd973e.tiff', 'Report': 'Small bilateral pleural effusions, left greater than right.'},
|
36 |
+
"Frontal ➔ Lateral + Report": {'Frontal': '81bca127-0c416084-67f8033c-ecb26476-6d1ecf60.tiff', 'Lateral': 'd52a0c5c-bb7104b0-b1d821a5-959984c3-33c04ccb.tiff', 'Report': 'No acute intrathoracic process. Heart Size is normal. Lungs are clear. No pneumothorax'},
|
37 |
+
"Lateral ➔ Frontal": {'Lateral': 'LtoF.png', 'Frontal': 'FfromL.png'},
|
38 |
+
"Lateral ➔ Report": {'Lateral': 'd52a0c5c-bb7104b0-b1d821a5-959984c3-33c04ccb.tiff', 'Report': 'no acute cardiopulmonary process. if concern for injury persists, a dedicated rib series with markers would be necessary to ensure no rib fractures.'},
|
39 |
+
"Lateral ➔ Frontal + Report": {'Lateral': 'reald52a0c5c-bb7104b0-b1d821a5-959984c3-33c04ccb.tiff', 'Frontal': 'ab37274f-b4c1fc04-e2ff24b4-4a130ba3-cd167968.tiff', 'Report': 'No acute intrathoracic process. If there is strong concern for rib fracture, a dedicated rib series may be performed.'},
|
40 |
+
"Report ➔ Frontal": {'Report': 'Left lung opacification which may reflect pneumonia superimposed on metastatic disease.', 'Frontal': '02aa804e-bde0afdd-112c0b34-7bc16630-4e384014.tiff'},
|
41 |
+
"Report ➔ Lateral": {'Report': 'Bilateral pleural effusions, cardiomegaly and mild edema suggest fluid overload.', 'Lateral': '489faba7-a9dc5f1d-fd7241d6-9638d855-eaa952b1.tiff'},
|
42 |
+
"Report ➔ Frontal + Lateral": {'Report': 'No acute intrathoracic process. The lungs are clean and heart is normal size.', 'Frontal': 'f27ba7cd-44486c2e-29f3e890-f2b9f94e-84110448.tiff', 'Lateral': 'b20c9570-de77944a-b8604ba0-73305a7b-d608a72b.tiff'},
|
43 |
+
"Frontal + Lateral ➔ Report": {'Frontal': '95856dd1-5878b5b1-9c104817-760c0122-6187946f.tiff', 'Lateral': '3723d912-71940d69-4fef2dd2-27af5a7b-127ba20c.tiff', 'Report': 'Opacities in the right upper or middle lobe, maybe early pneumonia.'},
|
44 |
+
"Frontal + Report ➔ Lateral": {'Frontal': 'e7f21453-7956d79a-44e44614-fae8ff16-d174d1a0.tiff', 'Report': 'No focal consolidation.', 'Lateral': '8037e6b9-06367464-a4ccd63a-5c5c5a81-ce3e7ffc.tiff'},
|
45 |
+
"Lateral + Report ➔ Frontal": {'Lateral': '02c66644-b1883a91-54aed0e7-62d25460-398f9865.tiff', 'Report': 'No evidence of acute cardiopulmonary process.', 'Frontal': 'b1f169f1-12177dd5-2fa1c4b1-7b816311-85d769e9.tiff'}
|
46 |
+
}
|
47 |
+
|
48 |
+
|
49 |
+
# CSS per personalizzare il tema
|
50 |
+
st.markdown("""
|
51 |
+
<style>
|
52 |
+
/* Sfondo scuro */
|
53 |
+
body {
|
54 |
+
background-color: #121212;
|
55 |
+
color: white;
|
56 |
+
}
|
57 |
+
/* Personalizzazione del titolo */
|
58 |
+
.title {
|
59 |
+
font-size: 35px !important;
|
60 |
+
font-weight: bold;
|
61 |
+
color: #f63366;
|
62 |
+
}
|
63 |
+
/* Personalizzazione dei sottotitoli e testi principali */
|
64 |
+
.stText, .stButton, .stMarkdown {
|
65 |
+
font-size: 18px !important;
|
66 |
+
}
|
67 |
+
</style>
|
68 |
+
""", unsafe_allow_html=True)
|
69 |
+
|
70 |
+
|
71 |
+
# Sostituisci questo con il link dell'immagine online
|
72 |
+
logo_1_path = "./DEMO/Loghi/Logo_UCBM.png" # Sostituisci con il percorso del primo logo
|
73 |
+
logo_2_path = "./DEMO/Loghi/Logo UmU.png" # Sostituisci con il percorso del secondo logo
|
74 |
+
logo_3_path = "./DEMO/Loghi/Logo COSBI.png" # Sostituisci con il percorso del terzo logo
|
75 |
+
logo_4_path = "./DEMO/Loghi/logo trasparent.png" # Sostituisci con il percorso del quarto logo
|
76 |
+
# Converti le immagini in base64
|
77 |
+
logo_1_base64 = image_to_base64(logo_1_path)
|
78 |
+
logo_2_base64 = image_to_base64(logo_2_path)
|
79 |
+
logo_3_base64 = image_to_base64(logo_3_path)
|
80 |
+
logo_4_base64 = image_to_base64(logo_4_path)
|
81 |
+
|
82 |
+
# CSS per posizionare i loghi in basso a destra e renderli piccoli
|
83 |
+
st.markdown(f"""
|
84 |
+
<style>
|
85 |
+
.footer {{
|
86 |
+
position: fixed;
|
87 |
+
bottom: 20px;
|
88 |
+
right: 20px;
|
89 |
+
z-index: 100;
|
90 |
+
display: flex;
|
91 |
+
gap: 10px; /* Spazio tra i loghi */
|
92 |
+
}}
|
93 |
+
.footer img {{
|
94 |
+
height: 60px; /* Altezza dei loghi */
|
95 |
+
width: auto; /* Mantiene il rapporto di aspetto originale */
|
96 |
+
}}
|
97 |
+
</style>
|
98 |
+
<div class="footer">
|
99 |
+
<img src="data:image/png;base64,{logo_1_base64}" alt="Logo 1">
|
100 |
+
<img src="data:image/png;base64,{logo_2_base64}" alt="Logo 2">
|
101 |
+
<img src="data:image/png;base64,{logo_3_base64}" alt="Logo 3">
|
102 |
+
<img src="data:image/png;base64,{logo_4_base64}" alt="Logo 4">
|
103 |
+
</div>
|
104 |
+
""", unsafe_allow_html=True)
|
105 |
+
|
106 |
+
# Inizializzazione dello stato della sessione
|
107 |
+
if 'step' not in st.session_state:
|
108 |
+
st.session_state['step'] = 1
|
109 |
+
if 'selected_option' not in st.session_state:
|
110 |
+
st.session_state['selected_option'] = None
|
111 |
+
if 'frontal_file' not in st.session_state:
|
112 |
+
st.session_state['frontal_file'] = None
|
113 |
+
if 'lateral_file' not in st.session_state:
|
114 |
+
st.session_state['lateral_file'] = None
|
115 |
+
if 'report' not in st.session_state:
|
116 |
+
st.session_state['report'] = ""
|
117 |
+
if 'inputs' not in st.session_state:
|
118 |
+
st.session_state['inputs'] = None
|
119 |
+
if 'outputs' not in st.session_state:
|
120 |
+
st.session_state['outputs'] = None
|
121 |
+
if 'frontal' not in st.session_state:
|
122 |
+
st.session_state['frontal'] = None
|
123 |
+
if 'lateral' not in st.session_state:
|
124 |
+
st.session_state['lateral'] = None
|
125 |
+
if 'report' not in st.session_state:
|
126 |
+
st.session_state['report'] = ""
|
127 |
+
if 'generate' not in st.session_state:
|
128 |
+
st.session_state['generate'] = False
|
129 |
+
|
130 |
+
# Inizializza inference_tester solo una volta
|
131 |
+
if 'inference_tester' not in st.session_state:
|
132 |
+
st.session_state['inference_tester'] = 1
|
133 |
+
|
134 |
+
# Usa inference_tester dalla sessione
|
135 |
+
inference_tester = st.session_state['inference_tester']
|
136 |
+
|
137 |
+
|
138 |
+
st.markdown('<h1 style="text-align: center" class="title">MedCoDi-M</h1>', unsafe_allow_html=True)
|
139 |
+
|
140 |
+
if st.session_state['step'] == 1:
|
141 |
+
# Breve descrizione del lavoro
|
142 |
+
st.markdown("""
|
143 |
+
<div style='text-align: justify; font-size: 18px; line-height: 1.6;'>
|
144 |
+
This work introduces MedCoDi-M, a novel multi-prompt foundation model for multi-modal medical data generation.
|
145 |
+
In this demo, you will be able to perform various generation tasks including frontal and lateral chest X-rays and clinical report generation.
|
146 |
+
MedCoDi-M enables flexible, any-to-any generation across different medical data modalities, utilizing contrastive learning and a modular approach for enhanced performance.
|
147 |
+
</div>
|
148 |
+
""", unsafe_allow_html=True)
|
149 |
+
|
150 |
+
# lasciamo un po' di spazio
|
151 |
+
st.markdown('<br>', unsafe_allow_html=True)
|
152 |
+
|
153 |
+
# Immagine con didascalia migliorata e con dimensione della caption aumentata
|
154 |
+
image_path = "./DEMO/Loghi/model_final.png" # Sostituisci con il percorso della tua immagine
|
155 |
+
st.image(image_path, caption='', use_container_width=True)
|
156 |
+
|
157 |
+
# Caption con dimensione del testo migliorata
|
158 |
+
st.markdown("""
|
159 |
+
<div style='text-align: center; font-size: 16px; font-style: italic; margin-top: 10px;'>
|
160 |
+
Framework of MedCoDi-M: This demo allows you to generate frontal and lateral chest X-rays, as well as medical reports, through the MedCoDi-M model.
|
161 |
+
</div>
|
162 |
+
""", unsafe_allow_html=True)
|
163 |
+
|
164 |
+
# lasciamo un po' di spazio
|
165 |
+
st.markdown('<br>', unsafe_allow_html=True)
|
166 |
+
|
167 |
+
# Bottone con testo "Try it out"
|
168 |
+
if st.button("Try it out!"):
|
169 |
+
st.session_state['step'] = 2
|
170 |
+
st.rerun()
|
171 |
+
|
172 |
+
|
173 |
+
# Fase 1: Selezione dell'opzione
|
174 |
+
if st.session_state['step'] == 2:
|
175 |
+
# Opzioni disponibili
|
176 |
+
options = [
|
177 |
+
"Frontal ➔ Lateral", "Frontal ➔ Report", "Frontal ➔ Lateral + Report",
|
178 |
+
"Lateral ➔ Frontal", "Lateral ➔ Report", "Lateral ➔ Frontal + Report",
|
179 |
+
"Report ➔ Frontal", "Report ➔ Lateral", "Report ➔ Frontal + Lateral",
|
180 |
+
"Frontal + Lateral ➔ Report", "Frontal + Report ➔ Lateral", "Lateral + Report ➔ Frontal"
|
181 |
+
]
|
182 |
+
|
183 |
+
# Messaggio di selezione con dimensione aumentata
|
184 |
+
st.markdown(
|
185 |
+
"<h4 style='text-align: justify'><strong>Select the type of generation you want to perform:</strong></h4>",
|
186 |
+
unsafe_allow_html=True)
|
187 |
+
|
188 |
+
# Aumentare la dimensione di "Please select an option:"
|
189 |
+
st.markdown(
|
190 |
+
"<h4 style='text-align: justify'><strong>Please select an option:</strong></h4>",
|
191 |
+
unsafe_allow_html=True)
|
192 |
+
|
193 |
+
# Reset esplicito del valore di `selectbox` in caso di reset
|
194 |
+
st.session_state['selected_option'] = st.selectbox(
|
195 |
+
"", options, key='selectbox_option', index=0) # Rimuoviamo il testo dal selectbox
|
196 |
+
|
197 |
+
st.markdown('<br>', unsafe_allow_html=True)
|
198 |
+
|
199 |
+
# Creiamo colonne per i pulsanti
|
200 |
+
col1, col2, col3 = st.columns(3)
|
201 |
+
|
202 |
+
# Pulsante per provare un esempio
|
203 |
+
with col1:
|
204 |
+
if st.button("Try an example"):
|
205 |
+
st.session_state['step'] = 5 # Passa al passo 5
|
206 |
+
st.rerun()
|
207 |
+
|
208 |
+
# Pulsante per tornare all'inizio
|
209 |
+
with col2:
|
210 |
+
if st.button("Return to the beginning"):
|
211 |
+
# Ripristina lo stato della sessione
|
212 |
+
st.session_state['step'] = 1
|
213 |
+
st.session_state['selected_option'] = None
|
214 |
+
st.session_state['selected_option2'] = None
|
215 |
+
st.session_state['frontal_file'] = None
|
216 |
+
st.session_state['lateral_file'] = None
|
217 |
+
st.session_state['report'] = ""
|
218 |
+
st.rerun()
|
219 |
+
|
220 |
+
|
221 |
+
# Fase 2: Caricamento file
|
222 |
+
if st.session_state['step'] == 3:
|
223 |
+
st.markdown(
|
224 |
+
f"<h4 style='text-align: justify'><strong>You selected: {st.session_state['selected_option']}. Now, please upload the required files below:</strong></h4>",
|
225 |
+
unsafe_allow_html=True)
|
226 |
+
|
227 |
+
# Carica l'immagine frontale
|
228 |
+
if "Frontal" in st.session_state['selected_option'].split(" ➔")[0]:
|
229 |
+
st.markdown("<h5 style='font-size: 18px;'>Load the Frontal X-ray in DICOM format</h5>", unsafe_allow_html=True)
|
230 |
+
st.session_state['frontal_file'] = st.file_uploader("", type=["dcm"])
|
231 |
+
|
232 |
+
# Carica l'immagine laterale
|
233 |
+
if "Lateral" in st.session_state['selected_option'].split(" ➔")[0]:
|
234 |
+
st.markdown("<h5 style='font-size: 18px;'>Load the Lateral X-ray in DICOM format</h5>", unsafe_allow_html=True)
|
235 |
+
st.session_state['lateral_file'] = st.file_uploader("", type=["dcm"])
|
236 |
+
|
237 |
+
# Inserisci il report clinico
|
238 |
+
if "Report" in st.session_state['selected_option'].split(" ➔")[0]:
|
239 |
+
st.markdown("<h5 style='font-size: 18px;'>Type the clinical report</h5>", unsafe_allow_html=True)
|
240 |
+
st.session_state['report'] = st.text_area("", value=st.session_state['report'])
|
241 |
+
|
242 |
+
# lasciamo un po' di spazio
|
243 |
+
st.markdown('<br>', unsafe_allow_html=True)
|
244 |
+
|
245 |
+
# Creare colonne per allineare i pulsanti in orizzontale
|
246 |
+
col1, col2 = st.columns(2)
|
247 |
+
|
248 |
+
with col1:
|
249 |
+
if st.button("Start Generation"):
|
250 |
+
frontal = None
|
251 |
+
lateral = None
|
252 |
+
report = None
|
253 |
+
# Dato che questo step è velocissimo, prima di procedere mettiamo una finta barra di caricamento di 3 secondi
|
254 |
+
with st.spinner("Preprocessing the data..."):
|
255 |
+
time.sleep(3)
|
256 |
+
# Controllo che i file necessari siano stati caricati
|
257 |
+
if "Frontal" in st.session_state['selected_option'].split(" ➔")[0] and not st.session_state['frontal_file']:
|
258 |
+
st.error("Load the Frontal image.")
|
259 |
+
elif "Lateral" in st.session_state['selected_option'].split(" ➔")[0] and not st.session_state['lateral_file']:
|
260 |
+
st.error("Load the Lateral image.")
|
261 |
+
elif "Report" in st.session_state['selected_option'].split(" ➔")[0] and not st.session_state['report']:
|
262 |
+
st.error("Type the clinical report.")
|
263 |
+
else:
|
264 |
+
st.write(f"Execution of: {st.session_state['selected_option']}")
|
265 |
+
|
266 |
+
# Carica l'immagine e avvia l'inferenza
|
267 |
+
if st.session_state['frontal_file']:
|
268 |
+
dicom = pydicom.dcmread(st.session_state['frontal_file'])
|
269 |
+
image = dicom.pixel_array
|
270 |
+
if dicom.PhotometricInterpretation == 'MONOCHROME1':
|
271 |
+
image = (2 ** dicom.BitsStored - 1) - image
|
272 |
+
if dicom.ImagerPixelSpacing != [0.139, 0.139]:
|
273 |
+
zoom_factor = [0.139 / dicom.ImagerPixelSpacing[0], 0.139 / dicom.ImagerPixelSpacing[1]]
|
274 |
+
image = zoom(image, zoom_factor)
|
275 |
+
image = image / (2 ** dicom.BitsStored - 1)
|
276 |
+
# Se l'immagine non è quadrata, facciamo padding
|
277 |
+
if image.shape[0] != image.shape[1]:
|
278 |
+
diff = abs(image.shape[0] - image.shape[1])
|
279 |
+
pad_size = diff // 2
|
280 |
+
if image.shape[0] > image.shape[1]:
|
281 |
+
padded_image = np.pad(image, ((0, 0), (pad_size, pad_size)))
|
282 |
+
else:
|
283 |
+
padded_image = np.pad(image, ((pad_size, pad_size), (0, 0)))
|
284 |
+
# Resizing a 256x256 e a 512x512
|
285 |
+
zoom_factor = [256 / padded_image.shape[0], 256 / padded_image.shape[1]]
|
286 |
+
image_256 = zoom(padded_image, zoom_factor)
|
287 |
+
frontal = image_256
|
288 |
+
if frontal.dtype != np.uint8:
|
289 |
+
frontal2 = (255 * (frontal - frontal.min()) / (frontal.max() - frontal.min())).astype(np.uint8)
|
290 |
+
frontal = torch.tensor(frontal, dtype=torch.float32).unsqueeze(0).unsqueeze(0)
|
291 |
+
frontal2 = Image.fromarray(frontal2)
|
292 |
+
st.write("Frontal Image loaded successfully!")
|
293 |
+
# Mostra l'immagine caricata
|
294 |
+
st.image(frontal2, caption="Frontal Image Loaded", use_container_width=True)
|
295 |
+
if st.session_state['lateral_file']:
|
296 |
+
dicom = pydicom.dcmread(st.session_state['lateral_file'])
|
297 |
+
image = dicom.pixel_array
|
298 |
+
if dicom.PhotometricInterpretation == 'MONOCHROME1':
|
299 |
+
image = (2 ** dicom.BitsStored - 1) - image
|
300 |
+
if dicom.ImagerPixelSpacing != [0.139, 0.139]:
|
301 |
+
zoom_factor = [0.139 / dicom.ImagerPixelSpacing[0], 0.139 / dicom.ImagerPixelSpacing[1]]
|
302 |
+
image = zoom(image, zoom_factor)
|
303 |
+
image = image / (2 ** dicom.BitsStored - 1)
|
304 |
+
# Se l'immagine non è quadrata, facciamo padding
|
305 |
+
if image.shape[0] != image.shape[1]:
|
306 |
+
diff = abs(image.shape[0] - image.shape[1])
|
307 |
+
pad_size = diff // 2
|
308 |
+
if image.shape[0] > image.shape[1]:
|
309 |
+
padded_image = np.pad(image, ((0, 0), (pad_size, pad_size)))
|
310 |
+
else:
|
311 |
+
padded_image = np.pad(image, ((pad_size, pad_size), (0, 0)))
|
312 |
+
# Resizing a 256x256 e a 512x512
|
313 |
+
zoom_factor = [256 / padded_image.shape[0], 256 / padded_image.shape[1]]
|
314 |
+
image_256 = zoom(padded_image, zoom_factor)
|
315 |
+
lateral = image_256
|
316 |
+
if lateral.dtype != np.uint8:
|
317 |
+
lateral2 = (255 * (lateral - lateral.min()) / (lateral.max() - lateral.min())).astype(np.uint8)
|
318 |
+
lateral = torch.tensor(lateral, dtype=torch.float32).unsqueeze(0).unsqueeze(0)
|
319 |
+
lateral2 = Image.Frontalmarray(lateral2)
|
320 |
+
st.write("Lateral Image loaded successfully!")
|
321 |
+
st.image(lateral2, caption="Lateral Image Loaded", use_container_width=True)
|
322 |
+
if st.session_state['report']:
|
323 |
+
report = st.session_state['report']
|
324 |
+
st.write(f"Loaded Report: {report}")
|
325 |
+
|
326 |
+
inputs = []
|
327 |
+
if "Frontal" in st.session_state['selected_option'].split(" ➔")[0]:
|
328 |
+
inputs.append('frontal')
|
329 |
+
if "Lateral" in st.session_state['selected_option'].split(" ➔")[0]:
|
330 |
+
inputs.append('lateral')
|
331 |
+
if "Report" in st.session_state['selected_option'].split(" ➔")[0]:
|
332 |
+
inputs.append('text')
|
333 |
+
|
334 |
+
# Ora vediamo cosa c'è dopo la freccia
|
335 |
+
outputs = []
|
336 |
+
if "Frontal" in st.session_state['selected_option'].split(" ➔")[1]:
|
337 |
+
outputs.append('frontal')
|
338 |
+
if "Lateral" in st.session_state['selected_option'].split(" ➔")[1]:
|
339 |
+
outputs.append('lateral')
|
340 |
+
if "Report" in st.session_state['selected_option'].split(" ➔")[1]:
|
341 |
+
outputs.append('text')
|
342 |
+
|
343 |
+
# Ultima cosa che va fatta è passare allo step 4, prima di farlo però, tutte le variabili che ci servono
|
344 |
+
# devono essere salvate nello stato della sessione
|
345 |
+
st.session_state['inputs'] = inputs
|
346 |
+
st.session_state['outputs'] = outputs
|
347 |
+
st.session_state['frontal'] = frontal
|
348 |
+
st.session_state['lateral'] = lateral
|
349 |
+
st.session_state['report'] = report
|
350 |
+
st.session_state['generate'] = True
|
351 |
+
|
352 |
+
st.session_state['step'] = 4
|
353 |
+
st.rerun()
|
354 |
+
|
355 |
+
with col2:
|
356 |
+
if st.button("Return to the beginning"):
|
357 |
+
# Ripristina lo stato della sessione
|
358 |
+
st.session_state['step'] = 1
|
359 |
+
st.session_state['selected_option'] = None
|
360 |
+
st.session_state['selected_option2'] = None
|
361 |
+
st.session_state['frontal_file'] = None
|
362 |
+
st.session_state['lateral_file'] = None
|
363 |
+
st.session_state['report'] = ""
|
364 |
+
st.rerun()
|
365 |
+
|
366 |
+
if st.session_state['step'] == 4:
|
367 |
+
st.write("Generation completed successfully!")
|
368 |
+
st.session_state['generate'] = False
|
369 |
+
|
370 |
+
if st.button("Return to the beginning"):
|
371 |
+
# Ripristina lo stato della sessione
|
372 |
+
st.session_state['generate'] = False
|
373 |
+
st.session_state['step'] = 1
|
374 |
+
st.session_state['selected_option'] = None
|
375 |
+
st.session_state['frontal_file'] = None
|
376 |
+
st.session_state['lateral_file'] = None
|
377 |
+
st.session_state['report'] = ""
|
378 |
+
st.session_state['inputs'] = None
|
379 |
+
st.session_state['outputs'] = None
|
380 |
+
st.session_state['frontal'] = None
|
381 |
+
st.session_state['lateral'] = None
|
382 |
+
st.session_state['report'] = ""
|
383 |
+
st.rerun()
|
384 |
+
|
385 |
+
if st.session_state['step'] == 5:
|
386 |
+
st.markdown(
|
387 |
+
f"<h4 style='text-align: justify'><strong>You selected: {st.session_state['selected_option']}</strong></h4>",
|
388 |
+
unsafe_allow_html=True)
|
389 |
+
|
390 |
+
inputs = []
|
391 |
+
if "Frontal" in st.session_state['selected_option'].split(" ➔")[0]:
|
392 |
+
inputs.append('Frontal')
|
393 |
+
if "Lateral" in st.session_state['selected_option'].split(" ➔")[0]:
|
394 |
+
inputs.append('Lateral')
|
395 |
+
if "Report" in st.session_state['selected_option'].split(" ➔")[0]:
|
396 |
+
inputs.append('Report')
|
397 |
+
|
398 |
+
outputs = []
|
399 |
+
if "Frontal" in st.session_state['selected_option'].split(" ➔")[1]:
|
400 |
+
outputs.append('Frontal')
|
401 |
+
if "Lateral" in st.session_state['selected_option'].split(" ➔")[1]:
|
402 |
+
outputs.append('Lateral')
|
403 |
+
if "Report" in st.session_state['selected_option'].split(" ➔")[1]:
|
404 |
+
outputs.append('Report')
|
405 |
+
|
406 |
+
esempio = esempi[st.session_state['selected_option']]
|
407 |
+
|
408 |
+
# Mostra i file associati all'esempio
|
409 |
+
st.markdown(
|
410 |
+
"<h3 style='text-align: center'><strong>INPUT:</strong></h3>",
|
411 |
+
unsafe_allow_html=True)
|
412 |
+
|
413 |
+
# Colonne per gli INPUTS
|
414 |
+
input_cols = st.columns(len(inputs))
|
415 |
+
|
416 |
+
for idx, inp in enumerate(inputs):
|
417 |
+
with input_cols[idx]:
|
418 |
+
if inp == 'Frontal':
|
419 |
+
path = "./DEMO/ESEMPI/" + esempio['Frontal']
|
420 |
+
print(path)
|
421 |
+
if path.endswith(".tiff"):
|
422 |
+
im = tifffile.imread(path)
|
423 |
+
im = np.clip(im, 0, 1)
|
424 |
+
elif path.endswith(".png"):
|
425 |
+
im = Image.open(path)
|
426 |
+
st.image(im, caption="Frontal Image")
|
427 |
+
elif inp == 'Lateral':
|
428 |
+
path = "./DEMO/ESEMPI/" + esempio['Lateral']
|
429 |
+
if path.endswith(".tiff"):
|
430 |
+
im = tifffile.imread(path)
|
431 |
+
im = np.clip(im, 0, 1)
|
432 |
+
elif path.endswith(".png"):
|
433 |
+
im = Image.open(path)
|
434 |
+
st.image(im, caption="Lateral Image")
|
435 |
+
elif inp == 'Report':
|
436 |
+
st.markdown(
|
437 |
+
f"<p style='font-size:20px;'><strong>Report:</strong> {esempio['Report']}</p>",
|
438 |
+
unsafe_allow_html=True
|
439 |
+
)
|
440 |
+
st.markdown(
|
441 |
+
"<h3 style='text-align: center'><strong>OUTPUT:</strong></h3>",
|
442 |
+
unsafe_allow_html=True)
|
443 |
+
|
444 |
+
# Colonne per gli OUTPUTS
|
445 |
+
output_cols = st.columns(len(outputs))
|
446 |
+
|
447 |
+
for idx, out in enumerate(outputs):
|
448 |
+
with output_cols[idx]:
|
449 |
+
if out == 'Frontal':
|
450 |
+
path = "./DEMO/ESEMPI/" + esempio['Frontal']
|
451 |
+
if path.endswith(".tiff"):
|
452 |
+
im = tifffile.imread(path)
|
453 |
+
# facciamo clamp tra 0 e 1
|
454 |
+
im = np.clip(im, 0, 1)
|
455 |
+
elif path.endswith(".png"):
|
456 |
+
im = Image.open(path)
|
457 |
+
st.image(im, caption="Frontal Image")
|
458 |
+
elif out == 'Lateral':
|
459 |
+
path = "./DEMO/ESEMPI/" + esempio['Lateral']
|
460 |
+
if path.endswith(".tiff"):
|
461 |
+
im = tifffile.imread(path)
|
462 |
+
# facciamo clamp tra 0 e 1
|
463 |
+
im = np.clip(im, 0, 1)
|
464 |
+
elif path.endswith(".png"):
|
465 |
+
im = Image.open(path)
|
466 |
+
st.image(im, caption="Lateral Image")
|
467 |
+
elif out == 'Report':
|
468 |
+
st.markdown(
|
469 |
+
f"<p style='font-size:20px;'><strong>Report:</strong> {esempio['Report']}</p>",
|
470 |
+
unsafe_allow_html=True
|
471 |
+
)
|
472 |
+
|
473 |
+
# Pulsante per tornare all'inizio
|
474 |
+
if st.button("Return to the beginning"):
|
475 |
+
# Ripristina lo stato della sessione
|
476 |
+
st.session_state['step'] = 1
|
477 |
+
st.session_state['selected_option'] = None
|
478 |
+
st.session_state['selected_option2'] = None
|
479 |
+
st.session_state['frontal_file'] = None
|
480 |
+
st.session_state['lateral_file'] = None
|
481 |
+
st.session_state['report'] = ""
|
482 |
+
st.rerun()
|