57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
import torch
|
|
import torch.nn.functional as F
|
|
|
|
class Blur:
|
|
def __init__(self):
|
|
pass
|
|
|
|
@classmethod
|
|
def INPUT_TYPES(s):
|
|
return {
|
|
"required": {
|
|
"image": ("IMAGE",),
|
|
"blur_radius": ("INT", {
|
|
"default": 1,
|
|
"min": 1,
|
|
"max": 15,
|
|
"step": 1
|
|
}),
|
|
"sigma": ("FLOAT", {
|
|
"default": 1.0,
|
|
"min": 0.1,
|
|
"max": 10.0,
|
|
"step": 0.1
|
|
}),
|
|
},
|
|
}
|
|
|
|
RETURN_TYPES = ("IMAGE",)
|
|
FUNCTION = "blur"
|
|
|
|
CATEGORY = "postprocessing"
|
|
|
|
def gaussian_kernel(self, kernel_size: int, sigma: float):
|
|
x, y = torch.meshgrid(torch.linspace(-1, 1, kernel_size), torch.linspace(-1, 1, kernel_size), indexing="ij")
|
|
d = torch.sqrt(x * x + y * y)
|
|
g = torch.exp(-(d * d) / (2.0 * sigma * sigma))
|
|
return g / g.sum()
|
|
|
|
def blur(self, image: torch.Tensor, blur_radius: int, sigma: float):
|
|
if blur_radius == 0:
|
|
return (image,)
|
|
|
|
batch_size, height, width, channels = image.shape
|
|
|
|
kernel_size = blur_radius * 2 + 1
|
|
kernel = self.gaussian_kernel(kernel_size, sigma).repeat(channels, 1, 1).unsqueeze(1)
|
|
|
|
image = image.permute(0, 3, 1, 2) # Torch wants (B, C, H, W) we use (B, H, W, C)
|
|
blurred = F.conv2d(image, kernel, padding=kernel_size // 2, groups=channels)
|
|
blurred = blurred.permute(0, 2, 3, 1)
|
|
|
|
return (blurred,)
|
|
|
|
NODE_CLASS_MAPPINGS = {
|
|
"Blur": Blur
|
|
}
|