一个基于苹果 Vision 框架构建的强大命令行 OCR 工具,支持单图像和批量处理,并能输出详细的文本框位置信息。
功能特点
- 支持多种图像格式(JPG、JPEG、PNG、WEBP)
- 支持单图像和批量处理模式
- 多语言识别(支持包括英语、中文、日语、韩语和欧洲语言在内的16种语言)
- 详细的 JSON 输出,包含文本位置和置信度分数
- 调试模式下可显示视觉边界框
- 支持 arm64 和 x86_64 架构
系统要求
- macOS 10.15 或更高版本
- 支持 arm64(Apple Silicon)或 x86_64(Intel)架构
建议使用 macOS 13 或更高版本以获得最佳的 OCR 识别效果。
安装
下载预构建的版本
目前 macos-vision-ocr 的版本是 0.0.1,可以在 macos-vision-ocr/releases 页面下载:
从源代码构建
-
确保已安装 Xcode 和命令行工具
-
克隆仓库:
git clone https://github.com/your-username/macos-vision-ocr.git
cd macos-vision-ocr
- 手动构建 macos-vision-ocr:
对于 Apple Silicon (arm64):
swift build -c release --arch arm64
对于 Intel (x86_64):
swift build -c release --arch x86_64
使用方法
单图像处理
处理单个图像并输出到控制台:
./macos-vision-ocr --img ./images/handwriting.webp
指定自定义输出目录:
./macos-vision-ocr --img ./images/handwriting.webp --output ./images
批量处理
处理目录中的多个图像:
./macos-vision-ocr --img-dir ./images --output-dir ./output
将识别后的所有文本合并到单个文件:
./macos-vision-ocr --img-dir ./images --output-dir ./output --merge
调试模式
启用调试模式以可视化文本检测:
./macos-vision-ocr --img ./images/handwriting.webp --debug
命令行选项
选项:
--img <path> 单个图像文件的路径
--output <path> 单图像模式的输出目录
--img-dir <path> 批处理模式的图像目录
--output-dir <path> 批处理模式的输出目录
--merge 在批处理模式下将所有文本输出合并到单个文件
--debug 调试模式:在图像上绘制边界框
--lang 显示支持的识别语言
--help 显示帮助信息
输出格式
工具输出的 JSON 结构如下:
{
"texts": "The Llama 3.2-Vision Collection of multimodal large langyage model5 (LLMS) is a\ncollection of instruction-tuned image reasoning generative models in l1B and 90B\nsizes (text + images in / text ovt). The Llama 3.2-Vision instruction-tuned models\nare optimized for visval recognittion, iage reasoning, captioning, and answering\ngeneral qvestions about an iage. The models outperform many of the available\nopen Source and Closed multimodal models on common industry benchmarKs.",
"info": {
"filepath": "./images/handwriting.webp",
"width": 1600,
"filename": "handwriting.webp",
"height": 720
},
"observations": [
{
"text": "The Llama 3.2-Vision Collection of multimodal large langyage model5 (LLMS) is a",
"confidence": 0.5,
"quad": {
"topLeft": {
"y": 0.28333333395755611,
"x": 0.09011629800287288
},
"topRight": {
"x": 0.87936045388666206,
"y": 0.28333333395755611
},
"bottomLeft": {
"x": 0.09011629800287288,
"y": 0.35483871098527953
},
"bottomRight": {
"x": 0.87936045388666206,
"y": 0.35483871098527953
}
}
}
]
}
调试输出
使用 --debug
时,工具将:
- 创建一个带有 "_boxes.png" 后缀的新图像
- 在检测到的文本周围绘制红色边界框
- 在输入图像的同一目录中保存调试图像
支持的语言
通过 macos-vision-ocr --lang
可以输出当前系统所支持的语言,在 kakuqo 电脑上(macOS 14.4.1)输出的结果如下:
- 英语 (en-US)
- 法语 (fr-FR)
- 意大利语 (it-IT)
- 德语 (de-DE)
- 西班牙语 (es-ES)
- 葡萄牙语(巴西) (pt-BR)
- 简体中文 (zh-Hans)
- 繁体中文 (zh-Hant)
- 简体粤语 (yue-Hans)
- 繁体粤语 (yue-Hant)
- 韩语 (ko-KR)
- 日语 (ja-JP)
- 俄语 (ru-RU)
- 乌克兰语 (uk-UA)
- 泰语 (th-TH)
- 越南语 (vi-VT)
Node.js 集成示例
以下是在 Node.js 应用程序中使用 macos-vision-ocr
的示例:
const { exec } = require("child_process");
const util = require("util");
const execPromise = util.promisify(exec);
async function performOCR(imagePath, outputDir = null) {
try {
// 构建命令
let command = `./macos-vision-ocr --img "${imagePath}"`;
if (outputDir) {
command += ` --output "${outputDir}"`;
}
// 执行 OCR 命令
const { stdout, stderr } = await execPromise(command);
if (stderr) {
console.error("错误:", stderr);
return null;
}
// 解析 JSON 输出
console.log("stdout:", stdout);
const result = JSON.parse(stdout);
return result;
} catch (error) {
console.error("OCR 处理失败:", error);
return null;
}
}
// 使用示例
async function example() {
const result = await performOCR("./images/handwriting.webp");
if (result) {
console.log("提取的文本:", result.texts);
console.log("文本位置:", result.observations);
}
}
example();
常见问题
-
图像加载失败
- 确保图像路径正确
- 验证图像格式是否受支持(JPG、JPEG、PNG、WEBP)
- 检查文件权限
-
未检测到文本
- 确保图像包含清晰、可读的文本
- 检查文本大小是否不太小(最小文本高度为图像高度的1%)
- 验证文本语言是否受支持