summaryrefslogtreecommitdiff
path: root/src/mcp_sd15/preprocessors/depth.py
blob: 861b889e8da53d0197dea59c651b9a22362a8813 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""MiDaS depth-map preprocessing."""

from __future__ import annotations

from typing import cast

from PIL import Image


class DepthPreprocessor:
    """Lazily loads the fixed MiDaS annotator from the local cache."""

    def __init__(self) -> None:
        self._detector: object | None = None

    def __call__(self, image: Image.Image) -> Image.Image:
        if self._detector is None:
            from controlnet_aux import MidasDetector

            self._detector = MidasDetector.from_pretrained(
                "lllyasviel/Annotators", local_files_only=True
            )
        result = cast(Image.Image, self._detector(image))  # type: ignore[operator]
        return result.convert("RGB")