使用Springboot + Tesseract OCR引擎实现图片文字自动识别


一、简介

1.1关于项目

Tesseract是一款优秀的开源OCR软件,是由HP实验室开发,Google维护的开源OCR(Optical Character Recognition , 光学字符识别)引擎,与Microsoft Office Document Imaging(MODI)相比,我们可以不断的训练的库,使图像转换文本的能力不断增强;如果团队深度需要,还可以以它为模板,开发出符合自身需求的OCR引擎。

目前由Google维护改进,已发展到5.0版本,从4.0版本起增加了基于LSTM神经网络的识别引擎

本项目使用Springboot + Tesseract OCR引擎实现图片文字自动识别功能。

1.2准备

JDK:17

Maven:3.6

开发工具:IntelliJ IDEA

Tesseract模型文件:chi_sim.traineddata

本项目源代码:可私信联系

1.3Tesseract模型文件下载

https://gitcode.com/tesseract-ocr/tessdata/blob/main/chi_sim.traineddata

项目文件预览 - tessdata - GitCode


二、新建SpringBoot项目

点击"Finish",项目创建。建议修改maven版本与配置文件(这里使用阿里云配置文件,以便支持后续导入依赖)

修改后,重新reload

三、项目配置

3.1引入依赖

    net.sourceforge.tess4j    tess4j    4.5.4

3.2yml配置

server:  port:8888# 训练数据文件夹的路径tess4j:  datapath: D:/tessdata

3.3 模型文件存到相应目录

有一点要注意的是,直接读resource目录下的路径是读不到的哈,所以我放到了D盘,训练数据本身也是更推荐放到独立的位置,方便后续训练数据。

四、开发

4.1配置类

我们新建一个配置类,初始化一下Tesseract类,交给Spring管理,这样借用了Spring的单例模式。

packagecom.example.tesseractocr.config;importnet.sourceforge.tess4j.Tesseract;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;/**

* @作者:

* @日期: 2023/10/12 22:58

* @描述:

*/@ConfigurationpublicclassTesseractOcrConfiguration{@Value("${tess4j.datapath}")privateString dataPath;@BeanpublicTesseracttesseract(){Tesseracttesseract=newTesseract();// 设置训练数据文件夹路径tesseract.setDatapath(dataPath);// 设置为中文简体tesseract.setLanguage("chi_sim");returntesseract;  }}4、service实现

4.2service实现

packagecom.example.tesseractocr.service;importlombok.AllArgsConstructor;importnet.sourceforge.tess4j.*;importorg.springframework.stereotype.Service;importorg.springframework.web.multipart.MultipartFile;importjavax.imageio.ImageIO;importjava.awt.image.BufferedImage;importjava.io.ByteArrayInputStream;importjava.io.IOException;importjava.io.InputStream;@Service@AllArgsConstructorpublicclassOcrService{privatefinalTesseract tesseract;/**    * 识别图片中的文字    *@paramimageFile 图片文件    *@return文字信息    */publicStringrecognizeText(MultipartFile imageFile)throwsTesseractException, IOException {// 转换InputStreamsbs=newByteArrayInputStream(imageFile.getBytes());BufferedImagebufferedImage=ImageIO.read(sbs);// 对图片进行文字识别returntesseract.doOCR(bufferedImage);    }}

4.3Controller控制器类

packagecom.example.tesseractocr.controller;importcom.example.tesseractocr.service.OcrService;importlombok.AllArgsConstructor;importnet.sourceforge.tess4j.TesseractException;importorg.springframework.http.MediaType;importorg.springframework.web.bind.annotation.PostMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;importorg.springframework.web.multipart.MultipartFile;importjava.io.IOException;@RequestMapping("/api")@RestController@AllArgsConstructorpublicclassOcrController{privatefinalOcrService ocrService;@PostMapping(value = "/recognize", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)publicStringrecognizeImage(@RequestParam("file")MultipartFile file)throwsTesseractException, IOException {// 调用OcrService中的方法进行文字识别returnocrService.recognizeText(file);    }}

五、测试

这里使用postman测试

这里是body中的参数,我们选择form-data中的File属性,表示以上传文件形式来调接口。

这里选取一个新闻内容


看下效果,其实还是挺不错的,我和图片比对了一下,基本上都识别出来了。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容