Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pydicom
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Set up the Streamlit app title
|
7 |
+
st.title("DICOM Viewer for CBCT Scans")
|
8 |
+
|
9 |
+
# Directory where DICOM files are stored in the repository
|
10 |
+
dicom_directory = "./dicom_files/"
|
11 |
+
|
12 |
+
# Function to get the slice location or image position for sorting
|
13 |
+
def get_slice_location(dicom_file):
|
14 |
+
ds = pydicom.dcmread(dicom_file)
|
15 |
+
# Try to get the 'Slice Location'; if not available, use 'Image Position Patient'
|
16 |
+
if 'SliceLocation' in ds:
|
17 |
+
return float(ds.SliceLocation)
|
18 |
+
elif 'ImagePositionPatient' in ds:
|
19 |
+
# 'ImagePositionPatient' is a list; use the first element (z-coordinate)
|
20 |
+
return float(ds.ImagePositionPatient[2])
|
21 |
+
else:
|
22 |
+
return 0 # Default to 0 if neither tag is available
|
23 |
+
|
24 |
+
# Read all DICOM files from the directory
|
25 |
+
dicom_files = [os.path.join(dicom_directory, f) for f in os.listdir(dicom_directory) if f.endswith('.dcm')]
|
26 |
+
|
27 |
+
if dicom_files:
|
28 |
+
# Sort DICOM files by slice location or image position
|
29 |
+
dicom_files.sort(key=lambda x: get_slice_location(x))
|
30 |
+
|
31 |
+
# Read all DICOM images (sorted)
|
32 |
+
dicom_images = [pydicom.dcmread(file).pixel_array for file in dicom_files]
|
33 |
+
|
34 |
+
# Interactive slider to select image index
|
35 |
+
st.sidebar.header("Image Slider")
|
36 |
+
image_index = st.sidebar.slider("Select Image Index", 0, len(dicom_images) - 1, 0)
|
37 |
+
|
38 |
+
# Display the selected DICOM image
|
39 |
+
st.header(f"Displaying Image {image_index + 1} of {len(dicom_images)}")
|
40 |
+
fig, ax = plt.subplots()
|
41 |
+
ax.imshow(dicom_images[image_index], cmap='gray')
|
42 |
+
ax.axis('off')
|
43 |
+
st.pyplot(fig)
|
44 |
+
else:
|
45 |
+
st.write("No DICOM files found in the directory. Please check the folder and try again.")
|