[python] Tesseract-OCR 글자 인식

Hanbi·2021년 12월 6일
2
post-thumbnail

OCR 엔진을 활용한 글자 인식

사전작업

  1. tesseract 프로그램 설치
    https://github.com/UB-Mannheim/tesseract/wiki

  2. tesseract 환경변수 등록
    2-1. cmd로 tesseract 설치 확인

  3. 한글팩 설치⭐
    다운 받아야하는 학습된 한글 데이터 파일명: kor.traineddata
    파일 위치: tesseract가 설치된 경로 C:\Program Files\Tesseract-OCR\tessdata
    https://github.com/tesseract-ocr/tessdata/

  4. 필요한 라이브러리 패키지 설치

pip install pillow
pip install pytesseract

본작업

import pytesseract

pytesseract.pytesseract.tesseract_cmd = r'C:\\Program Files\\Tesseract-OCR\\tesseract'
config = ('-l kor+eng --oem 3 --psm 11')
text = pytesseract.image_to_string(src_filtering, config=config)
print('==========텍스트 인식 결과==========')
print(text)

결과

이미지 해상도의 문제인지 인식률에는 조금 문제가 있다...😅



텍스트로 인식된 영역의 위치 bounding box로 표시

텍스트로 인식된 영역의 위치를 bounding box로 표시하기 위해 MSER 알고리즘을 이용.
detectRegions() 함수를 이용하여 이미지 오브젝트 위치를 구하고, rectangle() 함수를 이용해 해당 위치에 bounding box를 그림.

mser = cv2.MSER_create()
regions, _ = mser.detectRegions(src_transform)

hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]

for j, cnt in enumerate(hulls):
    x, y, w, h = cv2.boundingRect(cnt)

    if w > 160:
        continue

    cv2.rectangle(dst, (x, y), (x + w, y + h), (0, 255, 0), 1)

cv2.imwrite('receipt_output.jpg', dst)

⭐bounding box를 컬러로 표시하기 위해 그레이스케일 영상을 컬러 영상으로 바꿔줘야 함

결과

profile
👩🏻‍💻

1개의 댓글

comment-user-thumbnail
2022년 3월 15일

파이참에서 본 작업을 돌리는데 src_filtering이 정의 되어있지 않다고 하는데 문항에는 해당 항목이 없어요. 혹시 별도로 정의 하신 건가요?

답글 달기