CoolFace
Apppublic

multimodalart/EchoMimic-zero

sourceHugging Faceupdated 2y agoView on Hugging Face
8likes
img_utils.py84 linesDownload Raw Back to utils
1from PIL import Image2import cv23import numpy as np4from imageio_ffmpeg import get_ffmpeg_exe5import pathlib6import os7from IPython import embed8 9def pil_to_cv2(pil):10    return cv2.cvtColor(np.array(pil).astype(np.uint8), cv2.COLOR_RGB2BGR)11 12def cv2_to_pil(cv2_img):13    return Image.fromarray(cv2.cvtColor(cv2_img, cv2.COLOR_BGR2RGB).astype(np.uint8))14 15def center_crop_cv2(cv2_pic):16    h, w = cv2_pic.shape[0], cv2_pic.shape[1]17    if h > w:18        return cv2_pic[(h - w) // 2 : (h - w) // 2 + w, :]19    else:20        return cv2_pic[:, (w - h) // 2 : (w - h) // 2 + h]21 22def pils_from_video(video_path):23    cap = cv2.VideoCapture(video_path)24    pils = []25    while True:26        ret, frame = cap.read()27        if not ret:28            break29 30        frame = cv2.resize(center_crop_cv2(frame), (512, 512))31        pils.append(cv2_to_pil(frame))32 33    return pils34 35def save_videos_from_pils(pils, path, fps=24):36    width, height = pils[0].size37    print(width, height)38    fourcc = cv2.VideoWriter_fourcc(*'mp4v')    39 40    pathlib.Path(path).parent.mkdir(exist_ok=True, parents=True)41 42    output_name = pathlib.Path(path).stem43    temp_output_path = path.replace(output_name, output_name + '-temp')44    videowrite = cv2.VideoWriter(temp_output_path, fourcc, fps, (height, width))45    for pil in pils:46        frame = pil_to_cv2(pil)47        print(frame.shape, frame.min(), frame.max())48        videowrite.write(frame)49 50    videowrite.release()51    """52    embed()53 54    cmd = (f'{get_ffmpeg_exe()} -i "{temp_output_path}"'55           f'-map 0:v -map 1:a -c:v h264 -shortest -y "{path}" -loglevel quiet')56    os.system(cmd)57    os.remove(temp_output_path)58    """59def save_video_from_cv2_list(pic_cv2_list, output_path, fps=30.0):60    pathlib.Path(output_path).parent.mkdir(exist_ok=True, parents=True)61 62    num_frames = len(pic_cv2_list)63    height, width = pic_cv2_list[0].shape[:2]64    65    #video_tensor = video_tensor[0, ...]66    #_, num_frames, height, width = video_tensor.shape67 68    output_name = pathlib.Path(output_path).stem69    temp_output_path = output_path.replace(output_name, output_name)70    video_writer = cv2.VideoWriter(temp_output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))71 72    for i in range(num_frames):73        #frame_tensor = video_tensor[:, i, ...]  # [c, h, w]74        #frame_tensor = frame_tensor.permute(1, 2, 0)  # [h, w, c]75 76        #frame_image = (frame_tensor * 255).numpy().astype(np.uint8)77        #frame_image = cv2.cvtColor(frame_image, cv2.COLOR_RGB2BGR)78        frame_image = pic_cv2_list[i].astype(np.uint8)79        video_writer.write(frame_image)80 81    video_writer.release()82 83 84#ffmpeg -i input_file -c:v libx264 -crf 20 -c:a aac -strict experimental -b:a 192k output_file