Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional mask output for LivePortraitCropper #105

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
StitchingRetargetingNetwork,
)
from .liveportrait.utils.camera import get_rotation_matrix
from .liveportrait.utils.crop import _transform_img_kornia
from .liveportrait.utils.crop import _transform_img_kornia, _transform_pts


class InferenceConfig:
Expand Down Expand Up @@ -616,20 +616,22 @@ def INPUT_TYPES(s):
],
),
"rotate": ("BOOLEAN", {"default": True}),
"output_masks": ("BOOLEAN", {"default": False}),
},
}

RETURN_TYPES = ("IMAGE", "CROPINFO",)
RETURN_NAMES = ("cropped_image", "crop_info",)
RETURN_TYPES = ("IMAGE", "CROPINFO", "MASK")
RETURN_NAMES = ("cropped_image", "crop_info", "mask")
FUNCTION = "process"
CATEGORY = "LivePortrait"

def process(self, pipeline, cropper, source_image, dsize, scale, vx_ratio, vy_ratio, face_index, face_index_order, rotate):
def process(self, pipeline, cropper, source_image, dsize, scale, vx_ratio, vy_ratio, face_index, face_index_order, rotate, output_masks):
source_image_np = (source_image.contiguous() * 255).byte().numpy()

# Initialize lists
crop_info_list = []
cropped_images_list = []
masks_list = []
source_info = []
source_rot_list = []
f_s_list = []
Expand All @@ -640,7 +642,8 @@ def process(self, pipeline, cropper, source_image, dsize, scale, vx_ratio, vy_ra
for i in tqdm(range(len(source_image_np)), desc='Detecting, cropping, and processing..', total=len(source_image_np)):
# Cropping operation
crop_info, cropped_image_256 = cropper.crop_single_image(source_image_np[i], dsize, scale, vy_ratio, vx_ratio, face_index, face_index_order, rotate)

if output_masks:
mask = np.zeros(source_image_np[i].shape[:2],dtype=float)
# Processing source images
if crop_info:
crop_info_list.append(crop_info)
Expand All @@ -661,6 +664,11 @@ def process(self, pipeline, cropper, source_image, dsize, scale, vx_ratio, vy_ra
f_s = pipeline.live_portrait_wrapper.extract_feature_3d(I_s)
f_s_list.append(f_s)

if output_masks:
pts = np.array([[0,0],[0,dsize],[dsize,dsize],[dsize,0]],dtype=float)
pts = (_transform_pts(pts,crop_info["M_c2o"])+0.5).astype(np.int32)
cv2.fillPoly(mask, pts=[pts], color=1.0)

del I_s

else:
Expand All @@ -671,14 +679,19 @@ def process(self, pipeline, cropper, source_image, dsize, scale, vx_ratio, vy_ra
x_s_list.append(None)
source_info.append(None)
source_rot_list.append(None)


if output_masks:
masks_list.append(torch.from_numpy(mask).unsqueeze(0))

# Update progress bar
pbar.update(1)

cropped_tensors_out = (
torch.stack([torch.from_numpy(np_array) for np_array in cropped_images_list])
/ 255
)
if output_masks:
masks_list = torch.cat(masks_list,axis=0).float()

crop_info_dict = {
'crop_info_list': crop_info_list,
Expand All @@ -688,7 +701,7 @@ def process(self, pipeline, cropper, source_image, dsize, scale, vx_ratio, vy_ra
'source_info': source_info
}

return (cropped_tensors_out, crop_info_dict)
return (cropped_tensors_out, crop_info_dict, masks_list)

class LivePortraitRetargeting:
@classmethod
Expand Down