Passport photo reader: a camera view with a frame, read in the browser
CI / secret scan (push) Successful in 6s
CI / fmt, clippy, tests (push) Successful in 28s

On the new person and new passport forms, next to Read:

- "Take a photo" opens a camera view (getUserMedia, the back camera): a
  frame of the photo page's shape (125 by 88 mm), a band for the two lines,
  and a hint from four checks of the band four times a second: too dark,
  glare on the lines, no lines in the band, not sharp yet (variance of the
  Laplacian). Once the band has looked readable for a second it takes the
  photo itself; "Take the photo" and Cancel (or Escape) are there too.
  The band where the frame showed it is read first; when that holds no
  zone, the whole frame is searched.
- "Choose a picture" reads any image: EXIF orientation, the four quarter
  turns, a tilt of 7 degrees either way; a failed read says when there is
  glare in the picture.

Reading happens in the browser (static/mrz-photo.js): even light from a
local mean, the lines found with Tesseract's page layout, then the band cut
along their baselines, scaled and read as one block. Tesseract.js 7.0.0
and its core (SIMD and plain LSTM builds) with Doubango's MRZ model, all
served by the app from static/vendor/tesseract/ (README.txt: sources, npm
integrity, SHA-256, licenses Apache-2.0 and BSD-3), loaded at the first
photo. The photo never leaves the device: only the lines go into the zone's
field, and Read is pressed.

The server reads them like typed ones, through mrz::from_ocr when they are
no clean zone: the first line wherever it is, the second whose check digits
match best, a miscounted filler run cut or filled, K for < in the filler,
letters and digits where the zone has them, and look-alikes (O for 0, B for
8) swapped in the number fields only where a check digit asks for it,
preferring a digit. A photo's note asks to compare the names, which have no
check digit.

Only those two pages and the reader's files allow WebAssembly
('wasm-unsafe-eval', worker-src 'self', img-src blob:) and the camera
(Permissions-Policy camera=(self)); every other page keeps camera=().
Content types for .wasm and .gz.

Checks: fmt, clippy -D warnings, 99 unit and 50 integration tests (new:
from_ocr cases from real reads, tests/mrz_photo.rs for the markup, the two
policies, the files and a read from photo lines). In Edge (Playwright): 12
made-up phone photos and 16 specimen pages from Wikimedia Commons, every
zone that is readable read with all check digits matching (not: a hard
shadow across the lines; the specimens whose zone is blanked out), 1 to 5 s
each; the camera view with a fake camera: the glare and dark hints, and a
good picture taken by itself and the form filled in. Layout check 0
problems on both forms and a person, light and dark, desk and phone.

PLAN: task 7.3 done ahead of the client link, the decision recorded.
This commit is contained in:
grabowski committed 2026-11-20 16:47:40 +07:00
1 parent fc8c1b6a44
commit 1f54d93643
24 files changed
+2130 -52

No files matched your search

+23 -7
View File
@@ -40,6 +40,7 @@ The first round of questions was answered on November 17, 2026.
| Client link (Nov 20) | staff make a link for a person, often a new and empty record; the client fills in personal details, the passport (with the MRZ reader, also from a photo taken on the phone), the address in Thailand and contact, and uploads scans (passport pages, photo, TDAC) |
| Client input (Nov 20) | goes straight into the person's record, logged as coming from the link; staff check it |
| Link lifetime (Nov 20) | 14 days, or until the client presses Send or staff close the link |
| Passport photo (Nov 20) | read in the browser with Tesseract.js and an MRZ model served by the app; the photo never leaves the device, only the lines it finds are sent, and the server checks them like typed ones |
| Client agreement (later) | a box on the client link where the client signs the agreement with B4L to keep and work with their data; its text comes from B4L |
Kanidm keeps persons, groups and OAuth2 clients in one namespace, so the
@@ -446,13 +447,28 @@ Planned on November 20, 2026, from Alex's answers (decisions table).
saved on its own; the link's own form token and the same-site check;
nothing about anyone else. After Send, or 14 days, or once staff close
it, the link shows only that it is closed.
- [ ] 7.3 The passport from a photo: on a phone the camera takes the MRZ
(`<input type="file" accept="image/*" capture="environment">`); the
two lines it reads go into the MRZ field, whose check digits then fill
in the form. The same on the staff's new person and new passport forms.
To settle when building it: reading in the browser (the photo stays on
the phone; Tesseract.js with a model trained for the MRZ, served by the
app, needs `'wasm-unsafe-eval'` in the policy) or on the server.
- [x] 7.3 The passport from a photo, built ahead on November 20, 2026 on
the staff's new person and new passport forms (the client's pages
reuse `templates/_mrz_photo.html`). "Take a photo" opens a camera view:
a frame of the photo page's shape and a band for the two lines, with
a hint that says when it is too dark, when there is glare on the
lines, when no lines are in the band or the picture is not sharp, and
that takes the photo by itself once the band has looked readable for
a second. "Choose a picture" reads any image (EXIF orientation, the
four quarter turns, a tilt either way). Reading happens in the browser
(`static/mrz-photo.js`): Tesseract.js 7 with Doubango's MRZ model, all
served by the app (`static/vendor/tesseract/README.txt`, about 6 MB,
loaded at the first photo), so the photo stays on the device. Only
those pages and those files allow WebAssembly (`'wasm-unsafe-eval'`)
and the camera (`camera=(self)`). The lines found go into the zone's
field and Read is pressed; `mrz::from_ocr` picks the first line and
the second whose check digits match best among them, cuts or fills a
miscounted filler run, and swaps look-alikes (O for 0) only where a
check digit asks for it. The note then asks to compare the names,
which have no check digit. Tried on 12 made-up phone photos and 16
specimen pages: every readable zone read, except under a hard shadow
across the lines; the specimens that failed have their zone blanked
out.
- [ ] 7.4 Uploads from the client into the encrypted store: the passport's
photo page, the photo, the TDAC and other scans, each tied to its record
where there is one.