Adrit Rao commited on
Commit
18285c1
1 Parent(s): 3eb11cb

Add application file

Browse files
Files changed (1) hide show
  1. app.py +25 -15
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
- # Read and display DICOM images
 
 
 
28
  for dicom_file in dicom_files:
29
- dicom_data = pydicom.dcmread(f"temp_zip_folder/{dicom_file}")
30
- dicom_images.append(dicom_data.pixel_array)
 
 
 
 
31
 
32
  # User selects the slice using a slider
33
- selected_slice = st.slider("Select a DICOM slice", 0, num_slices - 1)
34
 
35
- # Display the selected DICOM image
36
- plt.imshow(dicom_images[selected_slice], cmap=plt.cm.bone)
37
- plt.axis("off")
38
- plt.title(f"DICOM Image - Slice {selected_slice + 1}/{num_slices}")
39
- plt.tight_layout()
 
40
 
41
- # Show the image in the Streamlit app
42
- st.pyplot(plt)
 
 
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)