Spaces:
Sleeping
Sleeping
vincentgao95
commited on
Commit
•
c59e55b
1
Parent(s):
b6beafe
Update app.py
Browse files
app.py
CHANGED
@@ -10,36 +10,22 @@ plt.switch_backend('Agg')
|
|
10 |
|
11 |
def load_nrrd(file_obj):
|
12 |
data, _ = nrrd.read(file_obj)
|
13 |
-
|
|
|
14 |
|
15 |
-
def
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
elif view == 'Sagittal':
|
21 |
-
return data.shape[0]
|
22 |
-
|
23 |
-
def extract_slice(data, view, slice_index):
|
24 |
-
if view == 'Axial':
|
25 |
-
# Axial view: XY plane
|
26 |
-
slice_image = data[:, :, slice_index]
|
27 |
-
elif view == 'Coronal':
|
28 |
-
# Coronal view: XZ plane
|
29 |
-
slice_image = data[:, slice_index, :]
|
30 |
-
# Flip the image vertically for correct orientation
|
31 |
-
slice_image = np.flipud(slice_image).T
|
32 |
-
elif view == 'Sagittal':
|
33 |
-
# Sagittal view: YZ plane
|
34 |
-
slice_image = data[slice_index, :, :]
|
35 |
-
# Rotate the image for correct orientation
|
36 |
-
slice_image = np.rot90(slice_image, k=3)
|
37 |
|
38 |
-
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
43 |
|
44 |
# Plot the slice
|
45 |
fig, ax = plt.subplots()
|
@@ -55,24 +41,20 @@ def visualize_slice(file_obj, view, slice_index):
|
|
55 |
|
56 |
return pil_img
|
57 |
|
58 |
-
def update_slider(file_obj
|
59 |
-
|
60 |
-
num_slices = get_num_slices(data, view)
|
61 |
return gr.update(maximum=num_slices-1, value=0)
|
62 |
|
63 |
with gr.Blocks() as app:
|
64 |
gr.Markdown("## NRRD Slice Visualizer")
|
65 |
-
gr.Markdown("Upload an NRRD file
|
66 |
|
67 |
file_input = gr.File(label="Upload NRRD File")
|
68 |
-
view_selector = gr.Radio(choices=["Axial", "Coronal", "Sagittal"], label="View Selector", value="Axial")
|
69 |
slider = gr.Slider(minimum=0, maximum=1, step=1, value=0, label="Slice Selector")
|
70 |
image_output = gr.Image(type="pil", label="Selected Slice")
|
71 |
|
72 |
-
file_input.change(fn=update_slider, inputs=
|
73 |
-
|
74 |
-
|
75 |
-
slider.change(fn=visualize_slice, inputs=[file_input, view_selector, slider], outputs=image_output)
|
76 |
-
view_selector.change(fn=visualize_slice, inputs=[file_input, view_selector, slider], outputs=image_output)
|
77 |
|
78 |
app.launch()
|
|
|
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 |
+
# Rotate the image 90 degrees clockwise
|
25 |
+
slice_image = np.rot90(slice_image, k=-1)
|
26 |
+
|
27 |
+
# Flip the image from left to right along the vertical axis
|
28 |
+
slice_image = np.fliplr(slice_image)
|
29 |
|
30 |
# Plot the slice
|
31 |
fig, ax = plt.subplots()
|
|
|
41 |
|
42 |
return pil_img
|
43 |
|
44 |
+
def update_slider(file_obj):
|
45 |
+
_, num_slices = load_nrrd(file_obj)
|
|
|
46 |
return gr.update(maximum=num_slices-1, value=0)
|
47 |
|
48 |
with gr.Blocks() as app:
|
49 |
gr.Markdown("## NRRD Slice Visualizer")
|
50 |
+
gr.Markdown("Upload an NRRD file and use the slider to select and visualize slices.")
|
51 |
|
52 |
file_input = gr.File(label="Upload NRRD File")
|
|
|
53 |
slider = gr.Slider(minimum=0, maximum=1, step=1, value=0, label="Slice Selector")
|
54 |
image_output = gr.Image(type="pil", label="Selected Slice")
|
55 |
|
56 |
+
file_input.change(fn=update_slider, inputs=file_input, outputs=slider)
|
57 |
+
file_input.change(fn=visualize_slice, inputs=[file_input, slider], outputs=image_output)
|
58 |
+
slider.change(fn=visualize_slice, inputs=[file_input, slider], outputs=image_output)
|
|
|
|
|
59 |
|
60 |
app.launch()
|