Windows下命令行及Java+Tesseract-OCR对图像进行(字母+数字+中文)识别,亲测可行
Windows下Java+Tesseract-OCR對(duì)圖像進(jìn)行字符識(shí)別,親測(cè)可行
- 1. 下載tesseract-ocr、中文語(yǔ)言包并安裝
- 2. 命令行對(duì)圖片進(jìn)行識(shí)別及效果圖
- 3. Java調(diào)用Tesseart-OCR
- 3.1 效果圖
- 3.2 源碼
- 4. 遺留問(wèn)題
- 4.1 Too many unichars in ambiguity on line 22991608,暫未解決
- 參考
這篇博客將介紹Java如何利用Tesseract-OCR對(duì)圖像進(jìn)行字符識(shí)別;
1. 下載tesseract-ocr、中文語(yǔ)言包并安裝
Tesseract-ocr是一款開(kāi)源工具,可用于OCR(Optical Character Recognition)。默認(rèn)識(shí)別字母+數(shù)字,識(shí)別中文需要安裝相應(yīng)的中文語(yǔ)言包)。
tesseract-ocr 3.02安裝包與中文語(yǔ)言包c(diǎn)hi_sim.traineddata 3.02版本-百度網(wǎng)盤(pán)鏈接:https://pan.baidu.com/s/1-tmDFZPvOO1GFtwJn50ahA
提取碼:3siq
中文語(yǔ)言包c(diǎn)hi_sim.traineddata 4.0版本在此下載
假設(shè)安裝在Tesseart-OCR安裝在D盤(pán),
將語(yǔ)言包解壓出的chi_sim.traineddata 放到 D:\Tesseract-OCR\tessdata\ 目錄下
驗(yàn)證安裝成功與否:
2. 命令行對(duì)圖片進(jìn)行識(shí)別及效果圖
切換到安裝目錄及識(shí)別
cd D:\Tesseract-OCR
tesseract.exe E:\mat\mvt\java-ocr-demo\ocr\opencv_logo.jpg opencv_logo
默認(rèn)識(shí)別(字母+數(shù)字),識(shí)別中文需要安裝中文語(yǔ)言包,并指定模型 -l chi_sim
tesseract 2.jpg 2 -l chi_sim -psm 7
- 參數(shù)1:2.jpg 圖片全路徑
- 參數(shù)2:2 識(shí)別文本輸出文字名,結(jié)果將寫(xiě)入2.txt
- 參數(shù)3:指定語(yǔ)言包,-l chi_sim表示用簡(jiǎn)體中文字庫(kù),默認(rèn)eng(識(shí)別字母+數(shù)字)
- 參數(shù)4:-psm 7 表示告訴tesseract 2.jpg圖片只有一行文本,從而可以減少識(shí)別錯(cuò)誤率,默認(rèn)為3。
字母成功識(shí)別效果圖如下:
圖過(guò)大,字符太小可能無(wú)法識(shí)別,需要將圖片截取下保證圖片中字符清晰可見(jiàn)即可正確識(shí)別出來(lái);
數(shù)字成功識(shí)別效果圖如下:
中文+字母識(shí)別:可以看到文字并沒(méi)有正確識(shí)別;不是特別準(zhǔn)確
中文+字母+數(shù)字識(shí)別,中文不太準(zhǔn)確,效果圖如下:
3. Java調(diào)用Tesseart-OCR
Java調(diào)用Tessert-OCR有如下倆種方法:
- 實(shí)質(zhì)上也是調(diào)用的 tesseart.exe執(zhí)行的ocr識(shí)別;
- 調(diào)用tess4j jar包內(nèi)的方法,依賴(lài)字體集(需要本地下載好字體集)
- 優(yōu)化版本—— Java-基于百度API的圖片文字識(shí)別(支持中文,英文和中英文混合)
3.1 效果圖
調(diào)用本地exe方式效果圖如下:英文比較準(zhǔn)確,中文的效果不太好
調(diào)用jar加載 eng 模型來(lái)識(shí)別 效果圖如下:多張圖片測(cè)試,字母+數(shù)字的效果要好一些,基本都識(shí)別出來(lái),中文識(shí)別均亂碼;
調(diào)用jar加載中文的 chi_sim 模型來(lái)識(shí)別 效果圖如下:多張圖片測(cè)試,字符字母部分未成功識(shí)別,也有識(shí)別錯(cuò)的,中文識(shí)別效果也不好;
3.2 源碼
<!-- https://mvnrepository.com/artifact/net.sourceforge.tess4j/tess4j -->
<dependency><groupId>net.sourceforge.tess4j</groupId><artifactId>tess4j</artifactId><version>3.2.1</version>
</dependency>
package com;import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.apache.commons.io.FileUtils;
import org.junit.platform.commons.util.StringUtils;import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;/**************************************Class Name: TessertOcr*Description: <java調(diào)用tessert-ocr識(shí)別>*@author: seminar*@create: 2021/7/8*@since 1.0.0*************************************/
public class TessertOcr {/*** 調(diào)用tesseart.exe進(jìn)行OCR識(shí)別** @param exePath exe絕對(duì)路徑* @param jpgPath jpg路徑* @param flag 默認(rèn)false,使用eng只支持字母+數(shù)字, True:支持中文+字母+數(shù)字* @return*/public static String callTesseartExe(String exePath, String jpgPath, boolean flag) {String res = "";try {Process process = null;if (!flag) {process = Runtime.getRuntime().exec(new String[]{exePath, jpgPath, jpgPath.replaceAll(".jpg", "")});} else {process = Runtime.getRuntime().exec(new String[]{exePath, jpgPath, jpgPath.replaceAll(".jpg", ""), "-l", "chi_sim"});}StringBuilder processOutput = new StringBuilder();try (BufferedReader processOutputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));) {String readLine;while ((readLine = processOutputReader.readLine()) != null) {processOutput.append(readLine + System.lineSeparator());}// 等待執(zhí)行完成process.waitFor();} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();} finally {if (process != null) {process.destroy();}}res = processOutput.toString().trim();res = FileUtils.readFileToString(new File(jpgPath.replaceAll(".jpg", ".txt")));} catch (IOException e) {e.printStackTrace();}return res;}public static void main(String[] args) {// 第一種方法String exePath = "D:/Tesseract-OCR/tesseract.exe";String jpgPath = "E:\\mat\\mvt\\java-ocr-demo\\images\\1622175322109_0.025711_cc.jpg";System.out.println("1622175322109_0.025711_cc.jpg callTesseartExe: " + callTesseartExe(exePath, jpgPath, false));System.out.println("chinese.jpg callTesseartExe: " + callTesseartExe(exePath,"E:\\mat\\mvt\\java-ocr-demo\\images\\chinese.jpg", true));String fileDir = "E:\\mat\\mvt\\java-ocr-demo\\images\\";File[] list = new File(fileDir).listFiles();ITesseract instance = new Tesseract();instance.setDatapath("E:\\mat\\mvt\\java-ocr-demo\\src\\test\\java\\com\\tessdata");// 默認(rèn)識(shí)別英文(字母+英文),如果識(shí)別中文(數(shù)字+中文)需要設(shè)置語(yǔ)言包
// instance.setLanguage("chi_sim");for (File file : list) {if (file.getName().endsWith(".jpg")) {try {String result = instance.doOCR(file);if (StringUtils.isNotBlank(result)) {result = result.replaceAll("\n", "");result = result.replaceAll(" ", "");if (result.length() > 30) {System.out.println(file.getName() + ": " + result.substring(result.length() - 30, result.length()));} else {System.out.println(file.getName() + ": " + result);}} else {System.out.println(file.getName() + ": ");}} catch (TesseractException e) {e.printStackTrace();}}}}
}
4. 遺留問(wèn)題
4.1 Too many unichars in ambiguity on line 22991608,暫未解決
參考
- 親測(cè)可用
- eng.traineddata,chi_sm.traineddata下載
- java+Tesseract-OCR實(shí)現(xiàn)圖片識(shí)別
- java利用tesseract-OCR對(duì)圖像進(jìn)行字符識(shí)別
- tesseract-ocr java開(kāi)發(fā)
- Java截取局部圖片~
總結(jié)
以上是生活随笔為你收集整理的Windows下命令行及Java+Tesseract-OCR对图像进行(字母+数字+中文)识别,亲测可行的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 医院验光多少钱啊?
- 下一篇: Python,OpenCV骨架化图像并显