Spaces:
Sleeping
Sleeping
Adrit Rao
commited on
Commit
·
affc586
1
Parent(s):
1c0e8ad
Add application file
Browse files
app.py
CHANGED
|
@@ -1,27 +1,49 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pydicom
|
| 3 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Streamlit app title
|
| 6 |
st.title("DICOM Image Viewer")
|
| 7 |
|
| 8 |
-
# Upload a
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
dicom_data = pydicom.dcmread(uploaded_file)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
plt.tight_layout()
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
st.error(f"Error: {str(e)}")
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pydicom
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
+
import zipfile
|
| 5 |
+
import io
|
| 6 |
|
| 7 |
# Streamlit app title
|
| 8 |
st.title("DICOM Image Viewer")
|
| 9 |
|
| 10 |
+
# Upload a ZIP file containing DICOMs
|
| 11 |
+
uploaded_zip = st.file_uploader("Upload a ZIP file containing DICOMs", type=["zip"])
|
| 12 |
|
| 13 |
+
# Initialize variables for DICOM data and image list
|
| 14 |
+
dicom_images = []
|
| 15 |
+
num_slices = 0
|
|
|
|
| 16 |
|
| 17 |
+
if uploaded_zip is not None:
|
| 18 |
+
# Unzip the uploaded ZIP file and extract DICOMs
|
| 19 |
+
with zipfile.ZipFile(uploaded_zip, "r") as zip_ref:
|
| 20 |
+
zip_ref.extractall("temp_zip_folder")
|
|
|
|
| 21 |
|
| 22 |
+
# Get a list of DICOM files
|
| 23 |
+
dicom_files = [f for f in os.listdir("temp_zip_folder") if f.lower().endswith(".dcm")]
|
| 24 |
+
num_slices = len(dicom_files)
|
|
|
|
| 25 |
|
| 26 |
+
# Read and display DICOM images
|
| 27 |
+
for dicom_file in dicom_files:
|
| 28 |
+
dicom_data = pydicom.dcmread(f"temp_zip_folder/{dicom_file}")
|
| 29 |
+
dicom_images.append(dicom_data.pixel_array)
|
| 30 |
+
|
| 31 |
+
# User selects the slice using a slider
|
| 32 |
+
selected_slice = st.slider("Select a DICOM slice", 0, num_slices - 1)
|
| 33 |
+
|
| 34 |
+
# Display the selected DICOM image
|
| 35 |
+
plt.imshow(dicom_images[selected_slice], cmap=plt.cm.bone)
|
| 36 |
+
plt.axis("off")
|
| 37 |
+
plt.title(f"DICOM Image - Slice {selected_slice + 1}/{num_slices}")
|
| 38 |
+
plt.tight_layout()
|
| 39 |
+
|
| 40 |
+
# Show the image in the Streamlit app
|
| 41 |
+
st.pyplot(plt)
|
| 42 |
+
|
| 43 |
+
# Remove the temporary folder and its contents
|
| 44 |
+
if num_slices > 0:
|
| 45 |
+
for dicom_file in dicom_files:
|
| 46 |
+
os.remove(f"temp_zip_folder/{dicom_file}")
|
| 47 |
+
os.rmdir("temp_zip_folder")
|
| 48 |
+
|
| 49 |
+
st.write("Upload a ZIP file containing DICOMs to view and scroll through the slices.")
|