Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Archives
Today
Total
관리 메뉴

잠 안 올 때 끄적끄적

OCR 입문기 - tessearct / pytesseract / 사진 한국어로 읽기 본문

코딩

OCR 입문기 - tessearct / pytesseract / 사진 한국어로 읽기

kerp 2023. 3. 4. 14:24

1. tessearct 설치

2. lang 한국어 추가


안익숙한 맥에 안익숙한 vscode 안익숙한 플젝 하려니까 애먹네 끙

pytesseract쓰려는데 자꾸

TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.

이게 뜨는거다. 나는 VSCODE에다가 pip으로 깐 건데 뭔 path를 찾아? 하면서 찾아봤는데 다들 exe 어쩌구 하는거다...

보니까 

일단 exe파일이 있어야 하는 게 맞고, pytesseract는 이 tesseract-ocr software에 접근할 수 있게 해 주는  wrapper래.

그래서 로컬에 깔긴 해야하더라고. 이제 해보자.

 

1. tessearct설치

 

터미널을 켜고 brew로 설치한다.

brew install tesseract

ㅎ.. 첨에 맥포트로 받아라 어쩌구 해서 그랬다가 일 두번함 ㅠ 

그담에 tesseract를 설치해줌 그래야 경로없다고 에러가 안 뜬다.

pip install tesseract
pip install tesseract-ocr

 

자 이제 잘 설치가 됨. 

내 설치파일 경로는 /opt/homebrew/Cellar/tesseract/5.3.0_1 다. 

 

(만약 경로를 몰라도 확인 가능)

brew info tesseract

 

 

2. lang 한국어 추가

 

이제 한국어로도 인식할 수 있게 해 볼 거다. 이건 다운이 필요함. 

 

1. 아래 git으로 가서 해당하는 언어를 받아준다. 나는 한국어 할거니까, kor.traineddata 를 받는다.

https://github.com/tesseract-ocr/tessdata

 

GitHub - tesseract-ocr/tessdata: Trained models with support for legacy and LSTM OCR engine

Trained models with support for legacy and LSTM OCR engine - GitHub - tesseract-ocr/tessdata: Trained models with support for legacy and LSTM OCR engine

github.com

 

2. 이걸 설치경로의 "tessdata"폴더에 넣어준다. 저 폴더 없으면 만들어서 넣으면 된다. 

3. 이제 옵션으로 링크를 넣어준다. 

text = pytesseract.image_to_string('image.png', lang=lang, config=f'--tessdata-dir "{korean_data_path}"')

저 대괄호 지우고 path넣으면 된다. 그리고 print(text)하면 됨 끝!

이 방법은 코드를 실행할때마다 언어파일을 읽어오는 방법이다. 그래서 만약 언어를 다양하게 쓸 거면 시간이 걸리기 때문에, tesseract-ocr package를 다운받아서 설치하는 게 좋다고 한다. 

 

>> 전체 코드

import cv2
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r'/opt/homebrew/Cellar/tesseract/5.3.0_1/tesseract.exe'
img_path = '/Users/d/Downloads/Screenshot_20230302_234348_Chrome.png'

# read the image
image = cv2.imread(img_path)

# convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# apply thresholding to preprocess the image
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# apply median blurring to remove noise
thresh = cv2.medianBlur(thresh, 3)



# perform text detection using Tesseract
text = pytesseract.image_to_string(thresh, lang="kor+eng", config=f'--tessdata-dir "/opt/homebrew/Cellar/tesseract/5.3.0_1/tessdata/"')

# print the recognized text
print(text)

 

영어는 못 읽고 띄워쓰기가 안되네 흠

'코딩' 카테고리의 다른 글

(1) mac M1에서 DNN 돌리기 - conda 설치 / miniforge3  (0) 2023.03.16