pangyuteng's picture
patched image normalization
ad868f8
"""
<script src="https://cdn.jsdelivr.net/npm/[email protected]/release/current/standard/papaya.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/release/current/standard/papaya.min.css" rel="stylesheet">
<div class="papaya" data-params="params"></div>
TODO:
https://www.gradio.app/guides/custom-components-in-five-minutes
https://github.com/gradio-app/gradio/issues/6821
"""
import os
import gradio as gr
import numpy as np
import PIL
import SimpleITK as sitk
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
example_image_path = os.path.join(THIS_DIR,'files','promis12-test-Case00.nii.gz') # from promise12
example_mask_path = os.path.join(THIS_DIR,'files','promis12-test-Case00-mask.nii.gz') # from promise12
mydict = {}
def load_data(input1,input2):
global mydict
mydict['image_path'] = input1.name
mydict['mask_path'] = input2.name
img = sitk.GetArrayFromImage(sitk.ReadImage(mydict['image_path']))
mask = sitk.GetArrayFromImage(sitk.ReadImage(mydict['mask_path']))
minval,maxval = np.min(img),np.max(img)
img = ((img-minval)/(maxval-minval)).clip(0,1)*255
mydict['img'] = img.astype(np.uint8)
mydict['mask'] = mask.astype(np.uint8)
return f"Image and mask files uploaded, move the above slider to trigger rendering of below component."
def render(x, state):
if len(mydict)==4:
if x > mydict['img'].shape[0]-1:
x = mydict['img'].shape[0]-1
if x < 0:
x = 0
image = mydict['img'][x,:,:]
mask = mydict['mask'][x,:,:]
value = (image,[(mask,"prostate")])
zmin, zmax = 0,mydict['img'].shape[0]-1
else:
image = np.zeros(10,10)
zmin, zmax = None, None
value = (image,[])
return value,f'z-value: {x}, (zmin: {zmin}, zmax: {zmax})'
with gr.Blocks() as demo:
with gr.Column():
gr.Markdown(
"""
# bare-bones nifti image and mask viewer with Gradio
""")
with gr.Row():
image_file = gr.File(label="upload image nifti file here")
mask_file = gr.File(label="upload mask nifti file here")
with gr.Column():
btn = gr.Button("upload")
slider = gr.Slider(0, 20, step=1)
state = gr.State(value=0)
out = gr.Textbox(label='text-comment')
with gr.Row():
myimage = gr.AnnotatedImage(height=512)
gr.Examples(
[[example_image_path,example_mask_path]],
[image_file, mask_file],
)
btn.click(fn=load_data,
inputs=[image_file,mask_file], outputs=out,
)
slider.change(
render,
inputs=[slider, state],
outputs=[myimage,out],
api_name="hohoho"
)
if __name__ == "__main__":
demo.launch()