Spaces:
Sleeping
Sleeping
西牧慧 commited on
Commit ·
801a214
1
Parent(s): 798d585
update: log
Browse files- src/parcellation.py +20 -1
src/parcellation.py
CHANGED
|
@@ -93,39 +93,57 @@ def run_inference(input_file, only_face_cropping, only_skull_stripping):
|
|
| 93 |
print(f"Using device: {device}")
|
| 94 |
|
| 95 |
# Load the pre-trained models from the fixed "model/" folder
|
|
|
|
| 96 |
cnet, ssnet, pnet_c, pnet_s, pnet_a, hnet_c, hnet_a = load_model("model/", device=device)
|
|
|
|
| 97 |
# cnet, ssnet, pnet_a, hnet_c, hnet_a = load_model("model/", device=device)
|
| 98 |
|
| 99 |
# --- Processing Flow (based on the original parcellation.py) ---
|
| 100 |
# 1. Load the input image, convert to canonical orientation, and remove extra dimensions
|
|
|
|
| 101 |
odata = nib.squeeze_image(nib.as_closest_canonical(nib.load(input_file.name)))
|
| 102 |
nii = nib.Nifti1Image(odata.get_fdata().astype(np.float32), affine=odata.affine)
|
| 103 |
os.makedirs(os.path.join(opt.o, "original"), exist_ok=True)
|
| 104 |
original_nii_path = os.path.join(opt.o, f"original/{basename}.nii")
|
| 105 |
nib.save(nii, original_nii_path)
|
|
|
|
| 106 |
|
| 107 |
# 2. Preprocess the image
|
|
|
|
| 108 |
odata, data = preprocessing(input_file.name, opt.o, basename)
|
|
|
|
| 109 |
|
| 110 |
# 3. Cropping
|
|
|
|
| 111 |
cropped, out_filename = cropping(opt.o, basename, odata, data, cnet, device)
|
|
|
|
| 112 |
if only_face_cropping:
|
| 113 |
pass
|
| 114 |
|
| 115 |
else:
|
| 116 |
# 4. Skull stripping
|
|
|
|
| 117 |
stripped, shift, out_filename = stripping(opt.o, basename, cropped, odata, data, ssnet, device)
|
|
|
|
| 118 |
if only_skull_stripping:
|
| 119 |
pass
|
| 120 |
else:
|
| 121 |
# 5. Parcellation
|
|
|
|
| 122 |
parcellated = parcellation(stripped, pnet_c, pnet_s, pnet_a, device)
|
|
|
|
| 123 |
# 6. Separate into hemispheres
|
|
|
|
| 124 |
separated = hemisphere(stripped, hnet_c, hnet_a, device)
|
|
|
|
| 125 |
# 7. Postprocessing
|
|
|
|
| 126 |
output = postprocessing(parcellated, separated, shift, device)
|
|
|
|
| 127 |
# 8. Create CSV with volume information, etc.
|
|
|
|
| 128 |
df = make_csv(output, opt.o, basename)
|
|
|
|
| 129 |
# 9. Create and save the parcellation result NIfTI file
|
| 130 |
nii_out = nib.Nifti1Image(output.astype(np.uint16), affine=data.affine)
|
| 131 |
header = odata.header
|
|
@@ -140,6 +158,7 @@ def run_inference(input_file, only_face_cropping, only_skull_stripping):
|
|
| 140 |
out_filename = os.path.join(out_parcellated_dir, f"{basename}_Type1_Level5.nii")
|
| 141 |
nib.save(nii_out, out_filename)
|
| 142 |
create_parcellated_images(output, opt.o, basename, odata, data)
|
|
|
|
| 143 |
|
| 144 |
# Zip the entire output directory into a ZIP file
|
| 145 |
zip_path = os.path.join(os.path.dirname(opt.o), f"{basename}_results.zip")
|
|
@@ -156,7 +175,7 @@ def run_inference(input_file, only_face_cropping, only_skull_stripping):
|
|
| 156 |
|
| 157 |
# *** Cleanup: Remove the temporary output directory ***
|
| 158 |
# Note: This is performed before returning. It is not possible to execute code after the return statement.
|
| 159 |
-
shutil.rmtree(opt.o)
|
| 160 |
|
| 161 |
# Return the ZIP file path and the two visualization images
|
| 162 |
return zip_path, Image.open(input_png_path), Image.open(parcellation_png_path)
|
|
|
|
| 93 |
print(f"Using device: {device}")
|
| 94 |
|
| 95 |
# Load the pre-trained models from the fixed "model/" folder
|
| 96 |
+
print("Loading models...")
|
| 97 |
cnet, ssnet, pnet_c, pnet_s, pnet_a, hnet_c, hnet_a = load_model("model/", device=device)
|
| 98 |
+
print("Models loaded successfully.")
|
| 99 |
# cnet, ssnet, pnet_a, hnet_c, hnet_a = load_model("model/", device=device)
|
| 100 |
|
| 101 |
# --- Processing Flow (based on the original parcellation.py) ---
|
| 102 |
# 1. Load the input image, convert to canonical orientation, and remove extra dimensions
|
| 103 |
+
print("Loading and preprocessing the input image...")
|
| 104 |
odata = nib.squeeze_image(nib.as_closest_canonical(nib.load(input_file.name)))
|
| 105 |
nii = nib.Nifti1Image(odata.get_fdata().astype(np.float32), affine=odata.affine)
|
| 106 |
os.makedirs(os.path.join(opt.o, "original"), exist_ok=True)
|
| 107 |
original_nii_path = os.path.join(opt.o, f"original/{basename}.nii")
|
| 108 |
nib.save(nii, original_nii_path)
|
| 109 |
+
print(f"Input image saved to: {original_nii_path}")
|
| 110 |
|
| 111 |
# 2. Preprocess the image
|
| 112 |
+
print("Preprocessing the input image...")
|
| 113 |
odata, data = preprocessing(input_file.name, opt.o, basename)
|
| 114 |
+
print("Preprocessing completed.")
|
| 115 |
|
| 116 |
# 3. Cropping
|
| 117 |
+
print("Cropping the input image...")
|
| 118 |
cropped, out_filename = cropping(opt.o, basename, odata, data, cnet, device)
|
| 119 |
+
print("Cropping completed.")
|
| 120 |
if only_face_cropping:
|
| 121 |
pass
|
| 122 |
|
| 123 |
else:
|
| 124 |
# 4. Skull stripping
|
| 125 |
+
print("Performing skull stripping...")
|
| 126 |
stripped, shift, out_filename = stripping(opt.o, basename, cropped, odata, data, ssnet, device)
|
| 127 |
+
print("Skull stripping completed.")
|
| 128 |
if only_skull_stripping:
|
| 129 |
pass
|
| 130 |
else:
|
| 131 |
# 5. Parcellation
|
| 132 |
+
print("Starting parcellation...")
|
| 133 |
parcellated = parcellation(stripped, pnet_c, pnet_s, pnet_a, device)
|
| 134 |
+
print("Parcellation completed.")
|
| 135 |
# 6. Separate into hemispheres
|
| 136 |
+
print("Separating hemispheres...")
|
| 137 |
separated = hemisphere(stripped, hnet_c, hnet_a, device)
|
| 138 |
+
print("Hemispheres separated.")
|
| 139 |
# 7. Postprocessing
|
| 140 |
+
print("Postprocessing the parcellated data...")
|
| 141 |
output = postprocessing(parcellated, separated, shift, device)
|
| 142 |
+
print("Postprocessing completed.")
|
| 143 |
# 8. Create CSV with volume information, etc.
|
| 144 |
+
print("Creating CSV with volume information...")
|
| 145 |
df = make_csv(output, opt.o, basename)
|
| 146 |
+
print("CSV created successfully.")
|
| 147 |
# 9. Create and save the parcellation result NIfTI file
|
| 148 |
nii_out = nib.Nifti1Image(output.astype(np.uint16), affine=data.affine)
|
| 149 |
header = odata.header
|
|
|
|
| 158 |
out_filename = os.path.join(out_parcellated_dir, f"{basename}_Type1_Level5.nii")
|
| 159 |
nib.save(nii_out, out_filename)
|
| 160 |
create_parcellated_images(output, opt.o, basename, odata, data)
|
| 161 |
+
print(f"Parcellation result saved to: {out_filename}")
|
| 162 |
|
| 163 |
# Zip the entire output directory into a ZIP file
|
| 164 |
zip_path = os.path.join(os.path.dirname(opt.o), f"{basename}_results.zip")
|
|
|
|
| 175 |
|
| 176 |
# *** Cleanup: Remove the temporary output directory ***
|
| 177 |
# Note: This is performed before returning. It is not possible to execute code after the return statement.
|
| 178 |
+
# shutil.rmtree(opt.o)
|
| 179 |
|
| 180 |
# Return the ZIP file path and the two visualization images
|
| 181 |
return zip_path, Image.open(input_png_path), Image.open(parcellation_png_path)
|