Download server/reading_rules.py from devmandan/qr-scan-difficulty-check: direct link, hf CLI and curl.
- Browser
- Download file 2.89 kB
-
https://huggingface.co/spaces/devmandan/qr-scan-difficulty-check/resolve/main/server/reading_rules.py
- Command line
-
hf download hf://spaces/devmandan/qr-scan-difficulty-check/server/reading_rules.py
-
curl -L -o reading_rules.py https://huggingface.co/spaces/devmandan/qr-scan-difficulty-check/resolve/main/server/reading_rules.py
2.89 kB
| """The reading's settings (see reading.py): OpenCV's pixel limit, where the scanners' files are, the self-test | |
| picture, the frame sizes, the scanners in reading order, the time limits and the many-codes rule; the error for a | |
| picture this server cannot open; and the frame sizes a picture is read at. Shared by the reader (reading.py), its | |
| decoder worker (reading_worker.py) and ZXing's Java process (reading_java.py). | |
| """ | |
| from __future__ import annotations | |
| import os | |
| from pathlib import Path | |
| os.environ.setdefault("OPENCV_IO_MAX_IMAGE_PIXELS", "50000000") # OpenCV refuses bigger pictures before decoding them | |
| VENDOR = Path(os.environ.get("QSD_VENDOR", Path(__file__).resolve().parent.parent / "vendor")) | |
| SELF_TEST = Path(__file__).with_name("selftest.png") # a clean code, which all five read at every size | |
| FRAMES = [1920, 1280, 896, 640, 448, 320, 224] | |
| POOL = ["zxing-cpp", "wechat", "opencv", "opencv-aruco"] # read in this order, as the measurement's pool read them | |
| SCANNERS = [*POOL, "zxing-java"] | |
| TIMEOUT_S = 20 # one decode, as in the measurement | |
| STAGE_TIMEOUT_S = 30 # opening and shrinking one picture | |
| DEADLINE_S = 40 # one picture in all, so that a request ends within a minute; decodes not done by then count | |
| # as misses and the result says "partial" | |
| MEASURE_TIMEOUT_S = 5 # a diagnostic on one picture: the picture-quality checks (about 60 ms), or the second look | |
| # at the unused error correction (a few ms); started before the deadline only | |
| MANY_CODES = 100 # from the first frame, largest first, where zxing-cpp reads this many codes or more, OpenCV's | |
| # newer detector, a single-code detector that slows sharply as the codes in a picture multiply, | |
| # isn't run on that frame or any smaller one, and those decodes count as misses ("not_run"). | |
| # Over all frames it took about 2 s on a Mac with 100 codes, which would be about 5 s on this | |
| # Space's server (2.44 times the Mac's time), an eighth of DEADLINE_S; with 225 to 255 codes, | |
| # 18 to 34 s on the Mac, past the deadline here. No test picture held 25 to 224 codes, apart | |
| # from crops made for these timings, so any cut in that range gives the same readings on | |
| # them; every measured picture held one code | |
| class PictureError(Exception): | |
| """A picture this server cannot open.""" | |
| def frame_sizes(h: int, w: int) -> dict[tuple[int, int], list[int]]: | |
| """{(w, h): [frame sizes]} for the frames, never enlarging (the measurement's rule, rounding included).""" | |
| sizes: dict[tuple[int, int], list[int]] = {} | |
| for f in FRAMES: | |
| k = min(1.0, f / max(h, w)) | |
| sizes.setdefault((max(1, round(w * k)), max(1, round(h * k))), []).append(f) | |
| return sizes | |