Skip to content

Commit 68c70fe

Browse files
CEQ151CEQ151
authored andcommitted
fix(mlwm-runtime): disable untrained neural sync template
1 parent 44e33fd commit 68c70fe

2 files changed

Lines changed: 34 additions & 11 deletions

File tree

blind_watermark/rwm_engine.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,15 @@
8787
NEURAL_PROFILES = {
8888
'balanced': {
8989
'residual_strength': 1.0,
90-
'template_strength': 0.008,
91-
'template_peaks': 96,
90+
'template_strength': 0.0,
91+
'template_peaks': 0,
92+
'sync_enabled': False,
9293
},
9394
'aggressive': {
9495
'residual_strength': 1.35,
95-
'template_strength': 0.012,
96-
'template_peaks': 128,
96+
'template_strength': 0.0,
97+
'template_peaks': 0,
98+
'sync_enabled': False,
9799
},
98100
}
99101

@@ -607,11 +609,12 @@ def _neural_embed_impl(img, text, password=1, quality='balanced', models_dir=Non
607609
rgb_watermarked = apply_neural_residual(rgb, encoded['residual'], strength=profile['residual_strength'])
608610
out = cv2.cvtColor(rgb_watermarked, cv2.COLOR_RGB2BGR).astype(np.float64)
609611

610-
gray = cv2.cvtColor(out.astype(np.uint8), cv2.COLOR_BGR2GRAY).astype(np.float64)
611-
gray_sync = _embed_template(gray, _seed(password), profile['template_strength'], profile['template_peaks'])
612-
total_diff = gray_sync - gray
613-
for c in range(3):
614-
out[:, :, c] += total_diff
612+
if profile.get('sync_enabled', False) and profile.get('template_strength', 0.0) > 0.0:
613+
gray = cv2.cvtColor(out.astype(np.uint8), cv2.COLOR_BGR2GRAY).astype(np.float64)
614+
gray_sync = _embed_template(gray, _seed(password), profile['template_strength'], profile['template_peaks'])
615+
total_diff = gray_sync - gray
616+
for c in range(3):
617+
out[:, :, c] += total_diff
615618
out = np.clip(np.round(out), 0, 255).astype(np.uint8)
616619

617620
if alpha is not None:
@@ -646,7 +649,13 @@ def _neural_extract_impl(img, password=1, quality='balanced', models_dir=None):
646649
except ImportError:
647650
from mlwm.infer import NeuralRuntimeUnavailable, neural_decode_views
648651

649-
corrected, geo = _rectify_neural_image(img[:, :, :3] if img.ndim == 3 else img, password)
652+
profile_name = _resolve_neural_profile(quality)
653+
profile = NEURAL_PROFILES[profile_name]
654+
if profile.get('sync_enabled', False):
655+
corrected, geo = _rectify_neural_image(img[:, :, :3] if img.ndim == 3 else img, password)
656+
else:
657+
corrected = img[:, :, :3] if img.ndim == 3 else img
658+
geo = {'angle': 0.0, 'scale': 1.0, 'confidence': 0.0, 'peaks': 0, 'syncEnabled': False}
650659
views = _build_neural_views(corrected)
651660
try:
652661
decoded = neural_decode_views(views, models_dir=models_dir, use_cuda=False)
@@ -661,7 +670,7 @@ def _neural_extract_impl(img, password=1, quality='balanced', models_dir=None):
661670
'fallback_used': False,
662671
'confidence': confidence,
663672
'diagnostics': {
664-
'profile': _resolve_neural_profile(quality),
673+
'profile': profile_name,
665674
'bitConfidence': float(decoded.get('bitConfidence', 0.0)),
666675
'decodeStrategy': decoded.get('strategy'),
667676
'geometricCorrection': geo,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import unittest
2+
3+
from blind_watermark import rwm_engine
4+
5+
6+
class MlwmRuntimeTests(unittest.TestCase):
7+
def test_alpha1_neural_profiles_do_not_inject_untrained_sync_template(self):
8+
for profile in rwm_engine.NEURAL_PROFILES.values():
9+
self.assertFalse(profile.get('sync_enabled', False))
10+
self.assertEqual(profile.get('template_strength'), 0.0)
11+
12+
13+
if __name__ == '__main__':
14+
unittest.main()

0 commit comments

Comments
 (0)