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

Add application file

Browse files
Files changed (1) hide show
  1. app.py +14 -10
app.py CHANGED
@@ -10,6 +10,18 @@ st.title("DICOM Image Viewer")
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
@@ -29,16 +41,8 @@ if uploaded_zip_file is not None:
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:
 
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
+ # Function to read and display the DICOM image
14
+ @st.cache(suppress_st_warning=True, show_spinner=False)
15
+ def display_dicom_image(selected_slice, dicom_files):
16
+ dicom_data = pydicom.dcmread(dicom_files[selected_slice])
17
+
18
+ # Display the DICOM image
19
+ plt.imshow(dicom_data.pixel_array, cmap=plt.cm.bone)
20
+ plt.axis("off")
21
+ plt.title("DICOM Image")
22
+ plt.tight_layout()
23
+ return plt
24
+
25
  if uploaded_zip_file is not None:
26
  try:
27
  # Create a temporary directory to unzip the files
 
41
  # Display a slider for selecting the slice
42
  selected_slice = st.slider("Select a slice", 0, len(dicom_files) - 1, 0)
43
 
44
+ # Display the DICOM image using the cached function
45
+ plt = display_dicom_image(selected_slice, dicom_files)
 
 
 
 
 
 
 
 
46
  st.pyplot(plt)
47
 
48
  except Exception as e: