Adrit Rao commited on
Commit
807f381
1 Parent(s): 18285c1

Add application file

Browse files
Files changed (1) hide show
  1. app.py +43 -51
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 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
- error_message = ""
17
-
18
- if uploaded_zip is not None:
19
- # Unzip the uploaded ZIP file and extract DICOMs
20
- with zipfile.ZipFile(uploaded_zip, "r") as zip_ref:
21
- zip_ref.extractall("temp_zip_folder")
22
-
23
- # Get a list of DICOM files
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)
 
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.")