image.vision\ocr\ocr_bench.mbt
85.7% · 108 / 126 points · 272 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 英文字符识别基准(S30) - 3
/// - 4
/// 目标不是"测一遍就算",而是把 GAP-22 的 289/348 换成一个**能复跑、有阈值**的数字: - 5
/// - 6
/// - 类别:0-9 + A-Z 共 36 类(与内建字库同集); - 7
/// - 倍率:3 / 4 / 5 三档(同一字符被渲染成不同像素密度); - 8
/// - 退化:`clean` 与 `degraded` 两档。退化档模拟扫描重采样——面积平均缩到 0.75 倍再双线性 - 9
/// 放大回原尺寸、3×3 均值模糊、±8 灰度噪声(确定性伪随机,同一输入永远同一输出)。 - 10
/// - 11
/// 因此 36 × 3 × 2 = 216 个样本。判对的条件是**整张图只切出一个字符且值等于标签**, - 12
/// 切多了或切少了都算错(宁可低估,不要靠"取第一个字符"把结果做漂亮)。 - 13
- 14
///| - 15
/// 单字符的命中统计。 - 16
pub(all) struct OcrBenchChar { - 17
label : String - 18
hits : Int32 - 19
total : Int32 - 20
} - 21
- 22
///| - 23
/// 一对混淆(真值 → 误判值,出现次数)。 - 24
pub(all) struct OcrConfusion { - 25
expect : String - 26
got : String - 27
count : Int32 - 28
} - 29
- 30
///| - 31
/// 整轮基准的结果。 - 32
pub(all) struct OcrBenchReport { - 33
cases : Int32 - 34
hits : Int32 - 35
accuracy : Double - 36
clean_accuracy : Double - 37
degraded_accuracy : Double - 38
per_char : Array[OcrBenchChar] - 39
confusion : Array[OcrConfusion] - 40
} - 41
- 42
///| - 43
/// 36 类标签(0-9 / A-Z),顺序稳定。 - 44
pub fn ocr_bench_labels() -> Array[Char] { - 45●
let labels : Array[Char] = [] - 46
for code in 48..<58 { - 47●
match code.to_char() { - 48●
Some(ch) => labels.push(ch) - 49○
None => () - 50
} - 51
} - 52
for code in 65..<91 { - 53●
match code.to_char() { - 54●
Some(ch) => labels.push(ch) - 55○
None => () - 56
} - 57
} - 58
labels - 59
} - 60
- 61
///| - 62
/// 确定性线性同余(噪声用):不依赖外部随机源,四后端一致。 - 63
fn ab_lcg(seed : Int32) -> Int32 { - 64●
(seed * 1103515245 + 12345) & 0x7FFFFFFF - 65
} - 66
- 67
///| - 68
/// 面积平均缩放到 `ratio` 再双线性放大回原尺寸(模拟扫描重采样)。 - 69
fn ab_resample(img : @types.Image, ratio : Double) -> @types.Image { - 70●
let w = img.width - 71
let h = img.height - 72●
let sw = @types.int_max(1, (w.to_double() * ratio).to_int()) - 73●
let sh = @types.int_max(1, (h.to_double() * ratio).to_int()) - 74
// 1) 下采样(每目标像素取源块均值) - 75●
let small : Array[Byte] = Array::make(sw * sh, b'\xFF') - 76
for y in 0..<sh { - 77●
for x in 0..<sw { - 78●
let x0 = x * w / sw - 79●
let x1 = @types.int_max(x0 + 1, (x + 1) * w / sw) - 80
let y0 = y * h / sh - 81●
let y1 = @types.int_max(y0 + 1, (y + 1) * h / sh) - 82
let mut sum = 0.0 - 83
let mut n = 0 - 84
for sy in y0..<y1 { - 85●
for sx in x0..<x1 { - 86●
sum += img.data[sy * w + sx].to_double() - 87
n += 1 - 88
} - 89
} - 90●
small[y * sw + x] = (sum / n.to_double()).to_int().to_byte() - 91
} - 92
} - 93
// 2) 放大回原尺寸(双线性) - 94●
let out : Array[Byte] = Array::make(w * h, b'\xFF') - 95
for y in 0..<h { - 96●
for x in 0..<w { - 97●
let fx = (x.to_double() + 0.5) * sw.to_double() / w.to_double() - 0.5 - 98●
let fy = (y.to_double() + 0.5) * sh.to_double() / h.to_double() - 0.5 - 99●
let x0 = fx.floor().to_int() - 100●
let y0 = fy.floor().to_int() - 101●
let dx = fx - x0.to_double() - 102●
let dy = fy - y0.to_double() - 103
let pick = fn(px : Int32, py : Int32) -> Double { - 104●
if px < 0 || py < 0 || px >= sw || py >= sh { - 105●
255.0 - 106
} else { - 107●
small[py * sw + px].to_double() - 108
} - 109
} - 110●
let v00 = pick(x0, y0) - 111●
let v10 = pick(x0 + 1, y0) - 112●
let v01 = pick(x0, y0 + 1) - 113●
let v11 = pick(x0 + 1, y0 + 1) - 114
let top = v00 + (v10 - v00) * dx - 115
let bottom = v01 + (v11 - v01) * dx - 116●
out[y * w + x] = (top + (bottom - top) * dy).to_int().to_byte() - 117
} - 118
} - 119
@types.Image::{ - 120
width: w, - 121
height: h, - 122
channels: 1, - 123●
data: Bytes::from_array(out), - 124
} - 125
} - 126
- 127
///| - 128
/// 3×3 均值模糊 + ±8 灰度噪声。 - 129
fn ab_blur_noise(img : @types.Image, seed : Int32) -> @types.Image { - 130●
let w = img.width - 131
let h = img.height - 132●
let out : Array[Byte] = Array::make(w * h, b'\xFF') - 133
let mut state = seed - 134
for y in 0..<h { - 135●
for x in 0..<w { - 136●
let mut sum = 0.0 - 137
let mut n = 0 - 138
for dy in -1..<2 { - 139●
for dx in -1..<2 { - 140●
let sx = x + dx - 141
let sy = y + dy - 142
if sx >= 0 && sy >= 0 && sx < w && sy < h { - 143●
sum += img.data[sy * w + sx].to_double() - 144
n += 1 - 145
} - 146
} - 147
} - 148●
state = ab_lcg(state) - 149
let noise = state % 17 - 8 - 150●
let v = sum / n.to_double() + noise.to_double() - 151●
let clamped = if v < 0.0 { 0.0 } else if v > 255.0 { 255.0 } else { v } - 152●
out[y * w + x] = clamped.to_int().to_byte() - 153
} - 154
} - 155
@types.Image::{ - 156
width: w, - 157
height: h, - 158
channels: 1, - 159●
data: Bytes::from_array(out), - 160
} - 161
} - 162
- 163
///| - 164
/// 造一个基准样本:`mode` 为 `clean` / `degraded`。 - 165
fn ab_sample(ch : Char, scale : Int32, degraded : Bool) -> @types.Image? { - 166●
match render_glyph(ch, scale~, margin=8) { - 167●
None => None - 168
Some(img) => - 169●
if degraded { - 170●
Some(ab_blur_noise(ab_resample(img, 0.75), scale * 7919 + 13)) - 171
} else { - 172●
Some(img) - 173
} - 174
} - 175
} - 176
- 177
///| - 178
/// 跑整轮基准:36 类 × 3 档倍率 × 2 档退化 = 216 个样本。 - 179
pub fn ocr_letter_bench() -> OcrBenchReport raise @types.LoadError { - 180●
let labels = ocr_bench_labels() - 181
let per_char : Array[OcrBenchChar] = [] - 182
let conf : Array[OcrConfusion] = [] - 183
let mut cases = 0 - 184
let mut hits = 0 - 185
let mut clean_cases = 0 - 186
let mut clean_hits = 0 - 187
let mut degraded_cases = 0 - 188
let mut degraded_hits = 0 - 189
for ch in labels { - 190●
let label = ch.to_string() - 191
let mut ch_hits = 0 - 192
let mut ch_total = 0 - 193
for scale in [3, 4, 5] { - 194●
for degraded in [false, true] { - 195●
match ab_sample(ch, scale, degraded) { - 196○
None => () - 197●
Some(img) => { - 198●
let res = recognize_english(img) - 199
let text = res.text - 200
// 只认「单字符且等于标签」;切多头/切秃都不算对 - 201●
let ok = text.length() == 1 && text == label - 202
cases += 1 - 203
ch_total += 1 - 204
if degraded { - 205●
degraded_cases += 1 - 206
} else { - 207●
clean_cases += 1 - 208
} - 209
if ok { - 210●
hits += 1 - 211
ch_hits += 1 - 212
if degraded { - 213●
degraded_hits += 1 - 214
} else { - 215●
clean_hits += 1 - 216
} - 217
} else { - 218○
let got = if text == "" { "(空)" } else { text } - 219
let mut found = false - 220○
for i in 0..<conf.length() { - 221○
if conf[i].expect == label && conf[i].got == got { - 222○
conf[i] = OcrConfusion::{ - 223
expect: label, - 224
got, - 225
count: conf[i].count + 1, - 226
} - 227
found = true - 228
} - 229
} - 230
if !found { - 231○
conf.push(OcrConfusion::{ expect: label, got, count: 1, }) - 232
} - 233
} - 234
} - 235
} - 236
} - 237
} - 238●
per_char.push(OcrBenchChar::{ label, hits: ch_hits, total: ch_total, }) - 239
} - 240
// 混淆对按次数降序(手写冒泡:数组最多几十项,不值得引排序依赖) - 241
let mut i = 0 - 242●
while i < conf.length() { - 243○
let mut j = i + 1 - 244○
while j < conf.length() { - 245○
if conf[j].count > conf[i].count { - 246○
let tmp = conf[i] - 247
conf[i] = conf[j] - 248
conf[j] = tmp - 249
} - 250
j += 1 - 251
} - 252
i += 1 - 253
} - 254◐
let acc = if cases > 0 { hits.to_double() / cases.to_double() } else { 0.0 } - 255
{ - 256
cases, - 257
hits, - 258
accuracy: acc, - 259
clean_accuracy: if clean_cases > 0 { - 260●
clean_hits.to_double() / clean_cases.to_double() - 261
} else { - 262○
0.0 - 263
}, - 264
degraded_accuracy: if degraded_cases > 0 { - 265●
degraded_hits.to_double() / degraded_cases.to_double() - 266
} else { - 267○
0.0 - 268
}, - 269
per_char, - 270
confusion: conf, - 271
} - 272
}