Layout-Control / annotator /dsine_hub.py
ysmao's picture
add layout controlnet
4342954
raw
history blame contribute delete
No virus
1.32 kB
import torch
import numpy as np
from PIL import Image
class NormalDetector:
def __init__(self):
self.model_path = "hugoycj/DSINE-hub"
self.dsine = torch.hub.load(self.model_path, "DSINE", trust_repo=True)
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@torch.no_grad()
def __call__(self, image):
self.dsine.model.to(self.device)
self.dsine.model.pixel_coords = self.dsine.model.pixel_coords.to(self.device)
H, W, C = image.shape
normal = self.dsine.infer_pil(image)[0] # Output shape: (H, W, 3)
normal = (normal + 1.0) / 2.0 # Convert values to the range [0, 1]
normal = (normal * 255).cpu().numpy().astype(np.uint8).transpose(1, 2, 0)
normal_img = Image.fromarray(normal).resize((W, H))
self.dsine.model.to("cpu")
self.dsine.model.pixel_coords = self.dsine.model.pixel_coords.to("cpu")
return normal_img
if __name__ == "__main__":
from diffusers.utils import load_image
image = load_image(
"https://qhstaticssl.kujiale.com/image/jpeg/1716177580588/9AAA49344B9CE33512C4EBD0A287495F.jpg"
)
image = np.asarray(image)
normal_detector = NormalDetector()
normal_image = normal_detector(image)
normal_image.save("normal_image.jpg")