|
|
|
import os |
|
import streamlit as st |
|
|
|
papaya_script = """ |
|
<style> |
|
:root { |
|
background-color: black; |
|
} |
|
</style> |
|
<script src="https://cdn.jsdelivr.net/npm/papaya-viewer@1.0.1455/release/current/standard/papaya.min.js"></script> |
|
<link href="https://cdn.jsdelivr.net/npm/papaya-viewer@1.0.1455/release/current/standard/papaya.min.css" rel="stylesheet"> |
|
<script type="text/javascript"> |
|
var params = []; |
|
params["luts"] = [{"name": "Custom", "data":[[0, 0, 0, 0], [0.5, 0, 0, 0], [1, 1, 0, 0]]}]; |
|
params["images"] = ["{{IMAGE_FULLPATH}}","{{MASK_FULLPATH}}"]; |
|
params["{{MASK_BASENAME}}"] = {"min": 0, "max": 1, "lut": "Custom", "alpha":0.5}; |
|
</script> |
|
<div class="papaya" data-params="params"></div> |
|
""" |
|
|
|
|
|
|
|
|
|
|
|
def handle_input(myimage,mymask,papaya_script): |
|
mydict = {} |
|
os.makedirs('static/tmp/',exist_ok=True) |
|
image_path = 'static/tmp/image.nii.gz' |
|
mask_path = 'static/tmp/mask.nii.gz' |
|
for file_path,uploaded_file in [(image_path,myimage),(mask_path,mymask)]: |
|
bytes_data = uploaded_file.getvalue() |
|
with open(file_path,'wb') as f: |
|
f.write(bytes_data) |
|
|
|
image_path = './app/static/tmp/image.nii.gz' |
|
mask_path = './app/static/tmp/mask.nii.gz' |
|
papaya_script = papaya_script.replace("{{IMAGE_FULLPATH}}",image_path) |
|
papaya_script = papaya_script.replace("{{MASK_FULLPATH}}",mask_path) |
|
papaya_script = papaya_script.replace("{{MASK_BASENAME}}","mask.nii.gz") |
|
|
|
st.components.v1.html(papaya_script,height=600,width=800) |
|
|
|
def main(): |
|
|
|
st.subheader("papaya-image-viewer") |
|
myimage = st.file_uploader( |
|
"Upload your image nifti file", accept_multiple_files=False) |
|
mymask = st.file_uploader( |
|
"Upload your mask nifti file here and click on 'Render'", accept_multiple_files=False) |
|
if st.button("Render"): |
|
with st.spinner("Processing"): |
|
handle_input(myimage,mymask,papaya_script) |
|
|
|
|
|
if __name__ == '__main__': |
|
main() |
|
|
|
|
|
|
|
|
|
|
|
|