Jiwift

[iOS/Swift] Swift OCR TesseractOCRiOS를 이용한 글자 인식(Text Recognition) 본문

라이브러리/기타

[iOS/Swift] Swift OCR TesseractOCRiOS를 이용한 글자 인식(Text Recognition)

지위프트 2023. 2. 11. 11:49
반응형

[iOS/Swift] Swift OCR - TesseractOCRiOS를 이용한 글자 인식(Text Recognition)

iOS Swift 환경에서 OCR을 적용해보려고 합니다. 정확한 이해보다는 실행에 중점을 두고 진행하겠습니다. 사용할 라이브러리 이름은 TesseractOCRiOS입니다.

 

진행 순서

1. 소개

2. 설치

3. 간단 사용법

4. traineddata 추가

5. 결과

 

gali8/Tesseract-OCR-iOS: Tesseract OCR iOS is a Framework for iOS7+, compiled also for armv7s and arm64. (github.com)

 

GitHub - gali8/Tesseract-OCR-iOS: Tesseract OCR iOS is a Framework for iOS7+, compiled also for armv7s and arm64.

Tesseract OCR iOS is a Framework for iOS7+, compiled also for armv7s and arm64. - GitHub - gali8/Tesseract-OCR-iOS: Tesseract OCR iOS is a Framework for iOS7+, compiled also for armv7s and arm64.

github.com

 지난번에 소개한 라이브러리보다 많은 수에 Star가 있는 것을 확인할 수 있습니다. 첫 등장이 거의 10년전인 만큼 OCR이 많이 나오지 않았을 때 많은 사용자들이 사용했기 때문에 그런 게 아닐까 추측합니다.

 

 마지막 commit 이력입니다. 

 

 라이브러리 설명입니다. tesseract 3.03을 지원하고 있습니다. 현재 git 기준으로 5.x까지 나온걸 생각하면 구형 버전이긴 합니다. 

 

 설치는 Carthage를 사용한다고 나옵니다. git에 소개는 없어도 pod을 지원하기 때문에 저는 pod을 사용해 설치했습니다.

 

pod 'TesseractOCRiOS'
post_install do |installer|
  installer.pods_project.targets.each do |target|
    if target.name == 'TesseractOCRiOS'
      target.build_configurations.each do |config|
        config.build_settings['ENABLE_BITCODE'] = 'NO'
      end
      header_phase = target.build_phases().select do |phase|
        phase.is_a? Xcodeproj::Project::PBXHeadersBuildPhase
      end.first

      duplicated_header_files = header_phase.files.select do |file|
        file.display_name == 'config_auto.h'
      end

      duplicated_header_files.each do |file|
        header_phase.remove_build_file file
      end
    end
  end
end

위 두 내용을 입력했습니다. 

 

 

2022.11.20 - [라이브러리/CocoaPods] - [iOS/Swift] CocoaPods Pod Init - 라이브러리 설치 준비 pod 준비

pod을 통한 설치 방법은 위 링크를 통해 확인 가능합니다.

 

 설치 완료된 모습입니다.

 

 코드는 매우 간단합니다.

 

import TesseractOCR

라이브러리를 먼저 import 하고

 

if let tesseract = G8Tesseract(language: "kor+eng") {
    // 2
    tesseract.engineMode = .tesseractOnly
    // 3
    tesseract.pageSegmentationMode = .auto
    // 4
    tesseract.image = UIImage(named: "NameCardTop")!
    // 5
    tesseract.recognize()

    print(tesseract.recognizedText)
}

동작하는 코드를 작성하면 됩니다. 이미지 불러오기는 카메라로 찍은 이미지를 사용하거나 리소스를 사용해도 됩니다. 저는 테스트하기 위해서 명함 이미지를 구해서 리소스에 넣어두었습니다.

(코드 작성해도 아직 작동 안 합니다.)

 

코드를 먼저 보면 launages와 engineMode를 설정합니다.

 

launages는 사용하고 싶은 언어 '+'를 사용해서 추가합니다.

engineMode는 TesseractOnly, CubeOnly, TesseractCubeCombined로 총 3가지가 존재합니다.

