Compare commits
1 Commits
e9f0006702
...
0133a489eb
Author | SHA1 | Date |
---|---|---|
|
0133a489eb |
|
@ -2,7 +2,6 @@ import os
|
||||||
import shutil
|
import shutil
|
||||||
from typing import Any
|
from typing import Any
|
||||||
import insightface
|
import insightface
|
||||||
import logging # Added logging import
|
|
||||||
|
|
||||||
import cv2
|
import cv2
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
@ -26,27 +25,18 @@ def get_face_analyser() -> Any:
|
||||||
|
|
||||||
|
|
||||||
def get_one_face(frame: Frame) -> Any:
|
def get_one_face(frame: Frame) -> Any:
|
||||||
faces = get_face_analyser().get(frame)
|
face = get_face_analyser().get(frame)
|
||||||
if not faces:
|
|
||||||
logging.debug("Face_analyser: get_one_face: No faces found by insightface.")
|
|
||||||
return None
|
|
||||||
try:
|
try:
|
||||||
return min(faces, key=lambda x: x.bbox[0])
|
return min(face, key=lambda x: x.bbox[0])
|
||||||
except ValueError:
|
except ValueError:
|
||||||
logging.debug("Face_analyser: get_one_face: ValueError, likely no faces after all.")
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_many_faces(frame: Frame) -> Any:
|
def get_many_faces(frame: Frame) -> Any:
|
||||||
faces = get_face_analyser().get(frame)
|
try:
|
||||||
if not faces: # Check if faces is None or an empty list
|
return get_face_analyser().get(frame)
|
||||||
logging.debug("Face_analyser: get_many_faces: No faces found by insightface.")
|
except IndexError:
|
||||||
# Depending on what insightface returns for no faces,
|
return None
|
||||||
# you might return None or an empty list.
|
|
||||||
# If .get() returns an empty list for no faces, this check is sufficient.
|
|
||||||
# If .get() returns None, this is also fine.
|
|
||||||
return faces # Return original (None or empty list)
|
|
||||||
return faces
|
|
||||||
|
|
||||||
def has_valid_map() -> bool:
|
def has_valid_map() -> bool:
|
||||||
for map in modules.globals.source_target_map:
|
for map in modules.globals.source_target_map:
|
||||||
|
|
|
@ -82,8 +82,7 @@ def get_face_enhancer() -> Any:
|
||||||
selected_device = torch.device("cpu")
|
selected_device = torch.device("cpu")
|
||||||
device_priority.append("CPU")
|
device_priority.append("CPU")
|
||||||
|
|
||||||
upscale_factor = getattr(modules.globals, 'gfpgan_upscale_factor', 2)
|
FACE_ENHANCER = gfpgan.GFPGANer(model_path=model_path, upscale=2, device=selected_device)
|
||||||
FACE_ENHANCER = gfpgan.GFPGANer(model_path=model_path, upscale=upscale_factor, device=selected_device)
|
|
||||||
|
|
||||||
# for debug:
|
# for debug:
|
||||||
print(f"Selected device: {selected_device} and device priority: {device_priority}")
|
print(f"Selected device: {selected_device} and device priority: {device_priority}")
|
||||||
|
|
|
@ -21,16 +21,6 @@ FACE_SWAPPER = None
|
||||||
THREAD_LOCK = threading.Lock()
|
THREAD_LOCK = threading.Lock()
|
||||||
NAME = "DLC.FACE-SWAPPER"
|
NAME = "DLC.FACE-SWAPPER"
|
||||||
|
|
||||||
|
|
||||||
def _validate_kernel_size(kernel_tuple, default_kernel_tuple):
|
|
||||||
if isinstance(kernel_tuple, tuple) and len(kernel_tuple) == 2 and \
|
|
||||||
isinstance(kernel_tuple[0], int) and kernel_tuple[0] > 0 and kernel_tuple[0] % 2 == 1 and \
|
|
||||||
isinstance(kernel_tuple[1], int) and kernel_tuple[1] > 0 and kernel_tuple[1] % 2 == 1:
|
|
||||||
return kernel_tuple
|
|
||||||
else:
|
|
||||||
logging.warning(f"Invalid kernel size {kernel_tuple} received. Must be a tuple of two positive odd integers. Falling back to default {default_kernel_tuple}.")
|
|
||||||
return default_kernel_tuple
|
|
||||||
|
|
||||||
abs_dir = os.path.dirname(os.path.abspath(__file__))
|
abs_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
models_dir = os.path.join(
|
models_dir = os.path.join(
|
||||||
os.path.dirname(os.path.dirname(os.path.dirname(abs_dir))), "models"
|
os.path.dirname(os.path.dirname(os.path.dirname(abs_dir))), "models"
|
||||||
|
@ -93,12 +83,8 @@ def swap_face(source_face: Face, target_face: Face, temp_frame: Frame) -> Frame:
|
||||||
if original_target_face_roi.size > 0:
|
if original_target_face_roi.size > 0:
|
||||||
swapped_face_roi = swapped_frame[y1:y2, x1:x2].copy()
|
swapped_face_roi = swapped_frame[y1:y2, x1:x2].copy()
|
||||||
if swapped_face_roi.size > 0:
|
if swapped_face_roi.size > 0:
|
||||||
try:
|
corrected_swapped_face_roi = apply_color_transfer(swapped_face_roi, original_target_face_roi)
|
||||||
corrected_swapped_face_roi = apply_color_transfer(swapped_face_roi, original_target_face_roi)
|
swapped_frame[y1:y2, x1:x2] = corrected_swapped_face_roi
|
||||||
swapped_frame[y1:y2, x1:x2] = corrected_swapped_face_roi
|
|
||||||
except Exception as e:
|
|
||||||
logging.error(f"Failed to apply statistical color transfer: {e}. Using original swapped ROI.")
|
|
||||||
# swapped_frame already contains the uncorrected swapped_face_roi in this region
|
|
||||||
else:
|
else:
|
||||||
# Apply the face swap without statistical color correction
|
# Apply the face swap without statistical color correction
|
||||||
swapped_frame = face_swapper.get(
|
swapped_frame = face_swapper.get(
|
||||||
|
@ -136,26 +122,16 @@ def process_frame(source_face: Face, temp_frame: Frame) -> Frame:
|
||||||
many_faces = get_many_faces(temp_frame)
|
many_faces = get_many_faces(temp_frame)
|
||||||
if many_faces:
|
if many_faces:
|
||||||
for target_face in many_faces:
|
for target_face in many_faces:
|
||||||
if source_face and target_face: # target_face from many_faces will always be valid here
|
if source_face and target_face:
|
||||||
temp_frame = swap_face(source_face, target_face, temp_frame)
|
temp_frame = swap_face(source_face, target_face, temp_frame)
|
||||||
elif not source_face: # Check source_face specifically
|
else:
|
||||||
logging.error("Source face is not available or no face detected in source image. Skipping swap for this target face.")
|
print("Face detection failed for target/source.")
|
||||||
# Optionally `continue` or `break` if source_face is essential for all
|
|
||||||
elif not source_face : # if many_faces is empty AND source_face is also an issue
|
|
||||||
logging.error("Source face is not available AND no faces detected in target frame.")
|
|
||||||
else: # many_faces is empty, but source_face is ok
|
|
||||||
logging.info(f"No faces detected in the current target frame for 'many_faces' mode.")
|
|
||||||
else:
|
else:
|
||||||
target_face = get_one_face(temp_frame)
|
target_face = get_one_face(temp_frame)
|
||||||
if target_face and source_face:
|
if target_face and source_face:
|
||||||
temp_frame = swap_face(source_face, target_face, temp_frame)
|
temp_frame = swap_face(source_face, target_face, temp_frame)
|
||||||
else:
|
else:
|
||||||
if not source_face:
|
logging.error("Face detection failed for target or source.")
|
||||||
logging.error("Source face is not available or no face detected in source image.")
|
|
||||||
elif not target_face:
|
|
||||||
logging.error(f"No face detected in the current target frame.")
|
|
||||||
else: # Should not happen if logic is right, but as a fallback
|
|
||||||
logging.error("Face detection failed for an unknown reason concerning target or source.")
|
|
||||||
return temp_frame
|
return temp_frame
|
||||||
|
|
||||||
|
|
||||||
|
@ -407,12 +383,8 @@ def create_lower_mouth_mask(
|
||||||
cv2.fillPoly(mask_roi, [expanded_landmarks - [min_x, min_y]], 255)
|
cv2.fillPoly(mask_roi, [expanded_landmarks - [min_x, min_y]], 255)
|
||||||
|
|
||||||
# Apply Gaussian blur to soften the mask edges
|
# Apply Gaussian blur to soften the mask edges
|
||||||
# Default kernel size for mouth mask blur is (9,9) as a balance between performance and smoothing.
|
kernel_size_mouth = getattr(modules.globals, 'mouth_mask_blur_kernel_size', (9, 9))
|
||||||
# Larger values (e.g., (15,15) - the previous hardcoded value) provide more smoothing but are slower.
|
mask_roi = cv2.GaussianBlur(mask_roi, kernel_size_mouth, 0)
|
||||||
# This is configurable via modules.globals.mouth_mask_blur_kernel_size.
|
|
||||||
kernel_size_mouth_config = getattr(modules.globals, 'mouth_mask_blur_kernel_size', (9, 9))
|
|
||||||
valid_kernel_mouth = _validate_kernel_size(kernel_size_mouth_config, (9, 9))
|
|
||||||
mask_roi = cv2.GaussianBlur(mask_roi, valid_kernel_mouth, 0)
|
|
||||||
|
|
||||||
# Place the mask ROI in the full-sized mask
|
# Place the mask ROI in the full-sized mask
|
||||||
mask[min_y:max_y, min_x:max_x] = mask_roi
|
mask[min_y:max_y, min_x:max_x] = mask_roi
|
||||||
|
@ -553,13 +525,7 @@ def apply_mouth_area(
|
||||||
feathered_mask = cv2.GaussianBlur(
|
feathered_mask = cv2.GaussianBlur(
|
||||||
polygon_mask.astype(float), (0, 0), feather_amount
|
polygon_mask.astype(float), (0, 0), feather_amount
|
||||||
)
|
)
|
||||||
|
feathered_mask = feathered_mask / feathered_mask.max()
|
||||||
mask_max_value = feathered_mask.max()
|
|
||||||
if mask_max_value < 1e-6: # Check if max is effectively zero
|
|
||||||
logging.warning("Mouth mask's feathered_mask is all zeros or near-zeros after blur. Resulting mask will be black.")
|
|
||||||
feathered_mask = np.zeros_like(polygon_mask, dtype=np.uint8)
|
|
||||||
else:
|
|
||||||
feathered_mask = (feathered_mask / mask_max_value * 255).astype(np.uint8)
|
|
||||||
|
|
||||||
face_mask_roi = face_mask[min_y:max_y, min_x:max_x]
|
face_mask_roi = face_mask[min_y:max_y, min_x:max_x]
|
||||||
combined_mask = feathered_mask * (face_mask_roi / 255.0)
|
combined_mask = feathered_mask * (face_mask_roi / 255.0)
|
||||||
|
@ -647,9 +613,8 @@ def create_face_mask(face: Face, frame: Frame) -> np.ndarray:
|
||||||
cv2.fillConvexPoly(mask, hull_padded, 255)
|
cv2.fillConvexPoly(mask, hull_padded, 255)
|
||||||
|
|
||||||
# Smooth the mask edges
|
# Smooth the mask edges
|
||||||
kernel_size_face_config = getattr(modules.globals, 'face_mask_blur_kernel_size', (5, 5))
|
kernel_size_face = getattr(modules.globals, 'face_mask_blur_kernel_size', (5, 5))
|
||||||
valid_kernel_face = _validate_kernel_size(kernel_size_face_config, (5, 5))
|
mask = cv2.GaussianBlur(mask, kernel_size_face, 0)
|
||||||
mask = cv2.GaussianBlur(mask, valid_kernel_face, 0)
|
|
||||||
|
|
||||||
return mask
|
return mask
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue