add requirements.txt and auto install opencv-python when needed

This commit is contained in:
Tung Nguyen
2023-09-25 10:59:28 +07:00
parent 5d1d909b53
commit a0bdb7e06c
3 changed files with 27 additions and 6 deletions
+4 -3
View File
@@ -11,7 +11,7 @@ from PIL.PngImagePlugin import PngInfo
import folder_paths
from .model_utils import get_available_models, load_motion_module
from .utils import pil2tensor
from .utils import pil2tensor, ensure_opencv
from .sampler import AnimateDiffSampler, AnimateDiffSlidingWindowOptions
@@ -222,6 +222,7 @@ class LoadVideo:
return frames
def load_video(self, video_path, frame_start: int, frame_limit: int):
ensure_opencv()
import cv2
video = cv2.VideoCapture(video_path)
@@ -249,12 +250,12 @@ class LoadVideo:
if ext.lower() in {".gif", ".webp"}:
frames = self.load_gif(video_path, frame_start, frame_limit)
elif ext.lower() in {".webp", ".mp4", ".mov", ".avi"}:
elif ext.lower() in {".webp", ".mp4", ".mov", ".avi", ".webm"}:
frames = self.load_video(video_path, frame_start, frame_limit)
else:
raise ValueError(f"Unsupported video format: {ext}")
return (torch.cat(frames, dim=0),)
return (torch.cat(frames, dim=0), len(frames))
@classmethod
def IS_CHANGED(s, image, *args, **kwargs):
+22 -3
View File
@@ -1,13 +1,32 @@
import sys
import torch
import numpy as np
import subprocess
from PIL import Image
from .logger import logger
# Tensor to PIL
def tensor2pil(image):
return Image.fromarray(
np.clip(255.0 * image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8)
)
return Image.fromarray(np.clip(255.0 * image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8))
# Convert PIL to Tensor
def pil2tensor(image):
return torch.from_numpy(np.array(image).astype(np.float32) / 255.0).unsqueeze(0)
def ensure_opencv():
if "python_embeded" in sys.executable or "python_embedded" in sys.executable:
pip_install = [sys.executable, "-s", "-m", "pip", "install"]
else:
pip_install = [sys.executable, "-m", "pip", "install"]
try:
import cv2
except Exception as e:
try:
subprocess.check_call(pip_install + ['opencv-python'])
except:
logger.error(f"Failed to install 'opencv-python'. Please, install manually.")
+1
View File
@@ -0,0 +1 @@
opencv-python