///|
pub struct Template {
priv label : String
priv pixels : Array[Bool]
}
///|
/// Build a glyph from five rows of three binary cells.
pub fn Template::new(
label : String,
rows : Array[String],
) -> Template raise OcrError {
let pixels : Array[Bool] = []
if label == "" || rows.length() != 5 {
raise InvalidTemplate("expected a label and five rows")
}
for row in rows {
if row.length() != 3 {
raise InvalidTemplate("expected three cells per row")
}
for ch in row {
match ch {
'0' => pixels.push(false)
'1' => pixels.push(true)
_ => raise InvalidTemplate("expected binary template cells")
}
}
}
{ label, pixels, }
}
///|
#derive(ToJson)
pub struct Glyph {
text : String
bounds : Box
distance : Int32
}
///|
pub extend Glyph with ToJson::{to_json}
///|
#derive(ToJson)
pub struct Line {
text : String
glyphs : Array[Glyph]
}
///|
pub extend Line with ToJson::{to_json}
///|
fn classify(image : Image, bounds : Box, templates : Array[Template]) -> Glyph {
let mut best = "�"
let mut distance = 16
let mut tied = false
let sample = Array::makei(15, index => {
let x = bounds.left + (index % 3 * 2 + 1) * (bounds.right - bounds.left) / 6
let y = bounds.top + (index / 3 * 2 + 1) * (bounds.bottom - bounds.top) / 10
image.pixel(x, y)
})
for template in templates {
let mut score = 0
for index in 0..<15 {
if sample[index] != template.pixels[index] {
score += 1
}
}
if score < distance {
distance = score
best = template.label
tied = false
} else if score == distance && template.label != best {
tied = true
}
}
{ text: if tied || distance > 2 { "�" } else { best }, bounds, distance, }
}
///|
fn recognize_line(
image : Image,
top : Int32,
bottom : Int32,
templates : Array[Template],
) -> Line {
let glyphs : Array[Glyph] = []
let text = StringBuilder()
let mut left = 0
while left < image.width {
if !image.column_span(left, top, bottom) {
left += 1
continue
}
let mut right = left + 1
while right < image.width && image.column_span(right, top, bottom) {
right += 1
}
let mut glyph_top = top
let mut glyph_bottom = bottom
while !image.row_span(glyph_top, left, right) {
glyph_top += 1
}
while !image.row_span(glyph_bottom - 1, left, right) {
glyph_bottom -= 1
}
let glyph = classify(
image,
{ left, top: glyph_top, right, bottom: glyph_bottom, },
templates,
)
text.write_string(glyph.text)
glyphs.push(glyph)
left = right
}
{ text: text.to_string(), glyphs, }
}
///|
/// Recognize separated glyphs in horizontal lines, in top-to-bottom order.
/// Bounds use exclusive right and bottom coordinates.
pub fn recognize(image : Image, templates : Array[Template]) -> Array[Line] {
let lines : Array[Line] = []
let mut top = 0
while top < image.height {
if !image.row_span(top, 0, image.width) {
top += 1
continue
}
let mut bottom = top + 1
while bottom < image.height && image.row_span(bottom, 0, image.width) {
bottom += 1
}
lines.push(recognize_line(image, top, bottom, templates))
top = bottom
}
lines
}
///|
/// Return the built-in 3×5 digit templates.
pub fn digit_templates() -> Array[Template] {
let patterns = [
["111", "101", "101", "101", "111"],
["010", "110", "010", "010", "111"],
["111", "001", "111", "100", "111"],
["111", "001", "111", "001", "111"],
["101", "101", "111", "001", "001"],
["111", "100", "111", "001", "111"],
["111", "100", "111", "101", "111"],
["111", "001", "010", "010", "010"],
["111", "101", "111", "101", "111"],
["111", "101", "111", "001", "111"],
]
Array::makei(patterns.length(), index => {
let pixels = []
for row in patterns[index] {
for ch in row {
pixels.push(ch == '1')
}
}
{ label: index.to_string(), pixels, }
})
}