Spaces:
Sleeping
Sleeping
Adrit Rao
commited on
Commit
·
807f381
1
Parent(s):
18285c1
Add application file
Browse files
app.py
CHANGED
|
@@ -7,54 +7,46 @@ import os
|
|
| 7 |
# Streamlit app title
|
| 8 |
st.title("DICOM Image Viewer")
|
| 9 |
|
| 10 |
-
# Upload a ZIP file containing
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
dicom_data = pydicom.dcmread(
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 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)
|
|
|
|
| 7 |
# Streamlit app title
|
| 8 |
st.title("DICOM Image Viewer")
|
| 9 |
|
| 10 |
+
# Upload a ZIP file containing DICOM slices
|
| 11 |
+
uploaded_zip_file = st.file_uploader("Upload a ZIP file containing DICOM slices", type=["zip"])
|
| 12 |
+
|
| 13 |
+
if uploaded_zip_file is not None:
|
| 14 |
+
try:
|
| 15 |
+
# Create a temporary directory to unzip the files
|
| 16 |
+
temp_dir = "temp_dicom_dir"
|
| 17 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 18 |
+
|
| 19 |
+
with zipfile.ZipFile(uploaded_zip_file, "r") as zip_ref:
|
| 20 |
+
zip_ref.extractall(temp_dir)
|
| 21 |
+
|
| 22 |
+
# Get a list of DICOM files in the directory
|
| 23 |
+
dicom_files = [os.path.join(temp_dir, f) for f in os.listdir(temp_dir) if f.endswith(".dcm")]
|
| 24 |
+
dicom_files.sort() # Sort the files
|
| 25 |
+
|
| 26 |
+
if len(dicom_files) == 0:
|
| 27 |
+
st.error("No DICOM files found in the ZIP archive.")
|
| 28 |
+
else:
|
| 29 |
+
# Display a slider for selecting the slice
|
| 30 |
+
selected_slice = st.slider("Select a slice", 0, len(dicom_files) - 1, 0)
|
| 31 |
+
|
| 32 |
+
# Read the selected DICOM file
|
| 33 |
+
dicom_data = pydicom.dcmread(dicom_files[selected_slice])
|
| 34 |
+
|
| 35 |
+
# Display the DICOM image
|
| 36 |
+
plt.imshow(dicom_data.pixel_array, cmap=plt.cm.bone)
|
| 37 |
+
plt.axis("off")
|
| 38 |
+
plt.title("DICOM Image")
|
| 39 |
+
plt.tight_layout()
|
| 40 |
+
|
| 41 |
+
# Show the image in the Streamlit app
|
| 42 |
+
st.pyplot(plt)
|
| 43 |
+
|
| 44 |
+
except Exception as e:
|
| 45 |
+
st.error(f"Error: {str(e)}")
|
| 46 |
+
finally:
|
| 47 |
+
# Clean up by removing the temporary directory
|
| 48 |
+
for file in dicom_files:
|
| 49 |
+
os.remove(file)
|
| 50 |
+
os.rmdir(temp_dir)
|
| 51 |
+
|
| 52 |
+
st.write("Upload a ZIP file containing DICOM slices to view the images.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|