Java文字识别: 使用Tess4j解析图片内的文本

Java文字识别,使用Tess4j解析图片内的文本

项目里突然接到一个需求,需要识别图片内的文本,经过一番调研查找,发现tesseract提供开箱即用的OCR功能,并且提供已经训练好的中文语言包,以及对应的javaApi的jar包tess4j,对俺这种没有OCR经验的小伙子十分友好。

Tesseract

github https://github.com/tesseract-ocr
官网 http://tess4j.sourceforge.net/
语言包 https://github.com/tesseract-ocr/tessdata  
      https://github.com/tesseract-ocr/tessdata_fast  
      https://github.com/tesseract-ocr/tessdata_best
语言包说明:tessdata_fast是速度最快的语言包,但是准确率最低,tessdata_best是准确率最高的语言包,但识别效率最低,tessdata介于两者之间,建议使用tessdata_best。

maven依赖

<dependency>
    <groupId>net.sourceforge.tess4j</groupId>
    <artifactId>tess4j</artifactId>
    <version>5.4.0</version>
</dependency>

安装Tesseract环境

win系统

安装包下载地址:https://github.com/UB-Mannheim/tesseract/wiki  
根据自己系统选择对应的安装包后,下一步下一步安装完成即可  

liunx系统下

#安装环境依赖包括Leptonica
yum -y install automake libtool libtiff-devel libjpeg-devel libpng-devel
wget https://pkg-config.freedesktop.org/releases/pkg-config-0.29.tar.gz
tar -xvf pkg-config-0.29.tar.gz
cd pkg-config-0.29/
./configure --with-internal-glib
make
make check
make install
wget http://www.leptonica.org/source/leptonica-1.78.0.tar.gz
tar -zxvf leptonica-1.78.0.tar.gz
cd leptonica-1.78.0/
./configure
make && make install

#编辑系统变量,修改/etc/profile文件,
vim /etc/profile
#追加以下
export LD_LIBRARY_PATH=$LD_LIBRARY_PAYT:/usr/local/lib
export LIBLEPT_HEADERSDIR=/usr/local/include
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
#保存后运行,生效环境变量 
source /etc/profile 

#安装Tesseract
wget https://github.com/tesseract-ocr/tesseract/archive/refs/tags/5.2.0.tar.gz
tar -zxvf tesseract-5.2.0.tar.gz
cd tesseract-5.2.0/
./autogen.sh
./configure --with-extra-includes=/usr/local/include --with-extra-libraries=/usr/local/lib
make && make install

mac系统下

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
brew install tesseract

配置中文识别库

下载 https://github.com/tesseract-ocr/tessdata_best 下的chi_sim开头的文件,
然后保存至tesseract的安装目录的tessdata包下即可。  

java文字识别代码

整图

String imgSrc = "test.png";
File imgFile = new File(imgSrc);
StopWatch watch = new StopWatch();
watch.start();
System.out.println("开始解析");
//使用ImageIO读取图片对象
BufferedImage image = ImageIO.read(imgFile);
//创建tesseract对象
Tesseract tes = new Tesseract();
//语言包位置  根据实际环境修改替换
tes.setDatapath("D:\\tesseract\\tessdata");
//配置使用的语言  中文
tes.setLanguage("chi_sim");
String imgText = tes.doOCR(image);
System.out.println("解析结束,使用时间:"+watch.getTotalTimeSeconds()+"秒");
System.out.println("解析结果");
System.out.println(imgText);

指定区域

String imgSrc = "test.png";
File imgFile = new File(imgSrc);
StopWatch watch = new StopWatch();
watch.start();
System.out.println("开始解析");
//使用ImageIO读取图片对象
BufferedImage image = ImageIO.read(imgFile);
//创建tesseract对象
Tesseract tes = new Tesseract();
//语言包位置  根据实际环境修改替换
tes.setDatapath("D:\\tesseract\\tessdata");
//配置使用的语言  中文
tes.setLanguage("chi_sim");
//配置解析区域 x,y 起点即左上角坐标, w,h 区域的宽高
int x = 100;
int y = 100;
int w = 100;
int h = 100;
Rectangle rect = new Rectangle(x, y, w, h);
String imgText = tes.doOCR(image, rect);
System.out.println("解析结束,使用时间:"+watch.getTotalTimeSeconds()+"秒");
System.out.println("解析结果");
System.out.println(imgText);

文末

使用tessdata_best的识别库主要是识别的准确率差别太大了,譬如数字5,tessdata_best能正常识别出来,tessdata和tessdata_fast会识别成字母s。
即使是tessdata_best仍有局限性,如果需要更进一步的准确率,就需要自己进行训练了,这个只能自行参照官网。
如非必要,建议还是使用指定区域去作文字识别,整图下来和指定区域的识别时间差的不是一星半点,图片越大差别会越大。
毕竟是开源还有开箱即用的,还要啥自行车呢是吧,简单的识别和应用也基本满足了。
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值