File size: 3,632 Bytes
3318510
e9f7fd2
 
affc586
3eb11cb
e665ab2
 
94e3d42
 
84f200a
e9f7fd2
 
 
807f381
 
 
2033578
 
 
 
 
 
 
 
 
 
 
 
807f381
 
 
023b513
807f381
 
5a2944b
 
19007de
 
 
 
 
 
 
 
94e3d42
19007de
 
 
 
 
 
94e3d42
45b14ca
19007de
45b14ca
19007de
 
 
 
 
45b14ca
19007de
45b14ca
19007de
 
 
 
 
45b14ca
19007de
 
1e635ad
807f381
 
 
 
 
 
 
 
 
 
 
 
 
2033578
 
807f381
 
 
 
 
 
 
 
 
 
94e3d42
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import streamlit as st
import pydicom
import matplotlib.pyplot as plt
import zipfile
import os
import subprocess

# Flag to track if subprocess commands have been executed
subprocess_executed = False

# Streamlit app title
st.title("DICOM Image Viewer")

# Upload a ZIP file containing DICOM slices
uploaded_zip_file = st.file_uploader("Upload a ZIP file containing DICOM slices", type=["zip"])

# Function to read and display the DICOM image
@st.cache(suppress_st_warning=True, show_spinner=False)
def display_dicom_image(selected_slice, dicom_files):
    dicom_data = pydicom.dcmread(dicom_files[selected_slice])

    # Display the DICOM image
    plt.imshow(dicom_data.pixel_array, cmap=plt.cm.bone)
    plt.axis("off")
    plt.title("DICOM Image")
    plt.tight_layout()
    return plt

if uploaded_zip_file is not None:
    try:
        # Create a temporary directory to unzip the files
        temp_dir = "/Comp2Comp-main/temp_dicom_dir"
        os.makedirs(temp_dir, exist_ok=True)

        st.write(f"Temporary directory path: {temp_dir}")

        # if not subprocess_executed:
        #     # Execute the subprocess commands only once
        #     command = "chmod +x install.sh"
        #     try:
        #         subprocess.run(command, shell=True, check=True)
        #         print("Script 'install.sh' has been made executable.")
        #     except subprocess.CalledProcessError as e:
        #         print(f"Error while making the script executable: {e}")

        #     command = "./install.sh"
        #     try:
        #         subprocess.run(command, shell=True, check=True)
        #         print("Script 'install.sh' has started running.")
        #     except subprocess.CalledProcessError as e:
        #         print(f"Error while running the script: {e}")

                
        #     command = "chmod +x inference.sh"
    
        #     try:
        #         subprocess.run(command, shell=True, check=True)
        #         print("Script 'inference.sh' has started running.")
        #     except subprocess.CalledProcessError as e:
        #         print(f"Error while making the script executable: {e}")
    
        #     command = "./inference.sh"
    
        #     try:
        #         subprocess.run(command, shell=True, check=True)
        #         print("Script 'inference.sh' has started running.")
        #     except subprocess.CalledProcessError as e:
        #         print(f"Error while making the script executable: {e}")

        #     # Set the flag to indicate that subprocess commands have been executed
        #     subprocess_executed = True

        with zipfile.ZipFile(uploaded_zip_file, "r") as zip_ref:
            zip_ref.extractall(temp_dir)

        # Get a list of DICOM files in the directory
        dicom_files = [os.path.join(temp_dir, f) for f in os.listdir(temp_dir) if f.endswith(".dcm")]
        dicom_files.sort()  # Sort the files

        if len(dicom_files) == 0:
            st.error("No DICOM files found in the ZIP archive.")
        else:
            # Display a slider for selecting the slice
            selected_slice = st.slider("Select a slice", 0, len(dicom_files) - 1, 0)

            # Display the DICOM image using the cached function
            plt = display_dicom_image(selected_slice, dicom_files)
            st.pyplot(plt)

    except Exception as e:
        st.error(f"Error: {str(e)}")
    finally:
        # Clean up by removing the temporary directory
        for file in dicom_files:
            os.remove(file)
        os.rmdir(temp_dir)

st.write("Upload a ZIP file containing DICOM slices to view the images.")