diff --git a/setup.py b/setup.py index 7c15d650c78d..119e7971ea65 100644 --- a/setup.py +++ b/setup.py @@ -142,6 +142,7 @@ "urllib3<=2.0.0", "black", "phonemizer", + "opencv-python", ] # this is a lookup table with items like: diff --git a/src/diffusers/__init__.py b/src/diffusers/__init__.py index 3c7e8654d223..a0245e2fe3ee 100644 --- a/src/diffusers/__init__.py +++ b/src/diffusers/__init__.py @@ -14,6 +14,7 @@ is_librosa_available, is_note_seq_available, is_onnx_available, + is_opencv_available, is_optimum_quanto_available, is_scipy_available, is_sentencepiece_available, @@ -352,7 +353,6 @@ "CogView3PlusPipeline", "CogView4ControlPipeline", "CogView4Pipeline", - "ConsisIDPipeline", "CycleDiffusionPipeline", "EasyAnimateControlPipeline", "EasyAnimateInpaintPipeline", @@ -518,6 +518,19 @@ ] ) +try: + if not (is_torch_available() and is_transformers_available() and is_opencv_available()): + raise OptionalDependencyNotAvailable() +except OptionalDependencyNotAvailable: + from .utils import dummy_torch_and_transformers_and_opencv_objects # noqa F403 + + _import_structure["utils.dummy_torch_and_transformers_and_opencv_objects"] = [ + name for name in dir(dummy_torch_and_transformers_and_opencv_objects) if not name.startswith("_") + ] + +else: + _import_structure["pipelines"].extend(["ConsisIDPipeline"]) + try: if not (is_torch_available() and is_transformers_available() and is_k_diffusion_available()): raise OptionalDependencyNotAvailable() @@ -909,7 +922,6 @@ CogView3PlusPipeline, CogView4ControlPipeline, CogView4Pipeline, - ConsisIDPipeline, CycleDiffusionPipeline, EasyAnimateControlPipeline, EasyAnimateInpaintPipeline, @@ -1088,6 +1100,15 @@ from .utils.dummy_torch_and_transformers_and_sentencepiece_objects import * # noqa F403 else: from .pipelines import KolorsImg2ImgPipeline, KolorsPAGPipeline, KolorsPipeline + + try: + if not (is_torch_available() and is_transformers_available() and is_opencv_available()): + raise OptionalDependencyNotAvailable() + except OptionalDependencyNotAvailable: + from .utils.dummy_torch_and_transformers_and_opencv_objects import * # noqa F403 + else: + from .pipelines import ConsisIDPipeline + try: if not (is_torch_available() and is_transformers_available() and is_onnx_available()): raise OptionalDependencyNotAvailable() diff --git a/src/diffusers/dependency_versions_table.py b/src/diffusers/dependency_versions_table.py index 520815d122de..4793972c1cd1 100644 --- a/src/diffusers/dependency_versions_table.py +++ b/src/diffusers/dependency_versions_table.py @@ -49,4 +49,5 @@ "urllib3": "urllib3<=2.0.0", "black": "black", "phonemizer": "phonemizer", + "opencv-python": "opencv-python", } diff --git a/src/diffusers/pipelines/__init__.py b/src/diffusers/pipelines/__init__.py index b901d42d9cf7..a2f618857ac1 100644 --- a/src/diffusers/pipelines/__init__.py +++ b/src/diffusers/pipelines/__init__.py @@ -10,6 +10,7 @@ is_librosa_available, is_note_seq_available, is_onnx_available, + is_opencv_available, is_sentencepiece_available, is_torch_available, is_torch_npu_available, @@ -155,7 +156,6 @@ ] _import_structure["cogview3"] = ["CogView3PlusPipeline"] _import_structure["cogview4"] = ["CogView4Pipeline", "CogView4ControlPipeline"] - _import_structure["consisid"] = ["ConsisIDPipeline"] _import_structure["controlnet"].extend( [ "BlipDiffusionControlNetPipeline", @@ -414,6 +414,18 @@ "KolorsImg2ImgPipeline", ] +try: + if not (is_torch_available() and is_transformers_available() and is_opencv_available()): + raise OptionalDependencyNotAvailable() +except OptionalDependencyNotAvailable: + from ..utils import ( + dummy_torch_and_transformers_and_opencv_objects, + ) + + _dummy_objects.update(get_objects_from_module(dummy_torch_and_transformers_and_opencv_objects)) +else: + _import_structure["consisid"] = ["ConsisIDPipeline"] + try: if not is_flax_available(): raise OptionalDependencyNotAvailable() @@ -512,7 +524,6 @@ ) from .cogview3 import CogView3PlusPipeline from .cogview4 import CogView4ControlPipeline, CogView4Pipeline - from .consisid import ConsisIDPipeline from .controlnet import ( BlipDiffusionControlNetPipeline, StableDiffusionControlNetImg2ImgPipeline, @@ -761,6 +772,14 @@ KolorsPipeline, ) + try: + if not (is_torch_available() and is_transformers_available() and is_opencv_available()): + raise OptionalDependencyNotAvailable() + except OptionalDependencyNotAvailable: + from ..utils.dummy_torch_and_transformers_and_opencv_objects import * + else: + from .consisid import ConsisIDPipeline + try: if not is_flax_available(): raise OptionalDependencyNotAvailable() diff --git a/src/diffusers/pipelines/consisid/__init__.py b/src/diffusers/pipelines/consisid/__init__.py index 5052e146f1df..7b9ba330fbd1 100644 --- a/src/diffusers/pipelines/consisid/__init__.py +++ b/src/diffusers/pipelines/consisid/__init__.py @@ -5,6 +5,7 @@ OptionalDependencyNotAvailable, _LazyModule, get_objects_from_module, + is_opencv_available, is_torch_available, is_transformers_available, ) @@ -15,12 +16,12 @@ try: - if not (is_transformers_available() and is_torch_available()): + if not (is_transformers_available() and is_torch_available() and is_opencv_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: - from ...utils import dummy_torch_and_transformers_objects # noqa F403 + from ...utils import dummy_torch_and_transformers_and_opencv_objects # noqa F403 - _dummy_objects.update(get_objects_from_module(dummy_torch_and_transformers_objects)) + _dummy_objects.update(get_objects_from_module(dummy_torch_and_transformers_and_opencv_objects)) else: _import_structure["pipeline_consisid"] = ["ConsisIDPipeline"] diff --git a/src/diffusers/pipelines/consisid/pipeline_consisid.py b/src/diffusers/pipelines/consisid/pipeline_consisid.py index 1a99c2a0e9ee..8abe24e80786 100644 --- a/src/diffusers/pipelines/consisid/pipeline_consisid.py +++ b/src/diffusers/pipelines/consisid/pipeline_consisid.py @@ -16,7 +16,6 @@ import math from typing import Any, Callable, Dict, List, Optional, Tuple, Union -import cv2 import numpy as np import PIL import torch @@ -29,12 +28,16 @@ from ...models.embeddings import get_3d_rotary_pos_embed from ...pipelines.pipeline_utils import DiffusionPipeline from ...schedulers import CogVideoXDPMScheduler -from ...utils import logging, replace_example_docstring +from ...utils import is_opencv_available, logging, replace_example_docstring from ...utils.torch_utils import randn_tensor from ...video_processor import VideoProcessor from .pipeline_output import ConsisIDPipelineOutput +if is_opencv_available(): + import cv2 + + logger = logging.get_logger(__name__) # pylint: disable=invalid-name diff --git a/src/diffusers/utils/__init__.py b/src/diffusers/utils/__init__.py index 438faa23e595..ed89955ba5a5 100644 --- a/src/diffusers/utils/__init__.py +++ b/src/diffusers/utils/__init__.py @@ -79,6 +79,7 @@ is_matplotlib_available, is_note_seq_available, is_onnx_available, + is_opencv_available, is_optimum_quanto_available, is_optimum_quanto_version, is_peft_available, diff --git a/src/diffusers/utils/dummy_torch_and_transformers_and_opencv_objects.py b/src/diffusers/utils/dummy_torch_and_transformers_and_opencv_objects.py new file mode 100644 index 000000000000..c0c4d9df5eee --- /dev/null +++ b/src/diffusers/utils/dummy_torch_and_transformers_and_opencv_objects.py @@ -0,0 +1,17 @@ +# This file is autogenerated by the command `make fix-copies`, do not edit. +from ..utils import DummyObject, requires_backends + + +class ConsisIDPipeline(metaclass=DummyObject): + _backends = ["torch", "transformers", "opencv"] + + def __init__(self, *args, **kwargs): + requires_backends(self, ["torch", "transformers", "opencv"]) + + @classmethod + def from_config(cls, *args, **kwargs): + requires_backends(cls, ["torch", "transformers", "opencv"]) + + @classmethod + def from_pretrained(cls, *args, **kwargs): + requires_backends(cls, ["torch", "transformers", "opencv"]) diff --git a/src/diffusers/utils/dummy_torch_and_transformers_objects.py b/src/diffusers/utils/dummy_torch_and_transformers_objects.py index b28fba948149..2136131126e9 100644 --- a/src/diffusers/utils/dummy_torch_and_transformers_objects.py +++ b/src/diffusers/utils/dummy_torch_and_transformers_objects.py @@ -392,21 +392,6 @@ def from_pretrained(cls, *args, **kwargs): requires_backends(cls, ["torch", "transformers"]) -class ConsisIDPipeline(metaclass=DummyObject): - _backends = ["torch", "transformers"] - - def __init__(self, *args, **kwargs): - requires_backends(self, ["torch", "transformers"]) - - @classmethod - def from_config(cls, *args, **kwargs): - requires_backends(cls, ["torch", "transformers"]) - - @classmethod - def from_pretrained(cls, *args, **kwargs): - requires_backends(cls, ["torch", "transformers"]) - - class CycleDiffusionPipeline(metaclass=DummyObject): _backends = ["torch", "transformers"] diff --git a/tests/others/test_dependencies.py b/tests/others/test_dependencies.py index c0839ef0236b..30c16a217c00 100644 --- a/tests/others/test_dependencies.py +++ b/tests/others/test_dependencies.py @@ -37,6 +37,8 @@ def test_backend_registration(self): backend = "k-diffusion" elif backend == "invisible_watermark": backend = "invisible-watermark" + elif backend == "opencv": + backend = "opencv-python" assert backend in deps, f"{backend} is not in the deps table!" def test_pipeline_imports(self):