1. 程式人生 > >java 使用 Tess4J 進行影象識別

java 使用 Tess4J 進行影象識別

在網上找了好多關於orc的資料,文件自己也demo了好幾個都不咋地靠譜.後來發現一個挺不錯的文件,自己也試著demo了下沒有

再想前幾個一樣報各種未知的錯誤.

轉 原文地址

圖片成功識別出來,但是識別率還是有點低..

第一步下載 Tess4J 的一些資源  http://tess4j.sourceforge.net/codesample.html

下載下來後解壓

然後在eclipse 或者 idea 裡面新建一個 java 工程(普通java工程就可以)

然後將上面的 lib,tessdata(語音包) 包 copy到你的工程下面去. 還需要將 dist 中的 tess4j.jar 包放在lib下面

再前面找的一些資料,部落格 讓把src包也直接複製過去. 但是我測試的結果時失敗,應該是我少了一些步驟吧,我也沒找出來.

然後呢將新增的jar包 如果是eclipse 直接選中所有jar包右擊(Build path --> configure build path)新增到專案中去.

idea的話 我也剛用所有很多操作都不熟悉,在此將步驟貼出來

點選頭部選單file  --> Project Structure 也可以使用快捷鍵(Ctrl+Alt+Shift+s)

 

然後開啟選擇 Modules -> Dependencies - > 然後點選 +  -> Jars or directores

然後將你要匯入的jar包選中點選ok

最後就是 Test

 

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 = "D://java//eclipse-work//web//orc";
        // 要識別的圖片
        File file = new File("E://2.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("chi_sim");
        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: ");
        System.out.println(result);
    }
}

執行結果  原圖:

大致還是解析出來了//,識別率還是有點低.