CoolFace
Apppublic

thienphuc12339/SignLanguage

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
utils.py56 linesDownload Raw Back to visualization
1import torch2import numpy as np3from imageio import mimsave4from PIL import Image, ImageDraw, ImageFont5 6 7def unnormalize_img(image: np.ndarray, std: tuple, mean: tuple) -> np.ndarray:8    image = (image * std) + mean9    image = (image * 255).astype('uint8')10    return image.clip(0, 255)11 12 13def save_as_gif(14    video_tensor: torch.Tensor,15    save_path: str = 'sample.gif',16    std: tuple = None,17    mean: tuple = None,18):19    frames = []20    for video_frame in video_tensor:21        frame_unnormalized = unnormalize_img(22            image=video_frame.permute(1, 2, 0).numpy(),23            std=std,24            mean=mean,25        )26        frames.append(frame_unnormalized)27    kargs = {'duration': 0.25}28    mimsave(save_path, frames, 'GIF', **kargs)29    return save_path30 31 32def display_gif(gif_path: str) -> Image:33    return Image(filename=gif_path)34 35 36def draw_text_on_image(37    image: np.ndarray,38    text: str,39    position: tuple = (20, 20),40    color: tuple = (0, 0, 255),41    font_size: int = 20,42) -> np.ndarray:43    font = ImageFont.truetype(44        font="fonts/OpenSans-Regular.ttf",45        size=font_size,46    )47    pil_image = Image.fromarray(image)48    draw = ImageDraw.Draw(pil_image)49    draw.text(50        xy=position,51        text=text,52        fill=color,53        font=font,54    )55    return np.array(pil_image)56