Compare commits

..

No commits in common. "166d5a34e270550e6e10aa0e7a3cbdadc7db6804" and "5f2e54552cadc29fa96b6119fbf9636dfcdd4521" have entirely different histories.

5 changed files with 60 additions and 118 deletions

View File

@ -26,45 +26,30 @@ def segment_hair(image_np: np.ndarray) -> np.ndarray:
try:
HAIR_SEGMENTER_PROCESSOR = SegformerImageProcessor.from_pretrained(MODEL_NAME)
HAIR_SEGMENTER_MODEL = SegformerForSemanticSegmentation.from_pretrained(MODEL_NAME)
if torch.cuda.is_available():
try:
HAIR_SEGMENTER_MODEL = HAIR_SEGMENTER_MODEL.to('cuda')
print("INFO: Hair segmentation model moved to CUDA (GPU).")
except Exception as e_cuda:
print(f"ERROR: Failed to move hair segmentation model to CUDA: {e_cuda}. Using CPU instead.")
# Fallback to CPU if .to('cuda') fails
HAIR_SEGMENTER_MODEL = HAIR_SEGMENTER_MODEL.to('cpu')
else:
print("INFO: CUDA not available. Hair segmentation model will use CPU.")
print("INFO: Hair segmentation model and processor loaded successfully (device: {}).".format(HAIR_SEGMENTER_MODEL.device))
# Optional: Move model to GPU if available and if other models use GPU
# if torch.cuda.is_available():
# HAIR_SEGMENTER_MODEL = HAIR_SEGMENTER_MODEL.to('cuda')
# print("Hair segmentation model moved to GPU.")
print("Hair segmentation model and processor loaded successfully.")
except Exception as e:
print(f"ERROR: Failed to load hair segmentation model/processor: {e}")
print(f"Failed to load hair segmentation model/processor: {e}")
# Return an empty mask compatible with expected output shape (H, W)
return np.zeros((image_np.shape[0], image_np.shape[1]), dtype=np.uint8)
# Ensure processor and model are loaded before proceeding
if HAIR_SEGMENTER_PROCESSOR is None or HAIR_SEGMENTER_MODEL is None:
print("Error: Hair segmentation models are not available.")
return np.zeros((image_np.shape[0], image_np.shape[1]), dtype=np.uint8)
# Convert BGR (OpenCV) to RGB (PIL)
image_rgb = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
image_pil = Image.fromarray(image_rgb)
inputs = HAIR_SEGMENTER_PROCESSOR(images=image_pil, return_tensors="pt")
if HAIR_SEGMENTER_MODEL.device.type == 'cuda':
try:
# SegformerImageProcessor output (BatchEncoding) is a dict-like object.
# We need to move its tensor components, commonly 'pixel_values'.
if 'pixel_values' in inputs:
inputs['pixel_values'] = inputs['pixel_values'].to('cuda')
else: # Fallback if the structure is different than expected
inputs = inputs.to('cuda')
# If inputs has other tensor components that need to be moved, they'd need similar handling.
except Exception as e_inputs_cuda:
print(f"ERROR: Failed to move inputs to CUDA: {e_inputs_cuda}. Attempting inference on CPU.")
# If moving inputs to CUDA fails, we should ensure model is also on CPU for this inference pass
# This is a tricky situation; ideally, this failure shouldn't happen if model moved successfully.
# For simplicity, we'll assume if model is on CUDA, inputs should also be.
# A more robust solution might involve moving model back to CPU if inputs can't be moved.
# Optional: Move inputs to GPU if model is on GPU
# if HAIR_SEGMENTER_MODEL.device.type == 'cuda':
# inputs = inputs.to(HAIR_SEGMENTER_MODEL.device)
with torch.no_grad(): # Important for inference
outputs = HAIR_SEGMENTER_MODEL(**inputs)

View File

