Windows下命令行及Java+Tesseract-OCR对图像进行(字母+数字+中文)识别,亲测可行
Windows下Java+Tesseract-OCR對圖像進行字符識別,親測可行
- 1. 下載tesseract-ocr、中文語言包并安裝
- 2. 命令行對圖片進行識別及效果圖
- 3. Java調用Tesseart-OCR
- 3.1 效果圖
- 3.2 源碼
- 4. 遺留問題
- 4.1 Too many unichars in ambiguity on line 22991608,暫未解決
- 參考
這篇博客將介紹Java如何利用Tesseract-OCR對圖像進行字符識別;
1. 下載tesseract-ocr、中文語言包并安裝
Tesseract-ocr是一款開源工具,可用于OCR(Optical Character Recognition)。默認識別字母+數字,識別中文需要安裝相應的中文語言包)。
tesseract-ocr 3.02安裝包與中文語言包chi_sim.traineddata 3.02版本-百度網盤鏈接:https://pan.baidu.com/s/1-tmDFZPvOO1GFtwJn50ahA
提取碼:3siq
中文語言包chi_sim.traineddata 4.0版本在此下載
假設安裝在Tesseart-OCR安裝在D盤,
將語言包解壓出的chi_sim.traineddata 放到 D:\Tesseract-OCR\tessdata\ 目錄下
驗證安裝成功與否:
2. 命令行對圖片進行識別及效果圖
切換到安裝目錄及識別
cd D:\Tesseract-OCR
tesseract.exe E:\mat\mvt\java-ocr-demo\ocr\opencv_logo.jpg opencv_logo
默認識別(字母+數字),識別中文需要安裝中文語言包,并指定模型 -l chi_sim
tesseract 2.jpg 2 -l chi_sim -psm 7
- 參數1:2.jpg 圖片全路徑
- 參數2:2 識別文本輸出文字名,結果將寫入2.txt
- 參數3:指定語言包,-l chi_sim表示用簡體中文字庫,默認eng(識別字母+數字)
- 參數4:-psm 7 表示告訴tesseract 2.jpg圖片只有一行文本,從而可以減少識別錯誤率,默認為3。
字母成功識別效果圖如下:
圖過大,字符太小可能無法識別,需要將圖片截取下保證圖片中字符清晰可見即可正確識別出來;
數字成功識別效果圖如下:
中文+字母識別:可以看到文字并沒有正確識別;不是特別準確
中文+字母+數字識別,中文不太準確,效果圖如下:
3. Java調用Tesseart-OCR
Java調用Tessert-OCR有如下倆種方法:
- 實質上也是調用的 tesseart.exe執行的ocr識別;
- 調用tess4j jar包內的方法,依賴字體集(需要本地下載好字體集)
- 優化版本—— Java-基于百度API的圖片文字識別(支持中文,英文和中英文混合)
3.1 效果圖
調用本地exe方式效果圖如下:英文比較準確,中文的效果不太好
調用jar加載 eng 模型來識別 效果圖如下:多張圖片測試,字母+數字的效果要好一些,基本都識別出來,中文識別均亂碼;
調用jar加載中文的 chi_sim 模型來識別 效果圖如下:多張圖片測試,字符字母部分未成功識別,也有識別錯的,中文識別效果也不好;
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調用tessert-ocr識別>*@author: seminar*@create: 2021/7/8*@since 1.0.0*************************************/
public class TessertOcr {/*** 調用tesseart.exe進行OCR識別** @param exePath exe絕對路徑* @param jpgPath jpg路徑* @param flag 默認false,使用eng只支持字母+數字, True:支持中文+字母+數字* @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());}// 等待執行完成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");// 默認識別英文(字母+英文),如果識別中文(數字+中文)需要設置語言包
// 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. 遺留問題
4.1 Too many unichars in ambiguity on line 22991608,暫未解決
參考
- 親測可用
- eng.traineddata,chi_sm.traineddata下載
- java+Tesseract-OCR實現圖片識別
- java利用tesseract-OCR對圖像進行字符識別
- tesseract-ocr java開發
- Java截取局部圖片~
總結
以上是生活随笔為你收集整理的Windows下命令行及Java+Tesseract-OCR对图像进行(字母+数字+中文)识别,亲测可行的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 医院验光多少钱啊?
- 下一篇: Python,OpenCV骨架化图像并显