Programming/JAVA

[Tesseract] 이미지에서 텍스트 추출하기 (OCR)

빈쿵바라기 2022. 9. 14. 10:53

OCR 이란?

OCR(Optical Character Recognition)은 광학 문자 인식이라는 뜻으로, 텍스트 이미지를 기계가 읽을 수 있는 텍스트 포맷을 변환하는 과정입니다.

 

테서랙트(Tesseract) 란?

테서랙트(Tesseract)는 OCR 엔진 중 하나로 다양한 운영체제를 위한 광학 문자 인식(OCR) 엔진입니다. 이 소프트웨어는 Apache License, 버전 2.0에 따라 배포되는 무료 소프트웨어이며 Google에서 개발을 후원했습니다.

 

Tesseract 공식 문서

 

Tesseract documentation

Documentation

tesseract-ocr.github.io

 

의존성 추가

<!-- https://mvnrepository.com/artifact/net.sourceforge.tess4j/tess4j -->
<dependency>
    <groupId>net.sourceforge.tess4j</groupId>
    <artifactId>tess4j</artifactId>
    <version>4.5.2</version>
</dependency>

 

tessdata 다운로드(LSTM 학습이 된 언어데이터)

  훈련된 모델 속도 정확성 Supports legacy Retrainable
tessdata Legacy + LSTM tessdata-best보다 빠름 tessdata-best보다 덜 정확함 Yes No
tessdata-best LSTM only 가장 느림 가장 정확함 No Yes
tessdata-fast Integerized LSTM of a smaller network than tessdata-best 가장 빠름 가능 정확하지 않음 No No

 

예제 코드

public void test() throws Exception {
		
    Tesseract tesseract = new Tesseract();

    // this includes the path of tessdata inside the extracted folder  
    // tessract 언어 데이터 폴더 경로 
    tesseract.setDatapath("tessdata 경로"); 
    // 추출할 텍스트의 언어 지정(여러 언어라면 +를 이용하여 지정)
    tesseract.setLanguage("kor+eng"); 
    // OCR Engine Mode
    tesseract.setOcrEngineMode(1); 
    // Page Segmentation Mode
    tesseract.setPageSegMode(6);


    // specifying the image that has to be read  
    // 로컬 이미지일 경우
    String text1 = tesseract.doOCR(new File("로컬 이미지 경로"));    
    System.out.print(text1);
    
    // 외부 이미지일 경우
    BufferedImage image = ImageIO.read(new URL("외부 이미지 URL"));
    String text2 = tesseract.doOCR(image);    
    System.out.print(text2);
}

 

Tesseract 지원 언어

 

Languages supported in different versions of Tesseract

Tesseract documentation

tesseract-ocr.github.io

 

OCR Engine Modes

  • 0 : Legacy engine only.
  • 1 : Neural nets LSTM engine only. (tessdata_best, tessdata_fast는 oem 1만 지원함)
  • 2 : Legacy + LSTM engines.
  • 3 : Default, based on what is available.

 

Page Segmentation Modes

  • 0 : Orientation and script detection (OSD) only.
  • 1 : Automatic page segmentation with OSD.
  • 2 : Automatic page segmentation, but no OSD, or OCR.
  • 3 : Fully automatic page segmentation, but no OSD. (Default)
  • 4 : Assume a single column of text of variable sizes.
  • 5 : Assume a single uniform block of vertically aligned text.
  • 6 : Assume a single uniform block of text.
  • 7 : Treat the image as a single text line.
  • 8 : Treat the image as a single word.
  • 9 : Treat the image as a single word in a circle.
  • 10 : Treat the image as a single character.
  • 11 : Sparse text. Find as much text as possible in no particular order.
  • 12 : Sparse text with OSD.
  • 13 : Raw line. Treat the image as a single text line, bypassing hacks that are Tesseract-specific.

 

성능 개선하기(이미지 전처리)

  1. Fix DPI (if needed) 300 DPI is minimum
  2. Fix text size (e.g. 12 pt should be ok)
  3. Try to fix text lines (deskew and dewarp text)
  4. Try to fix illumination of image (e.g. no dark part of image)
  5. Binarize and de-noise image
    1. 이미지 이진화(Binarize)