aaa_web_app / app.py
AdritRao's picture
Update app.py
de16dcc verified
raw
history blame
5.3 kB
import streamlit as st
import pydicom
import matplotlib.pyplot as plt
import zipfile
import os
import subprocess
from datetime import datetime
import shutil
import moviepy.video.io.ImageSequenceClip
from io import BytesIO
from tkinter import Tcl
from PIL import Image
subprocess_executed = False
# logo_image_path = '1.png' # Replace with your logo image path or URL
# # Display the logo at the top of the page
# st.image(logo_image_path, width=150)
# st.title("Automated Abdominal Aortic Aneurysm Detection")
# Upload a ZIP file containing DICOM slices
st.write("Upload a ZIP file containing DICOM slices")
uploaded_zip_file = st.file_uploader("Upload a .zip file", type=["zip"])
@st.cache_resource
def install_dependencies():
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.")
install_script_executed = True
except subprocess.CalledProcessError as e:
print(f"Error while running the script: {e}")
@st.cache_resource
def run_inference():
command = "chmod +x inference.sh"
try:
subprocess.run(command, shell=True, check=True)
print("Script 'inference.sh' has been made executable.")
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 running the script: {e}")
def zip_dir(directory, zip_filename):
with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, dirs, files in os.walk(directory):
for file in files:
zipf.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), os.path.join(directory, '..')))
uploaded_zip_file = st.file_uploader("Upload a .zip file", type=["zip"])
if uploaded_zip_file is not None:
try:
install_dependencies()
temp_dir = "/home/user/app/C2C/temp_dicom_dir"
os.makedirs(temp_dir, exist_ok=True)
with zipfile.ZipFile(uploaded_zip_file, "r") as zip_ref:
zip_ref.extractall(temp_dir)
dicom_files = [os.path.join(temp_dir, f) for f in os.listdir(temp_dir) if f.endswith(".dcm")]
dicom_files.sort()
# Anonymize DICOM files
for file_path in dicom_files:
ds = pydicom.dcmread(file_path)
# Anonymize personal information
if 'PatientName' in ds:
ds.PatientName = 'Anonymized'
if 'PatientID' in ds:
ds.PatientID = '00000000'
ds.save_as(file_path)
except Exception as e:
st.error(f"Error: {str(e)}")
if st.button("Analyze"):
st.success("Analysis started (expected time: 5 mins)")
run_inference()
outputs_directory = "/home/user/app/C2C/outputs"
if os.path.exists(outputs_directory):
zip_filename = "/home/user/app/C2C/outputs.zip"
zip_dir(outputs_directory, zip_filename)
st.markdown(f"Download your outputs [here](/{zip_filename})")
subdirectories = [subdir for subdir in os.listdir(outputs_directory) if os.path.isdir(os.path.join(outputs_directory, subdir))]
first_subdirectory = subdirectories[0] if subdirectories else None
if first_subdirectory:
subdirectory_path = os.path.join(outputs_directory, first_subdirectory)
temp_dicom_dir_path = os.path.join(subdirectory_path, "temp_dicom_dir")
dicom_subdirectories = [subdir for subdir in os.listdir(temp_dicom_dir_path) if os.path.isdir(os.path.join(temp_dicom_dir_path, subdir))]
first_dicom_subdirectory = dicom_subdirectories[0] if dicom_subdirectories else None
if first_dicom_subdirectory:
video_path = os.path.join(temp_dicom_dir_path, first_dicom_subdirectory, "images/summary/aaa.mp4")
image_path = os.path.join(temp_dicom_dir_path, first_dicom_subdirectory, "images/summary/diameter_graph.png")
largest_slice = os.path.join(temp_dicom_dir_path, first_dicom_subdirectory, "images/summary/out.png")
if os.path.exists(largest_slice):
st.title("Largest Slice")
st.image(largest_slice, use_column_width=True)
# Display the video
if os.path.exists(video_path):
st.title("Video")
st.video(video_path, format="video/mp4")
# Display the image
if os.path.exists(image_path):
st.title("Diameter Graph")
st.image(image_path, use_column_width=True)
else:
st.warning("No subdirectories or folders found inside 'temp_dicom_dir'.")
else:
st.warning("No subdirectories or folders found inside the 'outputs' directory.")