diff --git a/docs/PLAN.md b/docs/PLAN.md index 985cc6b..c4b0a85 100644 --- a/docs/PLAN.md +++ b/docs/PLAN.md @@ -453,9 +453,11 @@ Planned on November 20, 2026, from Alex's answers (decisions table). 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 + lines, when no lines are in the band, the picture is not sharp or + still moves, and that takes the photo by itself once the band has + looked readable and stood still for 2.5 seconds (never in the first + 1.5 seconds; a second was too quick on a phone, Nov 20), counting + down in the hint; "Take the photo" takes it at once. "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, diff --git a/static/mrz-photo.js b/static/mrz-photo.js index 13826d1..24a7d7c 100644 --- a/static/mrz-photo.js +++ b/static/mrz-photo.js @@ -481,6 +481,16 @@ // the variance of the band's Laplacian below which it is not sharp yet, // measured on the band scaled to 320 pixels across var SHARP = 150; + // The photo takes itself only once the band has looked readable and the + // picture has stood still for HOLD checks in a row (one every 250 ms, so + // 2.5 s), and never in the first START ms, while the passport is still + // being put into the frame. A second was too quick on a phone: it took + // the photo while the hand still moved. + var HOLD = 10; + var START = 1500; + // the mean change of a band pixel from one check to the next (0 to 255) + // above which the picture still moves + var STILL = 6; // What is wrong with the band in the picture, or "" when it looks // readable: too dark, glare, no lines in it, or not sharp. @@ -512,18 +522,28 @@ } var sharp = n ? s2 / n - (s1 / n) * (s1 / n) : 0; var m = { mean: Math.round(mean), glare: bright / g.length, ink: ink / g.length, sharp: Math.round(sharp) }; - if (mean < 50) return { why: "Too dark: find more light.", m: m }; + if (mean < 50) return { why: "Too dark: find more light.", m: m, g: g }; if (m.glare > 0.02) { return { why: "Glare on the two lines: tilt the passport a little, or turn away from the lamp.", m: m, + g: g, }; } if (m.ink < 0.04) { - return { why: "Put the two lines at the bottom of the photo page into the yellow band.", m: m }; + return { why: "Put the two lines at the bottom of the photo page into the yellow band.", m: m, g: g }; } - if (sharp < SHARP) return { why: "Hold still: the lines are not sharp yet.", m: m }; - return { why: "", m: m }; + if (sharp < SHARP) return { why: "Hold still: the lines are not sharp yet.", m: m, g: g }; + return { why: "", m: m, g: g }; + } + + // The mean change of a pixel between two looks at the band; Infinity + // when there is no earlier look of the same size. + function moved(a, b) { + if (!a || !b || a.length !== b.length) return Infinity; + var sum = 0; + for (var i = 0; i < a.length; i++) sum += Math.abs(a[i] - b[i]); + return sum / a.length; } function el(tag, cls, text) { @@ -621,6 +641,8 @@ var timer = 0; var good = 0; + var last = null; + var opened = Date.now(); var done = false; var small = canvas(320, 80); var sctx = small.getContext("2d", { willReadFrequently: true }); @@ -658,12 +680,26 @@ small.height = Math.max(8, Math.round((bh * 320) / bw)); sctx.drawImage(video, bx, by, bw, bh, 0, 0, 320, small.height); var j = judge(sctx, 320, small.height); - hint.textContent = j.why || "Good. Hold still..."; - root.classList.toggle("good", !j.why); + var change = moved(last, j.g); + last = j.g; + j.m.moved = Math.round(change * 10) / 10; + // the first look has nothing to compare with: not still yet, but + // nothing to say about it either + var why = j.why || (change > STILL && change !== Infinity ? "Hold still: the picture still moves." : ""); + var early = Date.now() - opened < START; + good = why || early || change > STILL ? 0 : good + 1; + if (why) { + hint.textContent = why; + } else if (early) { + hint.textContent = "Good. Hold still..."; + } else { + var left = Math.max(1, Math.ceil(((HOLD - good) * 250) / 1000)); + hint.textContent = "Good. Hold still: the photo is taken in " + left + " s, or press Take the photo."; + } + root.classList.toggle("good", !why); root.setAttribute("data-measure", JSON.stringify(j.m)); - good = j.why ? 0 : good + 1; - // a steady, readable band for about a second takes the photo - if (good >= 4) { + // a readable band that stands still for HOLD checks takes the photo + if (good >= HOLD) { take(); return; }