Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,15 +4,19 @@ import os
|
|
4 |
import shutil
|
5 |
import uuid
|
6 |
import zipfile
|
|
|
|
|
|
|
7 |
|
8 |
def run_segmentation(uploaded_file, modality):
|
9 |
job_id = str(uuid.uuid4())
|
10 |
input_filename = f"input_{job_id}.nii.gz"
|
11 |
output_folder = f"segmentations_{job_id}"
|
12 |
|
|
|
13 |
with open(input_filename, "wb") as f:
|
14 |
f.write(uploaded_file.read())
|
15 |
-
|
16 |
command = ["TotalSegmentator", "-i", input_filename, "-o", output_folder]
|
17 |
if modality == "MR":
|
18 |
command.extend(["--task", "total_mr"])
|
@@ -20,8 +24,8 @@ def run_segmentation(uploaded_file, modality):
|
|
20 |
try:
|
21 |
subprocess.run(command, check=True)
|
22 |
except subprocess.CalledProcessError as e:
|
23 |
-
return f"Error during segmentation: {e}"
|
24 |
-
|
25 |
zip_filename = f"segmentations_{job_id}.zip"
|
26 |
with zipfile.ZipFile(zip_filename, "w", zipfile.ZIP_DEFLATED) as zipf:
|
27 |
for root, dirs, files in os.walk(output_folder):
|
@@ -30,25 +34,49 @@ def run_segmentation(uploaded_file, modality):
|
|
30 |
arcname = os.path.relpath(file_path, output_folder)
|
31 |
zipf.write(file_path, arcname)
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
os.remove(input_filename)
|
34 |
shutil.rmtree(output_folder)
|
35 |
|
36 |
-
return zip_filename
|
37 |
|
38 |
with gr.Blocks() as demo:
|
39 |
gr.Markdown("# TotalSegmentator Gradio App")
|
40 |
gr.Markdown(
|
41 |
"Upload a CT or MR image (in NIfTI format) and run segmentation using TotalSegmentator. "
|
42 |
-
"For MR images, the task flag is set accordingly."
|
43 |
)
|
44 |
|
45 |
with gr.Row():
|
46 |
uploaded_file = gr.File(label="Upload NIfTI Image (.nii.gz)")
|
47 |
modality = gr.Radio(choices=["CT", "MR"], label="Select Image Modality", value="CT")
|
48 |
|
49 |
-
|
|
|
|
|
50 |
|
51 |
run_btn = gr.Button("Run Segmentation")
|
52 |
-
run_btn.click(fn=run_segmentation, inputs=[uploaded_file, modality], outputs=
|
53 |
|
54 |
demo.launch()
|
|
|
4 |
import shutil
|
5 |
import uuid
|
6 |
import zipfile
|
7 |
+
import nibabel as nib
|
8 |
+
import numpy as np
|
9 |
+
import matplotlib.pyplot as plt
|
10 |
|
11 |
def run_segmentation(uploaded_file, modality):
|
12 |
job_id = str(uuid.uuid4())
|
13 |
input_filename = f"input_{job_id}.nii.gz"
|
14 |
output_folder = f"segmentations_{job_id}"
|
15 |
|
16 |
+
|
17 |
with open(input_filename, "wb") as f:
|
18 |
f.write(uploaded_file.read())
|
19 |
+
|
20 |
command = ["TotalSegmentator", "-i", input_filename, "-o", output_folder]
|
21 |
if modality == "MR":
|
22 |
command.extend(["--task", "total_mr"])
|
|
|
24 |
try:
|
25 |
subprocess.run(command, check=True)
|
26 |
except subprocess.CalledProcessError as e:
|
27 |
+
return f"Error during segmentation: {e}", None
|
28 |
+
|
29 |
zip_filename = f"segmentations_{job_id}.zip"
|
30 |
with zipfile.ZipFile(zip_filename, "w", zipfile.ZIP_DEFLATED) as zipf:
|
31 |
for root, dirs, files in os.walk(output_folder):
|
|
|
34 |
arcname = os.path.relpath(file_path, output_folder)
|
35 |
zipf.write(file_path, arcname)
|
36 |
|
37 |
+
|
38 |
+
seg_files = [os.path.join(output_folder, f) for f in os.listdir(output_folder) if f.endswith('.nii.gz')]
|
39 |
+
if seg_files:
|
40 |
+
seg_file = seg_files[0]
|
41 |
+
try:
|
42 |
+
seg_img = nib.load(seg_file)
|
43 |
+
seg_data = seg_img.get_fdata()
|
44 |
+
slice_idx = seg_data.shape[2] // 2
|
45 |
+
seg_slice = seg_data[:, :, slice_idx]
|
46 |
+
plt.figure(figsize=(6, 6))
|
47 |
+
plt.imshow(seg_slice.T, cmap="gray", origin="lower")
|
48 |
+
plt.axis('off')
|
49 |
+
image_filename = f"segmentation_preview_{job_id}.png"
|
50 |
+
plt.savefig(image_filename, bbox_inches='tight')
|
51 |
+
plt.close()
|
52 |
+
except Exception as e:
|
53 |
+
print(f"Error creating preview: {e}")
|
54 |
+
image_filename = None
|
55 |
+
else:
|
56 |
+
image_filename = None
|
57 |
+
|
58 |
+
|
59 |
os.remove(input_filename)
|
60 |
shutil.rmtree(output_folder)
|
61 |
|
62 |
+
return zip_filename, image_filename
|
63 |
|
64 |
with gr.Blocks() as demo:
|
65 |
gr.Markdown("# TotalSegmentator Gradio App")
|
66 |
gr.Markdown(
|
67 |
"Upload a CT or MR image (in NIfTI format) and run segmentation using TotalSegmentator. "
|
68 |
+
"For MR images, the task flag is set accordingly. A preview of one segmentation slice will be displayed."
|
69 |
)
|
70 |
|
71 |
with gr.Row():
|
72 |
uploaded_file = gr.File(label="Upload NIfTI Image (.nii.gz)")
|
73 |
modality = gr.Radio(choices=["CT", "MR"], label="Select Image Modality", value="CT")
|
74 |
|
75 |
+
with gr.Row():
|
76 |
+
zip_output = gr.File(label="Download Segmentation Output (zip)")
|
77 |
+
preview_output = gr.Image(label="Segmentation Preview")
|
78 |
|
79 |
run_btn = gr.Button("Run Segmentation")
|
80 |
+
run_btn.click(fn=run_segmentation, inputs=[uploaded_file, modality], outputs=[zip_output, preview_output])
|
81 |
|
82 |
demo.launch()
|