AdritRao commited on
Commit
6044769
·
verified ·
1 Parent(s): 3528cd0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -125
app.py CHANGED
@@ -1,140 +1,89 @@
1
  import streamlit as st
2
  import pydicom
3
- import matplotlib.pyplot as plt
4
  import zipfile
5
  import os
6
  import subprocess
7
- from datetime import datetime
8
- import shutil
9
- import moviepy.video.io.ImageSequenceClip
10
- from io import BytesIO
11
- from tkinter import Tcl
12
- from PIL import Image
13
 
14
  subprocess_executed = False
15
 
16
- # logo_image_path = '1.png' # Replace with your logo image path or URL
17
-
18
- # # Display the logo at the top of the page
19
- # st.image(logo_image_path, width=150)
20
- # st.title("Automated Abdominal Aortic Aneurysm Detection")
21
-
22
- # Upload a ZIP file containing DICOM slices
23
- st.write("Upload a ZIP file containing DICOM slices")
24
- uploaded_zip_file = st.file_uploader("Upload a .zip file", type=["zip"])
25
-
26
- @st.cache_resource
27
  def install_dependencies():
28
- command = "chmod +x install.sh"
29
- try:
30
- subprocess.run(command, shell=True, check=True)
31
- print("Script 'install.sh' has been made executable.")
32
- except subprocess.CalledProcessError as e:
33
- print(f"Error while making the script executable: {e}")
34
 
35
- command = "./install.sh"
36
- try:
37
- subprocess.run(command, shell=True, check=True)
38
- print("Script 'install.sh' has started running.")
39
- install_script_executed = True
40
- except subprocess.CalledProcessError as e:
41
- print(f"Error while running the script: {e}")
42
 
43
- @st.cache_resource
 
44
  def run_inference():
45
- command = "chmod +x inference.sh"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  try:
47
- subprocess.run(command, shell=True, check=True)
48
- print("Script 'inference.sh' has been made executable.")
49
- except subprocess.CalledProcessError as e:
50
- print(f"Error while making the script executable: {e}")
51
 
52
- command = "./inference.sh"
53
- try:
54
- subprocess.run(command, shell=True, check=True)
55
- print("Script 'inference.sh' has started running.")
56
- except subprocess.CalledProcessError as e:
57
- print(f"Error while running the script: {e}")
58
-
59
- if uploaded_zip_file is not None:
60
- try:
61
- install_dependencies()
62
-
63
- temp_dir = "/home/user/app/C2C/temp_dicom_dir"
64
-
65
- os.makedirs(temp_dir, exist_ok=True)
66
- full_path = os.path.abspath(temp_dir)
67
- st.success("Zip file uploaded successfully")
68
-
69
-
70
- with zipfile.ZipFile(uploaded_zip_file, "r") as zip_ref:
71
- zip_ref.extractall(temp_dir)
72
-
73
- # dicom_files = [os.path.join(temp_dir, f) for f in os.listdir(temp_dir) if f.endswith(".dcm")]
74
- # dicom_files.sort()
75
-
76
- # # Anonymize DICOM files
77
- # for file_path in dicom_files:
78
- # ds = pydicom.dcmread(file_path)
79
-
80
- # # Anonymize personal information
81
- # if 'PatientName' in ds:
82
- # ds.PatientName = 'Anonymized'
83
- # if 'PatientID' in ds:
84
- # ds.PatientID = '00000000'
85
-
86
- # print("Anonymized")
87
-
88
- # ds.save_as(file_path)
89
-
90
- except Exception as e:
91
- st.error(f"Error: {str(e)}")
92
-
93
- if st.button("Analyze"):
94
- try:
95
- st.success("Analysis started (expected time: 5 mins)")
96
- run_inference()
97
-
98
- outputs_directory = "/home/user/app/C2C/outputs"
99
-
100
- if os.path.exists(outputs_directory):
101
- subdirectories = [subdir for subdir in os.listdir(outputs_directory)
102
- if os.path.isdir(os.path.join(outputs_directory, subdir))]
103
-
104
- first_subdirectory = subdirectories[0] if subdirectories else None
105
-
106
- if first_subdirectory:
107
- subdirectory_path = os.path.join(outputs_directory, first_subdirectory)
108
- temp_dicom_dir_path = os.path.join(subdirectory_path, "temp_dicom_dir")
109
-
110
- dicom_subdirectories = [subdir for subdir in os.listdir(temp_dicom_dir_path)
111
- if os.path.isdir(os.path.join(temp_dicom_dir_path, subdir))]
112
-
113
- first_dicom_subdirectory = dicom_subdirectories[0] if dicom_subdirectories else None
114
-
115
- if first_dicom_subdirectory:
116
- video_path = os.path.join(temp_dicom_dir_path, first_dicom_subdirectory, "images/summary/aaa.mp4")
117
- image_path = os.path.join(temp_dicom_dir_path, first_dicom_subdirectory, "images/summary/diameter_graph.png")
118
- largest_slice = os.path.join(temp_dicom_dir_path, first_dicom_subdirectory, "images/summary/out.png")
119
-
120
- if os.path.exists(largest_slice):
121
- st.title("Largest Slice")
122
- st.image(largest_slice, use_column_width=True)
123
-
124
- # Display the video
125
- if os.path.exists(video_path):
126
- st.title("Video")
127
- st.video(video_path, format="video/mp4")
128
-
129
- # Display the image
130
- if os.path.exists(image_path):
131
- st.title("Diameter Graph")
132
- st.image(image_path, use_column_width=True)
133
  else:
