Spaces:
Runtime error
Runtime error
Commit
·
5a72667
1
Parent(s):
30378f8
Recreated the app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import numpy as np
|
| 6 |
+
import skimage
|
| 7 |
+
import monai as mn
|
| 8 |
+
import torch
|
| 9 |
+
|
| 10 |
+
from io_utils import LoadImageD
|
| 11 |
+
|
| 12 |
+
# Gradio helper functions
|
| 13 |
+
|
| 14 |
+
current_img = None
|
| 15 |
+
live_preds = None
|
| 16 |
+
|
| 17 |
+
def rotate_btn_fn(img_path, xt, yt, zt, add_bone_cmap=False):
|
| 18 |
+
|
| 19 |
+
global current_img
|
| 20 |
+
|
| 21 |
+
angles = (xt, yt, zt)
|
| 22 |
+
out_img_path = f'data/cached_outputs/{os.path.basename(img_path)[:-4]}_{angles}.png'
|
| 23 |
+
out_img = skimage.io.imread(out_img_path)
|
| 24 |
+
if not add_bone_cmap:
|
| 25 |
+
return out_img
|
| 26 |
+
cmap = plt.get_cmap('bone')
|
| 27 |
+
out_img = cmap(out_img)
|
| 28 |
+
out_img = (out_img[..., :3] * 255).astype(np.uint8)
|
| 29 |
+
current_img = out_img
|
| 30 |
+
return out_img
|
| 31 |
+
|
| 32 |
+
css_style = "./style.css"
|
| 33 |
+
callback = gr.CSVLogger()
|
| 34 |
+
with gr.Blocks(css=css_style) as app:
|
| 35 |
+
gr.HTML("RadRotator: 3D Rotation of Radiographs with Diffusion Models", elem_classes="title")
|
| 36 |
+
gr.HTML("Developed by:<br>Pouria Rouzrokh, Bardia Khosravi, Shahriar Faghani, Kellen Mulford, Michael J. Taunton, Bradley J. Erickson, Cody C. Wyles", elem_classes="subtitle")
|
| 37 |
+
gr.HTML("Note: The demo operates on a CPU, and since diffusion models require more computational capacity to function, all predictions are precomputed.", elem_classes="note")
|
| 38 |
+
|
| 39 |
+
with gr.TabItem("Demo"):
|
| 40 |
+
with gr.Row():
|
| 41 |
+
input_img = gr.Image(type='filepath', label='Input image', sources='upload', interactive=False, elem_classes='imgs')
|
| 42 |
+
output_img = gr.Image(type='pil', label='Output image', interactive=False, elem_classes='imgs')
|
| 43 |
+
with gr.Row():
|
| 44 |
+
with gr.Column(scale=0.25):
|
| 45 |
+
pass
|
| 46 |
+
with gr.Column(scale=1):
|
| 47 |
+
gr.Examples(
|
| 48 |
+
examples = [os.path.join("./data/examples", f) for f in os.listdir("./data/examples") if "xr" in f],
|
| 49 |
+
inputs = [input_img],
|
| 50 |
+
label = "Xray Examples",
|
| 51 |
+
elem_id='examples',
|
| 52 |
+
)
|
| 53 |
+
with gr.Column(scale=0.25):
|
| 54 |
+
pass
|
| 55 |
+
with gr.Row():
|
| 56 |
+
gr.Markdown('Please select an example image, choose your rotation angles, and press Rotate!', elem_classes='text')
|
| 57 |
+
with gr.Row():
|
| 58 |
+
with gr.Column(scale=1):
|
| 59 |
+
xt = gr.Slider(label='Rotation angle in x axis:', elem_classes='angle', value=0, minimum=-15, maximum=15, step=5)
|
| 60 |
+
with gr.Column(scale=1):
|
| 61 |
+
yt = gr.Slider(label='Rotation angle in y axis:', elem_classes='angle', value=0, minimum=-15, maximum=15, step=5)
|
| 62 |
+
with gr.Column(scale=1):
|
| 63 |
+
zt = gr.Slider(label='Rotation angle in z axis:', elem_classes='angle', value=0, minimum=-15, maximum=15, step=5)
|
| 64 |
+
with gr.Row():
|
| 65 |
+
rotate_btn = gr.Button("Rotate!", elem_classes='rotate_button')
|
| 66 |
+
rotate_btn.click(fn=rotate_btn_fn, inputs=[input_img, xt, yt, zt], outputs=output_img)
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
app.close()
|
| 70 |
+
gr.close_all()
|
| 71 |
+
except:
|
| 72 |
+
pass
|
| 73 |
+
|
| 74 |
+
demo = app.launch(
|
| 75 |
+
max_threads=4,
|
| 76 |
+
share=True,
|
| 77 |
+
inline=False,
|
| 78 |
+
show_api=False,
|
| 79 |
+
show_error=False,
|
| 80 |
+
)
|