Spaces:
Sleeping
Sleeping
Adrit Rao
commited on
Commit
·
18285c1
1
Parent(s):
3eb11cb
Add application file
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@ import streamlit as st
|
|
2 |
import pydicom
|
3 |
import matplotlib.pyplot as plt
|
4 |
import zipfile
|
5 |
-
import io
|
6 |
import os
|
7 |
|
8 |
# Streamlit app title
|
@@ -14,6 +13,7 @@ uploaded_zip = st.file_uploader("Upload a ZIP file containing DICOMs", type=["zi
|
|
14 |
# Initialize variables for DICOM data and image list
|
15 |
dicom_images = []
|
16 |
num_slices = 0
|
|
|
17 |
|
18 |
if uploaded_zip is not None:
|
19 |
# Unzip the uploaded ZIP file and extract DICOMs
|
@@ -24,27 +24,37 @@ if uploaded_zip is not None:
|
|
24 |
dicom_files = [f for f in os.listdir("temp_zip_folder") if f.lower().endswith(".dcm")]
|
25 |
num_slices = len(dicom_files)
|
26 |
|
27 |
-
#
|
|
|
|
|
|
|
28 |
for dicom_file in dicom_files:
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
31 |
|
32 |
# User selects the slice using a slider
|
33 |
-
selected_slice = st.slider("Select a DICOM slice", 0,
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
40 |
|
41 |
-
|
42 |
-
|
|
|
|
|
43 |
|
44 |
-
# Remove the temporary folder and its contents
|
45 |
-
if num_slices > 0:
|
46 |
for dicom_file in dicom_files:
|
47 |
os.remove(f"temp_zip_folder/{dicom_file}")
|
48 |
os.rmdir("temp_zip_folder")
|
49 |
|
50 |
-
st.write("Upload a ZIP file containing DICOMs to view and scroll through the slices.")
|
|
|
|
2 |
import pydicom
|
3 |
import matplotlib.pyplot as plt
|
4 |
import zipfile
|
|
|
5 |
import os
|
6 |
|
7 |
# Streamlit app title
|
|
|
13 |
# Initialize variables for DICOM data and image list
|
14 |
dicom_images = []
|
15 |
num_slices = 0
|
16 |
+
error_message = ""
|
17 |
|
18 |
if uploaded_zip is not None:
|
19 |
# Unzip the uploaded ZIP file and extract DICOMs
|
|
|
24 |
dicom_files = [f for f in os.listdir("temp_zip_folder") if f.lower().endswith(".dcm")]
|
25 |
num_slices = len(dicom_files)
|
26 |
|
27 |
+
# Initialize a list to store successfully processed DICOMs
|
28 |
+
processed_dicoms = []
|
29 |
+
|
30 |
+
# Process DICOM files and skip problematic ones
|
31 |
for dicom_file in dicom_files:
|
32 |
+
try:
|
33 |
+
dicom_data = pydicom.dcmread(f"temp_zip_folder/{dicom_file}")
|
34 |
+
dicom_images.append(dicom_data.pixel_array)
|
35 |
+
processed_dicoms.append(dicom_data)
|
36 |
+
except Exception as e:
|
37 |
+
error_message += f"Skipping problematic DICOM ({dicom_file}): {str(e)}\n"
|
38 |
|
39 |
# User selects the slice using a slider
|
40 |
+
selected_slice = st.slider("Select a DICOM slice", 0, len(processed_dicoms) - 1)
|
41 |
|
42 |
+
if selected_slice < len(processed_dicoms):
|
43 |
+
# Display the selected DICOM image
|
44 |
+
plt.imshow(dicom_images[selected_slice], cmap=plt.cm.bone)
|
45 |
+
plt.axis("off")
|
46 |
+
plt.title(f"DICOM Image - Slice {selected_slice + 1}/{len(processed_dicoms)}")
|
47 |
+
plt.tight_layout()
|
48 |
|
49 |
+
# Show the image in the Streamlit app
|
50 |
+
st.pyplot(plt)
|
51 |
+
else:
|
52 |
+
error_message += "No DICOM slices available for the selected slice number."
|
53 |
|
54 |
+
# Remove the temporary folder and its contents
|
|
|
55 |
for dicom_file in dicom_files:
|
56 |
os.remove(f"temp_zip_folder/{dicom_file}")
|
57 |
os.rmdir("temp_zip_folder")
|
58 |
|
59 |
+
st.write("Upload a ZIP file containing DICOMs to view and scroll through the slices.")
|
60 |
+
st.write(error_message)
|