DHEIVER commited on
Commit
d67cff1
1 Parent(s): 5f49817

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -23
app.py CHANGED
@@ -3,13 +3,19 @@ import pydicom
3
  import matplotlib.pyplot as plt
4
  import os
5
 
6
- def visualize_dicom_sequence(file_paths):
7
- for file_path in file_paths:
8
- ds = pydicom.dcmread(file_path)
9
- image_sequence = ds.pixel_array
10
-
 
 
 
 
 
 
 
11
  for i, image in enumerate(image_sequence):
12
- # Plot DICOM image using Matplotlib
13
  fig, ax = plt.subplots()
14
  ax.imshow(image, cmap=plt.cm.gray)
15
  ax.axis('off')
@@ -18,27 +24,22 @@ def visualize_dicom_sequence(file_paths):
18
  def main():
19
  st.title("Visualizador DICOM")
20
 
21
- # Upload DICOM files
22
- uploaded_files = st.file_uploader("Selecione um ou mais arquivos DICOM", type=".dcm", multiple=True)
23
-
24
- if uploaded_files:
25
- file_paths = []
26
 
27
- # Save the uploaded files to temporary locations
28
- for uploaded_file in uploaded_files:
29
- file_bytes = uploaded_file.getvalue()
30
- file_path = f"temp_{uploaded_file.name}"
31
- file_paths.append(file_path)
32
 
33
- with open(file_path, "wb") as f:
34
- f.write(file_bytes)
 
35
 
36
- # Visualize the DICOM image sequences
37
- visualize_dicom_sequence(file_paths)
38
 
39
- # Remove the temporary files
40
- for file_path in file_paths:
41
- os.remove(file_path)
42
 
43
  if __name__ == "__main__":
44
  main()
 
3
  import matplotlib.pyplot as plt
4
  import os
5
 
6
+ def visualize_dicom_sequence(file_path):
7
+ ds = pydicom.dcmread(file_path)
8
+ image_sequence = ds.pixel_array
9
+
10
+ if image_sequence.ndim == 2:
11
+ # Only one image in the sequence
12
+ fig, ax = plt.subplots()
13
+ ax.imshow(image_sequence, cmap=plt.cm.gray)
14
+ ax.axis('off')
15
+ st.pyplot(fig)
16
+ else:
17
+ # Multiple images in the sequence
18
  for i, image in enumerate(image_sequence):
 
19
  fig, ax = plt.subplots()
20
  ax.imshow(image, cmap=plt.cm.gray)
21
  ax.axis('off')
 
24
  def main():
25
  st.title("Visualizador DICOM")
26
 
27
+ # Upload DICOM file
28
+ uploaded_file = st.file_uploader("Selecione um arquivo DICOM", type=".dcm")
 
 
 
29
 
30
+ if uploaded_file is not None:
31
+ # Convert uploaded file to bytes
32
+ file_bytes = uploaded_file.getvalue()
 
 
33
 
34
+ # Save the uploaded file to a temporary location
35
+ with open("temp.dcm", "wb") as f:
36
+ f.write(file_bytes)
37
 
38
+ # Visualize the DICOM image sequence
39
+ visualize_dicom_sequence("temp.dcm")
40
 
41
+ # Remove the temporary file
42
+ os.remove("temp.dcm")
 
43
 
44
  if __name__ == "__main__":
45
  main()