본문 바로가기

DeepLearning/OCR_

[OCR] CRNN_Model에 사용한 다른 util.py

반응형

crnn 학습 시 사용한 다양한 util에 대해서 소개

 

 

1. 한국어 데이터셋 부족 

< Data Augmentation >

# 라이브러리 로딩

import scipy
from scipy import ndimage
import matplotlib.pyplot as plt

- scipy를 사용해서 data augmentation을 할 예정

- crnn model에 grayscale로 들어가기 때문에, 다양한 방식을 사용하지않고 blur와 sharpening만 사용 할 예정

 

 

# 이미지 전처리

f = img
print(f.shape) #32, 256

blurred_f = ndimage.gaussian_filter(f, 1)

filter_blurred_f = ndimage.gaussian_filter(blurred_f, 4)

alpha = 50
sharpened = blurred_f + alpha * (blurred_f - filter_blurred_f)

- 이미지를 불러와서 crop_word함수를 사용해 현재 텍스트만 있는 이미지 상태라고 가정하고 시작

- f는 원본 이미지

- blurred_f는 원본 이미지에 블러 처리

- sharpened는 원본 이미지에 샤프닝 처리

 

 

# 결과 확인하기

plt.figure(figsize=(12, 4))

plt.subplot(131)
plt.imshow(f, cmap=plt.cm.gray)
plt.axis('off')
plt.subplot(132)
plt.imshow(blurred_f, cmap=plt.cm.gray)
plt.axis('off')
plt.subplot(133)
plt.imshow(sharpened, cmap=plt.cm.gray)
plt.axis('off')

plt.tight_layout()
plt.show()

- matplotlib을 활용하여 위의 3가지 형태의 이미지를 확인한다.

 

- 위와 같이 : 원본 > 블러 > 샤프닝 순서로 이미지가 출력된다.

- 우리는 위 코드를 사용하여 실제 이미지가 모델에 들어갈때 랜덤하게 위의 augmentation처리가 되도록 할 예정

 

 

crnn_data_fcl_aug_merge.py의 부분
for w in range(len(words)):
    su = random.randrange(1,4)
    if su == 1: # Blurring
    f = words[w][:,:,0]
    blurred_f = ndimage.gaussian_filter(f, 1)
    words[w] = np.expand_dims(blurred_f, axis=2)
    elif su == 2: # Sharpening
    f = words[w][:,:,0]
    alpha = 50
    blurred_f = ndimage.gaussian_filter(f, 1)
    filter_blurred_f = ndimage.gaussian_filter(blurred_f, 1)
    sharpened = blurred_f + alpha * (blurred_f - filter_blurred_f)
    words[w] = np.expand_dims(sharpened, axis=2)
    elif su ==3:
    words[w] = words[w]

- crop_words함수를 통과하고 나온 이미지들이 위의 data augmentation을 거친다.

 

반응형