mirror of
https://github.com/wiltodelta/remove-ai-watermarks.git
synced 2026-08-09 23:50:40 +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>
162 lines
5.7 KiB
Python
162 lines
5.7 KiB
Python
"""Registries shared by metadata extraction and provenance classification."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
def _tokens(value: str) -> tuple[str, ...]:
|
|
return tuple(value.split("|"))
|
|
|
|
|
|
SUPPORTED_FORMATS = frozenset(_tokens(".png|.jpg|.jpeg|.webp|.heic|.heif|.avif"))
|
|
AI_METADATA_KEYS = _tokens(
|
|
"parameters|postprocessing|extras|workflow|prompt|Dream|SD:mode|StableDiffusionVersion|"
|
|
"generation_time|Model|Model hash|Seed"
|
|
)
|
|
AI_KEYWORDS = _tokens(
|
|
"prompt|negative_prompt|sampler|cfg_scale|lora|diffusion|comfy|midjourney|dall-e|dalle|imagen|firefly|c2pa|chatgpt|gpt-4|sora|openai|truepic|stable_diffusion|invokeai"
|
|
)
|
|
|
|
PNG_SIGNATURE = b"\x89PNG\r\n\x1a\n"
|
|
C2PA_CHUNK_TYPE = b"caBX"
|
|
C2PA_SIGNATURES = tuple(
|
|
token.encode() for token in _tokens("c2pa|C2PA|jumb|jumd|JUMBF|jumbf|cbor|contentcreds|digid|assertions|manifest")
|
|
)
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class C2paAiVendor:
|
|
"""One issuer signature and its normalized product attribution."""
|
|
|
|
issuer: bytes
|
|
org: str
|
|
platform: str | None
|
|
needle: str | None
|
|
synthid: bool = False
|
|
asserts_ai: bool = False
|
|
|
|
|
|
def _vendor(
|
|
issuer: bytes | str,
|
|
org: str,
|
|
platform: str | None,
|
|
needle: str | None,
|
|
*,
|
|
synthid: bool = False,
|
|
asserts_ai: bool = False,
|
|
) -> C2paAiVendor:
|
|
token = issuer.encode() if isinstance(issuer, str) else issuer
|
|
return C2paAiVendor(token, org, platform, needle, synthid, asserts_ai)
|
|
|
|
|
|
# Order is product priority when a manifest mentions more than one organization.
|
|
C2PA_AI_VENDORS: tuple[C2paAiVendor, ...] = (
|
|
_vendor(b"Microsoft", "Microsoft", "Microsoft (Bing Image Creator / Designer)", "Microsoft"),
|
|
_vendor(b"Adobe", "Adobe", "Adobe Firefly", "Adobe"),
|
|
_vendor(b"OpenAI", "OpenAI", "OpenAI (ChatGPT / gpt-image / DALL-E / Sora)", "OpenAI", synthid=True),
|
|
_vendor(b"Google", "Google LLC", "Google (Gemini / Imagen)", "Google", synthid=True),
|
|
_vendor(b"Stability AI", "Stability AI", "Stability AI (Stable Image / DreamStudio)", "Stability AI"),
|
|
_vendor(b"Black Forest Labs", "Black Forest Labs", "Black Forest Labs (FLUX)", "Black Forest Labs"),
|
|
_vendor(b"volcengine", "ByteDance (Volcano Engine)", "ByteDance (Doubao / Jimeng / Volcano Engine)", "ByteDance"),
|
|
_vendor(
|
|
"北京火山引擎科技有限公司",
|
|
"ByteDance (Volcano Engine)",
|
|
"ByteDance (Doubao / Jimeng / Volcano Engine)",
|
|
"ByteDance",
|
|
),
|
|
_vendor(b"Byteplus", "BytePlus (ByteDance)", "ByteDance (Doubao / Jimeng / Volcano Engine)", "ByteDance"),
|
|
_vendor(
|
|
b"Dreamina",
|
|
"ByteDance (Dreamina)",
|
|
"ByteDance (Doubao / Jimeng / Volcano Engine)",
|
|
"ByteDance",
|
|
asserts_ai=True,
|
|
),
|
|
_vendor(b"Canva", "Canva", "Canva (Magic Media)", "Canva"),
|
|
_vendor(b"Eleven Labs", "ElevenLabs", "ElevenLabs", "ElevenLabs"),
|
|
_vendor(b"fal-ai", "fal.ai", "fal.ai", "fal.ai", asserts_ai=True),
|
|
_vendor(b"Bria", "Bria Artificial Intelligence", "Bria AI", "Bria", asserts_ai=True),
|
|
_vendor(b"Truepic", "Truepic", None, None),
|
|
)
|
|
|
|
C2PA_ISSUERS = {vendor.issuer: vendor.org for vendor in C2PA_AI_VENDORS}
|
|
C2PA_IDENTITY_AI_ORGS = frozenset(vendor.org for vendor in C2PA_AI_VENDORS if vendor.asserts_ai)
|
|
SYNTHID_C2PA_ISSUERS = frozenset(vendor.issuer for vendor in C2PA_AI_VENDORS if vendor.synthid)
|
|
|
|
C2PA_AI_TOOLS = {
|
|
token.encode(): label
|
|
for token, label in (
|
|
("GPT-4o", "GPT-4o"),
|
|
("ChatGPT", "ChatGPT"),
|
|
("Sora", "Sora"),
|
|
("DALL-E", "DALL-E"),
|
|
("DALL", "DALL-E"),
|
|
("Imagen", "Imagen"),
|
|
("Firefly", "Firefly"),
|
|
)
|
|
}
|
|
|
|
C2PA_SOFT_BINDINGS = {
|
|
b"com.adobe.trustmark": "Adobe TrustMark",
|
|
b"com.adobe.icn": "Adobe (content fingerprint)",
|
|
b"com.digimarc": "Digimarc",
|
|
b"com.imatag.lamark": "Imatag (Lamark)",
|
|
b"ai.steg": "Steg.AI",
|
|
b"com.microsoft.invismark": "Microsoft InvisMark",
|
|
b"com.microsoft.wavmark": "Microsoft WavMark",
|
|
b"com.verimatrix": "Verimatrix",
|
|
b"com.nagra.nexguard": "NAGRA NexGuard",
|
|
b"com.aiwatermark": "AIWatermark (Meta PixelSeal)",
|
|
b"ai.trufo": "Trufo",
|
|
b"app.overlai": "Overlai",
|
|
b"com.markany": "MarkAny",
|
|
b"com.mentaport": "Mentaport",
|
|
b"es.lumatrace": "LumaTrace",
|
|
b"ai.verda": "VerdaAI",
|
|
b"ai.contentlens": "ContentLens",
|
|
b"io.iscc": "ISCC (content code)",
|
|
}
|
|
|
|
AI_GENERATOR_TOKENS = frozenset(
|
|
{
|
|
"firefly",
|
|
"dall-e",
|
|
"dalle",
|
|
"midjourney",
|
|
"stable diffusion",
|
|
"stable-diffusion",
|
|
"stablediffusion",
|
|
"comfyui",
|
|
"automatic1111",
|
|
"invokeai",
|
|
"imagen",
|
|
"gpt-image",
|
|
"nightcafe",
|
|
"ideogram",
|
|
"leonardo",
|
|
"flux",
|
|
"dreamstudio",
|
|
"novelai",
|
|
"reve.com",
|
|
"aphrodite ai",
|
|
"apple photos clean up",
|
|
"fal-ai",
|
|
}
|
|
)
|
|
|
|
_C2PA_ACTION_NAMES = _tokens("created|converted|edited|filtered|cropped|resized|opened|placed")
|
|
C2PA_ACTIONS = {f"c2pa.{action}".encode(): action for action in _C2PA_ACTION_NAMES}
|
|
|
|
|
|
# TC260 producer identity -> the mark key whose vendor signs with it now lives on the
|
|
# registry rows (``KnownMark.tc260_producer_codes``, read through
|
|
# ``watermark_registry.tc260_producer_vendors``). Keeping the codes beside the mark is
|
|
# what stops a newly registered TC260 vendor from silently falling back to ByteDance.
|
|
#
|
|
# What a TC260 label confirms when its producer is absent or unmapped. Historical
|
|
# behaviour, kept as the fallback so an unrecognized producer never regresses to no
|
|
# relaxation at all: ByteDance's two products are the ones the relaxed band was
|
|
# calibrated on (see _text_mark_engine._DEFAULT_PROVENANCE_NCC_FACTOR).
|
|
TC260_FALLBACK_VENDORS: frozenset[str] = frozenset({"doubao", "jimeng"})
|