Commit MaskBoxDetect node, which can automatically detect the position through the mask and output it to the composite node. Commit X,Y to Percent node to convert absolute coordinates to percent coordinates. Commit GaussianBlur node. Commit GetImageSize node.
65 lines
2.5 KiB
Python
65 lines
2.5 KiB
Python
from .imagefunc import *
|
|
|
|
class MaskBoxDetect:
|
|
|
|
def __init__(self):
|
|
pass
|
|
|
|
@classmethod
|
|
def INPUT_TYPES(self):
|
|
detect_mode = ['min_bounding_rect', 'max_inscribed_rect']
|
|
return {
|
|
"required": {
|
|
"mask": ("MASK", ),
|
|
"detect": (detect_mode,), # 探测类型:最小外接矩形/最大内接矩形
|
|
"x_adjust": ("INT", {"default": 0, "min": -9999, "max": 9999, "step": 1}), # x轴修正
|
|
"y_adjust": ("INT", {"default": 0, "min": -9999, "max": 9999, "step": 1}), # y轴修正
|
|
"scale_adjust": ("FLOAT", {"default": 1.0, "min": 0.01, "max": 100, "step": 0.01}), # 比例修正
|
|
},
|
|
"optional": {
|
|
}
|
|
}
|
|
|
|
RETURN_TYPES = ("IMAGE", "FLOAT", "FLOAT", "INT", "INT", "INT", "INT",)
|
|
RETURN_NAMES = ("box_preview", "x_percent", "y_percent", "width", "height", "x", "y",)
|
|
FUNCTION = 'mask_box_detect'
|
|
CATEGORY = '😺dzNodes/LayerMask'
|
|
OUTPUT_NODE = True
|
|
|
|
def mask_box_detect(self,mask, detect, x_adjust, y_adjust, scale_adjust):
|
|
|
|
_mask = mask2image(mask).convert('RGB')
|
|
|
|
_mask = gaussian_blur(_mask, 20).convert('L')
|
|
x = 0
|
|
y = 0
|
|
width = 0
|
|
height = 0
|
|
|
|
if detect == "min_bounding_rect":
|
|
(x, y, width, height) = min_bounding_rect(_mask)
|
|
if detect == "max_inscribed_rect":
|
|
(x, y, width, height) = max_inscribed_rect(_mask)
|
|
_width = width
|
|
_height = height
|
|
if scale_adjust != 1.0:
|
|
_width = int(width * scale_adjust)
|
|
_height = int(height * scale_adjust)
|
|
x = x - int((_width - width) / 2)
|
|
y = y - int((_height - height) / 2)
|
|
x += x_adjust
|
|
y += y_adjust
|
|
x_percent = (x + _width / 2) / _mask.width * 100
|
|
y_percent = (y + _height / 2) / _mask.height * 100
|
|
preview_image = tensor2pil(mask).convert('RGB')
|
|
preview_image = draw_rect(preview_image, x - x_adjust, y - y_adjust, width, height, line_color="#F00000", line_width=int(preview_image.height / 60))
|
|
preview_image = draw_rect(preview_image, x, y, width, height, line_color="#00F000", line_width=int(preview_image.height / 40))
|
|
return ( pil2tensor(preview_image), round(x_percent, 2), round(y_percent, 2), _width, _height, x, y,)
|
|
|
|
NODE_CLASS_MAPPINGS = {
|
|
"LayerMask: MaskBoxDetect": MaskBoxDetect
|
|
}
|
|
|
|
NODE_DISPLAY_NAME_MAPPINGS = {
|
|
"LayerMask: MaskBoxDetect": "LayerMask: MaskBoxDetect"
|
|
} |