Spaces:
Running on Zero
Running on Zero
Download diffsynth/core/data/operators.py from happy0612/GeoNeXt: direct link, hf CLI and curl.
- Browser
- Download file 10.1 kB
-
https://huggingface.co/spaces/happy0612/GeoNeXt/resolve/main/diffsynth/core/data/operators.py
- Command line
-
hf download hf://spaces/happy0612/GeoNeXt/diffsynth/core/data/operators.py
-
curl -L -o operators.py https://huggingface.co/spaces/happy0612/GeoNeXt/resolve/main/diffsynth/core/data/operators.py
10.1 kB
| import torch, torchvision, imageio, os | |
| import imageio.v3 as iio | |
| from PIL import Image | |
| class DataProcessingPipeline: | |
| def __init__(self, operators=None): | |
| self.operators: list[DataProcessingOperator] = [] if operators is None else operators | |
| def __call__(self, data): | |
| for operator in self.operators: | |
| data = operator(data) | |
| return data | |
| def __rshift__(self, pipe): | |
| if isinstance(pipe, DataProcessingOperator): | |
| pipe = DataProcessingPipeline([pipe]) | |
| return DataProcessingPipeline(self.operators + pipe.operators) | |
| class DataProcessingOperator: | |
| def __call__(self, data): | |
| raise NotImplementedError("DataProcessingOperator cannot be called directly.") | |
| def __rshift__(self, pipe): | |
| if isinstance(pipe, DataProcessingOperator): | |
| pipe = DataProcessingPipeline([pipe]) | |
| return DataProcessingPipeline([self]).__rshift__(pipe) | |
| class DataProcessingOperatorRaw(DataProcessingOperator): | |
| def __call__(self, data): | |
| return data | |
| class ToInt(DataProcessingOperator): | |
| def __call__(self, data): | |
| return int(data) | |
| class ToFloat(DataProcessingOperator): | |
| def __call__(self, data): | |
| return float(data) | |
| class ToStr(DataProcessingOperator): | |
| def __init__(self, none_value=""): | |
| self.none_value = none_value | |
| def __call__(self, data): | |
| if data is None: data = self.none_value | |
| return str(data) | |
| class LoadImage(DataProcessingOperator): | |
| def __init__(self, convert_RGB=True): | |
| self.convert_RGB = convert_RGB | |
| def __call__(self, data: str): | |
| image = Image.open(data) | |
| if self.convert_RGB: image = image.convert("RGB") | |
| return image | |
| class ImageCropAndResize(DataProcessingOperator): | |
| def __init__(self, height=None, width=None, max_pixels=None, height_division_factor=1, width_division_factor=1): | |
| self.height = height | |
| self.width = width | |
| self.max_pixels = max_pixels | |
| self.height_division_factor = height_division_factor | |
| self.width_division_factor = width_division_factor | |
| def crop_and_resize(self, image, target_height, target_width): | |
| width, height = image.size | |
| scale = max(target_width / width, target_height / height) | |
| image = torchvision.transforms.functional.resize( | |
| image, | |
| (round(height*scale), round(width*scale)), | |
| interpolation=torchvision.transforms.InterpolationMode.BILINEAR | |
| ) | |
| image = torchvision.transforms.functional.center_crop(image, (target_height, target_width)) | |
| return image | |
| def get_height_width(self, image): | |
| if self.height is None or self.width is None: | |
| width, height = image.size | |
| if width * height > self.max_pixels: | |
| scale = (width * height / self.max_pixels) ** 0.5 | |
| height, width = int(height / scale), int(width / scale) | |
| height = height // self.height_division_factor * self.height_division_factor | |
| width = width // self.width_division_factor * self.width_division_factor | |
| else: | |
| height, width = self.height, self.width | |
| return height, width | |
| def __call__(self, data: Image.Image): | |
| image = self.crop_and_resize(data, *self.get_height_width(data)) | |
| return image | |
| class ToList(DataProcessingOperator): | |
| def __call__(self, data): | |
| return [data] | |
| class LoadVideo(DataProcessingOperator): | |
| def __init__(self, num_frames=81, time_division_factor=4, time_division_remainder=1, frame_processor=lambda x: x): | |
| self.num_frames = num_frames | |
| self.time_division_factor = time_division_factor | |
| self.time_division_remainder = time_division_remainder | |
| # frame_processor is build in the video loader for high efficiency. | |
| self.frame_processor = frame_processor | |
| def get_num_frames(self, reader): | |
| num_frames = self.num_frames | |
| if int(reader.count_frames()) < num_frames: | |
| num_frames = int(reader.count_frames()) | |
| while num_frames > 1 and num_frames % self.time_division_factor != self.time_division_remainder: | |
| num_frames -= 1 | |
| return num_frames | |
| def __call__(self, data: str): | |
| reader = imageio.get_reader(data) | |
| num_frames = self.get_num_frames(reader) | |
| frames = [] | |
| for frame_id in range(num_frames): | |
| frame = reader.get_data(frame_id) | |
| frame = Image.fromarray(frame) | |
| frame = self.frame_processor(frame) | |
| frames.append(frame) | |
| reader.close() | |
| return frames | |
| class SequencialProcess(DataProcessingOperator): | |
| def __init__(self, operator=lambda x: x): | |
| self.operator = operator | |
| def __call__(self, data): | |
| return [self.operator(i) for i in data] | |
| class LoadGIF(DataProcessingOperator): | |
| def __init__(self, num_frames=81, time_division_factor=4, time_division_remainder=1, frame_processor=lambda x: x): | |
| self.num_frames = num_frames | |
| self.time_division_factor = time_division_factor | |
| self.time_division_remainder = time_division_remainder | |
| # frame_processor is build in the video loader for high efficiency. | |
| self.frame_processor = frame_processor | |
| def get_num_frames(self, path): | |
| num_frames = self.num_frames | |
| images = iio.imread(path, mode="RGB") | |
| if len(images) < num_frames: | |
| num_frames = len(images) | |
| while num_frames > 1 and num_frames % self.time_division_factor != self.time_division_remainder: | |
| num_frames -= 1 | |
| return num_frames | |
| def __call__(self, data: str): | |
| num_frames = self.get_num_frames(data) | |
| frames = [] | |
| images = iio.imread(data, mode="RGB") | |
| for img in images: | |
| frame = Image.fromarray(img) | |
| frame = self.frame_processor(frame) | |
| frames.append(frame) | |
| if len(frames) >= num_frames: | |
| break | |
| return frames | |
| class RouteByExtensionName(DataProcessingOperator): | |
| def __init__(self, operator_map): | |
| self.operator_map = operator_map | |
| def __call__(self, data: str): | |
| file_ext_name = data.split(".")[-1].lower() | |
| for ext_names, operator in self.operator_map: | |
| if ext_names is None or file_ext_name in ext_names: | |
| return operator(data) | |
| raise ValueError(f"Unsupported file: {data}") | |
| class RouteByType(DataProcessingOperator): | |
| def __init__(self, operator_map): | |
| self.operator_map = operator_map | |
| def __call__(self, data): | |
| for dtype, operator in self.operator_map: | |
| if dtype is None or isinstance(data, dtype): | |
| return operator(data) | |
| raise ValueError(f"Unsupported data: {data}") | |
| class LoadTorchPickle(DataProcessingOperator): | |
| def __init__(self, map_location="cpu"): | |
| self.map_location = map_location | |
| def __call__(self, data): | |
| return torch.load(data, map_location=self.map_location, weights_only=False) | |
| class ToAbsolutePath(DataProcessingOperator): | |
| def __init__(self, base_path=""): | |
| self.base_path = base_path | |
| def __call__(self, data): | |
| return os.path.join(self.base_path, data) | |
| class LoadAudio(DataProcessingOperator): | |
| def __init__(self, sr=16000): | |
| self.sr = sr | |
| def __call__(self, data: str): | |
| import librosa | |
| input_audio, sample_rate = librosa.load(data, sr=self.sr) | |
| return input_audio | |
| class LoadVideoRange(DataProcessingOperator): | |
| """ | |
| 专门读取视频片段的算子。 | |
| 输入 data 应为字典: {'path': str, 'start_frame': int, 'end_frame': int} | |
| """ | |
| def __init__(self, frame_processor=lambda x: x): | |
| self.frame_processor = frame_processor | |
| def __call__(self, data: dict): | |
| path = data['path'] | |
| start_frame = data['start_frame'] | |
| end_frame = data['end_frame'] | |
| # 打开视频流 | |
| reader = imageio.get_reader(path) | |
| total_frames = reader.count_frames() | |
| frames = [] | |
| # 安全检查,防止越界 | |
| current_frame_idx = start_frame | |
| # 循环读取指定范围 | |
| # 注意:imageio 随机读取(seek)可能较慢,但对于长视频切片是必须的 | |
| # 优化:如果是顺序的大量读取,可以考虑更底层的 decord 库,但这里沿用 imageio | |
| try: | |
| for i in range(start_frame, end_frame): | |
| if i >= total_frames: | |
| break | |
| frame = reader.get_data(i) # 获取第 i 帧 | |
| frame = Image.fromarray(frame) | |
| frame = self.frame_processor(frame) # 调用 Resize/Crop | |
| frames.append(frame) | |
| except Exception as e: | |
| print(f"Error reading frame {current_frame_idx} from {path}: {e}") | |
| finally: | |
| reader.close() | |
| return frames | |
| class ComputeCutMask(DataProcessingOperator): | |
| """ | |
| 计算切镜 Mask 的算子。 | |
| 输入 data 应为字典: {'cuts_global': list, 'start_frame': int, 'num_frames': int} | |
| 输出: 一个形状为 [num_frames] 的 Tensor,切镜处为 1,其余为 0 (或者你需要的任何格式) | |
| """ | |
| def __init__(self, max_len=None): | |
| self.max_len = max_len | |
| def __call__(self, data: dict): | |
| cuts_global = data['cuts_global'] # 全局切镜帧号 [331, 433] | |
| start_frame = data['start_frame'] # 当前Clip开始帧 300 | |
| length = data['num_frames'] # 长度 150 | |
| # 创建全0 mask | |
| mask = torch.zeros(length, dtype=torch.float32) | |
| for cut_frame in cuts_global: | |
| # 转换由于全局帧 -> 局部帧 | |
| local_cut = cut_frame - start_frame | |
| # 判断是否在这个 5s 片段内 | |
| if 0 <= local_cut < length: | |
| mask[local_cut] = 1.0 # 标记切镜位置 | |
| return mask |