SpringBoot中使用tess4j进行OCR(在macos上面开发)

问题

最近需要做OCR的实现,需要在Spring Boot工程中引入tess4j库,进行OCR识别。然后,这里使用macos m1进行开发。出现了找不到动态链接库的问题。主要就是找不到如下动态链接库:

  • darwin-aarch64/libtesseract.dylib
  • darwin-aarch64/libleptonica.dylib

思路

在macos上面安装tesseract,tesseract-lang,leptonica这三个库,macos上面本地安装完成后,然后,把上面两个缺少的动态链接库文件,手动加入到tess4j的jar里面即可。

安装tesseract

bash 复制代码
# tesseract主要程序
brew install tesseract
# tesseract语音支持包,这里默认会把所有语言给安装上
brew install tesseract-lang
# 使用带中文文字的图片,手动测试即可
tesseract 0.png output -l chi_sim --psm 3 --oem 3

这里tesseract相关参数需要解释一下:

  • -l chi_sim:这是指定语言。这里指定的识别语音是中文简体;
  • --psm 3:Page segmentation modes (PSM)页面分割模式,这里使用3是自动模式,默认也是3,自动模式。详细参数如下:
bash 复制代码
Page segmentation modes (PSM):
  0|osd_only                Orientation and script detection (OSD) only.
  1|auto_osd                Automatic page segmentation with OSD.
  2|auto_only               Automatic page segmentation, but no OSD, or OCR. (not implemented)
  3|auto                    Fully automatic page segmentation, but no OSD. (Default)
  4|single_column           Assume a single column of text of variable sizes.
  5|single_block_vert_text  Assume a single uniform block of vertically aligned text.
  6|single_block            Assume a single uniform block of text.
  7|single_line             Treat the image as a single text line.
  8|single_word             Treat the image as a single word.
  9|circle_word             Treat the image as a single word in a circle.
 10|single_char             Treat the image as a single character.
 11|sparse_text             Sparse text. Find as much text as possible in no particular order.
 12|sparse_text_osd         Sparse text with OSD.
 13|raw_line                Raw line. Treat the image as a single text line,
                            bypassing hacks that are Tesseract-specific.
  • --oem 3:OCR Engine modes (OEM)OCR引擎模式,这里使用3为默认模式,即能用就用。详细如下:
bash 复制代码
OCR Engine modes (OEM):
  0|tesseract_only          Legacy engine only.
  1|lstm_only               Neural nets LSTM engine only.
  2|tesseract_lstm_combined Legacy + LSTM engines.
  3|default                 Default, based on what is available.

如果上面能够正常手动使用tesseract,进行OCR,那就说明tesseract安装没有问题,可以进行下一步了。

安装leptonica

bash 复制代码
brew install leptonica

这安装主要也是为了获得其中一个动态链接库。

springboot中使用tess4j

pom.xml

xml 复制代码
<dependency>
    <groupId>net.sourceforge.tess4j</groupId>
    <artifactId>tess4j</artifactId>
    <version>5.16.0</version>
</dependency>

TesseractConfig.java

java 复制代码
package com.xxxx.config;

import net.sourceforge.tess4j.Tesseract;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.IOException;

@Configuration
public class TesseractConfig {

    @Value("classpath:tessdata/")
    private Resource tessdataResource;

    @Bean
    Tesseract tesseract() throws IOException {
        Tesseract tesseract = new Tesseract();
        File tessdataDir = tessdataResource.getFile();
        tesseract.setDatapath(tessdataDir.getPath()); //files of the example : https://github.com/tesseract-ocr/tessdata
        tesseract.setLanguage("chi_sim");
        tesseract.setOcrEngineMode(3);
        tesseract.setPageSegMode(3);
        return tesseract;
    }

}

ITesseractOCRService.java

java 复制代码
package com.xxxx.service;

import org.springframework.web.multipart.MultipartFile;

/**
 * ocr识别服务
 */
public interface ITesseractOCRService {
    /**
     * ocr识别
     * @param multipartFile 上传文件对象
     * @return ocr识别字符串
     */
    String recognizeText(MultipartFile multipartFile);
}

