Spaces:
Sleeping
Sleeping
File size: 6,480 Bytes
3318510 e9f7fd2 affc586 3eb11cb e665ab2 acda587 97f584b 22773d6 20665dd e665ab2 94e3d42 84f200a e9f7fd2 d525e10 e9f7fd2 807f381 b531d2e 807f381 b531d2e d525e10 b531d2e d525e10 b531d2e b3c4662 b531d2e 22773d6 20665dd b531d2e 22773d6 b531d2e d525e10 3281da3 22773d6 3281da3 22773d6 3281da3 d525e10 9cfe6a8 807f381 057ede6 807f381 22773d6 b531d2e 807f381 b531d2e 36ee72b |
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
import streamlit as st
import pydicom
import matplotlib.pyplot as plt
import zipfile
import os
import subprocess
from datetime import datetime
import shutil
import moviepy.video.io.ImageSequenceClip
from io import BytesIO
from tkinter import Tcl
# Flag to track if subprocess commands have been executed
subprocess_executed = False
# Streamlit app title
st.title("DICOM Image Viewer")
# Upload a ZIP file containing DICOM slices
uploaded_zip_file = st.file_uploader("Upload a ZIP file containing DICOM slices", type=["zip"])
st.write("Upload a ZIP file containing DICOM slices to view the images.")
@st.cache_resource
def install_dependencies():
command = "chmod +x install.sh"
try:
subprocess.run(command, shell=True, check=True)
print("Script 'install.sh' has been made executable.")
except subprocess.CalledProcessError as e:
print(f"Error while making the script executable: {e}")
command = "./install.sh"
try:
subprocess.run(command, shell=True, check=True)
print("Script 'install.sh' has started running.")
install_script_executed = True
except subprocess.CalledProcessError as e:
print(f"Error while running the script: {e}")
@st.cache_resource
def run_inference():
command = "chmod +x inference.sh"
try:
subprocess.run(command, shell=True, check=True)
print("Script 'inference.sh' has been made executable.")
except subprocess.CalledProcessError as e:
print(f"Error while making the script executable: {e}")
command = "./inference.sh"
try:
subprocess.run(command, shell=True, check=True)
print("Script 'inference.sh' has started running.")
except subprocess.CalledProcessError as e:
print(f"Error while running the script: {e}")
# Function to read and display the DICOM image
def convert_dicom_to_png(dicom_data, output_dir, index):
# Convert DICOM to a PNG image
plt.imshow(dicom_data.pixel_array, cmap=plt.cm.bone)
plt.axis("off")
plt.title("DICOM Image")
plt.tight_layout()
img_buffer = BytesIO()
plt.savefig(img_buffer, format="png")
img_buffer.seek(0)
output_path = os.path.join(output_dir, f"image_{index:03d}.png")
with open(output_path, "wb") as f:
f.write(img_buffer.read())
return output_path
if uploaded_zip_file is not None:
try:
install_dependencies()
temp_dir = "/home/user/app/C2C/temp_dicom_dir"
output_image_dir = "/home/user/app/C2C/images_for_video"
os.makedirs(temp_dir, exist_ok=True)
os.makedirs(output_image_dir, exist_ok=True)
full_path = os.path.abspath(temp_dir)
st.write(f"Temporary directory path: {full_path}")
with zipfile.ZipFile(uploaded_zip_file, "r") as zip_ref:
zip_ref.extractall(temp_dir)
# Get a list of DICOM files in the directory
dicom_files = [os.path.join(temp_dir, f) for f in os.listdir(temp_dir) if f.endswith(".dcm")]
dicom_files.sort() # Sort the files
if len(dicom_files) == 0:
st.error("No DICOM files found in the ZIP archive.")
else:
# Convert DICOM slices to PNG frames and save them to the output folder
for i, dicom_file in enumerate(dicom_files):
dicom_data = pydicom.dcmread(dicom_file)
convert_dicom_to_png(dicom_data, output_image_dir, i)
image_folder = output_image_dir
fps = 20
image_files = [
os.path.join(image_folder, img)
for img in Tcl().call("lsort", "-dict", os.listdir(image_folder))
if img.endswith(".png")
]
clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(
image_files, fps=fps
)
clip.write_videofile("aaa.mp4")
st.video("aaa.mp4", format="video/mp4")
except Exception as e:
st.error(f"Error: {str(e)}")
if st.button("Analyze"):
run_inference()
# Assuming the 'outputs' directory is located at "/home/user/app/C2C/comp2comp/outputs"
outputs_directory = "/home/user/app/C2C/outputs"
# Get a list of subdirectories inside the 'outputs' directory
subdirectories = [subdir for subdir in os.listdir(outputs_directory)
if os.path.isdir(os.path.join(outputs_directory, subdir))]
# Use the first (or only) subdirectory, or None if there are no subdirectories
first_subdirectory = subdirectories[0] if subdirectories else None
if first_subdirectory:
# Construct the paths to the video and image files inside the first subdirectory
subdirectory_path = os.path.join(outputs_directory, first_subdirectory)
temp_dicom_dir_path = os.path.join(subdirectory_path, "temp_dicom_dir")
# Get a list of subdirectories inside the 'temp_dicom_dir' directory
dicom_subdirectories = [subdir for subdir in os.listdir(temp_dicom_dir_path)
if os.path.isdir(os.path.join(temp_dicom_dir_path, subdir))]
# Use the first (or only) subdirectory inside 'temp_dicom_dir', or None if there are none
first_dicom_subdirectory = dicom_subdirectories[0] if dicom_subdirectories else None
if first_dicom_subdirectory:
video_path = os.path.join(temp_dicom_dir_path, first_dicom_subdirectory, "images/summary/aaa.mp4")
image_path = os.path.join(temp_dicom_dir_path, first_dicom_subdirectory, "images/summary/diameter_graph.png")
largest_slice = os.path.join(temp_dicom_dir_path, first_dicom_subdirectory, "images/summary/out.png")
if os.path.exists(largest_slice):
st.title("Largest Slice")
st.image(largest_slice, use_column_width=True)
# Display the video generated by the inference script
if os.path.exists(video_path):
st.title("Video")
st.video(video_path, format="video/mp4")
# Display the image generated by the inference script
if os.path.exists(image_path):
st.title("Diameter Graph")
st.image(image_path, use_column_width=True)
else:
st.warning("No subdirectories or folders found inside 'temp_dicom_dir'.")
else:
st.warning("No subdirectories or folders found inside the 'outputs' directory.")
|