Spaces:
Sleeping
Sleeping
File size: 14,304 Bytes
5b3cd1e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | """The design test's scene: a design file printed and photographed in the size table's everyday scene, beside its
plain twin (the same content, version, error correction and mask, with a 4-square margin).
Each capture is a 1920 x 1440 grey JPEG of the printed sheet in a scene; design_frames.py shrinks it to the 1280 x 960
frame the scanners read. Its code gets `ppm` camera pixels per square in the 1280 frame, head-on at the code's centre,
before the 15-degree tilt. A seed is one scene for a design and its twin at every size, and seed_captures renders what
the two share at a seed once, with the same bytes as rendering each on its own.
The everyday scene: tilt 15 degrees, focus blur 0.7 px, a 2 px shake streak, ink 47 on paper 202 (61% contrast),
uneven light 0.15, noise 4 and JPEG quality 82, with pixel figures quoted at the 1280 frame.
"""
from __future__ import annotations
import hashlib
import math
import random
import cv2
import numpy as np
import zxingcpp
FRAME_W, FRAME_H = 1920, 1440 # the capture
K = FRAME_W / 1280 # pixels per square and blur are quoted at the 1280 frame
TEX = 12 # sheet texels per square
FOCAL = FRAME_W * 0.8 # pinhole focal length in pixels (about 64 degrees across)
MARGIN = 6 # squares of sheet shown round the code; anything of the design further out is cut
QUIET = 4 # the twin's margin, in squares
SUPER = 2 # the file is sampled on a grid this much finer than the sheet, then averaged
SNAP = 1e-4 # coverage this close to 0 or 1 is 0 or 1
SCENE = {"tilt": 15.0, "blur": 0.7, "motion": 2.0, "ink": 47.0, "paper": 202.0, "gradient": 0.15, "noise": 4.0,
"jpeg": 82}
SEEDS = "grid:everyday" # the size table's own scene streams, so a seed is one of its measured scenes
QR = zxingcpp.BarcodeFormat.QRCode
DROPPED = (zxingcpp.BarcodeFormat.MicroQRCode, zxingcpp.BarcodeFormat.RMQRCode)
W, H = FRAME_W, FRAME_H
YY, XX = np.mgrid[0:H, 0:W].astype(np.float32) # made once per process
# ---- the design file ---------------------------------------------------------------------------------------------
def open_design(src):
"""(luma float32 0..255, grey uint8) of a file, by its path or its bytes: BT.601 luma on the sRGB values, any
transparent part on white. Both reads take the pixels as stored: IMREAD_UNCHANGED ignores EXIF orientation, and
the grey read is told to, so the luma and the corners describe the same pixels."""
def read(flags):
if isinstance(src, bytes | bytearray):
return cv2.imdecode(np.frombuffer(src, np.uint8), flags)
return cv2.imread(str(src), flags)
im = read(cv2.IMREAD_UNCHANGED)
if im is None:
raise ValueError("cannot open the picture")
top = float(np.iinfo(im.dtype).max)
x = im.astype(np.float32) * np.float32(255.0 / top)
if x.ndim == 2:
lum = x
else:
bgr = x[..., :3] if x.shape[2] >= 3 else np.repeat(x[..., :1], 3, axis=2)
if x.shape[2] == 4:
a = x[..., 3:] / np.float32(255.0)
bgr = bgr * a + np.float32(255.0) * (1 - a)
lum = np.float32(0.114) * bgr[..., 0] + np.float32(0.587) * bgr[..., 1] + np.float32(0.299) * bgr[..., 2]
gray = read(cv2.IMREAD_GRAYSCALE | cv2.IMREAD_IGNORE_ORIENTATION)
if im.ndim == 3 and im.shape[2] == 4 and im[..., 3].min() < top:
gray = np.rint(lum).astype(np.uint8)
return lum.astype(np.float32), gray
def facts_of(r):
x, pos, raw = (r.extra or {}), r.position, bytes(r.bytes)
return {"text": r.text, "bytes": raw, "binary": r.content_type == zxingcpp.ContentType.Binary,
"version": int(x["Version"]), "ec": str(x["ECLevel"]), "mask": int(x["DataMask"]), "uec": x.get("UEC"),
"sha256": hashlib.sha256(raw).hexdigest(),
"corners": [[float(q.x), float(q.y)] for q in (pos.top_left, pos.top_right, pos.bottom_right,
pos.bottom_left)]}
def qr_reads(gray, **how):
"""zxing-cpp's reads at library defaults (QR only), or with the options how (design_rules._own: try_downscale
off): valid, and each with its version, error correction and mask."""
rs = [r for r in zxingcpp.read_barcodes(gray, formats=QR, **how) if r.format not in DROPPED and r.valid]
return [r for r in rs if (r.extra or {}).get("DataMask") is not None and (r.extra or {}).get("ECLevel")
and (r.extra or {}).get("Version")]
def same_code(r):
"""What copies of one code share: the content, version, error correction and mask."""
x = r.extra
return bytes(r.bytes), int(x["Version"]), str(x["ECLevel"]), int(x["DataMask"])
def nearest(rs, shape):
"""Of several reads, the one nearest the picture's centre."""
h, w = shape[:2]
def off(r):
p = r.position
c = np.mean([[q.x, q.y] for q in (p.top_left, p.top_right, p.bottom_right, p.bottom_left)], axis=0)
return float(np.hypot(c[0] - w / 2, c[1] - h / 2))
return min(rs, key=off)
def facts(gray):
"""zxing-cpp's read at library defaults (QR only): content, version, error correction, mask and corners (pixel
edges). One code, or copies of one content (the copy nearest the picture's centre); None otherwise, or when the
error correction or mask is unknown."""
rs = qr_reads(gray)
if not rs or len({bytes(r.bytes) for r in rs}) != 1:
return None
return facts_of(nearest(rs, gray.shape))
def px_per_square(corners, version):
c = np.float32(corners)
return float(np.mean([np.linalg.norm(c[i] - c[(i + 1) % 4]) for i in range(4)])) / (17 + 4 * version)
def twin_image(f, scale):
"""The plain twin at `scale` px per square with a 4-square margin, from the text (the bytes for binary content);
raises if it can't be made or reads back differently."""
content = f["bytes"] if f["binary"] else f["text"]
b = zxingcpp.create_barcode(content, QR, ec_level=f["ec"], version=f["version"], data_mask=f["mask"])
img = np.array(zxingcpp.write_barcode_to_image(b, scale=int(scale), add_quiet_zones=True))
back = facts(img)
if back is None or any(back[k] != f[k] for k in ("version", "ec", "mask", "bytes")):
raise ValueError("the twin reads back differently")
return img
# ---- onto the printed sheet --------------------------------------------------------------------------------------
def _shift(d):
return np.array([[1, 0, d], [0, 1, d], [0, 0, 1]], np.float64)
def place(L, corners, n, beyond="scene"):
"""The design's luma (0..1) and coverage on the sheet: t x t texels, TEX per square, the code's corners MARGIN
squares from each edge. Each file pixel prints as a solid square, sampled by area. beyond="paper" puts blank paper
beyond the file's edge (a check anchor); "scene" lets the scene show there."""
t = (n + 2 * MARGIN) * TEX
c = np.float32(corners)
k = px_per_square(corners, (n - 17) // 4)
if k > TEX:
w, h = max(1, round(L.shape[1] * TEX / k)), max(1, round(L.shape[0] * TEX / k))
sx, sy = w / L.shape[1], h / L.shape[0]
L = cv2.resize(L, (w, h), interpolation=cv2.INTER_AREA)
c = c * np.float32([sx, sy])
cover = np.ones(L.shape, np.float32)
u0, u1 = MARGIN * TEX * SUPER, (MARGIN + n) * TEX * SUPER
edge = cv2.getPerspectiveTransform(c, np.float32([[u0, u0], [u1, u0], [u1, u1], [u0, u1]]))
hc = _shift(-0.5) @ edge @ _shift(0.5)
ts = t * SUPER
Lt = cv2.warpPerspective(L, hc, (ts, ts), flags=cv2.INTER_NEAREST, borderMode=cv2.BORDER_REPLICATE)
cov = cv2.warpPerspective(cover, hc, (ts, ts), flags=cv2.INTER_NEAREST, borderMode=cv2.BORDER_CONSTANT,
borderValue=0)
if SUPER > 1:
Lt = cv2.resize(Lt, (t, t), interpolation=cv2.INTER_AREA)
cov = cv2.resize(cov, (t, t), interpolation=cv2.INTER_AREA)
cov[cov > 1 - SNAP] = 1
cov[cov < SNAP] = 0
Lt = np.clip(Lt, 0, 1).astype(np.float32)
if beyond == "paper":
return Lt * cov + (1 - cov), np.ones_like(cov)
return Lt, cov
class Design:
"""A design (or twin) with its facts, placed once on the sheet."""
def __init__(self, lum, gray, f=None, beyond="scene"):
self.lum, self.gray, self.beyond = lum.astype(np.float32), gray, beyond
self.facts = f if f is not None else facts(gray)
self._placed = None
@classmethod
def open(cls, path, beyond="scene"):
lum, gray = open_design(path)
return cls(lum, gray, beyond=beyond)
def twin(self, scale=None):
f = self.facts
scale = scale or max(1, round(px_per_square(f["corners"], f["version"])))
img = twin_image(f, scale)
return Design(img.astype(np.float32), img, beyond=self.beyond)
@property
def n(self):
return 17 + 4 * self.facts["version"]
def placed(self):
if self._placed is None:
self._placed = place(self.lum / np.float32(255.0), self.facts["corners"], self.n, self.beyond)
return self._placed
# ---- the scene ---------------------------------------------------------------------------------------------------
def smooth_field(shape, rng, scale, amp):
"""A low-frequency random field, mean 0."""
h, w = shape
small = np.array([[rng.gauss(0, 1) for _ in range(max(2, w // scale))] for _ in range(max(2, h // scale))],
dtype=np.float32)
return cv2.resize(small, (w, h), interpolation=cv2.INTER_CUBIC) * amp
def project(points, tilt_deg, axis_deg, psi_deg, center):
"""Turn plane points by psi, tilt them about an in-plane axis, and project them through a pinhole."""
psi, phi, th = map(math.radians, (psi_deg, axis_deg, tilt_deg))
c, s = math.cos(psi), math.sin(psi)
pts = np.array([[c * x - s * y, s * x + c * y, 0.0] for x, y in points])
ax = np.array([math.cos(phi), math.sin(phi), 0.0])
k = np.array([[0, -ax[2], ax[1]], [ax[2], 0, -ax[0]], [-ax[1], ax[0], 0]])
rot = np.eye(3) + math.sin(th) * k + (1 - math.cos(th)) * (k @ k)
p3 = pts @ rot.T
z = FOCAL + p3[:, 2]
return np.stack([FOCAL * p3[:, 0] / z + center[0], FOCAL * p3[:, 1] / z + center[1]], 1).astype(np.float32)
def streams(version, seed):
"""The scene's random streams for a version and seed: the turn and tilt axis, then the scene, the sheet's texture
and the shake, each from its own string seed."""
name = f"{SEEDS}:{int(version)}:{int(seed)}"
rng = random.Random(name)
psi, axis = rng.uniform(0, 360), rng.uniform(0, 180)
return rng, psi, axis, random.Random(f"{name}:texture"), random.Random(f"{name}:motion")
def seed_captures(objs, seed, steps):
"""{(step index, object index): JPEG bytes} for objects of one version (a design and its twin) at one seed. What
they share at the seed (the background, the sheet's paper, the shake and the noise) and at a step (the uneven
light) is computed once."""
n = objs[0].n
rng, psi, axis, trng, mrng = streams(objs[0].facts["version"], seed)
t = objs[0].placed()[0].shape[0]
paper = np.full((t, t), float(SCENE["paper"]), np.float32)
paper += smooth_field((t, t), trng, 96, 4.0) + np.random.default_rng(trng.randrange(1 << 30)).normal(
0, 2.0, (t, t))
u1, u2 = rng.uniform(-0.5, 0.5), rng.uniform(-0.5, 0.5)
back = rng.uniform(85, 135) + smooth_field((H, W), rng, 240, 14.0)
for _ in range(rng.randrange(2, 6)): # a few soft-edged objects
x0, y0 = rng.uniform(0, W), rng.uniform(0, H)
cv2.rectangle(back, (int(x0), int(y0)), (int(x0 + rng.uniform(100, 600)), int(y0 + rng.uniform(80, 500))),
rng.uniform(60, 180), -1)
back = cv2.GaussianBlur(back, (0, 0), 3)
beta = rng.uniform(0, 2 * math.pi) # uneven light
length = max(1, int(round(SCENE["motion"] * K))) # shake
kern = np.zeros((length, length), np.float32)
kern[length // 2, :] = 1.0 / length
rot = cv2.getRotationMatrix2D((length / 2 - 0.5, length / 2 - 0.5), mrng.uniform(0, 180), 1.0)
kern = cv2.warpAffine(kern, rot, (length, length))
kern = kern / max(kern.sum(), 1e-6)
nrng = np.random.default_rng(rng.randrange(1 << 30))
n1 = nrng.normal(0, 1.0 + SCENE["noise"], back.shape)
n2 = nrng.normal(0, 1, back.shape)
out = {}
for si, ppm in enumerate(steps):
mpx = ppm * K
s = mpx / TEX
half = t * s / 2
code_half = (n / 2 + 4) * mpx
room_x = max(0.0, W / 2 - code_half * 1.4)
room_y = max(0.0, H / 2 - code_half * 1.4)
center = (W / 2 + u1 * room_x, H / 2 + u2 * room_y)
dst = project([(-half, -half), (half, -half), (half, half), (-half, half)], SCENE["tilt"], axis, psi, center)
hm = cv2.getPerspectiveTransform(np.float32([[0, 0], [t, 0], [t, t], [0, t]]), dst)
code_w = n * mpx
ramp = ((XX - center[0]) * math.cos(beta) + (YY - center[1]) * math.sin(beta)) / max(code_w, 1)
light = 1 + SCENE["gradient"] * np.clip(ramp, -1.5, 1.5) / 1.5
for oi, obj in enumerate(objs):
L, cov = obj.placed()
tex = L * paper + (1 - L) * np.float32(SCENE["ink"]) # the sheet: L between ink and textured paper
if s < 1: # optical anti-aliasing before the warp
tex = cv2.GaussianBlur(tex, (0, 0), 0.42 / s)
if cov.min() < 1:
cov = np.clip(cv2.GaussianBlur(cov, (0, 0), 0.42 / s), 0, 1)
full = back.copy()
cv2.warpPerspective(tex, hm, (W, H), dst=full, flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_TRANSPARENT)
a = np.zeros_like(back)
cv2.warpPerspective(cov, hm, (W, H), dst=a, flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_TRANSPARENT)
a[a > 1 - SNAP] = 1
a[a < SNAP] = 0
sc = back * (1 - a) + full * a
sc *= light
sc = cv2.GaussianBlur(sc, (0, 0), SCENE["blur"] * K) # focus
sc = cv2.filter2D(sc, -1, kern)
sc += n1 + n2 * np.sqrt(np.clip(sc, 0, 255)) * 0.15
img = np.clip(sc, 0, 255).astype(np.uint8)
out[(si, oi)] = cv2.imencode(".jpg", img, [cv2.IMWRITE_JPEG_QUALITY, int(SCENE["jpeg"])])[1].tobytes()
return out
|