Skip to content
Snippets Groups Projects
Commit dee3f6ab authored by Amir MOHAMMADI's avatar Amir MOHAMMADI
Browse files

Improve bbox_cropper in load_utils.py

parent 1a775bdb
Branches
Tags
1 merge request!103Improve bbox_cropper in load_utils.py
Pipeline #36802 passed
...@@ -45,14 +45,20 @@ def number_of_frames(path): ...@@ -45,14 +45,20 @@ def number_of_frames(path):
def bbx_cropper(frame, annotations): def bbx_cropper(frame, annotations):
bbx = bounding_box_from_annotation(**annotations) for source in ("direct", "eyes", None):
return frame[..., bbx.top:bbx.bottom, bbx.left:bbx.right] try:
bbx = bounding_box_from_annotation(source=source, **annotations)
break
except Exception:
if source is None:
raise
return frame[..., bbx.top : bbx.bottom, bbx.left : bbx.right]
def min_face_size_normalizer(annotations, max_age=15, **kwargs): def min_face_size_normalizer(annotations, max_age=15, **kwargs):
return normalize_annotations(annotations, return normalize_annotations(
partial(min_face_size_validator, **kwargs), annotations, partial(min_face_size_validator, **kwargs), max_age=max_age
max_age=max_age) )
def yield_faces(padfile, cropper, normalizer=None): def yield_faces(padfile, cropper, normalizer=None):
...@@ -120,7 +126,7 @@ def scale_face(face, face_height, face_width=None): ...@@ -120,7 +126,7 @@ def scale_face(face, face_height, face_width=None):
face_width = face_height if face_width is None else face_width face_width = face_height if face_width is None else face_width
shape = list(face.shape) shape = list(face.shape)
shape[-2:] = (face_height, face_width) shape[-2:] = (face_height, face_width)
scaled_face = numpy.empty(shape, dtype='float64') scaled_face = numpy.empty(shape, dtype="float64")
scale(face, scaled_face) scale(face, scaled_face)
return scaled_face return scaled_face
...@@ -150,12 +156,12 @@ def blocks(data, block_size, block_overlap=(0, 0)): ...@@ -150,12 +156,12 @@ def blocks(data, block_size, block_overlap=(0, 0)):
data = numpy.asarray(data) data = numpy.asarray(data)
# if a gray scale image: # if a gray scale image:
if data.ndim == 2: if data.ndim == 2:
output = block(data, block_size, block_overlap, output = block(data, block_size, block_overlap, flat=True)
flat=True)
# if a color image: # if a color image:
elif data.ndim == 3: elif data.ndim == 3:
out_shape = list(data.shape[0:1]) + list(block_output_shape( out_shape = list(data.shape[0:1]) + list(
data[0], block_size, block_overlap, flat=True)) block_output_shape(data[0], block_size, block_overlap, flat=True)
)
output = numpy.empty(out_shape, dtype=data.dtype) output = numpy.empty(out_shape, dtype=data.dtype)
for i, img2d in enumerate(data): for i, img2d in enumerate(data):
...@@ -163,8 +169,7 @@ def blocks(data, block_size, block_overlap=(0, 0)): ...@@ -163,8 +169,7 @@ def blocks(data, block_size, block_overlap=(0, 0)):
output = numpy.moveaxis(output, 0, 1) output = numpy.moveaxis(output, 0, 1)
# if a color video: # if a color video:
elif data.ndim == 4: elif data.ndim == 4:
output = [blocks(img3d, block_size, block_overlap) output = [blocks(img3d, block_size, block_overlap) for img3d in data]
for img3d in data]
output = numpy.concatenate(output, axis=0) output = numpy.concatenate(output, axis=0)
else: else:
raise ValueError("Unknown data dimension {}".format(data.ndim)) raise ValueError("Unknown data dimension {}".format(data.ndim))
...@@ -206,7 +211,7 @@ def blocks_generator(data, block_size, block_overlap=(0, 0)): ...@@ -206,7 +211,7 @@ def blocks_generator(data, block_size, block_overlap=(0, 0)):
raise ValueError("Unknown data dimension {}".format(data.ndim)) raise ValueError("Unknown data dimension {}".format(data.ndim))
def color_augmentation(image, channels=('rgb',)): def color_augmentation(image, channels=("rgb",)):
"""Converts an RGB image to different color channels. """Converts an RGB image to different color channels.
Parameters Parameters
...@@ -225,13 +230,13 @@ def color_augmentation(image, channels=('rgb',)): ...@@ -225,13 +230,13 @@ def color_augmentation(image, channels=('rgb',)):
""" """
final_image = [] final_image = []
if 'rgb' in channels: if "rgb" in channels:
final_image.append(image) final_image.append(image)
if 'yuv' in channels: if "yuv" in channels:
final_image.append(rgb_to_yuv(image)) final_image.append(rgb_to_yuv(image))
if 'hsv' in channels: if "hsv" in channels:
final_image.append(rgb_to_hsv(image)) final_image.append(rgb_to_hsv(image))
return numpy.concatenate(final_image, axis=0) return numpy.concatenate(final_image, axis=0)
...@@ -251,26 +256,33 @@ def random_patches(image, block_size, n_random_patches=1): ...@@ -251,26 +256,33 @@ def random_patches(image, block_size, n_random_patches=1):
hl = numpy.random.randint(0, h - bh, size=n_random_patches) hl = numpy.random.randint(0, h - bh, size=n_random_patches)
wl = numpy.random.randint(0, w - bw, size=n_random_patches) wl = numpy.random.randint(0, w - bw, size=n_random_patches)
for ch, cw in zip(hl, wl): for ch, cw in zip(hl, wl):
yield image[..., ch:ch + bh, cw:cw + bw] yield image[..., ch : ch + bh, cw : cw + bw]
def extract_patches(image, block_size, block_overlap=(0, 0), def extract_patches(image, block_size, block_overlap=(0, 0), n_random_patches=None):
n_random_patches=None):
"""Yields either all patches from an image or N random patches.""" """Yields either all patches from an image or N random patches."""
if n_random_patches is None: if n_random_patches is None:
return blocks_generator(image, block_size, block_overlap) return blocks_generator(image, block_size, block_overlap)
else: else:
return random_patches( return random_patches(image, block_size, n_random_patches=n_random_patches)
image, block_size, n_random_patches=n_random_patches)
def the_giant_video_loader(
def the_giant_video_loader(paddb, padfile, paddb,
region='whole', scaling_factor=None, cropper=None, padfile,
normalizer=None, patches=False, region="whole",
block_size=(96, 96), block_overlap=(0, 0), scaling_factor=None,
random_patches_per_frame=None, augment=None, cropper=None,
multiple_bonafide_patches=1, keep_pa_samples=None, normalizer=None,
keep_bf_samples=None): patches=False,
block_size=(96, 96),
block_overlap=(0, 0),
random_patches_per_frame=None,
augment=None,
multiple_bonafide_patches=1,
keep_pa_samples=None,
keep_bf_samples=None,
):
"""Loads a video pad file frame by frame and optionally applies """Loads a video pad file frame by frame and optionally applies
transformations. transformations.
...@@ -316,41 +328,40 @@ def the_giant_video_loader(paddb, padfile, ...@@ -316,41 +328,40 @@ def the_giant_video_loader(paddb, padfile,
ValueError ValueError
If region is not whole or crop. If region is not whole or crop.
""" """
if region == 'whole': if region == "whole":
generator = padfile.frames generator = padfile.frames
elif region == 'crop': elif region == "crop":
generator = yield_faces( generator = yield_faces(padfile, cropper=cropper, normalizer=normalizer)
padfile, cropper=cropper, normalizer=normalizer)
else: else:
raise ValueError("Invalid region value: `{}'".format(region)) raise ValueError("Invalid region value: `{}'".format(region))
if scaling_factor is not None: if scaling_factor is not None:
generator = (scale(frame, scaling_factor) generator = (scale(frame, scaling_factor) for frame in generator)
for frame in generator)
if patches: if patches:
if random_patches_per_frame is None: if random_patches_per_frame is None:
generator = ( generator = (
patch for frame in generator patch
for patch in blocks_generator( for frame in generator
frame, block_size, block_overlap)) for patch in blocks_generator(frame, block_size, block_overlap)
)
else: else:
if padfile.attack_type is None: if padfile.attack_type is None:
random_patches_per_frame *= multiple_bonafide_patches random_patches_per_frame *= multiple_bonafide_patches
generator = ( generator = (
patch for frame in generator patch
for frame in generator
for patch in random_sample( for patch in random_sample(
blocks(frame, block_size, block_overlap), blocks(frame, block_size, block_overlap), random_patches_per_frame
random_patches_per_frame)) )
)
if augment is not None: if augment is not None:
generator = (augment(frame) for frame in generator) generator = (augment(frame) for frame in generator)
if keep_pa_samples is not None and padfile.attack_type is not None: if keep_pa_samples is not None and padfile.attack_type is not None:
generator = (frame for frame in generator generator = (frame for frame in generator if random.random() < keep_pa_samples)
if random.random() < keep_pa_samples)
if keep_bf_samples is not None and padfile.attack_type is None: if keep_bf_samples is not None and padfile.attack_type is None:
generator = (frame for frame in generator generator = (frame for frame in generator if random.random() < keep_bf_samples)
if random.random() < keep_bf_samples)
return generator return generator
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment