Spaces:
Runtime error
Runtime error
File size: 1,770 Bytes
87b62f4 |
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 29 30 31 32 33 34 35 36 37 38 39 40 |
# Pidinet
# https://github.com/hellozhuo/pidinet
import os
import torch
import numpy as np
from einops import rearrange
from annotator.pidinet.model import pidinet
from annotator.util import annotator_ckpts_path, safe_step
class PidiNetDetector:
def __init__(self):
remote_model_path = "https://huggingface.co/lllyasviel/Annotators/resolve/main/table5_pidinet.pth"
modelpath = os.path.join(annotator_ckpts_path, "table5_pidinet.pth")
if not os.path.exists(modelpath):
from basicsr.utils.download_util import load_file_from_url
load_file_from_url(remote_model_path, model_dir=annotator_ckpts_path)
self.netNetwork = pidinet()
# self.netNetwork.load_state_dict({k.replace('module.', ''): v for k, v in torch.load(modelpath)['state_dict'].items()})
self.netNetwork.load_state_dict({k.replace('module.', ''): v for k, v in torch.load(modelpath, map_location=torch.device('cpu'))['state_dict'].items()})
# self.netNetwork = self.netNetwork.cuda()
self.netNetwork = self.netNetwork.cpu()
self.netNetwork.eval()
def __call__(self, input_image, safe=False):
assert input_image.ndim == 3
input_image = input_image[:, :, ::-1].copy()
with torch.no_grad():
# image_pidi = torch.from_numpy(input_image).float().cuda()
image_pidi = torch.from_numpy(input_image).float().cpu()
image_pidi = image_pidi / 255.0
image_pidi = rearrange(image_pidi, 'h w c -> 1 c h w')
edge = self.netNetwork(image_pidi)[-1]
edge = edge.cpu().numpy()
if safe:
edge = safe_step(edge)
edge = (edge * 255.0).clip(0, 255).astype(np.uint8)
return edge[0][0]
|