Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -55,78 +55,86 @@ def run_inference():
|
|
| 55 |
print("Script 'inference.sh' has started running.")
|
| 56 |
except subprocess.CalledProcessError as e:
|
| 57 |
print(f"Error while running the script: {e}")
|
| 58 |
-
def zip_dir(directory, zip_filename):
|
| 59 |
-
with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 60 |
-
for root, dirs, files in os.walk(directory):
|
| 61 |
-
for file in files:
|
| 62 |
-
zipf.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), os.path.join(directory, '..')))
|
| 63 |
-
|
| 64 |
-
uploaded_zip_file = st.file_uploader("Upload a .zip file", type=["zip"], key="zip_uploader")
|
| 65 |
|
| 66 |
if uploaded_zip_file is not None:
|
| 67 |
-
try:
|
| 68 |
install_dependencies()
|
| 69 |
-
|
| 70 |
temp_dir = "/home/user/app/C2C/temp_dicom_dir"
|
|
|
|
| 71 |
os.makedirs(temp_dir, exist_ok=True)
|
|
|
|
|
|
|
| 72 |
|
|
|
|
| 73 |
with zipfile.ZipFile(uploaded_zip_file, "r") as zip_ref:
|
| 74 |
zip_ref.extractall(temp_dir)
|
| 75 |
|
| 76 |
dicom_files = [os.path.join(temp_dir, f) for f in os.listdir(temp_dir) if f.endswith(".dcm")]
|
| 77 |
-
dicom_files.sort()
|
| 78 |
|
| 79 |
# Anonymize DICOM files
|
| 80 |
for file_path in dicom_files:
|
| 81 |
ds = pydicom.dcmread(file_path)
|
|
|
|
| 82 |
# Anonymize personal information
|
| 83 |
if 'PatientName' in ds:
|
| 84 |
ds.PatientName = 'Anonymized'
|
| 85 |
if 'PatientID' in ds:
|
| 86 |
ds.PatientID = '00000000'
|
|
|
|
|
|
|
|
|
|
| 87 |
ds.save_as(file_path)
|
|
|
|
| 88 |
except Exception as e:
|
| 89 |
st.error(f"Error: {str(e)}")
|
| 90 |
|
| 91 |
if st.button("Analyze"):
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
else:
|
| 130 |
-
st.
|
| 131 |
-
|
| 132 |
-
st.
|
|
|
|
| 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)}")
|