什么是Tess4j庫(kù)
先簡(jiǎn)單給沒(méi)聽(tīng)過(guò)的xdm解釋下,這里要分清楚Tesseract和Tess4j的區(qū)別。
Tesseract是一個(gè)開(kāi)源的光學(xué)字符識(shí)別(OCR)引擎,它可以將圖像中的文字轉(zhuǎn)換為計(jì)算機(jī)可讀的文本。支持多種語(yǔ)言和書(shū)面語(yǔ)言,并且可以在命令行中執(zhí)行。它是一個(gè)流行的開(kāi)源OCR工具,可以在許多不同的操作系統(tǒng)上運(yùn)行。
Tess4J是一個(gè)基于Tesseract OCR引擎的Java接口,可以用來(lái)識(shí)別圖像中的文本,說(shuō)白了,就是封裝了它的API,讓Java可以直接調(diào)用。
搞清楚這倆東西,就足夠了。
案例
1、引入依賴(lài)
既然是SpringBoot,基礎(chǔ)依賴(lài)我就不贅述了,這里貼下Tess4J的依賴(lài),是可以用maven下載的。
<!-- tess4j -->
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>4.5.4</version>
</dependency>
2、yml配置
這里,我特地把訓(xùn)練數(shù)據(jù)的目錄路徑配置在yml里,后續(xù)可以擴(kuò)展到配置中心。
server:
port: 8888
# 訓(xùn)練數(shù)據(jù)文件夾的路徑
tess4j:
datapath: D:/tessdata
然后我解釋下什么是訓(xùn)練數(shù)據(jù)
Tesseract OCR庫(kù)通過(guò)訓(xùn)練數(shù)據(jù)來(lái)學(xué)習(xí)不同語(yǔ)言和字體的特征,以便更好地識(shí)別圖片中的文字。
在安裝Tesseract OCR庫(kù)時(shí),通常會(huì)生成一個(gè)包含多個(gè)子文件夾的訓(xùn)練數(shù)據(jù)文件夾,其中每個(gè)子文件夾都包含了特定語(yǔ)言或字體的訓(xùn)練數(shù)據(jù)。
比如我這里是下載后放到了D盤(pán)的tessdata目錄下,如圖所示,其實(shí)就是一個(gè).traineddata為后綴的文件,大小約2M多。
如果你沒(méi)有特定的訓(xùn)練數(shù)據(jù)需求,使用默認(rèn)的訓(xùn)練數(shù)據(jù)文件即可,我這里就是直接下載默認(rèn)的來(lái)用的。
還有一點(diǎn)要注意的是,直接讀resource目錄下的路徑是讀不到的哈,所以我放到了D盤(pán),訓(xùn)練數(shù)據(jù)本身也是更推薦放到獨(dú)立的位置,方便后續(xù)訓(xùn)練數(shù)據(jù)。
3、config配置類(lèi)
我們新建一個(gè)配置類(lèi),初始化一下Tesseract類(lèi),交給Spring管理,這樣借用了Spring的單例模式。
package com.example.tesseractocr.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;
/**
* @作者:
* @日期: 2023/10/12 22:58
* @描述:
*/
@Configuration
public class TesseractOcrConfiguration {
@Value("${tess4j.datapath}")
private String dataPath;
@Bean
public Tesseract tesseract() {
Tesseract tesseract = new Tesseract();
// 設(shè)置訓(xùn)練數(shù)據(jù)文件夾路徑
tesseract.setDatapath(dataPath);
// 設(shè)置為中文簡(jiǎn)體
tesseract.setLanguage("chi_sim");
return tesseract;
}
}
4、service實(shí)現(xiàn)
就幾行代碼,非常簡(jiǎn)單。
package com.example.tesseractocr.service;
import lombok.AllArgsConstructor;
import net.sourceforge.tess4j.*;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
@Service
@AllArgsConstructor
public class OcrService {
private final Tesseract tesseract;
/**
* 識(shí)別圖片中的文字
* @param imageFile 圖片文件
* @return 文字信息
*/
public String recognizeText(MultipartFile imageFile) throws TesseractException, IOException {
// 轉(zhuǎn)換
InputStream sbs = new ByteArrayInputStream(imageFile.getBytes());
BufferedImage bufferedImage = ImageIO.read(sbs);
// 對(duì)圖片進(jìn)行文字識(shí)別
return tesseract.doOCR(bufferedImage);
}
}
5、新增rest接口
我們新建一個(gè)rest接口,用來(lái)測(cè)試效果,使用上傳圖片文件的方式。
package com.example.tesseractocr.controller;
import com.example.tesseractocr.service.OcrService;
import lombok.AllArgsConstructor;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.http.MediaType;
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 java.io.IOException;
@RequestMapping("/api")
@RestController
@AllArgsConstructor
public class OcrController {
private final OcrService ocrService;
@PostMapping(value = "/recognize", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String recognizeImage(@RequestParam("file") MultipartFile file) throws TesseractException, IOException {
// 調(diào)用OcrService中的方法進(jìn)行文字識(shí)別
return ocrService.recognizeText(file);
}
}
6、測(cè)試效果
這里我用ApiPost工具來(lái)測(cè)試下最終效果
我準(zhǔn)備的一張圖片如下,是從知乎上隨便截取的一張。
我們調(diào)接口試一下,這里要設(shè)置Header的Content-Type,別忘了哈。
這里是body中的參數(shù),我們選擇form-data中的File屬性,表示以上傳文件形式來(lái)調(diào)接口。
看下效果,其實(shí)還是挺不錯(cuò)的,我和圖片比對(duì)了一下,基本上都識(shí)別出來(lái)了。
相關(guān)地址
1)、Tesseract-ocr官方Github地址:http://github.com/tesseract-o…
2)、Tesseract-ocr安裝下載:http://digi.bib.uni-mannheim.de/tesseract/
PS:這里我沒(méi)有用官方Github文檔中給的地址,因?yàn)樘?,找了一個(gè)下載比較快的,你們可以往下拉找到win64位的安裝即可,如果沒(méi)有訓(xùn)練需求,不用下也可以)
3)、訓(xùn)練文件:http://digi.bib.uni-mannheim.de/tesseract/t…
PS:在2)的路徑下,有一個(gè)tessdata_fast目錄,點(diǎn)進(jìn)去就能直接下載到默認(rèn)訓(xùn)練文件,這種比較簡(jiǎn)便,省去了前面安裝下載的過(guò)程。
4)、案例代碼:http://gitee.com/fangfuji/ja…
PS:代碼放在Gitee上,在同名博文目錄里面,包含代碼+安裝文件+訓(xùn)練文件。
總結(jié)
是不是非常簡(jiǎn)單xdm,反正我覺(jué)得挺有意思的,后面抽空再試試訓(xùn)練數(shù)據(jù)。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-757894.html
好了,今天的小知識(shí),你學(xué)會(huì)了嗎?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-757894.html
到了這里,關(guān)于Java也能做OCR!SpringBoot 整合 Tess4J 實(shí)現(xiàn)圖片文字識(shí)別的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!