Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pydicom | |
import matplotlib.pyplot as plt | |
import zipfile | |
import os | |
import subprocess | |
# Define the list of commands, including "echo" to simulate Enter keypress | |
commands = [ | |
"git clone https://github.com/StanfordMIMI/Comp2Comp/", | |
"cd Comp2Comp", | |
"pip install -e ." | |
] | |
for command in commands: | |
try: | |
subprocess.run(command, shell=True, check=True) | |
except subprocess.CalledProcessError as e: | |
print(f"Error running the command: {e}") | |
# Run the commands | |
for command in commands: | |
try: | |
subprocess.run(command, shell=True, check=True, text=True) | |
except subprocess.CalledProcessError as e: | |
print(f"Error running the command: {e}") | |
# 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"]) | |
# Function to read and display the DICOM image | |
def display_dicom_image(selected_slice, dicom_files): | |
dicom_data = pydicom.dcmread(dicom_files[selected_slice]) | |
# Display the DICOM image | |
plt.imshow(dicom_data.pixel_array, cmap=plt.cm.bone) | |
plt.axis("off") | |
plt.title("DICOM Image") | |
plt.tight_layout() | |
return plt | |
if uploaded_zip_file is not None: | |
try: | |
# Create a temporary directory to unzip the files | |
temp_dir = "temp_dicom_dir" | |
os.makedirs(temp_dir, exist_ok=True) | |
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: | |
# Display a slider for selecting the slice | |
selected_slice = st.slider("Select a slice", 0, len(dicom_files) - 1, 0) | |
# Display the DICOM image using the cached function | |
plt = display_dicom_image(selected_slice, dicom_files) | |
st.pyplot(plt) | |
except Exception as e: | |
st.error(f"Error: {str(e)}") | |
finally: | |
# Clean up by removing the temporary directory | |
for file in dicom_files: | |
os.remove(file) | |
os.rmdir(temp_dir) | |
st.write("Upload a ZIP file containing DICOM slices to view the images.") | |