Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,104 +1,102 @@
|
|
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'
|
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 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
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 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
95 |
st.success("Analysis started (expected time: 5 mins)")
|
96 |
run_inference()
|
97 |
|
98 |
-
|
99 |
outputs_directory = "/home/user/app/C2C/outputs"
|
100 |
-
|
101 |
-
|
|
|
|
|
102 |
|
103 |
subdirectories = [subdir for subdir in os.listdir(outputs_directory)
|
104 |
if os.path.isdir(os.path.join(outputs_directory, subdir))]
|
@@ -123,12 +121,10 @@ if st.button("Analyze"):
|
|
123 |
st.title("Largest Slice")
|
124 |
st.image(largest_slice, use_column_width=True)
|
125 |
|
126 |
-
# Display the video
|
127 |
if os.path.exists(video_path):
|
128 |
st.title("Video")
|
129 |
st.video(video_path, format="video/mp4")
|
130 |
|
131 |
-
# Display the image
|
132 |
if os.path.exists(image_path):
|
133 |
st.title("Diameter Graph")
|
134 |
st.image(image_path, use_column_width=True)
|
@@ -136,4 +132,3 @@ if st.button("Analyze"):
|
|
136 |
st.warning("No subdirectories or folders found inside 'temp_dicom_dir'.")
|
137 |
else:
|
138 |
st.warning("No subdirectories or folders found inside the 'outputs' directory.")
|
139 |
-
|
|
|
1 |
import streamlit as st
|
2 |
import pydicom
|
|
|
3 |
import zipfile
|
4 |
import os
|
5 |
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
subprocess_executed = False
|
8 |
|
9 |
+
logo_image_path = '1.png'
|
|
|
|
|
10 |
st.image(logo_image_path, width=150)
|
11 |
st.title("Automated Abdominal Aortic Aneurysm Detection")
|
12 |
|
|
|
13 |
st.write("Upload a ZIP file containing DICOM slices")
|
14 |
uploaded_zip_file = st.file_uploader("Upload a .zip file", type=["zip"])
|
15 |
|
16 |
@st.cache_resource
|
17 |
def install_dependencies():
|
18 |
+
command = "chmod +x install.sh"
|
19 |
+
try:
|
20 |
+
subprocess.run(command, shell=True, check=True)
|
21 |
+
except subprocess.CalledProcessError as e:
|
22 |
+
pass
|
23 |
+
|
24 |
+
command = "./install.sh"
|
25 |
+
try:
|
26 |
+
subprocess.run(command, shell=True, check=True)
|
27 |
+
except subprocess.CalledProcessError as e:
|
28 |
+
pass
|
|
|
|
|
|
|
29 |
|
30 |
@st.cache_resource
|
31 |
def run_inference():
|
32 |
+
command = "chmod +x inference.sh"
|
33 |
+
try:
|
34 |
+
subprocess.run(command, shell=True, check=True)
|
35 |
+
except subprocess.CalledProcessError as e:
|
36 |
+
pass
|
37 |
+
|
38 |
+
command = "./inference.sh"
|
39 |
+
try:
|
40 |
+
subprocess.run(command, shell=True, check=True)
|
41 |
+
except subprocess.CalledProcessError as e:
|
42 |
+
pass
|
43 |
+
|
44 |
+
def create_zip_file():
|
45 |
+
outputs_directory = "/home/user/app/C2C/outputs"
|
46 |
+
subdirectories = [subdir for subdir in os.listdir(outputs_directory)
|
47 |
+
if os.path.isdir(os.path.join(outputs_directory, subdir))]
|
48 |
+
first_subdirectory = subdirectories[0] if subdirectories else None
|
49 |
+
if first_subdirectory:
|
50 |
+
subdirectory_path = os.path.join(outputs_directory, first_subdirectory)
|
51 |
+
temp_dicom_dir_path = os.path.join(subdirectory_path, "temp_dicom_dir")
|
52 |
+
|
53 |
+
largest_slice = os.path.join(temp_dicom_dir_path, "largest_slice.png")
|
54 |
+
diameter_graph = os.path.join(temp_dicom_dir_path, "diameter_graph.png")
|
55 |
+
video_path = os.path.join(temp_dicom_dir_path, "video.mp4")
|
56 |
+
|
57 |
+
files_to_zip = [largest_slice, diameter_graph, video_path]
|
58 |
+
|
59 |
+
zip_file_path = os.path.join(outputs_directory, "outputs.zip")
|
60 |
+
with zipfile.ZipFile(zip_file_path, 'w') as zipf:
|
61 |
+
for file in files_to_zip:
|
62 |
+
zipf.write(file, os.path.basename(file))
|
63 |
+
return zip_file_path
|
64 |
+
else:
|
65 |
+
return None
|
66 |
|
67 |
if uploaded_zip_file is not None:
|
68 |
+
try:
|
69 |
install_dependencies()
|
70 |
+
|
71 |
temp_dir = "/home/user/app/C2C/temp_dicom_dir"
|
|
|
72 |
os.makedirs(temp_dir, exist_ok=True)
|
|
|
|
|
73 |
|
|
|
74 |
with zipfile.ZipFile(uploaded_zip_file, "r") as zip_ref:
|
75 |
zip_ref.extractall(temp_dir)
|
76 |
|
77 |
dicom_files = [os.path.join(temp_dir, f) for f in os.listdir(temp_dir) if f.endswith(".dcm")]
|
78 |
+
dicom_files.sort()
|
79 |
|
|
|
80 |
for file_path in dicom_files:
|
81 |
ds = pydicom.dcmread(file_path)
|
|
|
|
|
82 |
if 'PatientName' in ds:
|
83 |
ds.PatientName = 'Anonymized'
|
84 |
if 'PatientID' in ds:
|
85 |
ds.PatientID = '00000000'
|
|
|
|
|
|
|
86 |
ds.save_as(file_path)
|
87 |
+
|
88 |
except Exception as e:
|
89 |
st.error(f"Error: {str(e)}")
|
90 |
|
91 |
if st.button("Analyze"):
|
|
|
92 |
st.success("Analysis started (expected time: 5 mins)")
|
93 |
run_inference()
|
94 |
|
|
|
95 |
outputs_directory = "/home/user/app/C2C/outputs"
|
96 |
+
zip_file_path = create_zip_file()
|
97 |
+
if zip_file_path:
|
98 |
+
st.success("Analysis completed. Click the button below to download the outputs.")
|
99 |
+
st.download_button(label="Download Outputs", data=zip_file_path, file_name="outputs.zip", mime="application/zip")
|
100 |
|
101 |
subdirectories = [subdir for subdir in os.listdir(outputs_directory)
|
102 |
if os.path.isdir(os.path.join(outputs_directory, subdir))]
|
|
|
121 |
st.title("Largest Slice")
|
122 |
st.image(largest_slice, use_column_width=True)
|
123 |
|
|
|
124 |
if os.path.exists(video_path):
|
125 |
st.title("Video")
|
126 |
st.video(video_path, format="video/mp4")
|
127 |
|
|
|
128 |
if os.path.exists(image_path):
|
129 |
st.title("Diameter Graph")
|
130 |
st.image(image_path, use_column_width=True)
|
|
|
132 |
st.warning("No subdirectories or folders found inside 'temp_dicom_dir'.")
|
133 |
else:
|
134 |
st.warning("No subdirectories or folders found inside the 'outputs' directory.")
|
|