(글 작성 기준 cube가 포함된 모드는 cube 데이터가 필요한데,, 한글은 없어서 테스트를 진행하지 않았습니다.)

 

 

 이제 traineddata를 프로젝트에 추가해야 합니다. Tesseract 엔진은 미리 train 된 데이터를 사용한다고 합니다. Tesseract는 4.0 버전부터는 LSTM이 추가되어 같이 돌아간다고 합니다. 현재 라이브러리는 Tesseract3.x대를 지원하는 것으로 git에 표시됩니다.

 

이 라이브러리는 4.x대 traineddata 버전은 작동하지 않는 것으로 보입니다. 저는 3.04.00 traineddata를 사용했습니다.

 

traineddata는 'traineddata'와 'traineddata_best'가 있습니다.

Git에서 태그로 버전을 변경하는 모습

traineddata : 기본입니다. git에서 태그를 변경해서 3.x대부터 4.x대까지 선택 가능합니다.

traineddata_best : Tesseract 4.0에서만 지원한다고 나옵니다. 현재 Tesseract는 5.0까지 나왔는데 Swift에서 사용 가능한 라이브러리가 없어 테스트를 진행하지 못했습니다. 커밋 내용을 봤을 때 SwiftyTesseract는 4.x을 지원하는 것 같습니다. (정확한 버전을 알고 있으신 분은 알려주시면 감사하겠습니다.)

(저는 traineddata_best는 사용하지 않았습니다. traineddata도 3.x대 버전만 사용했습니다.)

 

다운로드 링크

tesseract-ocr/tessdata: Trained models with support for legacy and LSTM OCR engine (github.com)

tesseract-ocr/tessdata_best: Best (most accurate) trained LSTM models. (github.com)

 

프로젝트 구조

 프로젝트 폴더 구성에 'tessdata'라는 폴더를 두고 그 안에 언어 데이터를 넣어야 합니다. 하지만 여기서 저는 약간 문제가 있던 게 폴더를 xcode에서 생성하고 언어 데이터를 이동시키면 인식이 안 되는 문제가 있었습니다.

 

그래서 이렇게 외부에서 폴더를 만들어 그 안에 언어 데이터를 넣어두고 이동하는 방식을 사용했어야 했습니다.

(왜 그러는지는 모르겠는데,,, 이유를 아시는 분 있나요?)

 

 외부에서 생성한 tessdata 폴더를 넣을 땐 위와 같이 설정하고 넘겨야 합니다. 그냥 일본 애가 설명한 글에 이렇게 나와있길래 따라 하니 잘됩니다.. 

 

이제 원하는 언어를 코드로 설정하고 그 언어들 데이터까지 프로젝트에 넣어준다면 인식이 가능해집니다.

 

저는 인터넷에서 찾아 위 명함을 가지고 테스트를 진행했습니다.

 

현재 상태에서는 모드 1개와 traineddata도 한 버전만 가능해서 테스트 한 번만 진행했습니다.

시간 : 약 0.7732850313186646
결과 : 실장 강엔젤\n\nT. 02>326-3913\nF. 02-000-0000\n\nM. 010-0000-0000\n\nE. niacom2014@naver.com\n엔첼시 앤첼구 앤출르 1004\'\n엔췌티워 1004충\n\n \n\n

 

 

 

참고

Tesseract OCR Tutorial for iOS | Kodeco

 

Tesseract OCR Tutorial for iOS

In this tutorial, you’ll learn how to read and manipulate text extracted from images using OCR by Tesseract.

www.kodeco.com

4. Swift_ Tesseract OCR Tutorial for iOS (tistory.com)

 

4. Swift_ Tesseract OCR Tutorial for iOS

www.raywenderlich.com/2010498-tesseract-ocr-tutorial-for-ios Tesseract OCR Tutorial for iOS In this tutorial, you’ll learn how to read and manipulate text extracted from images using OCR by Tesseract. www.raywenderlich.com Optical Character Recognition (

hongssup.tistory.com

OCR 사용해보기 (tistory.com)

 

OCR 사용해보기

오늘은 OCR을 사용해볼 거예요. OCR은 글자를 인지해 텍스터 데이터로 바꾸는 기술입니다. 오늘 사용할 라이브러리는 테서랙트의 OCR 엔진 서비스입니다. 테서랙트는 구글에서 개발을 후원했고 200

hyerios.tistory.com

 

반응형