vincentgao95 commited on
Commit
c2f693f
·
verified ·
1 Parent(s): edfe705

ADD 3 VIEWS

Browse files
Files changed (1) hide show
  1. app.py +40 -19
app.py CHANGED
@@ -10,24 +10,43 @@ plt.switch_backend('Agg')
10
 
11
  def load_nrrd(file_obj):
12
  data, _ = nrrd.read(file_obj)
13
- num_slices = data.shape[2]
14
- return data, num_slices
15
 
16
- def visualize_slice(file_obj, slice_index):
17
- data, num_slices = load_nrrd(file_obj)
18
 
19
- # Ensure the slice index is within the range of available slices
20
- slice_index = min(max(0, slice_index), num_slices - 1)
21
-
22
- slice_image = data[:, :, slice_index]
23
 
24
- # Plot the slice
25
- fig, ax = plt.subplots()
26
- ax.imshow(slice_image, cmap='gray')
27
- plt.axis('off')
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  # Convert matplotlib figure to PIL Image
30
  buf = io.BytesIO()
 
31
  fig.savefig(buf, format='png')
32
  plt.close(fig)
33
  buf.seek(0)
@@ -36,19 +55,21 @@ def visualize_slice(file_obj, slice_index):
36
  return pil_img
37
 
38
  def update_slider(file_obj):
39
- _, num_slices = load_nrrd(file_obj)
40
- return gr.update(maximum=num_slices-1, value=0)
 
 
41
 
42
  with gr.Blocks() as app:
43
- gr.Markdown("## NRRD Slice Visualizer")
44
- gr.Markdown("Upload an NRRD file and use the slider to select and visualize slices.")
45
 
46
  file_input = gr.File(label="Upload NRRD File")
47
  slider = gr.Slider(minimum=0, maximum=1, step=1, value=0, label="Slice Selector")
48
- image_output = gr.Image(type="pil", label="Selected Slice")
49
 
50
  file_input.change(fn=update_slider, inputs=file_input, outputs=slider)
51
- file_input.change(fn=visualize_slice, inputs=[file_input, slider], outputs=image_output)
52
- slider.change(fn=visualize_slice, inputs=[file_input, slider], outputs=image_output)
53
 
54
  app.launch()
 
10
 
11
  def load_nrrd(file_obj):
12
  data, _ = nrrd.read(file_obj)
13
+ return data
 
14
 
15
+ def visualize_slices(file_obj, slice_index):
16
+ data = load_nrrd(file_obj)
17
 
18
+ num_slices_axial = data.shape[2]
19
+ num_slices_coronal = data.shape[1]
20
+ num_slices_sagittal = data.shape[0]
 
21
 
22
+ # Ensure the slice index is within the range of available slices for each view
23
+ slice_index_axial = min(max(0, slice_index), num_slices_axial - 1)
24
+ slice_index_coronal = min(max(0, slice_index), num_slices_coronal - 1)
25
+ slice_index_sagittal = min(max(0, slice_index), num_slices_sagittal - 1)
26
 
27
+ # Extract slices
28
+ slice_axial = data[:, :, slice_index_axial]
29
+ slice_coronal = data[:, slice_index_coronal, :]
30
+ slice_sagittal = data[slice_index_sagittal, :, :]
31
+
32
+ # Plot the slices
33
+ fig, axes = plt.subplots(1, 3, figsize=(15, 5))
34
+
35
+ axes[0].imshow(slice_axial, cmap='gray')
36
+ axes[0].set_title("Axial View")
37
+ axes[0].axis('off')
38
+
39
+ axes[1].imshow(slice_coronal, cmap='gray')
40
+ axes[1].set_title("Coronal View")
41
+ axes[1].axis('off')
42
+
43
+ axes[2].imshow(slice_sagittal, cmap='gray')
44
+ axes[2].set_title("Sagittal View")
45
+ axes[2].axis('off')
46
+
47
  # Convert matplotlib figure to PIL Image
48
  buf = io.BytesIO()
49
+ plt.tight_layout()
50
  fig.savefig(buf, format='png')
51
  plt.close(fig)
52
  buf.seek(0)
 
55
  return pil_img
56
 
57
  def update_slider(file_obj):
58
+ data = load_nrrd(file_obj)
59
+ # Set maximum based on the axis with the smallest number of slices
60
+ max_slices = min(data.shape)
61
+ return gr.update(maximum=max_slices-1, value=0)
62
 
63
  with gr.Blocks() as app:
64
+ gr.Markdown("## NRRD Multi-View Slice Visualizer")
65
+ gr.Markdown("Upload an NRRD file and use the slider to select and visualize axial, coronal, and sagittal slices.")
66
 
67
  file_input = gr.File(label="Upload NRRD File")
68
  slider = gr.Slider(minimum=0, maximum=1, step=1, value=0, label="Slice Selector")
69
+ image_output = gr.Image(type="pil", label="Selected Slices (Axial, Coronal, Sagittal)")
70
 
71
  file_input.change(fn=update_slider, inputs=file_input, outputs=slider)
72
+ file_input.change(fn=visualize_slices, inputs=[file_input, slider], outputs=image_output)
73
+ slider.change(fn=visualize_slices, inputs=[file_input, slider], outputs=image_output)
74
 
75
  app.launch()