134
- st.warning("No subdirectories or folders found inside 'temp_dicom_dir'.")
135
- else:
136
- st.warning("No subdirectories or folders found inside the 'outputs' directory.")
137
- else:
138
- st.error("The 'outputs' directory does not exist.")
139
- except Exception as e:
140
- st.error(f"An error occurred: {str(e)}")
 
1
  import streamlit as st
2
  import pydicom
 
3
  import zipfile
4
  import os
5
  import subprocess
 
 
 
 
 
 
6
 
7
  subprocess_executed = False
8
 
9
+ # Function to install dependencies
10
+ @st.cache(allow_output_mutation=True)
 
 
 
 
 
 
 
 
 
11
  def install_dependencies():
12
+ command = "chmod +x install.sh"
13
+ subprocess.run(command, shell=True, check=True)
 
 
 
 
14
 
15
+ command = "./install.sh"
16
+ subprocess.run(command, shell=True, check=True)
 
 
 
 
 
17
 
18
+ # Function to run inference
19
+ @st.cache(allow_output_mutation=True)
20
  def run_inference():
21
+ command = "chmod +x inference.sh"
22
+ subprocess.run(command, shell=True, check=True)
23
+
24
+ command = "./inference.sh"
25
+ subprocess.run(command, shell=True, check=True)
26
+
27
+ # Function to zip and download files
28
+ def zip_and_download(directory, zip_filename):
29
+ zipf = zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED)
30
+ for root, _, files in os.walk(directory):
31
+ for file in files:
32
+ zipf.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), directory))
33
+ zipf.close()
34
+
35
+ with open(zip_filename, 'rb') as f:
36
+ bytes_data = f.read()
37
+ st.download_button(label="Download Outputs", data=bytes_data, file_name=zip_filename, mime="application/zip")
38
+
39
+ # Main Streamlit code
40
+ def main():
41
+ st.write("Upload a ZIP file containing DICOM slices")
42
+ uploaded_zip_file = st.file_uploader("Upload a .zip file", type=["zip"])
43
+
44
+ if uploaded_zip_file is not None:
45
  try:
46
+ install_dependencies()
 
 
 
47
 
48
+ temp_dir = "/home/user/app/C2C/temp_dicom_dir"
49
+ os.makedirs(temp_dir, exist_ok=True)
50
+
51
+ with zipfile.ZipFile(uploaded_zip_file, "r") as zip_ref:
52
+ zip_ref.extractall(temp_dir)
53
+
54
+ st.success("Zip file uploaded successfully")
55
+
56
+ if st.button("Analyze"):
57
+ st.success("Analysis started (expected time: 5 mins)")
58
+ run_inference()
59
+
60
+ outputs_directory = "/home/user/app/C2C/outputs"
61
+
62
+ if os.path.exists(outputs_directory):
63
+ subdirectories = [subdir for subdir in os.listdir(outputs_directory)
64
+ if os.path.isdir(os.path.join(outputs_directory, subdir))]
65
+
66
+ first_subdirectory = subdirectories[0] if subdirectories else None
67
+
68
+ if first_subdirectory:
69
+ subdirectory_path = os.path.join(outputs_directory, first_subdirectory)
70
+ temp_dicom_dir_path = os.path.join(subdirectory_path, "temp_dicom_dir")
71
+
72
+ video_path = os.path.join(temp_dicom_dir_path, "images/summary/aaa.mp4")
73
+ image_path = os.path.join(temp_dicom_dir_path, "images/summary/diameter_graph.png")
74
+ largest_slice = os.path.join(temp_dicom_dir_path, "images/summary/out.png")
75
+
76
+ if os.path.exists(video_path) and os.path.exists(image_path) and os.path.exists(largest_slice):
77
+ zip_filename = os.path.join(temp_dir, "outputs.zip")
78
+ zip_and_download(temp_dicom_dir_path, zip_filename)
79
+ else:
80
+ st.error("Output files not found.")
81
+ else:
82
+ st.warning("No subdirectories found inside 'outputs'.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  else:
84
+ st.error("The 'outputs' directory does not exist.")
85
+ except Exception as e:
86
+ st.error(f"Error: {str(e)}")
87
+
88
+ if __name__ == "__main__":
89
+ main()