hlnicholls commited on
Commit
ea7140d
1 Parent(s): 538b81a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -18
app.py CHANGED
@@ -6,18 +6,9 @@ import os
6
  # Set up the Streamlit app title
7
  st.title("DICOM Viewer for CBCT Scans")
8
 
9
- # Directories where DICOM files are stored
10
- dicom_directories = {
11
- "00002067": "./00002067/",
12
- "00007FA6": "./00007FA6/"
13
- }
14
-
15
- # Sidebar for selecting DICOM folder
16
- st.sidebar.header("Select DICOM Folder")
17
- selected_folder = st.sidebar.selectbox("Choose a folder to view DICOM files", list(dicom_directories.keys()))
18
-
19
- # Get the selected directory path
20
- dicom_directory = dicom_directories[selected_folder]
21
 
22
  # Function to get the slice location or image position for sorting
23
  def get_slice_location(dicom_file):
@@ -31,18 +22,47 @@ def get_slice_location(dicom_file):
31
  else:
32
  return 0 # Default to 0 if neither tag is available
33
 
34
- # Get list of DICOM files in the selected directory
35
- dicom_files = [os.path.join(dicom_directory, f) for f in os.listdir(dicom_directory) if f.endswith('.dcm')]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- if dicom_files:
38
  # Sort DICOM files by slice location or image position
39
- dicom_files.sort(key=lambda x: get_slice_location(x))
40
 
41
  # Read all DICOM images (sorted)
42
- dicom_images = [pydicom.dcmread(file).pixel_array for file in dicom_files]
43
 
44
  # Interactive slider to select image index
45
  st.sidebar.header("Image Slider")
46
  image_index = st.sidebar.slider("Select Image Index", 0, len(dicom_images) - 1, 0)
47
 
48
- #
 
 
 
 
 
 
 
 
6
  # Set up the Streamlit app title
7
  st.title("DICOM Viewer for CBCT Scans")
8
 
9
+ # Directory paths where DICOM files are stored
10
+ single_image_directory = "./00002067/"
11
+ multiple_images_directory = "./00007FA6/"
 
 
 
 
 
 
 
 
 
12
 
13
  # Function to get the slice location or image position for sorting
14
  def get_slice_location(dicom_file):
 
22
  else:
23
  return 0 # Default to 0 if neither tag is available
24
 
25
+ # Display the single DICOM image from the first folder
26
+ st.header("Single DICOM Image:")
27
+
28
+ # Get the only DICOM file in the single_image_directory
29
+ single_image_files = [os.path.join(single_image_directory, f) for f in os.listdir(single_image_directory) if f.endswith('.dcm')]
30
+
31
+ if single_image_files:
32
+ # Read and display the single DICOM image
33
+ ds = pydicom.dcmread(single_image_files[0])
34
+ single_image = ds.pixel_array
35
+
36
+ # Display the image
37
+ fig, ax = plt.subplots()
38
+ ax.imshow(single_image, cmap='gray')
39
+ ax.axis('off')
40
+ st.pyplot(fig)
41
+ else:
42
+ st.write("No DICOM files found in the directory 00002067.")
43
+
44
+ # Display the multiple DICOM images from the second folder using a slider
45
+ st.header("Browse Multiple DICOM Images from 00007FA6")
46
+
47
+ # Get list of DICOM files in the multiple_images_directory
48
+ multiple_image_files = [os.path.join(multiple_images_directory, f) for f in os.listdir(multiple_images_directory) if f.endswith('.dcm')]
49
 
50
+ if multiple_image_files:
51
  # Sort DICOM files by slice location or image position
52
+ multiple_image_files.sort(key=lambda x: get_slice_location(x))
53
 
54
  # Read all DICOM images (sorted)
55
+ dicom_images = [pydicom.dcmread(file).pixel_array for file in multiple_image_files]
56
 
57
  # Interactive slider to select image index
58
  st.sidebar.header("Image Slider")
59
  image_index = st.sidebar.slider("Select Image Index", 0, len(dicom_images) - 1, 0)
60
 
61
+ # Display the selected DICOM image
62
+ st.header(f"Displaying Image {image_index + 1} of {len(dicom_images)} from folder 00007FA6")
63
+ fig, ax = plt.subplots()
64
+ ax.imshow(dicom_images[image_index], cmap='gray')
65
+ ax.axis('off')
66
+ st.pyplot(fig)
67
+ else:
68
+ st.write("No DICOM files found in the directory 00007FA6.")