App下載

簡單應(yīng)用JavaCV實(shí)現(xiàn)圖片OCR文字識(shí)別

青絲繭 2021-08-19 15:29:14 瀏覽數(shù) (6289)
反饋

現(xiàn)在識(shí)別的產(chǎn)品非常的多,例如人臉識(shí)別、文字識(shí)別以及語音識(shí)別等等。下面,分享一篇用短短幾行的Java代碼就可以實(shí)現(xiàn)圖片OCR文字識(shí)別的文章,有興趣的小伙伴們可以參考學(xué)習(xí)本文章。

spring boot項(xiàng)目pom文件中添加以下依賴 

		<!-- https://mvnrepository.com/artifact/org.bytedeco/javacv-platform -->
		<dependency>
			<groupId>org.bytedeco</groupId>
			<artifactId>javacv-platform</artifactId>
			<version>1.5.5</version>
		</dependency>

單類代碼實(shí)現(xiàn),復(fù)制到idea編輯器里,右鍵run運(yùn)行即可。

OCR方法參數(shù)說明,

1.lng 語言類型 分為兩種 1.eng 英語 2.chi_sim 中文簡體

2.dataPath 語言數(shù)據(jù)集文件夾路徑

3.imagePath 需要識(shí)別的圖片文件路徑

 
import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.leptonica.PIX;
import org.bytedeco.leptonica.global.lept;
import org.bytedeco.tesseract.TessBaseAPI;
 
public class OcrTest {
 
    public static String OCR(String lng,String dataPath,String imagePath) {
        TessBaseAPI api=new TessBaseAPI();
        if (api.Init(dataPath, lng)!=0){
            System.out.println("error");
        }
        PIX image= lept.pixRead(imagePath);
        if (image==null){
            return "";
        }
        api.SetImage(image);
        BytePointer outText=api.GetUTF8Text();
        String result=outText.getString();
        api.End();
        outText.deallocate();
        lept.pixDestroy(image);
        return result;
    }
 
    public static void main(String[] args) {
       String text= OCR("chi_sim", "E:\traineddata", "C:\Users\tarzan\Desktop\image\test5.png");
        System.out.println(text);
    }
}

測試樣例結(jié)果

test1.jpg

test2.jpg

test3.jpg

test4.jpg

test5.jpg

到此這篇關(guān)于簡單應(yīng)用javaCV實(shí)現(xiàn)圖片OCR文字識(shí)別的文章就介紹到這了,更多相關(guān)javaCV OCR文字識(shí)別內(nèi)容,請(qǐng)搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,也希望大家以后多多支持!

0 人點(diǎn)贊