侧边栏壁纸
博主头像
昂洋编程 博主等级

鸟随鸾凤飞腾远,人伴贤良品自高

  • 累计撰写 71 篇文章
  • 累计创建 79 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Springboot集成Tesseract5.3(含环境安装)

Administrator
2023-01-04 / 0 评论 / 0 点赞 / 70 阅读 / 0 字 / 正在检测是否收录...
温馨提示:
本文最后更新于2024-06-14,若内容或图片失效,请留言反馈。 部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

参考: https://blog.51cto.com/u_12826294/3094039

Tesseract安装

以下安装过程中不存在的目录请自行创建

准备文件

Tesseract源码

直接从github上下载源码即可,这里我下载的是5.3版本

Leptonica

tesseract依赖该组件,同样从github上下载1.83.0版本

语言包

语言包这里我只下载了简体中文和英语包,Github上有两个版本,fast版本best版本

以下是下载好的文件
image

安装编译环境

需要安装gcc,并且gcc版本需要在8.0以上,否则后续在编译tesseract时会报空错误,已安装可跳过

安装gcc

yum install gcc gcc-c++ make  -y

查看gcc版本

[root@localhost leptonica-1.83.0]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-9/root/usr/libexec/gcc/x86_64-redhat-linux/9/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-9/root/usr --mandir=/opt/rh/devtoolset-9/root/usr/share/man --infodir=/opt/rh/devtoolset-9/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-9.3.1-20200408/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC)

安装Tesseract-ocr编译必须的包

yum install autoconf automake libtool   libjpeg-devel libpng-devel libtiff-devel -y

安装Leptonica

上传

将leptonica-1.83.0.tar.gz上传到/opt/tesseract目录

安装

tar -xvf leptonica-1.83.0.tar.gz
cd leptonica-1.83.0
./autogen.sh 
./configure
make && make install

添加环境变量

vi /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

检查安装结果

执行命令确认有lept输出即可

[root@localhost leptonica-1.83.0]# pkg-config --list-all | grep lept
lept             leptonica - An open source C library for efficient image processing and image analysis operations

安装Tesseract

上传

将tesseract-main.zip上传到/opt/tesseract目录

解压

unzip tesseract-main.zip

安装

./autogen.sh
./configure
make && make install

这一步时间会较长,如果make和make install报错,检查GCC的版本是否低于8.X,升级完GCC之后configure和make、make install

查看Tesseract版本

[root@localhost tesseract-main]# tesseract -v
tesseract 5.3.0
 leptonica-1.83.0
  libjpeg 6b (libjpeg-turbo 1.2.90) : libpng 1.5.13 : libtiff 4.0.3 : zlib 1.2.7
 Found AVX512BW
 Found AVX512F
 Found AVX512VNNI
 Found AVX2
 Found AVX
 Found FMA
 Found SSE4.1
 Found OpenMP 201511

安装语言包

上传

将chi_sim.traineddata和eng.traineddata上传到/usr/local/share/tessdata

配置环境变量

vi /etc/profile

在最后一行添加

export TESSDATA_PREFIX=/usr/local/share/tessdata

生效环境变量

source /etc/profile

查看语言

[root@localhost tesseract-main]# tesseract --list-langs
List of available languages in "/usr/local/share/tessdata/" (2):
chi_sim
eng

如果查看语言报错,重新到tesseract-main目录执行configure、make、make install

测试效果

切换到tesseract-main目录,执行下面的命令

[root@localhost tesseract-main]# ./tesseract /opt/tesseract/test.png stdout -l chi_sim
和 data

和logs

番 program Fles

一 program Files gx86)
熏 projects

一 QMDownload

鲜 qqpcmgr docpro
条 smbsofpownload
熏Temp

生 test

生 usr

生 党用I具

生 我的四片

生 我的下载

呈 DNA_SAMPLE TQ_TEMPLATExlsx

嘲 DNA样本提取任务单20221209xlsx

2022/9/23 10:52
2021/1T1 17:55
2021/10/26 13:38
2022/2/8 13:20
2022/5/11 14.59
2022/5/5 8.30
2022/12/30 1440
2022/3/24 1439
2022/6/16 9:11
2022/12/19 16:13
2022/2/21 16:52
2022/12/22 13:07
2022/11/15 15:23
2021/10/26 15:35
2022/12/9 17:55
2022/12/9 17:55

文人去
文人去
文人去
文人去
文人去
文人去
文人去
文人去
文人去
文人去
文人去
文人去
文人去
文人去
Microsoft Excel          49 KB

Microsoft Excel          45 KB

命令是将结果输出到控制台了,下面是原图
test
结果有一定的差异,因为没有学习,也没有结合opencv,后面再将

Springboot集成

Maven引入依赖

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

测试代码

package com.hugo.modules.test.tesseract;

import com.hugo.annotation.rest.AnonymousPostMapping;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author zhangmy
 * @date 2023/1/4 11:31
 * @description
 */
@RestController
@RequestMapping("/tesseract")
public class TesseractController {

    @AnonymousPostMapping("/doOcr")
    public ResponseEntity<String> doOcr(MultipartFile file) throws IOException {
        String result = null;
        // 创建实例
        ITesseract instance = new Tesseract();
        // 设置识别语言
        instance.setLanguage("chi_sim");
        // 设置识别引擎
        instance.setOcrEngineMode(1);
        // 读取文件
        BufferedImage image = ImageIO.read(file.getInputStream());
        try {
            // 识别
            result = instance.doOCR(image);
            System.out.println(result);
        } catch (TesseractException e) {
            System.err.println(e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
}

@AnonymousPostMapping是自定义不需要权限的注解,用@PostMapping就行

部署Springboot项目

将刚编写的测试代码项目部署到Tesseract安装的Centos7的服务器上,然后通过postman测试

测试效果

这里使用Postman请求接口的方式测试,还是上面那张图,测试结果如下图
image-1672813543430

0

评论区