vincentgao95 commited on
Commit
49fb0f5
1 Parent(s): a5019f4

Update app.py

Browse files

Adjust the slice orientation by transposing and flipping the slice data.
Add options to visualize different views (axial, coronal, sagittal).

Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -10,16 +10,20 @@ plt.switch_backend('Agg')
10
 
11
  def load_nrrd(file_path):
12
  data, _ = nrrd.read(file_path.name)
13
- num_slices = data.shape[2]
14
- return data, num_slices
15
 
16
- def visualize_slice(file_path, slice_index):
17
- data, num_slices = load_nrrd(file_path)
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()
@@ -35,20 +39,22 @@ def visualize_slice(file_path, slice_index):
35
 
36
  return pil_img
37
 
38
- def update_slider(file_path):
39
- _, num_slices = load_nrrd(file_path)
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_path):
12
  data, _ = nrrd.read(file_path.name)
13
+ return data
 
14
 
15
+ def visualize_slice(file_path, slice_index, view_axis):
16
+ data = load_nrrd(file_path)
17
 
18
  # Ensure the slice index is within the range of available slices
19
+ slice_index = min(max(0, slice_index), data.shape[view_axis] - 1)
20
 
21
+ if view_axis == 0: # Axial
22
+ slice_image = np.flipud(np.rot90(data[slice_index, :, :], k=1))
23
+ elif view_axis == 1: # Coronal
24
+ slice_image = np.flipud(np.rot90(data[:, slice_index, :], k=1))
25
+ elif view_axis == 2: # Sagittal
26
+ slice_image = np.flipud(np.rot90(data[:, :, slice_index], k=1))
27
 
28
  # Plot the slice
29
  fig, ax = plt.subplots()
 
39
 
40
  return pil_img
41
 
42
+ def update_slider(file_path, view_axis):
43
+ data = load_nrrd(file_path)
44
+ return gr.update(maximum=data.shape[view_axis] - 1, value=0)
45
 
46
  with gr.Blocks() as app:
47
  gr.Markdown("## NRRD Slice Visualizer")
48
  gr.Markdown("Upload an NRRD file and use the slider to select and visualize slices.")
49
 
50
  file_input = gr.File(label="Upload NRRD File")
51
+ view_axis_selector = gr.Radio(choices=[0, 1, 2], value=2, label="View Axis", info="0: Axial, 1: Coronal, 2: Sagittal")
52
  slider = gr.Slider(minimum=0, maximum=1, step=1, value=0, label="Slice Selector")
53
  image_output = gr.Image(type="pil", label="Selected Slice")
54
 
55
+ file_input.change(fn=update_slider, inputs=[file_input, view_axis_selector], outputs=slider)
56
+ file_input.change(fn=visualize_slice, inputs=[file_input, slider, view_axis_selector], outputs=image_output)
57
+ view_axis_selector.change(fn=update_slider, inputs=[file_input, view_axis_selector], outputs=slider)
58
+ slider.change(fn=visualize_slice, inputs=[file_input, slider, view_axis_selector], outputs=image_output)
59
 
60
  app.launch()