TesseractOCRServiceImpl.java

java 复制代码
package com.xxxxservice.impl;

import com.xxxx.exception.ServiceException;
import com.xxxx.service.ITesseractOCRService;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

@Service
@Slf4j
public class TesseractOCRServiceImpl implements ITesseractOCRService {
    @Resource
    private Tesseract tesseract;

    @Override
    public String recognizeText(MultipartFile multipartFile) {
        try {
            BufferedImage image = ImageIO.read(multipartFile.getInputStream());
            return tesseract.doOCR(image);
        } catch (TesseractException e) {
            log.error("OCR识别异常", e);
            throw new ServiceException("OCR识别异常");
        } catch (IOException e) {
            log.error("OCR读文件异常", e);
            throw new ServiceException("OCR读文件异常");
        }
    }
}

OcrController.java

java 复制代码
package com.xxxx.web.controller.system.api;

import com.xxxx.system.service.ITesseractOCRService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/ocr")
public class OcrController {


    @Resource
    private ITesseractOCRService iTesseractOCRService;

    @PostMapping("/test")
    public String recognizeText(@RequestParam("file") MultipartFile file) {
        return iTesseractOCRService.recognizeText(file);
    }
}

改造tess4j jar文件

bash 复制代码
# 找到依赖库jar包位置
cd /Users/xxx/.m2/repository/net/sourceforge/tess4j/tess4j/5.16.0

# 创建os架构对应的文件夹
mkdir darwin-aarch64
# 上传文件夹到jar内部
jar uf tess4j-5.16.0.jar darwin-aarch64

# 复制libtesseract.dylib文件
cp /opt/homebrew/Cellar/tesseract/5.5.1/lib/libtesseract.5.dylib darwin-aarch64/libtesseract.dylib 
# 上传libtesseract.dylib文件到jar中
jar uf tess4j-5.16.0.jar darwin-aarch64/libtesseract.dylib 

# 复制libleptonica.dylib文件
cp /opt/homebrew/Cellar/leptonica/1.86.0/lib/libleptonica.6.dylib darwin-aarch64/libleptonica.dylib
# 上传libleptonica.dylib文件到jar中
jar uf tess4j-5.16.0.jar darwin-aarch64/libleptonica.dylib

# 检查jar文件
jar tf tess4j-5.16.0.jar

最终jar文件如下:

