使用homebrew安装tesseract
- 先安装homebrew
从清华源下载安装脚本
$ git clone --depth=1 https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install.git brew-install
不必加 sudo 权限,执行安装脚本(输入密码后,一直回车即可)
bash brew-install/install.sh
查看当前shell类型
$ echo $SHELL
/usr/bin/zsh
配置环境变量
$ (echo; echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"') >> ~/.zshrc
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
验证是否可用
$ brew -v
Homebrew 4.6.6
设置镜像源长期,编辑配置文件
$ nano .zshrc
文件末尾加入以下内容
export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export HOMEBREW_PIP_INDEX_URL="https://pypi.tuna.tsinghua.edu.cn/simple"
刷新环境变量
$ source .zshrc
更新
$ brew update
HOMEBREW_BREW_GIT_REMOTE set: using https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git as the Homebrew/brew Git remote.
Already up-to-date.
- 再安装 tesseract
通过 homebrew 来安装 tesseract 和 tesseract-lang,否则使用 chi_sim 语言包出现问题
$ brew install tesseract
$ brew install tesseract-lang
确认安装成功
$ tesseract -v
tesseract 5.5.1
leptonica-1.85.0
libgif 5.2.2 : libjpeg 8d (libjpeg-turbo 3.0.4) : libpng 1.6.50 : libtiff 4.7.0 : zlib 1.3.1 : libwebp 1.6.0 : libopenjp2 2.5.3
Found AVX2
Found AVX
Found FMA
Found SSE4.1
Found OpenMP 201511
Found libarchive 3.8.1 zlib/1.3.1 liblzma/5.8.1 bz2lib/1.0.8 liblz4/1.10.0 libzstd/1.5.7 expat/expat_2.7.1 libb2/system
确认语言包是否安装
$ tesseract --list-langs
List of available languages in "/home/linuxbrew/.linuxbrew/share/tessdata/" (163):
afr
amh
ara
...
cat
ceb
ces
chi_sim
chi_sim_vert
chi_tra
chi_tra_vert
chr
cos
cym
dan
deu
div
dzo
ell
eng
enm
epo
...
uzb_cyrl
vie
yid
yor
从图像提取文本
$ tesseract input_image.png output_text -l chi_sim
input_image.png:需要处理的图像文件。
output_text:输出的文本文件(Tesseract 会自动创建该文件,文件名为 output_text.txt)
-l chi_sim:指定简体中文(chi_sim)。对于其他语言,可以使用相应的语言代码,如 eng(英语)或 fra(法语)
实测感觉效果一般
- 使用 pip 安装 tesseract
$ pip list | grep "tesseract"
pytesseract 0.3.13
查看 tesseract 帮助
$ tesseract --help
Usage:
tesseract --help | --help-extra | --version
tesseract --list-langs
tesseract imagename outputbase [options...] [configfile...]
OCR options:
-l LANG[+LANG] Specify language(s) used for OCR.
NOTE: These options must occur before any configfile.
Single options:
--help Show this help message.
--help-extra Show extra help for advanced users.
--version Show version information.
--list-langs List available languages for tesseract engine.
同时使用 chi_sim+eng 字库识别,输出的是.txt文件,识别效果看起来不错
$ tesseract 图片.jpg 结果 -l chi_sim+eng
参考文章
https://blog.51cto.com/u_16213437/12912862
https://blog.csdn.net/Oh_Python/article/details/128417350
https://www.52runoob.com/archives/4672
https://www.cnblogs.com/linuxAndMcu/p/19049953
https://zhuanlan.zhihu.com/p/110647131
https://www.cnblogs.com/cloud-2-jane/p/18850911