imgcli\cli_ocrbench.mbt

68.8% · 22 / 32 points · 52 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/// `ocrbench` — 英文字符识别基准(S30 的可复算入口)。
  3. 3///
  4. 4/// 跑库里 36 类 × 3 档倍率 × 2 档退化 = 216 个样本,回准确率、clean/degraded 分档、
  5. 5/// 逐字符命中与混淆对(按次数降序,最多 12 对)。
  6. 6/// `--min` 给阈值(默认 0.85),不达标按普通失败处理(退出码 1),回执里同时带 `accuracy`。
  7. 7fn cmd_ocrbench(flags : Flags) -> Out raise @types.ImageError {
  8. 8● let min_acc = flags.dbl("min", 0.85)
  9. 9● let report = @ocr.ocr_letter_bench() catch {
  10. 10○ e => raise @types.image_error_from_load(e)
  11. 11 }
  12. 12 let per_char : Array[@json.Json] = []
  13. 13 for one in report.per_char {
  14. 14● per_char.push(
  15. 15● @json.Json::object({
  16. 16● "label": jstr(one.label),
  17. 17● "hits": jnum(one.hits),
  18. 18● "total": jnum(one.total),
  19. 19 }),
  20. 20 )
  21. 21 }
  22. 22 let confusion : Array[@json.Json] = []
  23. 23 for one in report.confusion {
  24. 24○ if confusion.length() >= 12 {
  25. 25○ break
  26. 26 }
  27. 27○ confusion.push(
  28. 28○ @json.Json::object({
  29. 29○ "expect": jstr(one.expect),
  30. 30○ "got": jstr(one.got),
  31. 31○ "count": jnum(one.count),
  32. 32 }),
  33. 33 )
  34. 34 }
  35. 35● let out = result({
  36. 36● "cases": jnum(report.cases),
  37. 37● "hits": jnum(report.hits),
  38. 38● "accuracy": q3(report.accuracy),
  39. 39● "clean_accuracy": q3(report.clean_accuracy),
  40. 40● "degraded_accuracy": q3(report.degraded_accuracy),
  41. 41● "min_accuracy": q3(min_acc),
  42. 42● "per_char": @json.Json::array(per_char),
  43. 43● "confusion": @json.Json::array(confusion),
  44. 44 })
  45. 45 if report.accuracy < min_acc {
  46. 46 // 不 raise:把 per_char / confusion 一起交给调用方,失败也有可读的回执
  47. 47● out["ok"] = jbool(false)
  48. 48● out["code"] = jstr("ocr/accuracy_below_min")
  49. 49● out["exit_code"] = jnum(1)
  50. 50 }
  51. 51 out
  52. 52}