marksheet

    Offline optical mark recognition with explicit review

    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    5 hours ago
    Downloads
    4

    No dependencies

    #MoonMarkSheet

    离线纸质填涂识别和复核工具。打印本项目的 A4 模板,用手机拍照或扫描,再在本机识别。浅涂、疑似多选和图像不清的题进入人工队列,复核时显示每个选项的原图裁剪。

    #Mooncakes

    moon add clbbbb/marksheet

    #浏览器上手

    需要 MoonBit 1.0.0-rc1 或更新版本,以及 Node.js 22 或更新版本。

    moon build --target js --deny-warn node web/serve.mjs

    打开 127.0.0.1:4173。也可以把端口作为 serve.mjs 的第一个参数传入。

    1. 先点「载入模拟样张」试用,或设置题数、选项数和每题最多选择数。
    2. 下载模板并按 A4 实际尺寸打印。可选印刷编号用于区分不同纸张,同一编号的不同照片在一个批次中会被拒绝。
    3. 保持整页和四个定位方块在画面内,页面尽量占满照片。导入 JPEG、PNG、WebP 或 PGM,建议一张图一张表。
    4. 逐题查看原图裁剪,确认选项、记为空白或标为无效。所有待复核题处理后才可导出批次 CSV。

    网页由本机回环地址提供,图片和计算留在浏览器。自动定位失败时,可以在原图上按模板的左上、右上、右下、左下顺序标记四角,识别仍需通过印刷模板码校验。

    「保存批次到本机」会将本批照片的灰度图和复核记录存入当前浏览器的 IndexedDB。同一浏览器打开同一个地址和端口,在空批次中点击「恢复本机批次」即可继续。可保存一个批次,保存会替换该存档。复核结束后可导入与 CLI 相同格式的答案键 JSON,下载包含成绩和逐题统计的 JSON。

    关闭页面前可下载每张表的复核记录。恢复时用相同模板和原文件重新导入,再载入复核记录。浏览器一批最多保留 40 张。

    #CLI 与批次

    库包可直接用于 MoonBit 应用。CLI 使用 Node.js 的文件系统接口,读取 P2/P5 灰度 PGM。

    node _build/js/debug/build/cmd/main/main.js --help node _build/js/debug/build/cmd/main/main.js print examples/template.json > form.svg python -B examples/demo.py demo-output

    演示会生成三张模拟表,逐张识别、裁切证据、复核、评分和导出。用 Python 的演示脚本可避免旧版 Windows PowerShell 重定向产生 UTF-16 文本或损坏二进制 PGM。

    CLI 常用子命令:

    命令用途
    template TITLE QUESTIONS OPTIONS MAX_MARKS生成均匀题型的模板 JSON
    print-numbered TEMPLATE SERIAL生成带印刷编号的 A4 SVG
    scan TEMPLATE IMAGE ID输出各题判定、裁剪坐标和待复核列表
    scan-corners TEMPLATE IMAGE ID CORNERS手工给出模板顺序的四个方块中心,仍校验印刷模板码
    start TEMPLATE IMAGE ID输出空复核记录
    review TEMPLATE IMAGE JOURNAL REVISION QUESTION ACTION NOTE应用一条人工决定,输出新记录
    inspect TEMPLATE IMAGE JOURNAL重放已保存的记录并输出最终状态
    export TEMPLATE MANIFEST导出整批 CSV,有待复核时返回退出码 3
    audit TEMPLATE MANIFEST csv\|json查看每题机器判定与最终结果分布
    grade TEMPLATE MANIFEST KEY csv\|json使用答案键评分,多选要求选项集合完全一致
    calibrate TEMPLATE MANIFEST POLICIES用已标注的样张比较判定阈值

    复核命令参数中的题号和选项从 1 开始,JSON 使用从 0 开始的索引。决定文件形如 {"action":"choose","options":[1]},也支持 blank 和 reject,后二者的 options 为空数组。修订号从 0 开始。一个批次清单形如 {"version":1,"sheets":[{"id":"A01","image":"a01.pgm","journal":"a01-review.json"}]},图片和日志路径相对于清单所在目录。导出照片相关数据前,人工抽查自动接受的行。

    #验证与边界

    moon test --deny-warn moon test --target js --deny-warn moon build --target js --deny-warn node examples/browser-api.mjs python -B examples/demo.py demo-output

    识别先按图像象限搜索四角,失败时再从全图筛选角标组合。单应变换对齐页面后,逐格按局部对比度核对模板码。填涂评分采用多点白底估计,内缘墨迹达到阈值时会将原本判空的选项送审。印刷编号带 CRC-8,用于检测读码误差。复核记录保存原图指纹、识别配置和人工变更,可以重放,适合本地复核数据交换。复核记录也保存识别算法版本,旧记录按旧算法恢复。

    演示使用已知答案的合成图片。正视、偏斜和阴影三张表各有 6 题,默认配置共自动接受 12 题,合成数据中未观察到错误,共 6 题进入人工队列。较高的浅涂候选阈值只送审 3 题,但漏掉另外 3 处浅涂。校准报告单独统计整页定位或读码失败,自动错误率和送审率按成功读取的题目计算。真实打印机、笔迹、曝光和遮挡需要单独测试。当前支持本项目的带定位标记模板。模板边缘缺失、定位码不一致或无法读取的图片会报告错误。当前算法识别填涂,不读取手写文字。

    定位对照实验可运行 python -B examples/registration-experiment.py。要对照旧版,另行构建提交 a001016,用 --baseline 传入该版本的 JS CLI 文件。正视、透视偏斜、阴影样张各 6 题,两版合计均为自动判定 12 题、送审 6 题、自动误判 0 题。缺一个角标时,两版自动定位均失败,新版手动恢复成功。两张相同模板同时入镜时,旧版会自动选定一张,新版会要求手动选角。这些结果来自合成样张。

    使用 Chromium 或 Microsoft Edge 验证实际页面时,设置 MARKSHEET_BROWSER 为浏览器可执行文件路径,再运行 node examples/browser-page.mjs。它会试跑浏览器 PNG 导入、人工复核和 CSV 下载。

    MarkError

    pub(all) suberror MarkError {
    Invalid(String)
    Unreadable(String)
    RegistrationFailed(String)
    ReviewRequired(String)
    } derive(
    Debug
    )

    AnswerKey

    pub struct AnswerKey {
    template : Template
    items : ArrayView[KeyItem]
    maximum : Int
    }

    AnswerKey::batch_grades

    fn AnswerKey::batch_grades(self : AnswerKey, batch : Batch) -> Array[Grade] raise MarkError

    AnswerKey::from_json

    fn AnswerKey::from_json(template : Template, value :
    Json
    ) -> AnswerKey raise MarkError

    AnswerKey::grade

    fn AnswerKey::grade(self : AnswerKey, sheet : Sheet) -> Grade raise MarkError

    AnswerKey::json

    AnswerKey::new

    fn AnswerKey::new(template : Template, answers : Array[Array[Int]], points : Array[Int]) -> AnswerKey raise MarkError

    Batch

    pub struct Batch {
    template : Template
    // private fields
    }

    Batch::add

    fn Batch::add(self : Batch, sheet : Sheet) -> Unit raise MarkError

    Batch::audit

    fn Batch::audit(self : Batch) -> Array[QuestionAudit] raise MarkError

    Batch::audit_csv

    fn Batch::audit_csv(self : Batch) -> String raise MarkError

    Batch::audit_json

    fn Batch::audit_json(self : Batch) ->
    Json
    raise MarkError

    Batch::csv

    fn Batch::csv(self : Batch, allow_pending? : Bool) -> String raise MarkError

    Batch::get

    fn Batch::get(self : Batch, id : String) -> Sheet?

    Batch::ids

    fn Batch::ids(self : Batch) -> Array[String]

    Batch::new

    fn Batch::new(template : Template) -> Batch

    Batch::remove

    fn Batch::remove(self : Batch, id : String) -> Bool

    Batch::summary

    fn Batch::summary(self : Batch) -> BatchSummary

    BatchSummary

    pub(all) struct BatchSummary {
    sheets : Int
    answered : Int
    blank : Int
    invalid : Int
    pending : Int
    manually_reviewed : Int
    } derive(Eq,
    Debug
    )

    BatchSummary::json

    BubbleReading

    pub struct BubbleReading {
    option : Int
    state : MarkState
    score : Double
    dark_fraction : Double
    edge_ink : Double
    paper : Double
    ring : Double
    crop : CropBox
    }

    BubbleReading::json

    CalibrationCase

    pub struct CalibrationCase {
    scan : Scan
    truths : ArrayView[ArrayView[Int]]
    }

    Review coverage trade-offs over already extracted bubble evidence.

    CalibrationCase::new

    fn CalibrationCase::new(scan : Scan, truths : Array[Array[Int]]) -> CalibrationCase raise MarkError

    CalibrationCount

    pub(all) struct CalibrationCount {
    total : Int
    deferred : Int
    automatic_errors : Int
    missed_marks : Int
    false_marks : Int
    } derive(Eq,
    Debug
    )

    CalibrationPoint

    pub struct CalibrationPoint {
    policy : DetectionPolicy
    counts : CalibrationCount
    }

    CropBox

    pub(all) struct CropBox {
    x : Int
    y : Int
    width : Int
    height : Int
    } derive(Eq,
    Debug
    )

    Decision

    pub struct Decision {
    action : ReviewAction
    note : String
    }

    DetectionPolicy

    pub struct DetectionPolicy {
    possible : Double
    filled : Double
    minimum_paper : Double
    minimum_ring : Double
    }

    DetectionPolicy::from_json

    DetectionPolicy::new

    fn DetectionPolicy::new(possible : Double, filled : Double, minimum_paper : Double, minimum_ring : Double) -> DetectionPolicy raise MarkError

    DetectionPolicy::standard

    Evaluation

    pub(all) struct Evaluation {
    questions : Int
    auto_questions : Int
    auto_errors : Int
    review_questions : Int
    reviewed_correct : Int
    reviewed_wrong : Int
    unresolved : Int
    } derive(Eq,
    Debug
    )

    Evaluation::combine

    fn Evaluation::combine(self : Evaluation, other : Evaluation) -> Evaluation

    Evaluation::empty

    fn Evaluation::empty() -> Evaluation

    Evaluation::json

    Finding

    pub(all) enum Finding {
    Accepted
    Blank
    Ambiguous
    TooMany
    PoorImage
    } derive(Eq,
    Debug
    )

    Finding::needs_review

    fn Finding::needs_review(self : Finding) -> Bool

    Grade

    pub struct Grade {
    sheet_id : String
    points : Int
    maximum : Int
    results : ArrayView[ItemGrade]
    }

    Grade::json

    GrayImage

    pub struct GrayImage {
    width : Int
    height : Int
    pixels : Bytes
    }

    GrayImage::crop

    fn GrayImage::crop(self : GrayImage, x : Int, y : Int, width : Int, height : Int) -> GrayImage raise MarkError

    GrayImage::from_rgba

    fn GrayImage::from_rgba(width : Int, height : Int, pixels : Bytes) -> GrayImage raise MarkError

    GrayImage::get

    fn GrayImage::get(self : GrayImage, x : Int, y : Int) -> Int?

    GrayImage::new

    fn GrayImage::new(width : Int, height : Int, pixels : Bytes) -> GrayImage raise MarkError

    GrayImage::pgm

    fn GrayImage::pgm(self : GrayImage) -> Bytes

    Ink

    pub(all) struct Ink {
    question : Int
    option : Int
    darkness : Int
    radius : Int
    dx : Int
    dy : Int
    } derive(Eq,
    Debug
    )

    A mark in a synthetic software fixture. Its coordinates identify a bubble. Offset and radius are measured in the same units as the SVG viewBox.

    Ink::solid

    fn Ink::solid(question : Int, option : Int) -> Ink

    ItemGrade

    pub(all) struct ItemGrade {
    question : Int
    kind : ResultKind
    awarded : Int
    possible : Int
    } derive(Eq,
    Debug
    )

    KeyItem

    pub struct KeyItem {
    correct : ArrayView[Int]
    points : Int
    }

    MarkState

    pub(all) enum MarkState {
    Clear
    Possible
    Filled
    } derive(Eq,
    Debug
    )

    Outcome

    pub(all) enum Outcome {
    MissingReview
    Responded(ArrayView[Int])
    NoResponse
    InvalidResponse
    } derive(Eq,
    Debug
    )

    Outcome::json

    Point

    pub(all) struct Point {
    x : Double
    y : Double
    } derive(Eq,
    Debug
    )

    Question

    pub(all) struct Question {
    options : Int
    max_marks : Int
    } derive(Eq,
    Debug
    )

    QuestionAudit

    pub struct QuestionAudit {
    index : Int
    sheets : Int
    machine : Map[String, Int]
    final_states : Map[String, Int]
    manual : Int
    changed : Int
    option_counts : ArrayView[Int]
    }

    Per-question distribution preserves original machine findings after a reviewer corrects an answer.

    QuestionAudit::json

    QuestionReading

    pub struct QuestionReading {
    index : Int
    finding : Finding
    selected : ArrayView[Int]
    bubbles : ArrayView[BubbleReading]
    }

    Registration

    pub struct Registration {
    corners : ArrayView[Point]
    orientation : Int
    contrast : Double
    manual : Bool
    // private fields
    }

    Registration::json

    Registration::rectify

    fn Registration::rectify(self : Registration, image : GrayImage) -> GrayImage raise MarkError

    ResultKind

    pub(all) enum ResultKind {
    Correct
    Incorrect
    Unanswered
    Disqualified
    } derive(Eq,
    Debug
    )

    ReviewAction

    pub(all) enum ReviewAction {
    Choose(ArrayView[Int])
    MarkBlank
    Reject
    } derive(Eq,
    Debug
    )

    ReviewAction::from_json

    ReviewAction::json

    ReviewEvent

    pub struct ReviewEvent {
    revision : Int
    question : Int
    previous : Decision?
    current : Decision?
    }

    ReviewEvent::json

    Scan

    pub struct Scan {
    template : Template
    policy : DetectionPolicy
    registration : Registration
    readings : ArrayView[QuestionReading]
    image_width : Int
    image_height : Int
    image_fingerprint : UInt64
    printed_serial : Int?
    // private fields
    }

    Scan::crop

    fn Scan::crop(self : Scan, image : GrayImage, question : Int, option : Int) -> GrayImage raise MarkError

    Scan::json

    Scan::pending

    fn Scan::pending(self : Scan) -> Array[Int]

    Sheet

    pub struct Sheet {
    id : String
    scan : Scan
    // private fields
    }

    Sheet::csv

    fn Sheet::csv(self : Sheet, allow_pending? : Bool) -> String raise MarkError

    Sheet::decision

    fn Sheet::decision(self : Sheet, index : Int) -> Decision?

    Sheet::history

    fn Sheet::history(self : Sheet) -> Array[ReviewEvent]

    Sheet::journal

    Sheet::json

    Sheet::new

    fn Sheet::new(id : String, scan : Scan) -> Sheet raise MarkError

    Sheet::outcome

    fn Sheet::outcome(self : Sheet, index : Int) -> Outcome raise MarkError

    Sheet::pending

    fn Sheet::pending(self : Sheet) -> Array[Int]

    Sheet::reset

    fn Sheet::reset(self : Sheet, index : Int, expected_revision : Int) -> Unit raise MarkError

    Sheet::review

    fn Sheet::review(self : Sheet, index : Int, action : ReviewAction, note : String, expected_revision : Int) -> Unit raise MarkError

    Sheet::revision

    fn Sheet::revision(self : Sheet) -> Int

    Template

    pub struct Template {
    title : String
    questions : ArrayView[Question]
    signature : UInt
    }

    Template::bubble

    fn Template::bubble(self : Template, question : Int, option : Int) -> Point raise MarkError

    Template::from_json

    Template::json

    Template::new

    fn Template::new(title : String, questions : Array[Question]) -> Template raise MarkError

    Template::numbered_svg

    fn Template::numbered_svg(self : Template, serial : Int) -> String raise MarkError

    Template::svg

    fn Template::svg(self : Template) -> String

    Template::uniform

    fn Template::uniform(title : String, question_count : Int, options : Int, max_marks : Int) -> Template raise MarkError

    calibrate

    fn calibrate(cases : Array[CalibrationCase], policies : Array[DetectionPolicy]) -> Array[CalibrationPoint] raise MarkError

    corners_from_json

    fn corners_from_json(data :
    Json
    ) -> Array[Point] raise MarkError

    error_message

    fn error_message(error : MarkError) -> String

    evaluate

    fn evaluate(sheet : Sheet, truth : Array[Array[Int]]) -> Evaluation raise MarkError

    grades_csv

    fn grades_csv(grades : Array[Grade]) -> String

    parse_json

    fn parse_json(text : String) ->
    Json
    raise MarkError

    read_pgm

    fn read_pgm(input : Bytes) -> GrayImage raise MarkError

    register

    fn register(image : GrayImage, template : Template) -> Registration raise MarkError

    register_corners

    fn register_corners(image : GrayImage, template : Template, centers : Array[Point]) -> Registration raise MarkError

    Use the four printed square centers in template order after automatic registration fails. The printed layout code remains mandatory.

    render_fixture

    fn render_fixture(template : Template, marks : Array[Ink], serial? : Int?) -> GrayImage raise MarkError

    Render the machine-readable portion of a form for reproducible tests.

    replay_journal

    fn replay_journal(scan : Scan, journal :
    Json
    ) -> Sheet raise MarkError

    resume_sheet

    fn resume_sheet(image : GrayImage, template : Template, journal :
    Json
    ) -> Sheet raise MarkError

    scan

    fn scan(image : GrayImage, template : Template, policy? : DetectionPolicy) -> Scan raise MarkError

    scan_corners

    fn scan_corners(image : GrayImage, template : Template, centers : Array[Point], policy? : DetectionPolicy) -> Scan raise MarkError

    simulate_capture

    fn simulate_capture(source : GrayImage, width : Int, height : Int, corners : Array[Point], shadow : Int, noise : Int, seed : UInt) -> GrayImage raise MarkError

    Simulate a photograph with a specified page quadrilateral, exposure drop, and seeded noise. Corner order is canonical top-left through bottom-left.

    truth_from_json

    fn truth_from_json(value :
    Json
    ) -> Array[Array[Int]] raise MarkError