tess4j圖片識別 和訓練語言庫提高圖片識別率


1.pom文件添加依賴

 

         <!-- 圖形驗證碼識別https://mvnrepository.com/artifact/net.sourceforge.tess4j/tess4j -->
        <dependency>
            <groupId>net.sourceforge.tess4j</groupId>
            <artifactId>tess4j</artifactId>
            <version>4.5.4</version>
        </dependency>

2.下載文件配置

鏈接:https://pan.baidu.com/s/1BsFJ7uTl-AEzcUEfuV9ESw
提取碼:4wzp

解壓后顯示

 

 3.進入dist文件,拷貝文件tess4j-3.4.8.jar到lib文件下

 

 

 4.將lib文件和tessdata文件方到項目根目錄

 

 5.進入項目編碼軟件刷新項目目錄

 

 6.testdata可以放到項目文件目錄下

 

 7.被識別的圖片存放位置

新建目錄,放在項目路徑下

 

 

 

 8.編寫識別代碼

package com.app.ocr;

import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import java.io.File;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        //  當前項目存放路徑
        String path = "E://heduijiang//APP-test//APP-test//src//test//resources";
        // 要識別的圖片
        File file = new File("E://heduijiang//APP-test//APP-test//src//test//resources//tupian//9188.png");
        ITesseract instance = new Tesseract();
        
        /**
         *  獲取項目根路徑,
         */
        File directory = new File(path);
        String courseFile = null;
        try {
            courseFile = directory.getCanonicalPath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //設置訓練庫的位置
        instance.setDatapath(courseFile + "/tessdata");
        //chi_sim :簡體中文, eng    根據需求選擇語言庫
        instance.setLanguage("eng");
        String result = null;
        try {
            long startTime = System.currentTimeMillis();
            result =  instance.doOCR(file);
            long endTime = System.currentTimeMillis();
            System.out.println("Time is:" + (endTime - startTime) + "ms");
        } catch (TesseractException e) {
            e.printStackTrace();
        }
        System.out.println("result: "+ result+"");
    }
}

 

 9.在圖片文件下放入要識別的圖片

 

 10.代碼路徑指定圖片的絕對路徑輸入圖片名稱,進行識別

 

 

訓練語言庫適合自己的項目,自己編輯訓練一個

1.截圖自己的項目的圖片

 

 2.下載軟件 jTessBoxEditorFX-2.3.1  和 tesseract-ocr-w64-setup-v5.0.0

鏈接:https://pan.baidu.com/s/1PUlfRKC5Xt4SWeBjfGKVUA
提取碼:ad1q

 

 esseract-ocr-w64-setup-v5.0.0安裝后配置環境變量,找到自己的安裝的位置

https://digi.bib.uni-mannheim.de/tesseract/下載tesseract版本完成安裝。配置好環境變量,打開命令行窗口執行tesseract -v 看到相關版本信息,則表示安裝配置成功。

 

 

jTessBoxEditorFX-2.3.1 解壓后顯示

 

雙擊啟動 train.bat

 

 把圖片文件夾放在此目錄下,也可以在其它位置,這里我放到這個文件夾下

 

 

1、生成tif和box文件

       可參照第一種方法生成。但這里介紹通過自定義圖片使用jTessBoxEditor 合並tif

 

 

 

全選圖片

 

輸入名字num.font.exp0.tif

 

 

 

生成了文件

 

 2、生成box文件

在生成圖片文件夾下,打開cmd命令框

 

 

打開命令行執行命令tesseract num.font.exp0.tif num.font.exp0 batch.nochop makebox生成box文件,執行命令后輸出如下:

D:\wspace\tess4j-demo2\test-data\num>tesseract num.font.exp0.tif num.font.exp0 batch.nochop makebox
Tesseract Open Source OCR Engine v4.0.0.20181030 with Leptonica
Page 1
Page 2
Page 3
Page 4

 

生成文件 box

 

3、字符配置文件font_properties

在文件夾文件夾內,新建一個文本文件,名為font_properties,刪掉.txt,用記事本打開,寫入內容為:

font 0 0 0 0 0

 

 

 

 

 

准備環節

  • 將5個tif文件,num.font.exp0.tif,生成的num.font.exp0.box文件,還有font_properties文件放在同一個目錄下,如上面已經在一個目錄下了

字符矯正

  • 打開 jTessBoxEditor>【BOX Editor】> 【Open】,打開num.font.exp0.tif;矯正【Char】上的字符
  • 操作截圖:

 

 

 

 開始進行編寫識別

insert 添加

delete 刪除

merge 合並

split 分離 幾乎不用此按鈕

 

 

 

 只選擇x值調

 

 

 

 依次循環,第一張完成后,切換第二張 最后點擊保存

 

 所有圖片編輯完后,點擊 Save 保存即可

8.執行批處理文件

  • 【注意】:執行該批處理文件前,先要目錄下創建font_properties文件 ,也就是滴 5 步
  • 在目標目錄下,新建一個txt文件,復制代碼,重命名為 do.bat,直接更改后綴名就可以
  • 代碼如下

 

 打開復制代碼-保存

 

 

