AdritRao commited on
Commit
35ff262
·
1 Parent(s): f137d02

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -10
app.py CHANGED
@@ -52,15 +52,16 @@ def run_inference():
52
 
53
  # Function to read and display the DICOM image
54
  @st.cache_resource
55
- def display_dicom_image(selected_slice, dicom_files):
56
- dicom_data = pydicom.dcmread(dicom_files[selected_slice])
57
-
58
- # Display the DICOM image
59
  plt.imshow(dicom_data.pixel_array, cmap=plt.cm.bone)
60
  plt.axis("off")
61
  plt.title("DICOM Image")
62
  plt.tight_layout()
63
- return plt
 
 
 
64
 
65
  if uploaded_zip_file is not None:
66
  try:
@@ -83,12 +84,20 @@ if uploaded_zip_file is not None:
83
  if len(dicom_files) == 0:
84
  st.error("No DICOM files found in the ZIP archive.")
85
  else:
86
- # Display a slider for selecting the slice
87
- selected_slice = st.slider("Select a slice", 0, len(dicom_files) - 1, 0)
 
 
 
 
 
 
 
 
 
88
 
89
- # Display the DICOM image using the cached function
90
- plt = display_dicom_image(selected_slice, dicom_files)
91
- st.pyplot(plt)
92
 
93
  except Exception as e:
94
  st.error(f"Error: {str(e)}")
 
52
 
53
  # Function to read and display the DICOM image
54
  @st.cache_resource
55
+ def convert_dicom_to_png(dicom_data):
56
+ # Convert DICOM to a PNG image
 
 
57
  plt.imshow(dicom_data.pixel_array, cmap=plt.cm.bone)
58
  plt.axis("off")
59
  plt.title("DICOM Image")
60
  plt.tight_layout()
61
+ img_buffer = BytesIO()
62
+ plt.savefig(img_buffer, format="png")
63
+ img_buffer.seek(0)
64
+ return img_buffer
65
 
66
  if uploaded_zip_file is not None:
67
  try:
 
84
  if len(dicom_files) == 0:
85
  st.error("No DICOM files found in the ZIP archive.")
86
  else:
87
+ # Create a list of DICOM data
88
+ dicom_data_list = [pydicom.dcmread(dicom_file) for dicom_file in dicom_files]
89
+
90
+ # Convert DICOM slices to PNG frames
91
+ png_frames = [convert_dicom_to_png(dicom_data) for dicom_data in dicom_data_list]
92
+
93
+ # Create a list of image clips from PNG frames
94
+ clips = [mp.ImageClip(png_frame, duration=1) for png_frame in png_frames]
95
+
96
+ # Concatenate the image clips to create a video
97
+ video_clip = mp.concatenate_videoclips(clips, method="compose")
98
 
99
+ # Display the video in Streamlit
100
+ st.video(video_clip, format="video/mp4")
 
101
 
102
  except Exception as e:
103
  st.error(f"Error: {str(e)}")