Image Classification
ONNX
onnxruntime
document-orientation
deskew
ocr
image-preprocessing
cpu
File size: 2,678 Bytes
a4cdb00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Measure default and fast quarter-turn decisions on the identical decoded views."""

import argparse
from pathlib import Path
import hashlib
import json
import sys
from time import perf_counter

from PIL import Image

HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE.parent))
from folio_orient import PageOrienter, transpose_cw


def run(args):
    model = PageOrienter(args.model)
    if args.cohort == "contemporary":
        rows = json.loads((args.images / "manifest.json").read_text())
        samples = [
            (r["page_hash"] + ".png", r["category"], "print")
            for r in rows
            if r["reason"] == "eligible"
        ]
    else:
        rows = json.loads((HERE / "pages.json").read_text())
        samples = [
            (r["file"], r["book"], r["page_type"]) for r in rows if r["split"] == "test"
        ]
    results = []
    for filename, category, page_type in samples:
        with Image.open(args.images / filename) as image:
            page = image.convert("RGB")
        seed = int.from_bytes(hashlib.sha256(filename.encode()).digest()[:4], "big")
        for tilt in (0, (1.5 + seed % 550 / 100) * (-1 if seed % 2 else 1)):
            tested = (
                page.rotate(
                    tilt, Image.Resampling.BICUBIC, fillcolor=page.getpixel((0, 0))
                )
                if tilt
                else page
            )
            for turn in (0, 90, 180, 270):
                view = transpose_cw(tested, (-turn) % 360)
                row = dict(
                    file=filename,
                    category=category,
                    page_type=page_type,
                    target_cw=turn,
                    tilt_ccw=tilt,
                )
                for mode in ("fast", "default"):
                    start = perf_counter()
                    pred = model.estimate(view, fast=(mode == "fast"), deskew=False)
                    row[mode + "_ms"] = (perf_counter() - start) * 1000
                    row[mode + "_turn"] = pred["quarter_turn_cw"]
                    row[mode + "_score"] = pred["agreement"]
                    if mode == "fast":
                        row["fast_path"] = pred["fast_path"]
                results.append(row)
    args.output.write_text(json.dumps(results, indent=1) + "\n")


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("cohort", choices=["historical", "contemporary"])
    parser.add_argument("images", type=Path)
    parser.add_argument("output", type=Path)
    parser.add_argument("--model", type=Path, default=HERE.parent / "model.onnx")
    run(parser.parse_args())