mirror of
https://github.com/wiltodelta/remove-ai-watermarks.git
synced 2026-08-10 16:10:33 +02:00
The visible-mark path had grown three copies of one ladder sweep, four
near-identical `detect` arms, and four hand-rolled `footprint_mask` overrides;
mark knowledge sat in five hand-maintained tables across three modules; and the
flagship `all`/`batch` pipeline existed only in cli.py, written twice with
divergent behavior.
Detection is now one measurement. `_ladder_best` replaces the three sweeps,
`_scan`/`_verdict` replace the four arms, and the winning box travels to the
mask on `TextMarkDetection.match_box` instead of being swept a second time.
`detect_both` returns the strict and relaxed verdicts from one scan, which
halves the arbiter's perception cost (260 -> 130 matchTemplate calls on a 2048²
image, verdicts identical field for field). A per-mark demotion goes in the new
`_post_gate` hook, never in a `detect` override -- an override is invisible to
the single-pass path, which is how the RunningHub and Yuanbao anchor gates
briefly stopped applying.
Everything about a mark is now one registry row: product, label regime, the
platform sentence `identify` reports, the metadata signals that confirm it, and
its TC260 producer codes. `identify._VISIBLE_MARK_PLATFORM`, the signal mapping
in `api.visible_provenance`, `_PRODUCT_OF` and the pill veto are derived from
those rows.
`api.remove_all` / `api.remove_batch` are the library form of the `all` and
`batch` commands; the CLI is a wrapper that owns console text and exit codes.
Progress is a `(stage, detail)` pair of stable tokens, so the CLI keys its
wording off structure rather than parsing the library's prose back.
Two intentional behavior changes, both verified against a recorded 811-image
sample of detector verdicts, removal-mask hashes, arbiter decisions and
`identify` reports:
* A TC260 label now relaxes the vendor its `ContentProducer` names rather than
ByteDance's pair on every China-AIGC image. 333 of 811 samples move; on 185
of them the previously relaxed pair was simply the wrong vendor, and the
mark actually present never reached the relaxed gate its own
`provenance_ncc_factor` was calibrated for.
* A confident LibLibAI detection suppresses the Jimeng pill, like every other
TC260 product's mark. It was registered alongside RunningHub and Baidu, both
of which were added to the hand-written veto list, and it was not. 1 sample
moves, and it is exactly the co-firing case.
Nothing else in that record changes: detector verdicts, mask hashes and
`identify` verdicts are byte-identical, and all 200 calibration constants are
untouched.
Also: `aigc_label` and friends plus `extract_c2pa_info` are memoized on
(path, mtime_ns, size) -- size because this package rewrites in place; the
native TC260 container readers route on magic bytes instead of the file
extension, so a mislabeled AVI or FLV is no longer invisible; `identify` shares
one pixel decode between the DWT-DCT and visible stages (TrustMark keeps its own
Pillow decode, which is not substitutable); and the six `stabilize_*` video
wrappers collapse into one policy table.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
154 lines
6.1 KiB
Python
154 lines
6.1 KiB
Python
"""Baidu visible watermark detector/localizer.
|
|
|
|
Baidu stamps its generations with a white bold "百度" text run plus a separate
|
|
white rounded tag carrying dark "AI生成", bottom-right -- the China TC260
|
|
explicit AIGC label. Detection keys on the **百度 text run only**: a
|
|
two-component template (text + pill tag) was measured and REJECTED -- the solid
|
|
white pill is a bright-blob magnet and both front-ends scored the clean arm at
|
|
cohort levels (tophat clean p95 0.445 / gray clean p95 0.487 vs cohort ~0.5,
|
|
2026-07-22). The text-only silhouette separates cleanly (below). The white tag
|
|
is still removed with the mark: the fill blob covers both bright components in
|
|
the corner box.
|
|
|
|
Removal is the shared **localize -> fill** (:meth:`footprint_mask` ->
|
|
``region_eraser``). This module supplies only Baidu's tuned
|
|
:class:`TextMarkConfig` (``assets/baidu_alpha.png`` -- a font-rendered
|
|
synthetic silhouette from ``scripts/render_vendor_silhouettes.py``, never cut
|
|
from an upload).
|
|
|
|
The detector uses a synthetic silhouette, short-side geometry, a strict
|
|
confidence gate, and a Qwen rival margin. The footprint covers both the text
|
|
run and its adjacent pill tag.
|
|
"""
|
|
# The module-level _alpha_template / _glyph_silhouette / _template_match_score below
|
|
# are thin test-facing shims (imported by tests/), so pyright's src-only pass sees them
|
|
# as unused; the use is cross-module.
|
|
# pyright: reportUnusedFunction=false
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from remove_ai_watermarks import _text_mark_engine
|
|
from remove_ai_watermarks._text_mark_engine import (
|
|
TextMarkConfig,
|
|
TextMarkDetection,
|
|
TextMarkEngine,
|
|
TextMarkLocation,
|
|
)
|
|
|
|
if TYPE_CHECKING:
|
|
from numpy.typing import NDArray
|
|
|
|
# Locate geometry as a fraction of the image SHORT side (measured basis). The
|
|
# box covers the text run AND the pill tag to its right (tag right edge ~0.002
|
|
# off the frame edge, text run left edge ~0.19 off).
|
|
WM_WIDTH_FRAC = 0.25
|
|
WM_HEIGHT_FRAC = 0.07
|
|
MARGIN_RIGHT_FRAC = 0.002
|
|
MARGIN_BOTTOM_FRAC = 0.002
|
|
|
|
# Glyph appearance: white bold text on a usually-darker background (white
|
|
# top-hat), same overlay class as Doubao -- inherited, harmless because the
|
|
# tophat front-end turns these gates into weights.
|
|
MAX_SATURATION = 55
|
|
LOGO_MIN_LUMA = 150
|
|
TOPHAT_DELTA = 12
|
|
|
|
DETECT_MIN_COVERAGE = 0.04 # unused by the tophat front-end (kept for config parity)
|
|
# Calibrated against vendor, rival-mark, and clean compatibility examples.
|
|
# The Qwen rival margin handles visually similar marks; the threshold rejects
|
|
# remaining unrelated bottom-right text.
|
|
DETECT_NCC_THRESHOLD = 0.48
|
|
|
|
# Detection-silhouette geometry (fraction of the short side): the 百度 text run
|
|
# only, measured 0.090 wide with aspect 0.51.
|
|
_ALPHA_WIDTH_FRAC = 0.090
|
|
_ALPHA_HEIGHT_FRAC = 0.046
|
|
|
|
# Tight ladder: the NCC comb is sharp in size (see runninghub_engine), so the
|
|
# nominal sits exactly on the measured 0.090 with +-5% rungs.
|
|
_LADDER = (0.95, 1.0, 1.05)
|
|
|
|
_CONFIG = TextMarkConfig(
|
|
name="Baidu",
|
|
asset_name="baidu_alpha.png",
|
|
corner="br",
|
|
margin_floor=4,
|
|
width_frac=WM_WIDTH_FRAC,
|
|
height_frac=WM_HEIGHT_FRAC,
|
|
margin_x_frac=MARGIN_RIGHT_FRAC,
|
|
margin_bottom_frac=MARGIN_BOTTOM_FRAC,
|
|
max_saturation=MAX_SATURATION,
|
|
logo_min_luma=LOGO_MIN_LUMA,
|
|
tophat_delta=TOPHAT_DELTA,
|
|
morph_open_size=5,
|
|
detect_min_coverage=DETECT_MIN_COVERAGE,
|
|
detect_ncc_threshold=DETECT_NCC_THRESHOLD,
|
|
detect_frontend="tophat",
|
|
scale_basis="short",
|
|
ladder=_LADDER,
|
|
alpha_width_frac=_ALPHA_WIDTH_FRAC,
|
|
alpha_height_frac=_ALPHA_HEIGHT_FRAC,
|
|
min_gw=8,
|
|
# Load-bearing rival margins (crossfire measured 2026-07-22): the 百度 and
|
|
# 豆包 silhouettes share their second glyph and a similar first, and 百度 vs
|
|
# 千问 are near-identical after binarization -- at the 0.37 gate this
|
|
# template fires on 45.8% of 400 Doubao-marked frames AND on Qwen-marked
|
|
# frames at 0.38-0.43. Doubao's template beats it by ~0.56 on Doubao marks,
|
|
# Qwen's by 0.17-0.35 on Qwen marks, so the 0.10 margin suppresses all of
|
|
# that crossfire at zero genuine-Baidu cost (cohort fire+m == fire).
|
|
rivals=("doubao_alpha.png", "qwen_alpha.png"),
|
|
# STRICT ONLY: small cohort, the relaxed band is unmeasured.
|
|
provenance_ncc_factor=1.0,
|
|
)
|
|
|
|
|
|
def _alpha_template() -> NDArray[Any] | None:
|
|
"""The bundled Baidu alpha template (float [0,1]), or None."""
|
|
return _text_mark_engine.load_alpha_template(_CONFIG.asset_name)
|
|
|
|
|
|
class BaiduEngine(TextMarkEngine):
|
|
"""Detect/localize the visible Baidu "百度 AI生成" mark (bottom-right; localize -> fill)."""
|
|
|
|
def __init__(self) -> None:
|
|
super().__init__(_CONFIG)
|
|
|
|
def _footprint_rect(
|
|
self,
|
|
image: NDArray[Any],
|
|
loc: TextMarkLocation,
|
|
*,
|
|
force: bool,
|
|
detection: TextMarkDetection | None,
|
|
) -> tuple[int, int, int, int] | None:
|
|
"""Bound the fill by the detector's match box, never by the binary glyph blob.
|
|
|
|
The base class's blob-bbox footprint UNDERCOVERS this mark: the white tag's
|
|
flat interior gives no top-hat response (a top-hat answers edges, not flats),
|
|
so the blob ends at the text run and the fill leaves the tag's right half as
|
|
a ghost (measured 2026-07-22 on the 768x1024 cohort frame: blob bbox x
|
|
632..746 vs the tag ending ~758).
|
|
"""
|
|
return self._match_box_rect(image, loc, force=force, detection=detection)
|
|
|
|
def _extend_match_box(
|
|
self, box: tuple[int, int, int, int], loc: TextMarkLocation, frame: tuple[int, int]
|
|
) -> tuple[int, int, int, int]:
|
|
"""Extend the match box RIGHT to the corner end of the locate box.
|
|
|
|
The layout is measured and fixed: the text run is at the left of the locate
|
|
box and the tag runs to the corner, so the mark's right edge is the box's.
|
|
"""
|
|
gx0, gy0, _gx1, gy1 = box
|
|
bx, by, bw, bh = loc.bbox
|
|
h, w = frame
|
|
pad = max(4, int(0.15 * bh))
|
|
return (
|
|
max(0, bx + gx0 - pad),
|
|
max(0, by + gy0 - pad),
|
|
min(w, bx + bw), # the tag runs to the corner end of the box
|
|
min(h, by + gy1 + 1 + pad),
|
|
)
|