File size: 3,178 Bytes
ae97c5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""Conservative page orientation from detached dots beside Latin letter stems.

This examines glyph geometry only; it performs no text readout. Pages without
enough consistent evidence retain their supplied orientation.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from PIL import Image


def document_rotation(image: Image.Image) -> tuple[int, list[int]]:
    import cv2
    import numpy as np

    page = image.convert("L")
    page.thumbnail((1600, 1600))
    pixels = np.asarray(page)
    _, ink = cv2.threshold(pixels, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
    _, _, stats, _ = cv2.connectedComponentsWithStats(ink, connectivity=8)
    stats = stats[1:]
    votes = [0, 0, 0, 0]
    if not len(stats) or len(stats) > 12_000:
        return 0, votes
    width, height, area = stats[:, 2:].T
    short, long = np.minimum(width, height), np.maximum(width, height)
    stems = stats[(long >= 9) & (long <= 60) & (long >= 2.5 * short) & (area >= 8)]
    dots = stats[(width <= 12) & (height <= 12) & (area >= 2) & (area <= 100)]
    if not len(stems) or not len(dots) or len(stems) > 1200 or len(dots) > 2400:
        return 0, votes
    dx, dy, dw, dh, da = dots.T
    for sx, sy, sw, sh, sa in stems:
        vertical = sh > sw
        across = dx + dw / 2 if vertical else dy + dh / 2
        center = sx + sw / 2 if vertical else sy + sh / 2
        start, length = (sy, sh) if vertical else (sx, sw)
        left, size = (dy, dh) if vertical else (dx, dw)
        thin = sw if vertical else sh
        aligned = (abs(across - center) <= max(1.5, thin * 0.6)) & (
            size <= length * 0.4
        )
        aligned &= (dw <= max(3, thin * 2)) if vertical else (dh <= max(3, thin * 2))
        aligned &= da < sa * 0.7
        before = (
            aligned & (left + size <= start) & (start - left - size <= length * 0.45)
        )
        after = (
            aligned
            & (left >= start + length)
            & (left - start - length <= length * 0.45)
        )
        if vertical:
            votes[0] += int(np.any(before))
            votes[2] += int(np.any(after))
        else:
            votes[1] += int(np.any(before))
            votes[3] += int(np.any(after))
    letters = (area >= 10) & (short >= 3) & (long <= 60) & (long <= 4 * short)
    if int(letters.sum()) < 80:
        return 0, votes
    aspect = float(np.median(width[letters] / height[letters]))
    if aspect < 0.8:
        directions = (0, 2)
    elif aspect > 1.25:
        directions = (1, 3)
    else:
        return 0, votes
    # Text-line direction excludes accidental dot/stem pairs in the other axis.
    winner = max(directions, key=votes.__getitem__)
    runner = votes[directions[0] if winner == directions[1] else directions[1]]
    if (
        votes[winner] < 12
        or votes[winner] < 3 * max(1, runner)
        or votes[winner] < 0.7 * (votes[winner] + runner)
    ):
        return 0, votes
    return winner * 90, votes


def orient_document(image: Image.Image) -> Image.Image:
    rotation, _ = document_rotation(image)
    return image.rotate(-rotation, expand=True) if rotation else image