mirror of
https://github.com/wiltodelta/remove-ai-watermarks.git
synced 2026-06-05 02:28:00 +02:00
bc3228d387
Add deterministic, CPU-only removal of the visible Doubao "豆包AI生成" mark and
a position-agnostic region eraser for any other visible watermark/logo.
- doubao_engine.py: locate (geometry, scales with width) + polarity-aware
white-top-hat glyph mask + cv2 inpaint; coverage-gated detection and a
dense-text safety guard. No GPU, ~30ms.
- region_eraser.py + `erase` command: inpaint arbitrary --region box(es).
Default cv2 backend (no deps); optional big-LaMa via onnxruntime (`lama`
extra, Carve/LaMa-ONNX, model downloaded on first use, never bundled).
- cli `visible --mark auto|gemini|doubao`: auto routes by detector confidence.
- tests for both engines; seed previously-unseeded CLI image fixtures to stop
the Doubao detector flaking on random corners.
- .gitignore: doubao_capture/{seeds,captures} scratch (alpha-map calibration).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
76 lines
2.8 KiB
Python
76 lines
2.8 KiB
Python
"""Tests for the universal region eraser."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
from remove_ai_watermarks.region_eraser import boxes_to_mask, erase, lama_available
|
|
|
|
|
|
class TestBoxesToMask:
|
|
def test_mask_set_inside_box(self):
|
|
mask = boxes_to_mask((100, 100), [(10, 20, 30, 40)], dilate=0)
|
|
assert mask[25, 15] == 255 # inside
|
|
assert mask[0, 0] == 0 # outside
|
|
assert mask.shape == (100, 100)
|
|
|
|
def test_multiple_boxes(self):
|
|
mask = boxes_to_mask((100, 100), [(0, 0, 10, 10), (90, 90, 10, 10)], dilate=0)
|
|
assert mask[5, 5] == 255
|
|
assert mask[95, 95] == 255
|
|
assert mask[50, 50] == 0
|
|
|
|
def test_dilate_grows_mask(self):
|
|
m0 = boxes_to_mask((100, 100), [(40, 40, 10, 10)], dilate=0)
|
|
m5 = boxes_to_mask((100, 100), [(40, 40, 10, 10)], dilate=5)
|
|
assert m5.sum() > m0.sum()
|
|
|
|
def test_box_clipped_to_bounds(self):
|
|
# box partly outside the image must not raise and stays in-bounds
|
|
mask = boxes_to_mask((50, 50), [(40, 40, 100, 100)], dilate=0)
|
|
assert mask[45, 45] == 255
|
|
|
|
|
|
class TestEraseCv2:
|
|
def _image_with_logo(self) -> tuple[np.ndarray, tuple[int, int, int, int]]:
|
|
img = np.full((200, 200, 3), 120, np.uint8) # flat gray background
|
|
box = (140, 160, 50, 30)
|
|
x, y, w, h = box
|
|
img[y : y + h, x : x + w] = (255, 255, 255) # bright "logo"
|
|
return img, box
|
|
|
|
def test_erase_changes_region(self):
|
|
img, box = self._image_with_logo()
|
|
out = erase(img, boxes=[box], backend="cv2")
|
|
x, y, w, h = box
|
|
# on a flat background the logo region should be repainted near gray
|
|
region = out[y : y + h, x : x + w]
|
|
assert abs(float(region.mean()) - 120) < 20
|
|
assert not np.array_equal(out, img)
|
|
|
|
def test_pixels_outside_box_untouched(self):
|
|
img, box = self._image_with_logo()
|
|
out = erase(img, boxes=[box], backend="cv2", dilate=0)
|
|
# a far corner must be identical
|
|
assert np.array_equal(img[:50, :50], out[:50, :50])
|
|
|
|
def test_no_boxes_returns_copy(self):
|
|
img = np.full((100, 100, 3), 50, np.uint8)
|
|
out = erase(img, boxes=[], backend="cv2")
|
|
assert np.array_equal(img, out)
|
|
|
|
def test_empty_mask_returns_copy(self):
|
|
img = np.full((100, 100, 3), 50, np.uint8)
|
|
out = erase(img, mask=np.zeros((100, 100), np.uint8), backend="cv2")
|
|
assert np.array_equal(img, out)
|
|
|
|
|
|
class TestLamaBackend:
|
|
def test_lama_raises_when_unavailable(self):
|
|
img = np.full((100, 100, 3), 50, np.uint8)
|
|
if lama_available():
|
|
pytest.skip("onnxruntime installed; cannot test the unavailable path")
|
|
with pytest.raises(RuntimeError, match="onnxruntime"):
|
|
erase(img, boxes=[(10, 10, 20, 20)], backend="lama")
|