작업환경 세팅

!pip install easyocr

⇒ Colab에서 진행할 때 !를 쓰면 cmd 명령어 효과를 낼 수 있다.

작업환경 세팅 - easyocr pip.png

실제 easyOCR을 적용시킨 결과이다.

import easyocr
import numpy as np
from PIL import ImageFont, ImageDraw, Image
import cv2 
import random 
import matplotlib.pyplot as plt

reader = easyocr.Reader(['ko', 'en'])
result = reader.readtext("/content/drive/MyDrive/Nengcipe/OCR/Goods/data1.jpg")
img    = cv2.imread("/content/drive/MyDrive/Nengcipe/OCR/Goods/data1.jpg")
img = Image.fromarray(img)
font = ImageFont.truetype("/content/drive/MyDrive/Nengcipe/OCR/fonts/HMKMRHD.TTF",25)
draw = ImageDraw.Draw(img)
np.random.seed(42)
COLORS = np.random.randint(0, 255, size=(255, 3),dtype="uint8")
for i in result :
    x = i[0][0][0] 
    y = i[0][0][1] 
    w = i[0][1][0] - i[0][0][0] 
    h = i[0][2][1] - i[0][1][1]

    color_idx = random.randint(0,255) 
    color = [int(c) for c in COLORS[color_idx]]
    draw.rectangle(((x, y), (x+w, y+h)), outline=tuple(color), width=2)
    draw.text((int((x + x + w) / 2) , y-2),str(i[1]), font=font, fill=tuple(color),)
plt.imshow(img)
plt.show()

작업환경세팅.png

결과

코드 문제점

opencv를 활용하여 easyOCR를 사용하면, 영어 이외의 언어는 ????로 보이게 된다. (인코딩 문제x)

코드 해결점

Pillow를 이용해 텍스트를 그릴 때 언어 관계없이 결과를 확인할 수 있다.

주의점

모든 루트는 colab 즉, drive 저장된 img 루트로 작성해야한다.

colab의 불편함.png

colab의 불편함2.png

OPENCV를 통한 전처리

OCR 인식의 정확도를 높이기 위해 contour 인식이 필요하다.