imgcli\cli_vision.mbt

73.1% · 38 / 52 points · 69 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/// `vision` — 文档侧预处理入口(同尺寸,输出不吃缩放)。
  3. 3///
  4. 4/// 五个 op 全走 `image.vision/ocr` 与 `image.process/color` 的既有实现,CLI 只做
  5. 5/// 「多通道先转灰度 → 按 op 分发 → 落盘」三件事。加这条是为了让 vision 侧的
  6. 6/// 处理管道也能被 `imgtool quality` 同一口径点评(S29 golden 用它)。
  7. 7fn cmd_vision(flags : Flags) -> Out raise @types.ImageError {
  8. 8● let src = flags.positional[0]
  9. 9 let dst = flags.positional[1]
  10. 10● let input = src_bytes(src)
  11. 11● let img = @lib.decode_with_limits(input, limits_of(flags))
  12. 12● let op = flags.str("op", "otsu")
  13. 13 let gray = if img.channels >= 3 {
  14. 14● @color.to_grayscale(img) catch {
  15. 15○ e => raise @types.image_error_from_load(e)
  16. 16 }
  17. 17 } else {
  18. 18● img
  19. 19 }
  20. 20● let block = flags.int("block", 15)
  21. 21● let kappa = flags.dbl("kappa", 0.5)
  22. 22● let out = match op {
  23. 23● "gray" => gray
  24. 24 "otsu" =>
  25. 25● @ocr.otsu_threshold(gray) catch {
  26. 26○ e => raise @types.image_error_from_load(e)
  27. 27 }
  28. 28 "sauvola" =>
  29. 29○ @ocr.sauvola_threshold(gray, window_size=block, k=kappa) catch {
  30. 30○ e => raise @types.image_error_from_load(e)
  31. 31 }
  32. 32 "shadow" =>
  33. 33○ @ocr.shadow_removal(gray, kernel_size=block) catch {
  34. 34○ e => raise @types.image_error_from_load(e)
  35. 35 }
  36. 36 "normalize" =>
  37. 37○ @ocr.background_normalization(gray, kernel_size=block) catch {
  38. 38○ e => raise @types.image_error_from_load(e)
  39. 39 }
  40. 40 _ =>
  41. 41● raise @types.ImageError::Process(
  42. 42 @types.ErrorCode::InvalidArgument,
  43. 43 "未知 --op: \{op}(可用 gray/otsu/sauvola/shadow/normalize)",
  44. 44 )
  45. 45 }
  46. 46● let fmt = out_format(flags, dst, writable_format_of(input))
  47. 47● let quality = quality_of(flags)
  48. 48● let data = encode_image(out, fmt, quality)
  49. 49● let dry = flags.has("dry-run")
  50. 50● match budget_gate(src, dst, fmt, data, flags.int("max-bytes", 0), dry) {
  51. 51○ Some(b) => return b
  52. 52● None => ()
  53. 53 }
  54. 54● let to_stdout = emit_bytes(dst, data, dry)
  55. 55● result({
  56. 56● "src": jstr(src),
  57. 57● "dst": jstr(dst),
  58. 58● "op": jstr(op),
  59. 59● "block": jnum(block),
  60. 60● "channels": jnum(out.channels),
  61. 61● "width": jnum(out.width),
  62. 62● "height": jnum(out.height),
  63. 63● "format": jstr(format_name(fmt)),
  64. 64● "bytes": jnum(data.length().to_int()),
  65. 65● "dry_run": jbool(dry),
  66. 66● "wrote": jbool(!dry),
  67. 67● "to_stdout": jbool(to_stdout),
  68. 68 })
  69. 69}