File size: 960 Bytes
7a1ec93
a660631
 
 
 
 
 
 
 
 
 
7a1ec93
 
a660631
 
f521e88
 
a660631
 
 
 
 
f521e88
a660631
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import torch
import numpy as np
import PIL.Image
from controlnet_aux.util import HWC3
from transformers import pipeline

from cv_utils import resize_image


class DepthEstimator:
    def __init__(self):
        self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        self.model = pipeline("depth-estimation", device=self.device)

    def __call__(self, image: np.ndarray, **kwargs) -> PIL.Image.Image:
        detect_resolution = kwargs.pop("detect_resolution", 512)
        image_resolution = kwargs.pop("image_resolution", 512)
        image = np.array(image)
        image = HWC3(image)
        image = resize_image(image, resolution=detect_resolution)
        image = PIL.Image.fromarray(image)
        image = self.model(image)
        image = image["depth"]
        image = np.array(image)
        image = HWC3(image)
        image = resize_image(image, resolution=image_resolution)
        return PIL.Image.fromarray(image)