perf(processing): optimize post-processing with float32 and buffer reuse
- Replace float64 with float32 in apply_mouth_area() blending masks — float32 provides sufficient precision for 8-bit image blending and halves memory bandwidth - Use float32 in apply_mask_area() mask computations - Vectorize hull padding loop in create_face_mask() (face_masking.py) replacing per-point Python loop with NumPy array operations - Fix apply_color_transfer() to use proper [0,1] LAB conversion — cv2.cvtColor with float32 input expects [0,1] range, not [0,255] - Pre-compute inverse masks to avoid repeated (1.0 - mask) subtraction - Use np.broadcast_to instead of np.repeat for face mask expansion Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1004,7 +1004,7 @@ def apply_mouth_area(
|
||||
feather_amount = max(1, min(30, feather_base_dim // max(1, mask_feather_ratio))) # Avoid div by zero
|
||||
# Ensure kernel size is odd and positive
|
||||
kernel_size = 2 * feather_amount + 1
|
||||
feathered_polygon_mask = cv2.GaussianBlur(polygon_mask_roi.astype(float), (kernel_size, kernel_size), 0)
|
||||
feathered_polygon_mask = cv2.GaussianBlur(polygon_mask_roi.astype(np.float32), (kernel_size, kernel_size), 0)
|
||||
|
||||
# Normalize feathered mask to [0.0, 1.0] range
|
||||
max_val = feathered_polygon_mask.max()
|
||||
@@ -1019,9 +1019,9 @@ def apply_mouth_area(
|
||||
# Get the corresponding ROI from the *full face mask* (already blurred)
|
||||
# Ensure face_mask is float and normalized [0.0, 1.0]
|
||||
if face_mask.dtype != np.float64 and face_mask.dtype != np.float32:
|
||||
face_mask_float = face_mask.astype(float) / 255.0
|
||||
face_mask_float = face_mask.astype(np.float32) / 255.0
|
||||
else: # Assume already float [0,1] if type is float
|
||||
face_mask_float = face_mask
|
||||
face_mask_float = face_mask.astype(np.float32) if face_mask.dtype == np.float64 else face_mask
|
||||
face_mask_roi = face_mask_float[min_y:max_y, min_x:max_x]
|
||||
|
||||
# Combine the feathered mouth polygon mask with the face mask ROI
|
||||
@@ -1033,14 +1033,14 @@ def apply_mouth_area(
|
||||
if len(frame.shape) == 3 and frame.shape[2] == 3:
|
||||
combined_mask_3channel = combined_mask[:, :, np.newaxis]
|
||||
|
||||
# Ensure data types are compatible for blending (float or double for mask, uint8 for images)
|
||||
color_corrected_mouth_uint8 = color_corrected_mouth.astype(np.uint8)
|
||||
roi_uint8 = roi.astype(np.uint8)
|
||||
combined_mask_float = combined_mask_3channel.astype(np.float64) # Use float64 for precision in mask
|
||||
# Ensure data types are compatible for blending
|
||||
# float32 provides sufficient precision for 8-bit image blending
|
||||
combined_mask_f32 = combined_mask_3channel.astype(np.float32)
|
||||
inv_mask = np.float32(1.0) - combined_mask_f32
|
||||
|
||||
# Blend: (original_mouth * combined_mask) + (swapped_face_roi * (1 - combined_mask))
|
||||
blended_roi = (color_corrected_mouth_uint8 * combined_mask_float +
|
||||
roi_uint8 * (1.0 - combined_mask_float))
|
||||
blended_roi = (color_corrected_mouth * combined_mask_f32 +
|
||||
roi * inv_mask)
|
||||
|
||||
# Place the blended ROI back into the frame
|
||||
frame[min_y:max_y, min_x:max_x] = blended_roi.astype(np.uint8)
|
||||
|
||||
Reference in New Issue
Block a user