supli6669 commited on
Commit ·
bf9109c
1
Parent(s): c27ce82
feat: add Studio Portrait Beauty Suite with teeth whitening, ocular catchlight sparkle, and auto tone glow balance
Browse files- app.py +11 -1
- handover.md +34 -0
- pipeline.py +10 -5
- tools/test_batch_pipeline.py +39 -3
- wink_enhancer.py +198 -53
app.py
CHANGED
|
@@ -428,9 +428,11 @@ with st.sidebar:
|
|
| 428 |
color_match = st.checkbox("Auto Skin Tone Alignment", value=default_color)
|
| 429 |
|
| 430 |
st.markdown("**🎭 Facial Organ Enhancements**")
|
| 431 |
-
enable_eyes = st.checkbox("👁️ Eye Sparkle &
|
| 432 |
enable_lips = st.checkbox("👄 Lip Saturation & Definition", value=default_lip)
|
| 433 |
enable_skin = st.checkbox("💆 Real Skin Grain Retention", value=default_skin)
|
|
|
|
|
|
|
| 434 |
|
| 435 |
chromatic_fix = st.toggle("🌈 Chromatic Aberration Correction", value=False, help="Radial channel realignment for old lenses and color fringing")
|
| 436 |
bg_upscale = st.toggle("Real-ESRGAN Background Upscale", value=False)
|
|
@@ -452,6 +454,8 @@ with st.sidebar:
|
|
| 452 |
'eye': enable_eyes,
|
| 453 |
'lip': enable_lips,
|
| 454 |
'skin': enable_skin,
|
|
|
|
|
|
|
| 455 |
'chromatic': chromatic_fix
|
| 456 |
}
|
| 457 |
st.success(f"Saved custom preset: '{p_name}'")
|
|
@@ -514,6 +518,8 @@ with tab_photo:
|
|
| 514 |
'eye': enable_eyes,
|
| 515 |
'lip': enable_lips,
|
| 516 |
'skin': enable_skin,
|
|
|
|
|
|
|
| 517 |
'chromatic': chromatic_fix,
|
| 518 |
'bg_up': bg_upscale,
|
| 519 |
'face_up': face_upscale
|
|
@@ -560,6 +566,8 @@ with tab_photo:
|
|
| 560 |
'enable_eyes': enable_eyes,
|
| 561 |
'enable_lips': enable_lips,
|
| 562 |
'enable_skin': enable_skin,
|
|
|
|
|
|
|
| 563 |
'chromatic_aberration': chromatic_fix,
|
| 564 |
'progress_callback': local_progress_callback,
|
| 565 |
}
|
|
@@ -828,6 +836,8 @@ with tab_batch:
|
|
| 828 |
enable_eyes=enable_eyes,
|
| 829 |
enable_lips=enable_lips,
|
| 830 |
enable_skin=enable_skin,
|
|
|
|
|
|
|
| 831 |
chromatic_aberration=chromatic_fix,
|
| 832 |
progress_callback=b_callback
|
| 833 |
)
|
|
|
|
| 428 |
color_match = st.checkbox("Auto Skin Tone Alignment", value=default_color)
|
| 429 |
|
| 430 |
st.markdown("**🎭 Facial Organ Enhancements**")
|
| 431 |
+
enable_eyes = st.checkbox("👁️ Eye Sparkle & Sclera Glow", value=default_eye)
|
| 432 |
enable_lips = st.checkbox("👄 Lip Saturation & Definition", value=default_lip)
|
| 433 |
enable_skin = st.checkbox("💆 Real Skin Grain Retention", value=default_skin)
|
| 434 |
+
enable_teeth = st.checkbox("🦷 Natural Teeth Whitening", value=True)
|
| 435 |
+
enable_tone_glow = st.checkbox("✨ Studio Skin Glow & White Balance", value=True)
|
| 436 |
|
| 437 |
chromatic_fix = st.toggle("🌈 Chromatic Aberration Correction", value=False, help="Radial channel realignment for old lenses and color fringing")
|
| 438 |
bg_upscale = st.toggle("Real-ESRGAN Background Upscale", value=False)
|
|
|
|
| 454 |
'eye': enable_eyes,
|
| 455 |
'lip': enable_lips,
|
| 456 |
'skin': enable_skin,
|
| 457 |
+
'teeth': enable_teeth,
|
| 458 |
+
'tone_glow': enable_tone_glow,
|
| 459 |
'chromatic': chromatic_fix
|
| 460 |
}
|
| 461 |
st.success(f"Saved custom preset: '{p_name}'")
|
|
|
|
| 518 |
'eye': enable_eyes,
|
| 519 |
'lip': enable_lips,
|
| 520 |
'skin': enable_skin,
|
| 521 |
+
'teeth': enable_teeth,
|
| 522 |
+
'tone_glow': enable_tone_glow,
|
| 523 |
'chromatic': chromatic_fix,
|
| 524 |
'bg_up': bg_upscale,
|
| 525 |
'face_up': face_upscale
|
|
|
|
| 566 |
'enable_eyes': enable_eyes,
|
| 567 |
'enable_lips': enable_lips,
|
| 568 |
'enable_skin': enable_skin,
|
| 569 |
+
'enable_teeth': enable_teeth,
|
| 570 |
+
'enable_tone_glow': enable_tone_glow,
|
| 571 |
'chromatic_aberration': chromatic_fix,
|
| 572 |
'progress_callback': local_progress_callback,
|
| 573 |
}
|
|
|
|
| 836 |
enable_eyes=enable_eyes,
|
| 837 |
enable_lips=enable_lips,
|
| 838 |
enable_skin=enable_skin,
|
| 839 |
+
enable_teeth=enable_teeth,
|
| 840 |
+
enable_tone_glow=enable_tone_glow,
|
| 841 |
chromatic_aberration=chromatic_fix,
|
| 842 |
progress_callback=b_callback
|
| 843 |
)
|
handover.md
CHANGED
|
@@ -1273,6 +1273,40 @@ Integrated high-productivity batch workflows, live browser webcam capture, and l
|
|
| 1273 |
- [MODIFY] [tools/test_all.py](file:///d:/.gemini-scratch/custom-ai-enhancer/tools/test_all.py) (Added batch test to master suite)
|
| 1274 |
- [MODIFY] [app.py](file:///d:/.gemini-scratch/custom-ai-enhancer/app.py) (Added Tab 3 Batch Processing, live camera snapshot, and chromatic toggle)
|
| 1275 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
|
|
|
| 1273 |
- [MODIFY] [tools/test_all.py](file:///d:/.gemini-scratch/custom-ai-enhancer/tools/test_all.py) (Added batch test to master suite)
|
| 1274 |
- [MODIFY] [app.py](file:///d:/.gemini-scratch/custom-ai-enhancer/app.py) (Added Tab 3 Batch Processing, live camera snapshot, and chromatic toggle)
|
| 1275 |
|
| 1276 |
+
---
|
| 1277 |
+
|
| 1278 |
+
## Task 25: Studio Portrait Beauty Suite (Teeth Whitening, Eye Catchlight Sparkle & Auto Tone Balance)
|
| 1279 |
+
|
| 1280 |
+
**Date:** 2026-09-02
|
| 1281 |
+
**Status:** ✅ Completed
|
| 1282 |
+
|
| 1283 |
+
### Overview
|
| 1284 |
+
Elevated aesthetic visual quality across portraits by implementing studio-grade facial beautification algorithms:
|
| 1285 |
+
|
| 1286 |
+
1. **Natural Teeth Whitening (`whiten_teeth`)**:
|
| 1287 |
+
- Isolates teeth region using facexlib parsing mask (category index 11).
|
| 1288 |
+
- In LAB space, desaturates yellow color cast ($b$-channel pull towards 128) and lifts luminance $L$ with smooth edge feathering to prevent chalky/over-bleached artifacts.
|
| 1289 |
+
|
| 1290 |
+
2. **Ocular Catchlight Sparkle & Sclera Glow (`brighten_eyes_and_sclera`)**:
|
| 1291 |
+
- Isolates eye regions (indices 4, 5).
|
| 1292 |
+
- Isolates specular reflections ($L > 190$) and amplifies catchlights with micro-contrast.
|
| 1293 |
+
- Clarifies eye whites and applies subtle edge-preserving sharpening on eyelashes.
|
| 1294 |
+
|
| 1295 |
+
3. **Studio Skin Glow & Auto White Balance (`balance_portrait_lighting_and_tone`)**:
|
| 1296 |
+
- Implemented Shades of Gray / Gray-World auto white balance to fix color casts.
|
| 1297 |
+
- Quadratic shadow recovery lifts dark shadows while protecting highlights, adding a natural healthy pink/warm skin radiance.
|
| 1298 |
+
|
| 1299 |
+
4. **Streamlit UI Controls & Test Coverage**:
|
| 1300 |
+
- Added `🦷 Natural Teeth Whitening` and `✨ Studio Skin Glow & White Balance` checkboxes to sidebar and presets.
|
| 1301 |
+
- Expanded `tools/test_batch_pipeline.py` with `test_studio_beauty_engine()`.
|
| 1302 |
+
|
| 1303 |
+
### Code Changes
|
| 1304 |
+
- [MODIFY] [wink_enhancer.py](file:///d:/.gemini-scratch/custom-ai-enhancer/wink_enhancer.py) (Added `whiten_teeth()`, `brighten_eyes_and_sclera()`, `balance_portrait_lighting_and_tone()`, updated `enhance_eyes_and_lips()` and `enhance_face()`)
|
| 1305 |
+
- [MODIFY] [pipeline.py](file:///d:/.gemini-scratch/custom-ai-enhancer/pipeline.py) (Connected `enable_teeth` and `enable_tone_glow` into `process_image` and `paste_faces_custom_blend`)
|
| 1306 |
+
- [MODIFY] [app.py](file:///d:/.gemini-scratch/custom-ai-enhancer/app.py) (Added beauty checkboxes to sidebar, custom preset manager, Tab 1, and Tab 3)
|
| 1307 |
+
- [MODIFY] [tools/test_batch_pipeline.py](file:///d:/.gemini-scratch/custom-ai-enhancer/tools/test_batch_pipeline.py) (Added `test_studio_beauty_engine` unit tests)
|
| 1308 |
+
|
| 1309 |
+
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
pipeline.py
CHANGED
|
@@ -271,7 +271,7 @@ class LocalAIEnhancerPipeline:
|
|
| 271 |
ort_outs = self.ort_session_cf.run(None, ort_inputs)
|
| 272 |
return ort_outs[0]
|
| 273 |
|
| 274 |
-
def process_image(self, img, w=0.5, detection_model='retinaface_mobile0.25', upscale=2, blend_softness=0.5, bg_upsampler=None, det_threshold=0.5, sharpen_amount=0.0, face_upsample=False, batch_size=0, parallel=False, face_restore=True, wink_mode=True, eye_enhancement=True, skin_grain=0.15, color_match=True, enable_eyes=True, enable_lips=True, enable_skin=True, preset_mode='Custom', chromatic_aberration=False, progress_callback=None):
|
| 275 |
|
| 276 |
"""Enhance one image without sharing request-specific state.
|
| 277 |
|
|
@@ -286,12 +286,13 @@ class LocalAIEnhancerPipeline:
|
|
| 286 |
img, w, detection_model, upscale, blend_softness, bg_upsampler,
|
| 287 |
det_threshold, sharpen_amount, face_upsample, batch_size, parallel,
|
| 288 |
face_restore, wink_mode, eye_enhancement, skin_grain, color_match,
|
| 289 |
-
enable_eyes, enable_lips, enable_skin,
|
|
|
|
| 290 |
)
|
| 291 |
finally:
|
| 292 |
_active_progress_callback.reset(callback_token)
|
| 293 |
|
| 294 |
-
def _process_image(self, img, w=0.5, detection_model='retinaface_mobile0.25', upscale=2, blend_softness=0.5, bg_upsampler=None, det_threshold=0.5, sharpen_amount=0.0, face_upsample=False, batch_size=0, parallel=False, face_restore=True, wink_mode=True, eye_enhancement=True, skin_grain=0.15, color_match=True, enable_eyes=True, enable_lips=True, enable_skin=True, preset_mode='Custom', chromatic_aberration=False):
|
| 295 |
|
| 296 |
"""
|
| 297 |
Enhance an image using the local CodeFormer pipeline.
|
|
@@ -565,7 +566,9 @@ class LocalAIEnhancerPipeline:
|
|
| 565 |
color_match=color_match,
|
| 566 |
enable_eyes=enable_eyes,
|
| 567 |
enable_lips=enable_lips,
|
| 568 |
-
enable_skin=enable_skin
|
|
|
|
|
|
|
| 569 |
)
|
| 570 |
|
| 571 |
self._report_progress("blending", 1.0, "Blending complete!")
|
|
@@ -573,7 +576,7 @@ class LocalAIEnhancerPipeline:
|
|
| 573 |
|
| 574 |
return enhanced_img
|
| 575 |
|
| 576 |
-
def paste_faces_custom_blend(self, face_helper, upscale, blend_softness, bg_img=None, sharpen_amount=0.0, face_upsample=False, w=0.5, wink_mode=True, eye_enhancement=True, skin_grain=0.15, color_match=True, enable_eyes=True, enable_lips=True, enable_skin=True):
|
| 577 |
"""Custom implementation of face pasting with adjustable soft blending mask."""
|
| 578 |
h, w_img, _ = face_helper.input_img.shape
|
| 579 |
h_up, w_up = int(h * upscale), int(w_img * upscale)
|
|
@@ -616,6 +619,8 @@ class LocalAIEnhancerPipeline:
|
|
| 616 |
enable_eyes=enable_eyes,
|
| 617 |
enable_lips=enable_lips,
|
| 618 |
enable_skin=enable_skin,
|
|
|
|
|
|
|
| 619 |
sharpen_amount=sharpen_amount
|
| 620 |
)
|
| 621 |
|
|
|
|
| 271 |
ort_outs = self.ort_session_cf.run(None, ort_inputs)
|
| 272 |
return ort_outs[0]
|
| 273 |
|
| 274 |
+
def process_image(self, img, w=0.5, detection_model='retinaface_mobile0.25', upscale=2, blend_softness=0.5, bg_upsampler=None, det_threshold=0.5, sharpen_amount=0.0, face_upsample=False, batch_size=0, parallel=False, face_restore=True, wink_mode=True, eye_enhancement=True, skin_grain=0.15, color_match=True, enable_eyes=True, enable_lips=True, enable_skin=True, enable_teeth=True, enable_tone_glow=True, preset_mode='Custom', chromatic_aberration=False, progress_callback=None):
|
| 275 |
|
| 276 |
"""Enhance one image without sharing request-specific state.
|
| 277 |
|
|
|
|
| 286 |
img, w, detection_model, upscale, blend_softness, bg_upsampler,
|
| 287 |
det_threshold, sharpen_amount, face_upsample, batch_size, parallel,
|
| 288 |
face_restore, wink_mode, eye_enhancement, skin_grain, color_match,
|
| 289 |
+
enable_eyes, enable_lips, enable_skin, enable_teeth, enable_tone_glow,
|
| 290 |
+
preset_mode, chromatic_aberration,
|
| 291 |
)
|
| 292 |
finally:
|
| 293 |
_active_progress_callback.reset(callback_token)
|
| 294 |
|
| 295 |
+
def _process_image(self, img, w=0.5, detection_model='retinaface_mobile0.25', upscale=2, blend_softness=0.5, bg_upsampler=None, det_threshold=0.5, sharpen_amount=0.0, face_upsample=False, batch_size=0, parallel=False, face_restore=True, wink_mode=True, eye_enhancement=True, skin_grain=0.15, color_match=True, enable_eyes=True, enable_lips=True, enable_skin=True, enable_teeth=True, enable_tone_glow=True, preset_mode='Custom', chromatic_aberration=False):
|
| 296 |
|
| 297 |
"""
|
| 298 |
Enhance an image using the local CodeFormer pipeline.
|
|
|
|
| 566 |
color_match=color_match,
|
| 567 |
enable_eyes=enable_eyes,
|
| 568 |
enable_lips=enable_lips,
|
| 569 |
+
enable_skin=enable_skin,
|
| 570 |
+
enable_teeth=enable_teeth,
|
| 571 |
+
enable_tone_glow=enable_tone_glow
|
| 572 |
)
|
| 573 |
|
| 574 |
self._report_progress("blending", 1.0, "Blending complete!")
|
|
|
|
| 576 |
|
| 577 |
return enhanced_img
|
| 578 |
|
| 579 |
+
def paste_faces_custom_blend(self, face_helper, upscale, blend_softness, bg_img=None, sharpen_amount=0.0, face_upsample=False, w=0.5, wink_mode=True, eye_enhancement=True, skin_grain=0.15, color_match=True, enable_eyes=True, enable_lips=True, enable_skin=True, enable_teeth=True, enable_tone_glow=True):
|
| 580 |
"""Custom implementation of face pasting with adjustable soft blending mask."""
|
| 581 |
h, w_img, _ = face_helper.input_img.shape
|
| 582 |
h_up, w_up = int(h * upscale), int(w_img * upscale)
|
|
|
|
| 619 |
enable_eyes=enable_eyes,
|
| 620 |
enable_lips=enable_lips,
|
| 621 |
enable_skin=enable_skin,
|
| 622 |
+
enable_teeth=enable_teeth,
|
| 623 |
+
enable_tone_glow=enable_tone_glow,
|
| 624 |
sharpen_amount=sharpen_amount
|
| 625 |
)
|
| 626 |
|
tools/test_batch_pipeline.py
CHANGED
|
@@ -57,14 +57,50 @@ def test_batch_processing_and_html_report():
|
|
| 57 |
html = pipeline.generate_html_report(batch_data)
|
| 58 |
assert "<!DOCTYPE html>" in html
|
| 59 |
assert "test_p1.png" in html
|
| 60 |
-
|
| 61 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
|
| 64 |
def main():
|
| 65 |
test_chromatic_aberration_filter()
|
|
|
|
| 66 |
test_batch_processing_and_html_report()
|
| 67 |
-
print("SUCCESS: All batch processing & chromatic
|
| 68 |
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
|
|
|
| 57 |
html = pipeline.generate_html_report(batch_data)
|
| 58 |
assert "<!DOCTYPE html>" in html
|
| 59 |
assert "test_p1.png" in html
|
| 60 |
+
def test_studio_beauty_engine():
|
| 61 |
+
print("=== Testing Studio Beauty Engine (Teeth, Eye Glow, Tone Balance) ===")
|
| 62 |
+
wink = WinkQualityEnhancer()
|
| 63 |
+
face = np.full((512, 512, 3), 160, dtype=np.uint8)
|
| 64 |
+
mock_mask = np.zeros((512, 512), dtype=np.uint8)
|
| 65 |
+
mock_mask[200:250, 150:200] = 4 # Eye
|
| 66 |
+
mock_mask[350:380, 220:280] = 11 # Teeth
|
| 67 |
+
|
| 68 |
+
# Test teeth whitening
|
| 69 |
+
whitened = wink.whiten_teeth(face, mock_mask, strength=0.5)
|
| 70 |
+
assert whitened.shape == (512, 512, 3)
|
| 71 |
+
assert whitened.dtype == np.uint8
|
| 72 |
+
|
| 73 |
+
# Test ocular catchlight
|
| 74 |
+
bright_eyes = wink.brighten_eyes_and_sclera(face, mock_mask, strength=0.5)
|
| 75 |
+
assert bright_eyes.shape == (512, 512, 3)
|
| 76 |
+
assert bright_eyes.dtype == np.uint8
|
| 77 |
+
|
| 78 |
+
# Test lighting and skin glow tone balance
|
| 79 |
+
balanced = wink.balance_portrait_lighting_and_tone(face, strength=0.3)
|
| 80 |
+
assert balanced.shape == (512, 512, 3)
|
| 81 |
+
assert balanced.dtype == np.uint8
|
| 82 |
+
|
| 83 |
+
# Test full enhance_face with beauty options
|
| 84 |
+
enhanced = wink.enhance_face(
|
| 85 |
+
face,
|
| 86 |
+
cropped_original=face,
|
| 87 |
+
parse_mask=mock_mask,
|
| 88 |
+
enable_teeth=True,
|
| 89 |
+
enable_tone_glow=True,
|
| 90 |
+
enable_eyes=True,
|
| 91 |
+
enable_lips=True,
|
| 92 |
+
enable_skin=True
|
| 93 |
+
)
|
| 94 |
+
assert enhanced.shape == (512, 512, 3)
|
| 95 |
+
assert enhanced.dtype == np.uint8
|
| 96 |
+
print("[OK] Studio Beauty Engine (Teeth, Eye, Glow, Tone) passed all tests!")
|
| 97 |
|
| 98 |
|
| 99 |
def main():
|
| 100 |
test_chromatic_aberration_filter()
|
| 101 |
+
test_studio_beauty_engine()
|
| 102 |
test_batch_processing_and_html_report()
|
| 103 |
+
print("SUCCESS: All batch processing, beauty suite & chromatic tests passed cleanly with exit code 0!")
|
| 104 |
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|
wink_enhancer.py
CHANGED
|
@@ -104,20 +104,19 @@ class WinkQualityEnhancer:
|
|
| 104 |
print(f"[WinkEnhancer] Skin grain warning: {e}")
|
| 105 |
return restored_face
|
| 106 |
|
| 107 |
-
def
|
| 108 |
"""
|
| 109 |
-
|
|
|
|
|
|
|
| 110 |
"""
|
| 111 |
-
if parse_mask is None:
|
| 112 |
-
|
| 113 |
-
blur = cv2.GaussianBlur(face_img, (0, 0), 2.0)
|
| 114 |
-
return cv2.addWeighted(face_img, 1.15, blur, -0.15, 0)
|
| 115 |
|
| 116 |
try:
|
| 117 |
parse_mask_2d = np.squeeze(parse_mask)
|
| 118 |
if parse_mask_2d.ndim != 2:
|
| 119 |
-
|
| 120 |
-
return cv2.addWeighted(face_img, 1.15, blur, -0.15, 0)
|
| 121 |
|
| 122 |
h, w = face_img.shape[:2]
|
| 123 |
if parse_mask_2d.shape[:2] != (h, w):
|
|
@@ -125,48 +124,183 @@ class WinkQualityEnhancer:
|
|
| 125 |
else:
|
| 126 |
parse_mask_res = parse_mask_2d
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
# Facial feature mask IDs in facexlib:
|
| 130 |
-
#
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
result = face_img.copy()
|
| 135 |
-
|
| 136 |
-
# 1. Enhance Eyes: CLAHE on L channel + Unsharp Masking
|
| 137 |
-
if enable_eyes and np.any(eye_mask):
|
| 138 |
-
# Expand eye mask slightly for seamless blending
|
| 139 |
-
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
|
| 140 |
-
eye_mask_dilated = cv2.dilate(eye_mask, kernel, iterations=1)
|
| 141 |
-
|
| 142 |
-
# Convert to LAB for luminance contrast
|
| 143 |
-
lab = cv2.cvtColor(result, cv2.COLOR_BGR2LAB)
|
| 144 |
-
l, a, b = cv2.split(lab)
|
| 145 |
-
|
| 146 |
-
l_eye_clahe = self.clahe_eye.apply(l)
|
| 147 |
-
l_blended = np.where(eye_mask_dilated == 1, l_eye_clahe, l)
|
| 148 |
-
lab_enhanced = cv2.merge([l_blended, a, b])
|
| 149 |
-
result = cv2.cvtColor(lab_enhanced, cv2.COLOR_LAB2BGR)
|
| 150 |
-
|
| 151 |
-
# Unsharp Mask on Eyes
|
| 152 |
-
eye_blur = cv2.GaussianBlur(result, (3, 3), 0)
|
| 153 |
-
eye_sharp = cv2.addWeighted(result, 1.3, eye_blur, -0.3, 0)
|
| 154 |
-
|
| 155 |
-
eye_mask_float = cv2.GaussianBlur(eye_mask_dilated.astype(np.float32), (3, 3), 0)[:, :, np.newaxis]
|
| 156 |
-
result = (result * (1.0 - eye_mask_float) + eye_sharp * eye_mask_float).astype(np.uint8)
|
| 157 |
-
|
| 158 |
-
# 2. Enhance Lips: Subtle contrast and saturation boost
|
| 159 |
if enable_lips and np.any(lip_mask):
|
| 160 |
lip_mask_float = cv2.GaussianBlur(lip_mask.astype(np.float32), (3, 3), 0)[:, :, np.newaxis]
|
| 161 |
hsv = cv2.cvtColor(result, cv2.COLOR_BGR2HSV).astype(np.float32)
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
| 167 |
except Exception as e:
|
| 168 |
-
print(f"[WinkEnhancer]
|
| 169 |
-
return
|
| 170 |
|
| 171 |
def balance_skin_tone_lab(self, face_img: np.ndarray) -> np.ndarray:
|
| 172 |
"""
|
|
@@ -253,7 +387,7 @@ class WinkQualityEnhancer:
|
|
| 253 |
print(f"[WinkEnhancer] Adaptive sharpening warning: {e}")
|
| 254 |
return img
|
| 255 |
|
| 256 |
-
def enhance_face(self, restored_face: np.ndarray, cropped_original: np.ndarray = None, parse_mask: np.ndarray = None, wink_mode: bool = True, eye_enhancement: bool = True, skin_grain: float = 0.15, color_match: bool = True, enable_eyes: bool = True, enable_lips: bool = True, enable_skin: bool = True, sharpen_amount: float = 0.2) -> np.ndarray:
|
| 257 |
"""
|
| 258 |
Master method to execute Wink-level enhancement pipeline on a restored face crop.
|
| 259 |
"""
|
|
@@ -262,22 +396,33 @@ class WinkQualityEnhancer:
|
|
| 262 |
|
| 263 |
out_face = restored_face.copy()
|
| 264 |
|
| 265 |
-
# Step A:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
if color_match and cropped_original is not None:
|
| 267 |
out_face = self.match_color_reinhard(out_face, cropped_original, blend=0.4)
|
| 268 |
|
| 269 |
-
# Step
|
| 270 |
out_face = self.balance_skin_tone_lab(out_face)
|
| 271 |
|
| 272 |
-
# Step
|
| 273 |
-
if eye_enhancement and (enable_eyes or enable_lips):
|
| 274 |
-
out_face = self.enhance_eyes_and_lips(
|
| 275 |
-
|
| 276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
if sharpen_amount > 0.0:
|
| 278 |
out_face = self.apply_adaptive_sharpening(out_face, sharpen_amount=sharpen_amount)
|
| 279 |
|
| 280 |
-
# Step
|
| 281 |
if enable_skin and skin_grain > 0.0 and cropped_original is not None:
|
| 282 |
out_face = self.apply_skin_grain(out_face, cropped_original, skin_mask=parse_mask, grain_amount=skin_grain)
|
| 283 |
|
|
|
|
| 104 |
print(f"[WinkEnhancer] Skin grain warning: {e}")
|
| 105 |
return restored_face
|
| 106 |
|
| 107 |
+
def whiten_teeth(self, face_img: np.ndarray, parse_mask: np.ndarray = None, strength: float = 0.35) -> np.ndarray:
|
| 108 |
"""
|
| 109 |
+
Studio Natural Teeth Whitening:
|
| 110 |
+
Isolates teeth/mouth region using parsing mask, desaturates yellow color cast in LAB space,
|
| 111 |
+
and lifts luminance naturally without artificial chalky/over-bleached artifacts.
|
| 112 |
"""
|
| 113 |
+
if strength <= 0.0 or parse_mask is None or face_img is None:
|
| 114 |
+
return face_img
|
|
|
|
|
|
|
| 115 |
|
| 116 |
try:
|
| 117 |
parse_mask_2d = np.squeeze(parse_mask)
|
| 118 |
if parse_mask_2d.ndim != 2:
|
| 119 |
+
return face_img
|
|
|
|
| 120 |
|
| 121 |
h, w = face_img.shape[:2]
|
| 122 |
if parse_mask_2d.shape[:2] != (h, w):
|
|
|
|
| 124 |
else:
|
| 125 |
parse_mask_res = parse_mask_2d
|
| 126 |
|
| 127 |
+
# Teeth / Mouth opening mask (index 11 in facexlib / BiSeNet)
|
| 128 |
+
teeth_mask = (parse_mask_res == 11).astype(np.uint8)
|
| 129 |
+
if not np.any(teeth_mask):
|
| 130 |
+
return face_img
|
| 131 |
+
|
| 132 |
+
# Convert to LAB color space
|
| 133 |
+
lab = cv2.cvtColor(face_img, cv2.COLOR_BGR2LAB).astype(np.float32)
|
| 134 |
+
l, a, b = cv2.split(lab)
|
| 135 |
+
|
| 136 |
+
# Mask smoothing for seamless transition
|
| 137 |
+
teeth_mask_float = cv2.GaussianBlur(teeth_mask.astype(np.float32), (5, 5), 0)
|
| 138 |
+
|
| 139 |
+
# In LAB: b > 128 is yellow, b < 128 is blue. Desaturate yellow cast:
|
| 140 |
+
b_diff = b - 128.0
|
| 141 |
+
b_corrected = b - (b_diff * strength * 0.70)
|
| 142 |
+
|
| 143 |
+
# Gently lift lightness L on teeth (boost bright teeth pixels safely)
|
| 144 |
+
l_boost = l + ((255.0 - l) * strength * 0.18)
|
| 145 |
+
|
| 146 |
+
l_final = l * (1.0 - teeth_mask_float) + l_boost * teeth_mask_float
|
| 147 |
+
b_final = b * (1.0 - teeth_mask_float) + b_corrected * teeth_mask_float
|
| 148 |
+
|
| 149 |
+
lab_whitened = cv2.merge([l_final, a, b_final])
|
| 150 |
+
lab_whitened = np.clip(lab_whitened, 0, 255).astype(np.uint8)
|
| 151 |
+
return cv2.cvtColor(lab_whitened, cv2.COLOR_LAB2BGR)
|
| 152 |
+
except Exception as e:
|
| 153 |
+
print(f"[WinkEnhancer] Teeth whitening warning: {e}")
|
| 154 |
+
return face_img
|
| 155 |
+
|
| 156 |
+
def brighten_eyes_and_sclera(self, face_img: np.ndarray, parse_mask: np.ndarray = None, strength: float = 0.35) -> np.ndarray:
|
| 157 |
+
"""
|
| 158 |
+
Studio Ocular Catchlight & Sclera Glow:
|
| 159 |
+
Boosts iris specular highlights (catchlights) and clarifies eye whites without over-sharpening.
|
| 160 |
+
"""
|
| 161 |
+
if strength <= 0.0 or parse_mask is None or face_img is None:
|
| 162 |
+
return face_img
|
| 163 |
+
|
| 164 |
+
try:
|
| 165 |
+
parse_mask_2d = np.squeeze(parse_mask)
|
| 166 |
+
if parse_mask_2d.ndim != 2:
|
| 167 |
+
return face_img
|
| 168 |
+
|
| 169 |
+
h, w = face_img.shape[:2]
|
| 170 |
+
if parse_mask_2d.shape[:2] != (h, w):
|
| 171 |
+
parse_mask_res = cv2.resize(parse_mask_2d.astype(np.uint8), (w, h), interpolation=cv2.INTER_NEAREST)
|
| 172 |
+
else:
|
| 173 |
+
parse_mask_res = parse_mask_2d
|
| 174 |
+
|
| 175 |
+
eye_mask = ((parse_mask_res == 4) | (parse_mask_res == 5)).astype(np.uint8)
|
| 176 |
+
if not np.any(eye_mask):
|
| 177 |
+
return face_img
|
| 178 |
+
|
| 179 |
+
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
|
| 180 |
+
eye_mask_dilated = cv2.dilate(eye_mask, kernel, iterations=1)
|
| 181 |
+
eye_mask_float = cv2.GaussianBlur(eye_mask_dilated.astype(np.float32), (3, 3), 0)[:, :, np.newaxis]
|
| 182 |
+
|
| 183 |
+
# In LAB color space: apply CLAHE for iris depth and specular highlights
|
| 184 |
+
lab = cv2.cvtColor(face_img, cv2.COLOR_BGR2LAB)
|
| 185 |
+
l, a, b = cv2.split(lab)
|
| 186 |
+
l_clahe = self.clahe_eye.apply(l)
|
| 187 |
+
|
| 188 |
+
# Specular catchlight amplification on bright reflections
|
| 189 |
+
bright_catchlight = (l > 190).astype(np.float32)
|
| 190 |
+
l_sparkle = np.clip(l.astype(np.float32) + bright_catchlight * (35.0 * strength), 0, 255).astype(np.uint8)
|
| 191 |
+
l_enhanced = np.where(eye_mask_dilated == 1, cv2.addWeighted(l_clahe, 0.7, l_sparkle, 0.3, 0), l)
|
| 192 |
+
|
| 193 |
+
lab_eye = cv2.merge([l_enhanced, a, b])
|
| 194 |
+
eye_bgr = cv2.cvtColor(lab_eye, cv2.COLOR_LAB2BGR)
|
| 195 |
+
|
| 196 |
+
# High-pass subtle micro-sharpening on eye details (eyelashes, pupils)
|
| 197 |
+
eye_blur = cv2.GaussianBlur(eye_bgr, (3, 3), 0)
|
| 198 |
+
eye_sharp = cv2.addWeighted(eye_bgr, 1.0 + (0.35 * strength), eye_blur, -(0.35 * strength), 0)
|
| 199 |
+
|
| 200 |
+
result = (face_img.astype(np.float32) * (1.0 - eye_mask_float) + eye_sharp.astype(np.float32) * eye_mask_float)
|
| 201 |
+
return np.clip(result, 0, 255).astype(np.uint8)
|
| 202 |
+
except Exception as e:
|
| 203 |
+
print(f"[WinkEnhancer] Eye brightness warning: {e}")
|
| 204 |
+
return face_img
|
| 205 |
+
|
| 206 |
+
def balance_portrait_lighting_and_tone(self, face_img: np.ndarray, strength: float = 0.25) -> np.ndarray:
|
| 207 |
+
"""
|
| 208 |
+
Studio Lighting & Skin Glow Tone Balancer:
|
| 209 |
+
Performs Gray-World auto white balance and shadows recovery with healthy skin radiance.
|
| 210 |
+
"""
|
| 211 |
+
if strength <= 0.0 or face_img is None:
|
| 212 |
+
return face_img
|
| 213 |
+
|
| 214 |
+
try:
|
| 215 |
+
# 1. Shades of Gray Auto White Balance
|
| 216 |
+
img_f = face_img.astype(np.float32)
|
| 217 |
+
mean_b = np.mean(img_f[:, :, 0]) + 1e-5
|
| 218 |
+
mean_g = np.mean(img_f[:, :, 1]) + 1e-5
|
| 219 |
+
mean_r = np.mean(img_f[:, :, 2]) + 1e-5
|
| 220 |
+
mean_gray = (mean_b + mean_g + mean_r) / 3.0
|
| 221 |
+
|
| 222 |
+
scale_b = 1.0 + ((mean_gray / mean_b) - 1.0) * (strength * 0.6)
|
| 223 |
+
scale_g = 1.0 + ((mean_gray / mean_g) - 1.0) * (strength * 0.6)
|
| 224 |
+
scale_r = 1.0 + ((mean_gray / mean_r) - 1.0) * (strength * 0.6)
|
| 225 |
+
|
| 226 |
+
wb_img = np.zeros_like(img_f)
|
| 227 |
+
wb_img[:, :, 0] = img_f[:, :, 0] * scale_b
|
| 228 |
+
wb_img[:, :, 1] = img_f[:, :, 1] * scale_g
|
| 229 |
+
wb_img[:, :, 2] = img_f[:, :, 2] * scale_r
|
| 230 |
+
wb_img = np.clip(wb_img, 0, 255).astype(np.uint8)
|
| 231 |
+
|
| 232 |
+
# 2. LAB Shadow Lift & Radiance Glow
|
| 233 |
+
lab = cv2.cvtColor(wb_img, cv2.COLOR_BGR2LAB).astype(np.float32)
|
| 234 |
+
l, a, b = cv2.split(lab)
|
| 235 |
+
|
| 236 |
+
# Quadratic shadow recovery curve: lifts dark shadows while protecting highlights
|
| 237 |
+
shadow_boost = (l * (255.0 - l) / 255.0) * (strength * 0.25)
|
| 238 |
+
l_lifted = np.clip(l + shadow_boost, 0, 255)
|
| 239 |
+
|
| 240 |
+
# Subtle pink/warm skin tone enrichment (slight a-channel boost)
|
| 241 |
+
a_glow = np.clip(a + (strength * 2.0), 0, 255)
|
| 242 |
+
|
| 243 |
+
balanced_lab = cv2.merge([l_lifted, a_glow, b])
|
| 244 |
+
balanced_bgr = cv2.cvtColor(balanced_lab.astype(np.uint8), cv2.COLOR_LAB2BGR)
|
| 245 |
+
return cv2.addWeighted(face_img, 1.0 - strength, balanced_bgr, strength, 0)
|
| 246 |
+
except Exception as e:
|
| 247 |
+
print(f"[WinkEnhancer] Portrait lighting balance warning: {e}")
|
| 248 |
+
return face_img
|
| 249 |
+
|
| 250 |
+
def enhance_eyes_and_lips(self, face_img: np.ndarray, parse_mask: np.ndarray = None, enable_eyes: bool = True, enable_lips: bool = True, enable_teeth: bool = True, enable_tone_glow: bool = True) -> np.ndarray:
|
| 251 |
+
"""
|
| 252 |
+
Enhance eyes (catchlight sparkle), teeth (whitening), lips and overall studio skin glow.
|
| 253 |
+
"""
|
| 254 |
+
result = face_img.copy()
|
| 255 |
+
|
| 256 |
+
# 1. Studio Lighting and Skin Glow Tone Balance
|
| 257 |
+
if enable_tone_glow:
|
| 258 |
+
result = self.balance_portrait_lighting_and_tone(result, strength=0.25)
|
| 259 |
+
|
| 260 |
+
# 2. Teeth Whitening
|
| 261 |
+
if enable_teeth and parse_mask is not None:
|
| 262 |
+
result = self.whiten_teeth(result, parse_mask, strength=0.35)
|
| 263 |
+
|
| 264 |
+
# 3. Ocular Catchlight & Sclera Glow
|
| 265 |
+
if enable_eyes and parse_mask is not None:
|
| 266 |
+
result = self.brighten_eyes_and_sclera(result, parse_mask, strength=0.35)
|
| 267 |
+
|
| 268 |
+
if parse_mask is None:
|
| 269 |
+
# Fallback: General soft unsharp mask on entire face
|
| 270 |
+
blur = cv2.GaussianBlur(result, (0, 0), 2.0)
|
| 271 |
+
return cv2.addWeighted(result, 1.15, blur, -0.15, 0)
|
| 272 |
+
|
| 273 |
+
try:
|
| 274 |
+
parse_mask_2d = np.squeeze(parse_mask)
|
| 275 |
+
if parse_mask_2d.ndim != 2:
|
| 276 |
+
blur = cv2.GaussianBlur(result, (0, 0), 2.0)
|
| 277 |
+
return cv2.addWeighted(result, 1.15, blur, -0.15, 0)
|
| 278 |
+
|
| 279 |
+
h, w = result.shape[:2]
|
| 280 |
+
if parse_mask_2d.shape[:2] != (h, w):
|
| 281 |
+
parse_mask_res = cv2.resize(parse_mask_2d.astype(np.uint8), (w, h), interpolation=cv2.INTER_NEAREST)
|
| 282 |
+
else:
|
| 283 |
+
parse_mask_res = parse_mask_2d
|
| 284 |
|
| 285 |
# Facial feature mask IDs in facexlib:
|
| 286 |
+
# 12: Upper Lip, 13: Lower Lip
|
| 287 |
+
lip_mask = ((parse_mask_res == 12) | (parse_mask_res == 13)).astype(np.uint8)
|
| 288 |
+
|
| 289 |
+
# 4. Enhance Lips: Subtle natural saturation & contrast boost
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
if enable_lips and np.any(lip_mask):
|
| 291 |
lip_mask_float = cv2.GaussianBlur(lip_mask.astype(np.float32), (3, 3), 0)[:, :, np.newaxis]
|
| 292 |
hsv = cv2.cvtColor(result, cv2.COLOR_BGR2HSV).astype(np.float32)
|
| 293 |
+
h_ch, s_ch, v_ch = cv2.split(hsv)
|
| 294 |
+
s_boost = np.clip(s_ch * 1.15, 0, 255)
|
| 295 |
+
v_boost = np.clip(v_ch * 1.05, 0, 255)
|
| 296 |
+
hsv_lip = cv2.merge([h_ch, s_boost, v_boost]).astype(np.uint8)
|
| 297 |
+
lip_bgr = cv2.cvtColor(hsv_lip, cv2.COLOR_HSV2BGR)
|
| 298 |
+
result = (result.astype(np.float32) * (1.0 - lip_mask_float) + lip_bgr.astype(np.float32) * lip_mask_float).astype(np.uint8)
|
| 299 |
+
|
| 300 |
+
return np.clip(result, 0, 255).astype(np.uint8)
|
| 301 |
except Exception as e:
|
| 302 |
+
print(f"[WinkEnhancer] Organ enhancement warning: {e}")
|
| 303 |
+
return result
|
| 304 |
|
| 305 |
def balance_skin_tone_lab(self, face_img: np.ndarray) -> np.ndarray:
|
| 306 |
"""
|
|
|
|
| 387 |
print(f"[WinkEnhancer] Adaptive sharpening warning: {e}")
|
| 388 |
return img
|
| 389 |
|
| 390 |
+
def enhance_face(self, restored_face: np.ndarray, cropped_original: np.ndarray = None, parse_mask: np.ndarray = None, wink_mode: bool = True, eye_enhancement: bool = True, skin_grain: float = 0.15, color_match: bool = True, enable_eyes: bool = True, enable_lips: bool = True, enable_skin: bool = True, enable_teeth: bool = True, enable_tone_glow: bool = True, sharpen_amount: float = 0.2) -> np.ndarray:
|
| 391 |
"""
|
| 392 |
Master method to execute Wink-level enhancement pipeline on a restored face crop.
|
| 393 |
"""
|
|
|
|
| 396 |
|
| 397 |
out_face = restored_face.copy()
|
| 398 |
|
| 399 |
+
# Step A: Studio Lighting and Skin Glow Tone Balance (Auto White Balance & Radiance)
|
| 400 |
+
if enable_tone_glow:
|
| 401 |
+
out_face = self.balance_portrait_lighting_and_tone(out_face, strength=0.25)
|
| 402 |
+
|
| 403 |
+
# Step B: Reinhard Color Transfer (Auto Skin Tone Alignment to original face/neck)
|
| 404 |
if color_match and cropped_original is not None:
|
| 405 |
out_face = self.match_color_reinhard(out_face, cropped_original, blend=0.4)
|
| 406 |
|
| 407 |
+
# Step C: Skin tone & micro-contrast balance
|
| 408 |
out_face = self.balance_skin_tone_lab(out_face)
|
| 409 |
|
| 410 |
+
# Step D: Eye (sparkle), Teeth (whitening) & Lip local enhancement
|
| 411 |
+
if eye_enhancement and (enable_eyes or enable_lips or enable_teeth):
|
| 412 |
+
out_face = self.enhance_eyes_and_lips(
|
| 413 |
+
out_face,
|
| 414 |
+
parse_mask=parse_mask,
|
| 415 |
+
enable_eyes=enable_eyes,
|
| 416 |
+
enable_lips=enable_lips,
|
| 417 |
+
enable_teeth=enable_teeth,
|
| 418 |
+
enable_tone_glow=False
|
| 419 |
+
)
|
| 420 |
+
|
| 421 |
+
# Step E: Multi-Scale Edge-Aware Adaptive Sharpening
|
| 422 |
if sharpen_amount > 0.0:
|
| 423 |
out_face = self.apply_adaptive_sharpening(out_face, sharpen_amount=sharpen_amount)
|
| 424 |
|
| 425 |
+
# Step F: Real Skin Grain Injection (Frequency Separation)
|
| 426 |
if enable_skin and skin_grain > 0.0 and cropped_original is not None:
|
| 427 |
out_face = self.apply_skin_grain(out_face, cropped_original, skin_mask=parse_mask, grain_amount=skin_grain)
|
| 428 |
|