Upload volumes from local directory to Supervisely
Upload NRRD format volume
Source code:
local_path ="src/upload/nrrd/MRHead.nrrd"nrrd_info = api.volume.upload_nrrd_serie_path( dataset.id,"MRHead.nrrd", local_path,)print(f'"{nrrd_info.name}" volume uploaded to Supervisely with ID:{nrrd_info.id}')
Output:
# "NRRD_1.nrrd" volume uploaded to Supervisely with ID:18562981
Upload volume as NumPy array
Source code:
np_volume, meta = sly.volume.read_nrrd_serie_volume_np(local_path)nrrd_info_np = api.volume.upload_np( dataset.id,"MRHead_np.nrrd", np_volume, meta,)print(f"Volume uploaded as NumPy array to Supervisely with ID:{nrrd_info_np.id}")
Output:
# Volume uploaded as NumPy array to Supervisely with ID:18562982
Upload DICOM series from local directory
Inspect you local directory and collect all dicom series.
Upload DICOM series from local directory to Supervisely platform.
Source code:
for serie_id, files in series_infos.items(): item_path = files[0] name =f"{sly.fs.get_file_name(path=item_path)}.nrrd" dicom_info = api.volume.upload_dicom_serie_paths( dataset_id=dataset.id, name=name, paths=files, anonymize=True, )print(f"DICOM volume has been uploaded to Supervisely with ID: {dicom_info.id}")
Set anonymize=True if you want to anonymize DICOM series and hide PatientID and PatientName fields.
Output:
# DICOM volume has been uploaded to Supervisely with ID: 18630608
Upload list of volumes from local directory
Source code:
local_dir_name ="src/upload/nrrd/"all_nrrd_names = os.listdir(local_dir_name)names = [f"1_{name}"for name in all_nrrd_names]paths = [os.path.join(local_dir_name, name)for name in all_nrrd_names]volume_infos = api.volume.upload_nrrd_series_paths(dataset.id, names, paths)print(f"All volumes has been uploaded with IDs: {[x.id for x in volume_infos]}")
Output:
# All volumes has been uploaded with IDs: [18630605, 18630606, 18630607]
Read NRRD file from local directory and get meta and volume (as NumPy array).
Source code:
# read NRRD file from local directorynrrd_path = os.path.join(download_dir_name, "MRHead.nrrd")volume_np, meta = sly.volume.read_nrrd_serie_volume_np(nrrd_path)pprint(meta)
✅ There is a built-in function supervisely.image.write which reads file extension from path and saves image (slice) with the desired format in local directory.
Example:
import supervisely as slysly.image.write("folder/slice.nrrd", image_np)# save as NRRDsly.image.write("folder/slice.jpg", image_np)# save as JPG
Save slice as NRRD
Recommended way to save slice as NRRD file to preserve image quality (pixel depth)
Source code:
# save slice as NRRD filesave_dir ="src/download/"nrrd_slice_path = os.path.join(save_dir, 'slice.nrrd')sly.image.write(nrrd_slice_path, image_np)
Save slice as JPG
Source code:
# save slice as jpgsave_dir ="src/download/"image_slice_path = os.path.join(save_dir, 'slice.jpg')sly.image.write(jpg_slice_path, image_np)
Note:
In case you save slice using nrrd library, it is recommended to use C-order indexing.