Skip to content
FastVideo
Esc
↑↓navigate↵open⌘Jpreview
On this page

data_utils

fastvideo.models.dits.hyworld.data_utils.resize_and_center_crop

resize_and_center_crop(image, target_width, target_height)
Source
def resize_and_center_crop(image, target_width, target_height):
    if target_height == image.shape[0] and target_width == image.shape[1]:
        return image
pil_image = Image.fromarray(image)
original_width, original_height = pil_image.size
scale_factor = max(target_width / original_width, target_height / original_height)
resized_width = int(round(original_width * scale_factor))
resized_height = int(round(original_height * scale_factor))
resized_image = pil_image.resize((resized_width, resized_height), Image.LANCZOS)
left = (resized_width - target_width) / 2
top = (resized_height - target_height) / 2
right = (resized_width + target_width) / 2
bottom = (resized_height + target_height) / 2
cropped_image = resized_image.crop((left, top, right, bottom))
return np.array(cropped_image)</code></pre></details>

fastvideo.models.dits.hyworld.data_utils.get_closest_ratio

get_closest_ratio(height: float, width: float, ratios: list, buckets: list)

Get the closest ratio in the buckets.

Parameters

height: float

video height

width: float

video width

ratios: list

video aspect ratio

buckets: list

buckets generated by generate_crop_size_list

Returns

the closest size in the buckets and the corresponding ratio

Source
def get_closest_ratio(height: float, width: float, ratios: list, buckets: list):
    """
    Get the closest ratio in the buckets.
Args:
    height (float): video height
    width (float): video width
    ratios (list): video aspect ratio
    buckets (list): buckets generated by `generate_crop_size_list`

Returns:
    the closest size in the buckets and the corresponding ratio
&quot;&quot;&quot;
aspect_ratio = float(height) / float(width)

ratios_array = np.array(ratios)
closest_ratio_id = np.abs(ratios_array - aspect_ratio).argmin()
closest_size = buckets&#91;closest_ratio_id&#93;
closest_ratio = ratios_array&#91;closest_ratio_id&#93;

return closest_size, closest_ratio</code></pre></details>

fastvideo.models.dits.hyworld.data_utils.generate_crop_size_list

generate_crop_size_list(base_size = 256, patch_size = 16, max_ratio = 4.0)
Source
def generate_crop_size_list(base_size=256, patch_size=16, max_ratio=4.0):
    num_patches = round((base_size / patch_size)**2)
    assert max_ratio >= 1.0
    crop_size_list = []
    wp, hp = num_patches, 1
    while wp > 0:
        if max(wp, hp) / min(wp, hp) <= max_ratio:
            crop_size_list.append((wp * patch_size, hp * patch_size))
        if (hp + 1) * wp <= num_patches:
            hp += 1
        else:
            wp -= 1
    return crop_size_list

Was this page helpful?