aaa_web_app / app.py
AdritRao's picture
Update app.py
8c97be5
raw
history blame
5.58 kB
import streamlit as st
import pydicom
import matplotlib.pyplot as plt
import zipfile
import os
import subprocess
from datetime import datetime
import shutil
# 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"])
st.write("Upload a ZIP file containing DICOM slices to view the images.")
@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}")
# Function to read and display the DICOM image
@st.cache_resource
def convert_dicom_to_png(_dicom_data):
# Convert DICOM to a PNG image
plt.imshow(_dicom_data.pixel_array, cmap=plt.cm.bone)
plt.axis("off")
plt.title("DICOM Image")
plt.tight_layout()
img_buffer = BytesIO()
plt.savefig(img_buffer, format="png")
img_buffer.seek(0)
return img_buffer
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)
full_path = os.path.abspath(temp_dir)
st.write(f"Temporary directory path: {full_path}")
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:
# Create a list of DICOM data
dicom_data_list = [pydicom.dcmread(dicom_file) for dicom_file in dicom_files]
# Convert DICOM slices to PNG frames
png_frames = [convert_dicom_to_png(_dicom_data) for _dicom_data in dicom_data_list]
# Create a list of image clips from PNG frames
clips = [mp.ImageClip(png_frame, duration=1) for png_frame in png_frames]
# Concatenate the image clips to create a video
video_clip = mp.concatenate_videoclips(clips, method="compose")
# Display the video in Streamlit
st.video(video_clip, format="video/mp4")
except Exception as e:
st.error(f"Error: {str(e)}")
if st.button("Analyze"):
run_inference()
# Assuming the 'outputs' directory is located at "/home/user/app/C2C/comp2comp/outputs"
outputs_directory = "/home/user/app/C2C/outputs"
# Get a list of subdirectories inside the 'outputs' directory
subdirectories = [subdir for subdir in os.listdir(outputs_directory)
if os.path.isdir(os.path.join(outputs_directory, subdir))]
# Use the first (or only) subdirectory, or None if there are no subdirectories
first_subdirectory = subdirectories[0] if subdirectories else None
if first_subdirectory:
# Construct the paths to the video and image files inside the first subdirectory
video_path = os.path.join(outputs_directory, first_subdirectory, "temp_dicom_dir/images/summary/aaa.mp4")
image_path = os.path.join(outputs_directory, first_subdirectory, "temp_dicom_dir/images/summary/diameter_graph.png")
largest_slice = os.path.join(outputs_directory, first_subdirectory, "temp_dicom_dir/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 generated by the inference script
if os.path.exists(video_path):
st.title("Video")
st.video(video_path, format="video/mp4")
# Display the image generated by the inference script
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 the 'outputs' directory.")