echo Run Tesseract for Training..
tesseract.exe num.font.exp0.tif num.font.exp0 nobatch box.train

echo Compute the Character Set..
unicharset_extractor.exe num.font.exp0.box
mftraining -F font_properties -U unicharset -O num.unicharset num.font.exp0.tr


echo Clustering..
cntraining.exe num.font.exp0.tr

echo Rename Files..
rename normproto num.normproto
rename inttemp num.inttemp
rename pffmtable num.pffmtable
rename shapetable num.shapetable  

echo Create Tessdata..
combine_tessdata.exe num.

echo. & pause

修改為 .bat文件

 

 

 

 

 

 雙擊do.bat文件前

 

  雙擊do.bat文件后

9.拷貝 num.trainddata 文件

  • 最后將 num.trainddata 復制到 Tesseract-OCR 安裝目錄下的 tessdata 文件夾
  • 【注意】:這里是【Tesseract-OCR 安裝目錄下的 tessdata 文件夾】

 

 

 在圖片文件下 輸入cmd

再次輸入命令 tesseract num1.jpg num01 -l num

num1.jpg 換成自己的圖片

 

 

遇到問題 mftraining.exe 停止運行

解決方法 替換此文件就可以

 

 用上面安裝的軟件,找到此路徑下這個文件直接替換就可

找到本地文件位置

 

下載可以啟動的文件

鏈接:https://pan.baidu.com/s/1NklBXlIi3W4D30O20lvXdw
提取碼:d2pb

地址只把替換的文件拿出來了

 

 然后在執行 do.bat 就不報錯了

 

 

其他

基於圖片識別的識別率不高,所以我們一般先做圖片的處理再進行識別。這個時候我們可以用到Tess4J專門提供的ImageHelper。里面分別有如下方法:

getScaledInstance 放大圖片
getSubImage 截取圖片
convertImageToBinary 轉二進制
convertImageToGrayscale 將圖像轉換為灰度
invertImageColor 反轉圖像顏色
rotateImage 旋轉影像

 

        //圖片轉圖片流
        BufferedImage img = ImageIO.read(file);
        // 這里對圖片黑白處理,增強識別率.這里先通過截圖,截取圖片中需要識別的部分
        img = ImageHelper.convertImageToGrayscale(img);
        // 圖片銳化,自己使用中影響識別率的主要因素是針式打印機字跡不連貫,所以銳化反而降低識別率
  // img = ImageHelper.convertImageToBinary(img);
        // 圖片放大5倍,增強識別率(很多圖片本身無法識別,放大7倍時就可以輕易識,但是考濾到客戶電腦配置低,針式打印機打印不連貫的問題,這里就放大7倍)
        img = ImageHelper.getScaledInstance(img, img.getWidth() * 7, img.getHeight() * 7);
package com.app.ocr;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import net.sourceforge.tess4j.util.ImageHelper;

public class Tess4jmain {
    public static void main(String[] args) throws IOException {
        // 識別圖片的文件(修改為自己的圖片路徑)
        String imagePath = ClassLoader.getSystemResource("tupian/9188.png").getPath();
        if (imagePath.length()>0){
            imagePath = imagePath.substring(1);
        }
        System.out.println("imagePath:"+imagePath);
        File file = new File(imagePath);
        
        //圖片轉圖片流
        BufferedImage img = ImageIO.read(file);
        // 這里對圖片黑白處理,增強識別率.這里先通過截圖,截取圖片中需要識別的部分
        img = ImageHelper.convertImageToGrayscale(img);
        // 圖片銳化,自己使用中影響識別率的主要因素是針式打印機字跡不連貫,所以銳化反而降低識別率
        img = ImageHelper.convertImageToBinary(img);
        // 圖片放大5倍,增強識別率(很多圖片本身無法識別,放大7倍時就可以輕易識,但是考濾到客戶電腦配置低,針式打印機打印不連貫的問題,這里就放大7倍)
        img = ImageHelper.getScaledInstance(img, img.getWidth() * 7, img.getHeight() * 7);
        
        // 創建ITesseract對象
        ITesseract instance = new Tesseract();
        //設置訓練庫的位置
        String path = ClassLoader.getSystemResource("tessdata").getPath();
        if (path.length()>0){
            path = path.substring(1);
        }
        //打印一下路徑,看有沒有問題
        System.out.println("tessdata:"+path);
        instance.setDatapath(path);
        //chi_sim :簡體中文, eng    根據需求選擇語言庫
        instance.setLanguage("eng");
        String result = null;
        try {
            long startTime = System.currentTimeMillis();
            result =  instance.doOCR(file);
            long endTime = System.currentTimeMillis();
            System.out.println("Time is:" + (endTime - startTime) + " 毫秒");
        } catch (TesseractException e) {
            e.printStackTrace();
        }

        System.out.println("result: "+result);
    }
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM