Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datasets #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions datasets/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from .dataset import Dataset, Dataloader, Cephalogram

from .aariz import AarizDataset
from .isbi import ISBIDataset
from .pku import PKUDataset

from .paths import Paths
16 changes: 16 additions & 0 deletions datasets/paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os

class Paths(object):
root = "path\to\datasets\folder"

@classmethod
def dataset_root_path(cls, name: str=None):

if name == "isbi":
return os.path.join(cls.root, "ISBI")
elif name == "pku":
return os.path.join(cls.root, "PKU")
elif name == "aariz":
return os.path.join(cls.root, "Aariz")
else:
raise ValueError("\'{}\' dataset doesn't exists in your paths.py file".format(name))
82 changes: 82 additions & 0 deletions datasets/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import numpy as np
import cv2

def calculate_new_dimensions(height, width, max_size: int=512):
aspect_ratio = width / height
if height > width:
height = max_size
width = int(height * aspect_ratio)
else:
width = max_size
height = int(width / aspect_ratio)

return height, width

def resize(
image: np.ndarray,
landmarks: np.ndarray=None,
dimensions: tuple=(int, int)
):
image_height, image_width = image.shape[0:2]
new_height, new_width = dimensions
ratio_height, ratio_width = (image_height / new_height), (image_width / new_width)

image = cv2.resize(
image,
(new_width, new_height),
interpolation=cv2.INTER_AREA
)

if landmarks is not None:
landmarks = np.stack([
landmarks[:, 0] / ratio_width,
landmarks[:, 1] / ratio_height
], axis=-1)

return image, landmarks

return image

def normalize_landmarks(
landmarks: np.ndarray,
height: int,
width: int,
num_landmarks: int=19
):
original_shape = landmarks.shape
landmarks = np.reshape(landmarks, (-1, num_landmarks, 2))

landmarks = np.stack([
landmarks[:, :, 0] / width,
landmarks[:, :, 1] / height
], axis=-1, dtype=np.float32)

if len(original_shape) == 2:
landmarks = np.reshape(landmarks, original_shape)

return landmarks

def denormalize_landmarks(
landmarks: np.ndarray,
height: int,
width: int,
num_landmarks: int=19
):
original_shape = landmarks.shape
landmarks = np.reshape(landmarks, (-1, num_landmarks, 2))

landmarks = np.stack([
landmarks[:, :, 0] * width,
landmarks[:, :, 1] * height
], axis=-1, dtype=np.float32)

if len(original_shape) == 2:
landmarks = np.reshape(landmarks, original_shape)

return landmarks.round()

def rescale(image, scale: float, offset: int = 0, dtype: str="float32"):
scale = np.array(scale, dtype)
offset = np.array(offset, dtype)
image = image * scale + offset
return image