image.vision\ocr\ocr_bench.mbt

85.7% · 108 / 126 points · 272 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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