Spaces:
Sleeping
Sleeping
File size: 13,740 Bytes
2a2a176 ca6e756 a59c3c4 2a2a176 d1f47f9 5b3cd1e 2a2a176 5b3cd1e 2a2a176 5b3cd1e d1f47f9 2a2a176 ca6e756 2a2a176 a59c3c4 2a2a176 a59c3c4 2a2a176 a59c3c4 2a2a176 ca6e756 2a2a176 ca6e756 2a2a176 ca6e756 a59c3c4 2a2a176 a59c3c4 ca6e756 5b3cd1e 2a2a176 a59c3c4 2a2a176 ca6e756 2a2a176 a59c3c4 2a2a176 a59c3c4 2a2a176 ca6e756 a59c3c4 2a2a176 a59c3c4 2a2a176 a59c3c4 2a2a176 a59c3c4 2a2a176 a59c3c4 2a2a176 a59c3c4 d1f47f9 5b3cd1e d1f47f9 2a2a176 d1f47f9 2a2a176 | 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 292 293 294 295 296 297 298 299 | """The decoder worker process (see reading.py): it stages each picture's frames, zxing-cpp, WeChat's scanner and
OpenCV's two detectors read them in it, and the picture-quality checks (quality.py) run in it. _Worker, in the
server, starts one, talks to it, and replaces it when it gets stuck or dies.
"""
from __future__ import annotations
import os
import time
from pathlib import Path
from .reading_facts import read_facts, second_look
from .reading_rules import (
MANY_CODES,
MEASURE_TIMEOUT_S,
POOL,
STAGE_TIMEOUT_S,
TIMEOUT_S,
VENDOR,
PictureError,
frame_sizes,
)
class _TooBig(Exception):
"""More pixels than OPENCV_IO_MAX_IMAGE_PIXELS."""
# ---- the worker process: zxing-cpp, WeChat's scanner, OpenCV's two detectors --------------------------------------
def _corners(quality, scanner: str, take, *args):
"""quality.corners_from(scanner, *take(*args)) as plain data (four [x, y] floats, and squares per side or None), or
None: take reads the scanner's own result (zxing-cpp's position and extra, WeChat's or OpenCV's points) inside this
guard, so that an error in the corners never costs a read and the worker's message always pickles."""
try:
got = quality.corners_from(scanner, *take(*args))
if got is None:
return None
corners = [[float(x), float(y)] for x, y in got[0]]
return (corners, None if got[1] is None else int(got[1])) if len(corners) == 4 else None
except Exception: # noqa: BLE001 - the corners are optional
return None
def _decoders(wechat_dir: str, quality):
"""Per scanner, a function from a frame to (contents, corners): one corners entry per content, what
quality.corners_from makes of the scanner's own result for that code, or None. The contents are as measured.
zxing-cpp's also takes a list, which it fills with each content's facts (reading_facts.read_facts), and options
for the looks only (reading_facts.second_look: another of its binarizers, by name, or the frame's own size alone):
the reading uses the library's defaults."""
import cv2
import zxingcpp
cv2.setNumThreads(1)
d = Path(wechat_dir)
wechat = cv2.wechat_qrcode.WeChatQRCode(str(d / "detect.prototxt"), str(d / "detect.caffemodel"),
str(d / "sr.prototxt"), str(d / "sr.caffemodel"))
qr_format = zxingcpp.BarcodeFormat.QRCode # in zxing-cpp 3 this also returns Micro QR and rMQR, dropped below
dropped = (zxingcpp.BarcodeFormat.MicroQRCode, zxingcpp.BarcodeFormat.RMQRCode)
def zxing_cpp(bgr, facts=None, how=None):
out, geo = [], []
how = {k: getattr(zxingcpp.Binarizer, v) if k == "binarizer" else v for k, v in (how or {}).items()}
for r in zxingcpp.read_barcodes(cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY), formats=qr_format, **how):
if r.format in dropped or not getattr(r, "valid", True):
continue
out.append(bytes(r.bytes) if r.bytes is not None else (r.text or "").encode("utf-8"))
geo.append(_corners(quality, "zxing-cpp", lambda x: (x.position, x.extra), r))
if facts is not None:
facts.append(read_facts(r))
return out, geo
def wechat_qr(bgr):
try:
texts, points = wechat.detectAndDecode(bgr)
except UnicodeDecodeError as e: # binary content: OpenCV's binding fails to make text of it
return [bytes(e.object)], [None]
out = [t.encode("utf-8") for t in texts if t]
return out, [_corners(quality, "wechat", lambda i: (points[i],), i) for i, t in enumerate(texts) if t]
def single(name, detector): # detectAndDecode, the single-code call apps make
def read(bgr):
gray = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY)
try:
text, points, straight = detector.detectAndDecode(gray)
raw = text.encode("utf-8") if text else b""
except UnicodeDecodeError: # bytes that aren't UTF-8 (kanji mode, binary); the error's own stop at a
raw, points, straight = detector.detectAndDecodeBytes(gray) # NUL, so its bytes call reads them all
return ([bytes(raw)], [_corners(quality, name, lambda: (points, straight))]) if raw else ([], [])
return read
return cv2, {"zxing-cpp": zxing_cpp, "wechat": wechat_qr, "opencv": single("opencv", cv2.QRCodeDetector()),
"opencv-aruco": single("opencv-aruco", cv2.QRCodeDetectorAruco())}
def _open(cv2, path: str):
"""The picture in gray as the measurement opened it, any transparent part on white; None if it can't be opened."""
import numpy as np
try:
gray = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
if gray is None or not path.endswith((".png", ".webp")):
return gray
full = cv2.imread(path, cv2.IMREAD_UNCHANGED) # gray (or colour) with alpha comes back as 4 channels
except cv2.error as e: # past OPENCV_IO_MAX_IMAGE_PIXELS OpenCV raises, rather than return None
if "MAX_IMAGE_PIXELS" in str(e):
raise _TooBig from e
return None
if full is None or full.ndim != 3 or full.shape[2] != 4:
return gray
top = np.iinfo(full.dtype).max
if full[..., 3].min() == top: # no pixel is transparent: the measured path, unchanged
return gray
a = full[..., 3].astype(np.float32) / top
g = cv2.cvtColor(full[..., :3], cv2.COLOR_BGR2GRAY).astype(np.float32) / top
return np.rint(255 * (g * a + (1 - a))).astype(np.uint8)
def _child(conn, wechat_dir: str) -> None:
try:
from . import quality # in the worker only, as OpenCV
cv2, decoders = _decoders(wechat_dir, quality)
except Exception as e: # noqa: BLE001 - reported to the parent, which refuses to start
conn.send(("broken", repr(e)))
return
conn.send(("ready",))
while True:
try:
msg = conn.recv()
except EOFError:
return
if msg[0] == "stage":
_, path, outdir = msg
try:
gray = _open(cv2, path)
if gray is None:
conn.send(("unreadable",))
continue
frames = []
for k, ((w, h), labels) in enumerate(frame_sizes(*gray.shape).items()):
g = gray if (h, w) == gray.shape else cv2.resize(gray, (w, h), interpolation=cv2.INTER_AREA)
p = os.path.join(outdir, f"{k}.png")
cv2.imwrite(p, g, [cv2.IMWRITE_PNG_COMPRESSION, 1])
frames.append((w, h, labels, p))
conn.send(("staged", gray.shape[1], gray.shape[0], frames))
except _TooBig:
conn.send(("too big",))
except Exception: # noqa: BLE001 - a picture that can't be staged (out of memory, say) is refused
conn.send(("unreadable",))
elif msg[0] == "decode":
_, frames, skip, many = msg # many: OpenCV's newer detector is off from here on (MANY_CODES)
for k, (_, _, _, p) in enumerate(frames):
todo = [n for n in POOL if (k, n) not in skip]
if not todo:
continue
bgr = cv2.cvtColor(cv2.imread(p, cv2.IMREAD_GRAYSCALE), cv2.COLOR_GRAY2BGR)
for n in todo:
if many and n == "opencv-aruco":
conn.send(("off", k, n))
continue
conn.send(("start", k, n))
facts = [] if n == "zxing-cpp" else None # its facts, one per content (reading_facts)
try:
payloads, corners = decoders[n](bgr) if facts is None else decoders[n](bgr, facts)
except Exception: # noqa: BLE001 - a decoder that fails reads nothing, as in the measurement
payloads, corners, facts = [], [], None
if facts is None or len(facts) != len(payloads):
facts = [None] * len(payloads)
conn.send(("done", k, n, payloads, corners, facts))
many = many or (n == "zxing-cpp" and len(payloads) >= MANY_CODES)
conn.send(("end",))
elif msg[0] == "measure":
conn.send(("measured", quality.measure_staged(*msg[1:])))
elif msg[0] == "look":
conn.send(("look", second_look(cv2, decoders["zxing-cpp"], *msg[1:])))
class _Worker:
"""One decoder worker process. A stuck one is killed; a new one starts when the next message is sent."""
def __init__(self, ctx):
self._ctx = ctx
self._spawn()
def _spawn(self) -> None:
self.conn, child = self._ctx.Pipe()
self.proc = self._ctx.Process(target=_child, args=(child, str(VENDOR / "wechat")), daemon=True)
self.proc.start()
child.close()
msg = self._recv(180)
if msg is None or msg[0] != "ready":
raise RuntimeError(f"decoder worker did not start: {msg}")
def close(self) -> None:
self.proc.kill()
self.proc.join(5)
self.conn.close()
def alive(self) -> bool:
return not self.conn.closed and self.proc.is_alive()
def _send(self, msg) -> None:
"""Send to a live worker: one that was killed, or died while idle (killed for memory, say), is replaced."""
if self.conn.closed or not self.proc.is_alive():
self.close()
self._spawn()
try:
self.conn.send(msg)
except OSError: # it died just now
self.close()
self._spawn()
self.conn.send(msg)
def _recv(self, timeout: float):
try:
if not self.conn.poll(timeout):
return None
return self.conn.recv()
except (EOFError, OSError):
return None
def stage(self, path: str, outdir: str):
t0 = time.monotonic()
self._send(("stage", path, outdir))
msg = self._recv(STAGE_TIMEOUT_S)
if msg is None: # too slow, or the worker crashed on this picture
slow = time.monotonic() - t0 >= STAGE_TIMEOUT_S
self.close()
raise PictureError("This picture took too long to open." if slow else "This picture could not be opened.")
if msg[0] == "too big":
raise PictureError("This picture is over 50 megapixels.")
if msg[0] == "unreadable":
raise PictureError("This picture could not be opened.")
return msg[1], msg[2], msg[3]
def decode(self, frames, deadline: float):
"""{(frame index, scanner): payloads}, the same for the corners (one per payload) and the facts (zxing-cpp's,
reading_facts.read_facts; None for the others), whether the deadline (or crashes) cut the reading short, and
the decodes the many-codes rule (MANY_CODES) left out, kept across a replaced worker."""
got: dict[tuple[int, str], list[bytes]] = {}
geo: dict[tuple[int, str], list] = {}
facts: dict[tuple[int, str], list] = {}
missed: set[tuple[int, str]] = set()
off, many, crashes = set(), False, 0
while True:
self._send(("decode", frames, missed | off | set(got), many))
current = None
while True:
msg = self._recv(max(0.0, min(TIMEOUT_S, deadline - time.monotonic())))
if msg is None:
break
if msg[0] == "start":
current = (msg[1], msg[2])
elif msg[0] == "done":
got[(msg[1], msg[2])] = msg[3]
geo[(msg[1], msg[2])] = msg[4]
facts[(msg[1], msg[2])] = msg[5]
many = many or (msg[2] == "zxing-cpp" and len(msg[3]) >= MANY_CODES)
current = None
elif msg[0] == "off":
off.add((msg[1], msg[2]))
elif msg[0] == "end":
return got, geo, facts, False, off
# silence: a decode ran past TIMEOUT_S (a miss), the worker died, or the deadline passed
self.close()
if time.monotonic() >= deadline:
return got, geo, facts, True, off
if current is not None:
missed.add(current)
else:
crashes += 1
if crashes > 1:
return got, geo, facts, True, off
def measure(self, path: str, found: dict, sizes: list, scale: dict) -> dict | None:
"""The picture-quality checks, by quality.measure_staged in this worker (see _ask)."""
return self._ask(("measure", path, found, sizes, scale), "measured")
def second_look(self, path: str, how: dict | None = None) -> list | None:
"""zxing-cpp's second look at a staged frame, or its look with how, by reading_facts.second_look (see _ask)."""
return self._ask(("look", path, how), "look")
def _ask(self, msg: tuple, reply: str):
"""A diagnostic's answer from this worker; None when it isn't running (diagnostics never start one), or when
it fails or runs past MEASURE_TIMEOUT_S: it is then closed, and replaced at the next send, as after a stuck
decode."""
if not self.alive():
return None
try:
self.conn.send(msg)
got = self._recv(MEASURE_TIMEOUT_S)
if got is not None and got[0] == reply:
return got[1]
except Exception: # noqa: BLE001 - diagnostics never cost a reading
pass
self.close()
return None
|