bash 复制代码
META-INF/
META-INF/MANIFEST.MF
com/
com/recognition/
com/recognition/software/
com/recognition/software/jdeskew/
net/
net/sourceforge/
net/sourceforge/tess4j/
net/sourceforge/tess4j/util/
tessdata/
tessdata/configs/
win32-x86/
win32-x86-64/
META-INF/maven/
META-INF/maven/net.sourceforge.tess4j/
META-INF/maven/net.sourceforge.tess4j/tess4j/
com/recognition/software/jdeskew/ImageDeskew$HoughLine.class
com/recognition/software/jdeskew/ImageDeskew.class
com/recognition/software/jdeskew/ImageUtil.class
net/sourceforge/tess4j/ITessAPI$CANCEL_FUNC.class
net/sourceforge/tess4j/ITessAPI$EANYCODE_CHAR.class
net/sourceforge/tess4j/ITessAPI$ETEXT_DESC.class
net/sourceforge/tess4j/ITessAPI$TessBaseAPI.class
net/sourceforge/tess4j/ITessAPI$TessCancelFunc.class
net/sourceforge/tess4j/ITessAPI$TessChoiceIterator.class
net/sourceforge/tess4j/ITessAPI$TessMutableIterator.class
net/sourceforge/tess4j/ITessAPI$TessOcrEngineMode.class
net/sourceforge/tess4j/ITessAPI$TessOrientation.class
net/sourceforge/tess4j/ITessAPI$TessPageIterator.class
net/sourceforge/tess4j/ITessAPI$TessPageIteratorLevel.class
net/sourceforge/tess4j/ITessAPI$TessPageSegMode.class
net/sourceforge/tess4j/ITessAPI$TessParagraphJustification.class
net/sourceforge/tess4j/ITessAPI$TessPolyBlockType.class
net/sourceforge/tess4j/ITessAPI$TessProgressFunc.class
net/sourceforge/tess4j/ITessAPI$TessResultIterator.class
net/sourceforge/tess4j/ITessAPI$TessResultRenderer.class
net/sourceforge/tess4j/ITessAPI$TessTextlineOrder.class
net/sourceforge/tess4j/ITessAPI$TessWritingDirection.class
net/sourceforge/tess4j/ITessAPI$TimeVal.class
net/sourceforge/tess4j/ITessAPI.class
net/sourceforge/tess4j/ITesseract$RenderedFormat.class
net/sourceforge/tess4j/ITesseract.class
net/sourceforge/tess4j/OCRResult.class
net/sourceforge/tess4j/OSDResult.class
net/sourceforge/tess4j/TessAPI.class
net/sourceforge/tess4j/TessAPI1.class
net/sourceforge/tess4j/Tesseract$1.class
net/sourceforge/tess4j/Tesseract.class
net/sourceforge/tess4j/Tesseract1$1.class
net/sourceforge/tess4j/Tesseract1.class
net/sourceforge/tess4j/TesseractException.class
net/sourceforge/tess4j/util/Hocr2PdfParser$1.class
net/sourceforge/tess4j/util/Hocr2PdfParser.class
net/sourceforge/tess4j/util/ImageHelper.class
net/sourceforge/tess4j/util/ImageIOHelper.class
net/sourceforge/tess4j/util/LoadLibs.class
net/sourceforge/tess4j/util/LoggHelper.class
net/sourceforge/tess4j/util/PdfBoxUtilities$1.class
net/sourceforge/tess4j/util/PdfBoxUtilities$2.class
net/sourceforge/tess4j/util/PdfBoxUtilities.class
net/sourceforge/tess4j/util/PdfUtilities.class
net/sourceforge/tess4j/util/Utils.class
net/sourceforge/tess4j/Word.class
readme.html
tessdata/configs/alto
tessdata/configs/api_config
tessdata/configs/bazaar
tessdata/configs/digits
tessdata/configs/hocr
tessdata/configs/lstmbox
tessdata/configs/pdf
tessdata/configs/quiet
tessdata/configs/tsv
tessdata/configs/txt
tessdata/configs/unlv
tessdata/configs/wordstrbox
tessdata/eng.traineddata
tessdata/osd.traineddata
tessdata/pdf.ttf
versionchanges.txt
win32-x86/libtesseract551.dll
win32-x86-64/libtesseract551.dll
META-INF/maven/net.sourceforge.tess4j/tess4j/pom.xml
META-INF/maven/net.sourceforge.tess4j/tess4j/pom.properties
META-INF/INDEX.LIST
darwin-aarch64/
darwin-aarch64/libtesseract.dylib
darwin-aarch64/libleptonica.dylib

测试ocr效果

参考

相关推荐
JH30736 小时前
SpringBoot 优雅处理金额格式化:拦截器+自定义注解方案
java·spring boot·spring
Coder_Boy_7 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
2401_836235867 小时前
中安未来SDK15:以AI之眼,解锁企业档案的数字化基因
人工智能·科技·深度学习·ocr·生活
invicinble7 小时前
对tomcat的提供的功能与底层拓扑结构与实现机制的理解
java·tomcat
较真的菜鸟8 小时前
使用ASM和agent监控属性变化
java
黎雁·泠崖8 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
肖永威9 小时前
macOS环境安装/卸载python实践笔记
笔记·python·macos
qq_12498707539 小时前
基于SSM的动物保护系统的设计与实现(源码+论文+部署+安装)
java·数据库·spring boot·毕业设计·ssm·计算机毕业设计
Coder_Boy_9 小时前
基于SpringAI的在线考试系统-考试系统开发流程案例
java·数据库·人工智能·spring boot·后端
Mr_sun.9 小时前
Day06——权限认证-项目集成
java