@ -78,54 +78,18 @@ def _prepare_warped_source_material_and_mask(
Prepares warped source material (full image) and a combined (face+hair) mask for blending.
Returns (None, None) if essential masks cannot be generated.
"""
try:
# Generate Hair Mask
hair_only_mask_source_raw = segment_hair(source_frame_full)
if hair_only_mask_source_raw is None:
logging.error("segment_hair returned None, which is unexpected.")
return None, None
if hair_only_mask_source_raw.ndim == 3 and hair_only_mask_source_raw.shape[2] == 3:
hair_only_mask_source_raw = cv2.cvtColor(hair_only_mask_source_raw, cv2.COLOR_BGR2GRAY)
_, hair_only_mask_source_binary = cv2.threshold(hair_only_mask_source_raw, 127, 255, cv2.THRESH_BINARY)
except Exception as e:
logging.error(f"Hair segmentation failed: {e}", exc_info=True)
return None, None
# Generate Hair Mask
hair_only_mask_source_raw = segment_hair(source_frame_full)
if hair_only_mask_source_raw.ndim == 3 and hair_only_mask_source_raw.shape[2] == 3:
hair_only_mask_source_raw = cv2.cvtColor(hair_only_mask_source_raw, cv2.COLOR_BGR2GRAY)
_, hair_only_mask_source_binary = cv2.threshold(hair_only_mask_source_raw, 127, 255, cv2.THRESH_BINARY)
try:
# Generate Face Mask
face_only_mask_source_raw = create_face_mask(source_face_obj, source_frame_full)
if face_only_mask_source_raw is None:
logging.error("create_face_mask returned None, which is unexpected.")
return None, None
_, face_only_mask_source_binary = cv2.threshold(face_only_mask_source_raw, 127, 255, cv2.THRESH_BINARY)
except Exception as e:
logging.error(f"Face mask creation failed for source: {e}", exc_info=True)
return None, None
# Generate Face Mask
face_only_mask_source_raw = create_face_mask(source_face_obj, source_frame_full)
_, face_only_mask_source_binary = cv2.threshold(face_only_mask_source_raw, 127, 255, cv2.THRESH_BINARY)
# Combine Face and Hair Masks and Warp
try:
if face_only_mask_source_binary.shape != hair_only_mask_source_binary.shape:
logging.warning("Resizing hair mask to match face mask for source during preparation.")
hair_only_mask_source_binary = cv2.resize(
hair_only_mask_source_binary,
(face_only_mask_source_binary.shape[1], face_only_mask_source_binary.shape[0]),
interpolation=cv2.INTER_NEAREST
)
actual_combined_source_mask = cv2.bitwise_or(face_only_mask_source_binary, hair_only_mask_source_binary)
actual_combined_source_mask_blurred = cv2.GaussianBlur(actual_combined_source_mask, (5, 5), 3)
warped_full_source_material = cv2.warpAffine(source_frame_full, matrix, dsize)
warped_combined_mask_temp = cv2.warpAffine(actual_combined_source_mask_blurred, matrix, dsize)
_, warped_combined_mask_binary_for_clone = cv2.threshold(warped_combined_mask_temp, 127, 255, cv2.THRESH_BINARY)
except Exception as e:
logging.error(f"Mask combination or warping failed: {e}", exc_info=True)
return None, None
return warped_full_source_material, warped_combined_mask_binary_for_clone
def _blend_material_onto_frame(
# Combine Face and Hair Masks
if face_only_mask_source_binary.shape != hair_only_mask_source_binary.shape:
logging.warning("Resizing hair mask to match face mask for source during preparation.")
hair_only_mask_source_binary = cv2.resize(
hair_only_mask_source_binary,
@ -189,7 +153,7 @@ def swap_face(source_face_obj: Face, target_face: Face, source_frame_full: Frame
swapped_frame = face_swapper.get(temp_frame, target_face, source_face_obj, paste_back=True)
final_swapped_frame = swapped_frame # Initialize with the base swap. Copy is made only if needed.
if getattr(modules.globals, 'enable_hair_swapping', True): # Default to True if attribute is missing
if modules.globals.enable_hair_swapping:
if not (source_face_obj.kps is not None and \
target_face.kps is not None and \
source_face_obj.kps.shape[0] >= 3 and \
@ -217,11 +181,7 @@ def swap_face(source_face_obj: Face, target_face: Face, source_frame_full: Frame
# Make a copy only now that we are sure we will modify it for hair.
final_swapped_frame = swapped_frame.copy()
try:
color_corrected_material = apply_color_transfer(warped_material, final_swapped_frame)
except Exception as e:
logging.warning(f"Color transfer failed: {e}. Proceeding with uncorrected material for hair blending.", exc_info=True)
color_corrected_material = warped_material # Use uncorrected material as fallback
color_corrected_material = apply_color_transfer(warped_material, final_swapped_frame) # Use final_swapped_frame for color context
final_swapped_frame = _blend_material_onto_frame(
final_swapped_frame,

View File

@ -1015,7 +1015,7 @@ def create_webcam_preview(camera_index: int):
if not modules.globals.map_faces:
# Case 1: map_faces is False - source_face_obj_for_cam and source_frame_full_for_cam are pre-loaded
if source_face_obj_for_cam is not None and source_frame_full_for_cam is not None: # Check if valid after pre-loading
if source_face_obj_for_cam and source_frame_full_for_cam is not None: # Check if valid after pre-loading
for frame_processor in frame_processors:
if frame_processor.NAME == "DLC.FACE-ENHANCER":
if modules.globals.fp_ui["face_enhancer"]:
@ -1032,10 +1032,8 @@ def create_webcam_preview(camera_index: int):
for frame_processor in frame_processors:
if frame_processor.NAME == "DLC.FACE-ENHANCER":
if modules.globals.fp_ui["face_enhancer"]:
# Corrected: face_enhancer.process_frame_v2 is expected to take only temp_frame
temp_frame = frame_processor.process_frame_v2(temp_frame)
temp_frame = frame_processor.process_frame_v2(source_frame_full_for_cam_map_faces, temp_frame)
else:
# This is for other processors when map_faces is True
temp_frame = frame_processor.process_frame_v2(source_frame_full_for_cam_map_faces, temp_frame)
# If source_frame_full_for_cam_map_faces was invalid, error is persistent from pre-loop check.

View File

@ -15,9 +15,8 @@ if errorlevel 1 (
:: Optional: Check Python version (e.g., >= 3.9 or >=3.10).
:: This is a bit more complex in pure batch. For now, rely on user having a modern Python 3.
:: The README will recommend 3.10.
:: If we reach here, Python is found.
echo Python was found. Attempting to display version:
for /f "delims=" %%i in ('python --version 2^>^&1') do echo %%i
echo Found Python:
python --version
:: 2. Check for ffmpeg (informational)
echo Checking for ffmpeg...