Utils Module#
Utils module.
- landmarker.utils.all_annotations_to_landmarks(paths, class_names)[source]#
Convert all annotations of LabelMe to landmarks.
- Parameters:
paths (list[str]) – list of paths to the annotations
class_names (list[str]) – class names
- Returns:
landmarks
- Return type:
torch.Tensor
- landmarker.utils.all_annotations_to_landmarks_numpy(paths, class_names)[source]#
Convert all annotations of LabelMe to landmarks (Numpy version).
- Parameters:
paths (list[str]) – list of paths to the annotations
class_names (list[str]) – class names
- Returns:
landmarks
- Return type:
np.ndarray
- landmarker.utils.annotation_to_landmark(json_obj, class_names)[source]#
Convert the annotation of LabelMe to landmarks.
- Parameters:
json_obj (dict) – annotation in json format
class_names (list[str]) – class names
- Returns:
landmarks
- Return type:
torch.Tensor
- landmarker.utils.annotation_to_landmark_numpy(json_obj, class_names)[source]#
Convert the annotation of LabelMe to landmarks (Numpy version).
- Parameters:
json_obj (dict) – annotation in json format
class_names (list[str]) – class names
- Returns:
landmarks
- Return type:
np.ndarray
- landmarker.utils.covert_video_to_frames(video_path, frames_path, zero_fill=6)[source]#
Convert a video to frames.
- Parameters:
video_path (str) – path to the video
frames_path (str) – path to the folder where the frames are saved
zero_fill (int, optional) – number of digits in the frame name. Defaults to 6.
- Returns:
None
- Return type:
None
- landmarker.utils.extract_roi(imgs, roi_middle, landmarks, size=256, spatial_dims=2, ensure_dim=True)[source]#
Extract a region of interest around the landmarks in the image.
- Parameters:
img (torch.Tensor) – Image to extract the region of interest from.
roi_middle (torch.Tensor) – Middle of the region of interest.
landmark (torch.Tensor) – Landmark to transform to the region of interest.
size (int | tuple[int, int]) – Size of the region of interest.
spatial_dims (int) – Number of spatial dimensions of the image.
ensure_dim (bool) – Ensure the output has the same number of dimensions as the input.
imgs (Tensor) –
landmarks (Tensor) –
- Returns:
Region of interest around the landmarks. torch.Tensor: Landmarks in the region of interest. torch.Tensor: Upper left corner of the region of interest.
- Return type:
torch.Tensor
- landmarker.utils.get_angle(p1, p2, p3, radial=True)[source]#
Calculate the angle between three points. The angle is calculated in degrees or radians. The angle is calculated in the direction of p2 -> p3. If the angle is radial, the angle is between 0 and 2pi. If the angle is not radial, the angle is between 0 and 360.
- Parameters:
p1 (torch.Tensor) – first point
p2 (torch.Tensor) – second point
p3 (torch.Tensor) – third point
radial (bool, optional) – whether the angle is radial. Defaults to True.
- Returns:
angle
- Return type:
torch.Tensor
- landmarker.utils.get_angle_numpy(p1, p2, p3, radial=True)[source]#
Calculate the angle between three points (Numpy version). See documentation of get_angle.
- Parameters:
p1 (np.ndarray) – first point
p2 (np.ndarray) – second point
p3 (np.ndarray) – third point
radial (bool, optional) – whether the angle is radial. Defaults to True.
- Returns:
angle
- Return type:
np.ndarray
- landmarker.utils.get_paths(folder_path, extension)[source]#
Retrieve all paths with a defined extension files recursively from the folder path.
- Parameters:
folder_path (str) – path to the folder
extension (str) – file extension
- Returns:
list of paths
- Return type:
list[str]
- landmarker.utils.normalize(img)[source]#
Normalize image to 0-1 range
- Parameters:
img (np.ndarray) – Image to normalize
- Returns:
Normalized image
- Return type:
np.ndarray
- landmarker.utils.pixel_to_unit(landmarks, pixel_spacing=None, dim=None, dim_orig=None, padding=None)[source]#
Convert the landmarks from pixel to unit.
- Parameters:
landmarks (torch.Tensor) – landmarks
pixel_spacing (Optional[torch.Tensor], optional) – pixel spacing. Defaults to None.
dim (Optional[tuple[int, ...] | torch.Tensor], optional) – image size. Defaults to None.
dim_orig (Optional[torch.Tensor], optional) – original image size. Defaults to None.
padding (Optional[torch.Tensor], optional) – padding. Defaults to None.
- Returns:
landmarks in units
- Return type:
torch.Tensor
- landmarker.utils.pixel_to_unit_numpy(landmarks, pixel_spacing=None, dim=None, dim_orig=None, padding=None)[source]#
Convert the landmarks from pixel to unit (Numpy version).
- Parameters:
landmarks (np.ndarray) – landmarks
pixel_spacing (Optional[np.ndarray], optional) – pixel spacing. Defaults to None.
dim (Optional[tuple[int, ...] | np.ndarray], optional) – image size. Defaults to None.
dim_orig (Optional[np.ndarray], optional) – original image size. Defaults to None.
padding (Optional[np.ndarray], optional) – padding. Defaults to None.
- Returns:
landmarks in units
- Return type:
np.ndarray
- landmarker.utils.preprocess_all(folder, output_folder, fun_name)[source]#
Apply a preprocessing function to all images in a folder.
- Parameters:
folder (str) – Folder containing images to preprocess
output_folder (str) – Folder to store the preprocessed images
fun_name (str) – Name of the function to apply. (Only normalize supported for now)
- Return type:
None