|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| from __future__ import annotations
|
|
|
| import os
|
| import sys
|
| import importlib
|
| import threading
|
| from typing import Tuple
|
|
|
| import torch
|
|
|
| _IMPORT_LOCK = threading.Lock()
|
| _RIFE_CLASS = None
|
|
|
|
|
|
|
|
|
| _HARDCODED_CKPT_NAME = "rife47.pth"
|
| _HARDCODED_CLEAR_CACHE_AFTER_N_FRAMES = 10
|
| _HARDCODED_FAST_MODE = True
|
| _HARDCODED_ENSEMBLE = True
|
| _HARDCODED_SCALE_FACTOR = 1.0
|
|
|
|
|
| def _lazy_get_rife_class():
|
| """
|
| Lazily import ComfyUI-Frame-Interpolation's RIFE_VFI class without importing
|
| the whole package at ComfyUI startup.
|
| """
|
| global _RIFE_CLASS
|
| if _RIFE_CLASS is not None:
|
| return _RIFE_CLASS
|
|
|
| with _IMPORT_LOCK:
|
| if _RIFE_CLASS is not None:
|
| return _RIFE_CLASS
|
|
|
|
|
|
|
|
|
|
|
| this_dir = os.path.dirname(os.path.abspath(__file__))
|
| custom_nodes_dir = os.path.abspath(os.path.join(this_dir, "..", ".."))
|
| cfi_dir = os.path.join(custom_nodes_dir, "ComfyUI-Frame-Interpolation")
|
|
|
| if not os.path.isdir(cfi_dir):
|
| raise FileNotFoundError(
|
| f"Could not find ComfyUI-Frame-Interpolation folder at:\n {cfi_dir}\n"
|
| f"Expected it at:\n {os.path.join(custom_nodes_dir, 'ComfyUI-Frame-Interpolation')}"
|
| )
|
|
|
|
|
|
|
|
|
|
|
|
|
| if cfi_dir not in sys.path:
|
| sys.path.insert(0, cfi_dir)
|
|
|
| rife_mod = importlib.import_module("vfi_models.rife")
|
| rife_cls = getattr(rife_mod, "RIFE_VFI", None)
|
| if rife_cls is None:
|
| raise ImportError("vfi_models.rife imported, but RIFE_VFI class was not found.")
|
|
|
| _RIFE_CLASS = rife_cls
|
| return _RIFE_CLASS
|
|
|
|
|
| class SALIA_RIFE_INSERT_BETWEEN:
|
| @classmethod
|
| def INPUT_TYPES(cls):
|
| return {
|
| "required": {
|
| "batch": ("IMAGE",),
|
| "start": ("INT", {"default": 0, "min": 0, "step": 1}),
|
| "end": ("INT", {"default": 1, "min": 0, "step": 1}),
|
|
|
| "multiplier": ("INT", {"default": 1, "min": 1, "step": 1}),
|
| }
|
| }
|
|
|
| RETURN_TYPES = ("IMAGE",)
|
| RETURN_NAMES = ("IMAGE",)
|
| FUNCTION = "insert"
|
| CATEGORY = "salia_online/VFI"
|
|
|
| def insert(self, batch: torch.Tensor, start: int, end: int, multiplier: int) -> Tuple[torch.Tensor]:
|
| if batch is None or not hasattr(batch, "shape"):
|
| raise ValueError("Input 'batch' must be an IMAGE tensor.")
|
|
|
| if batch.shape[0] < 2:
|
| raise ValueError(f"Input batch must have at least 2 frames, got {batch.shape[0]}.")
|
|
|
| start = int(start)
|
| end = int(end)
|
| multiplier = int(multiplier)
|
|
|
| n = int(batch.shape[0])
|
| if not (0 <= start < n) or not (0 <= end < n):
|
| raise ValueError(f"start/end out of range. batch has {n} frames, got start={start}, end={end}.")
|
|
|
| if start == end:
|
| raise ValueError("start and end must be different indices.")
|
|
|
| if start > end:
|
| raise ValueError(f"start must be < end. Got start={start}, end={end}.")
|
|
|
|
|
| frame_start = batch[start:start + 1]
|
| frame_end = batch[end:end + 1]
|
|
|
|
|
| frames = torch.cat([frame_start, frame_end], dim=0)
|
|
|
|
|
|
|
|
|
|
|
| rife_multiplier = multiplier + 1
|
|
|
|
|
| RIFE_VFI = _lazy_get_rife_class()
|
| rife_node = RIFE_VFI()
|
|
|
| (rife_out,) = rife_node.vfi(
|
| ckpt_name=_HARDCODED_CKPT_NAME,
|
| frames=frames,
|
| clear_cache_after_n_frames=_HARDCODED_CLEAR_CACHE_AFTER_N_FRAMES,
|
| multiplier=int(rife_multiplier),
|
| fast_mode=_HARDCODED_FAST_MODE,
|
| ensemble=_HARDCODED_ENSEMBLE,
|
| scale_factor=_HARDCODED_SCALE_FACTOR,
|
| optional_interpolation_states=None,
|
| )
|
|
|
|
|
|
|
| middle = rife_out[1:-1] if rife_out.shape[0] >= 2 else rife_out[0:0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| before = batch[: start + 1]
|
| after = batch[end:]
|
|
|
|
|
| if middle.device != before.device:
|
| middle = middle.to(before.device)
|
|
|
| out = torch.cat([before, middle, after], dim=0)
|
| return (out,)
|
|
|
|
|
| NODE_CLASS_MAPPINGS = {
|
| "SALIA_RIFE_INSERT_BETWEEN": SALIA_RIFE_INSERT_BETWEEN,
|
| }
|
|
|
| NODE_DISPLAY_NAME_MAPPINGS = {
|
| "SALIA_RIFE_INSERT_BETWEEN": "RIFE Insert Between (Lazy, hardcoded rife47)",
|
| } |