sd-comic-lettering / tests /test_detect_measured.py
Kazzze's picture
Update to current version
55306cf verified
Raw History Blame Contribute Delete
4.95 kB
"""The bubble rule against measurements taken from generated scenes.
Each row is what detect.scan() measured on a region of a generated image: real bubbles the rule
used to miss (jagged ones full of shouted letters, dark ones with light letters, tiny ones), and
the regions it used to take for bubbles or nearly did (faces, a wall, an open mouth, a hood, a
strip of white ground beside a dark dress). The images themselves are not kept.
Run from the repository folder: python -m pytest tests
"""
import os
import sys
import numpy as np
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from comic_lettering.detect import is_bubble # noqa: E402
class Found(object):
def __init__(self, color, dark, box):
self.color = color
self.dark = dark
self.filled = np.ones(box, dtype=bool)
def item(share, flatness, solidity, extent, outline, body, color, contrast=100.0, box=(40, 40)):
dark = max(color) <= 60
return {"bubble": Found(tuple(color), dark, box), "share": share, "flatness": flatness, "solidity": solidity,
"extent": extent, "outline": outline, "body": body, "line_contrast": contrast}
# share, flatness, solidity, extent, outline, body, fill color[, line contrast, box (height, width)]
MISSED = {
"jagged shout, letters to the edge": (0.050, 1.14, 0.78, 0.61, 0.99, 0.86, (223, 213, 200)),
"long spikes": (0.024, 1.25, 0.55, 0.40, 0.99, 0.65, (224, 217, 210)),
"spiky shout": (0.054, 1.29, 0.64, 0.57, 0.99, 0.76, (230, 221, 205)),
"jagged with two lines of letters": (0.018, 1.59, 0.71, 0.65, 1.00, 0.87, (230, 221, 205)),
"cream burst of huge letters": (0.068, 2.02, 0.74, 0.63, 0.967, 0.76, (228, 210, 183), 100.0, (452, 244)),
"cream oval": (0.033, 1.07, 0.96, 0.79, 1.00, 0.99, (243, 226, 206)),
"small burst": (0.013, 1.47, 0.76, 0.56, 1.00, 0.95, (213, 202, 186)),
"rounded jagged": (0.024, 1.33, 0.90, 0.72, 1.00, 0.98, (219, 217, 205)),
# dark bubbles: on a light ground there is no line of their own, so the contrast is about 0
"black oval on a light ground": (0.020, 0.93, 0.96, 0.79, 0.99, 0.99, (3, 3, 3), 5.0),
"black message box": (0.015, 1.18, 1.00, 0.92, 1.00, 0.99, (4, 4, 5), 1.0),
"black bubble, broken white line": (0.047, 1.26, 0.90, 0.76, 0.76, 0.97, (4, 5, 5), 200.0),
"black bubble at a panel's edge": (0.054, 0.85, 0.99, 0.80, 0.83, 1.00, (3, 4, 4), 106.0),
"black cloud": (0.050, 1.05, 0.84, 0.60, 1.00, 1.00, (11, 11, 11), 107.0),
"black burst, faint line": (0.070, 0.89, 0.95, 0.81, 0.948, 0.97, (4, 5, 4), 77.0),
# a word or two
"tiny bubble": (0.0024, 1.05, 1.00, 0.79, 1.00, 0.99, (230, 223, 213)),
}
WRONG = {
"a face in a night scene": (0.035, 0.96, 0.88, 0.65, 0.82, 0.91, (127, 125, 135)),
"a gray wall patch": (0.020, 1.16, 0.96, 0.77, 0.93, 0.95, (127, 133, 132)),
"a face in a gray office": (0.018, 1.04, 0.89, 0.60, 0.91, 0.97, (179, 167, 147)),
"a small face": (0.0034, 1.33, 0.86, 0.62, 0.90, 0.88, (180, 168, 148)),
"white ground beside a dark dress": (0.029, 1.58, 0.73, 0.49, 0.99, 0.80, (196, 196, 194), 1.0, (522, 116)),
# Near misses from the scenes without bubbles: bright, but open, or dim and ragged.
"a lit window": (0.016, 3.06, 0.75, 0.52, 0.94, 0.93, (146, 138, 156)),
"sea foam": (0.011, 4.15, 0.56, 0.26, 0.00, 0.54, (174, 188, 181)),
"a plate": (0.067, 0.46, 0.79, 0.69, 0.00, 0.93, (248, 247, 240)),
"a shirt in shade": (0.014, 2.34, 0.82, 0.60, 0.79, 0.89, (144, 139, 155)),
# dark look-alikes
"an open mouth": (0.013, 0.97, 0.65, 0.49, 0.96, 0.93, (5, 5, 5), 31.0),
"a hood": (0.048, 3.80, 0.93, 0.74, 0.66, 0.97, (18, 24, 31), 36.0),
"a chalkboard": (0.025, 0.87, 0.87, 0.85, 0.75, 0.86, (23, 9, 7), 227.0),
"a grand piano": (0.031, 0.45, 0.91, 0.77, 0.54, 0.99, (6, 6, 6), 152.0),
"a dark monitor": (0.012, 1.09, 0.98, 0.94, 0.82, 0.98, (23, 28, 31), 33.0),
}
STILL_FOUND = {
"plain white oval": (0.040, 0.68, 0.99, 0.82, 1.00, 1.00, (215, 216, 215)),
"paper bubble": (0.042, 0.83, 0.93, 0.81, 1.00, 0.99, (223, 215, 200)),
"spiky bubble found before": (0.072, 0.90, 0.87, 0.72, 1.00, 0.98, (228, 217, 202)),
"dim bubble in a horror panel": (0.055, 1.26, 0.94, 0.79, 0.99, 0.97, (167, 173, 172)),
"black bubble with a white line": (0.025, 1.01, 0.96, 0.80, 1.00, 0.99, (3, 4, 4), 146.0),
}
def test_bubbles_from_the_corpus_are_found():
# type: () -> None
for name, row in MISSED.items():
assert is_bubble(item(*row)), name
def test_what_was_found_before_still_is():
# type: () -> None
for name, row in STILL_FOUND.items():
assert is_bubble(item(*row)), name
def test_look_alikes_are_not_bubbles():
# type: () -> None
for name, row in WRONG.items():
assert not is_bubble(item(*row)), name