이번 영상에서는 Keras와 OpenCV를 사용하여 직접 쓴 손글씨를 인식하는 방법을 소개합니다. 









영상에서 사용한 코드입니다. 


from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.preprocessing.image import ImageDataGenerator


batch_size = 128
num_classes = 10
epochs = 12


img_rows, img_cols = 28, 28

(x_train, y_train), (x_test, y_test) = mnist.load_data()


x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)


x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255


y_train = to_categorical(y_train, num_classes)
y_test = to_categorical(y_test, num_classes)

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
                activation='relu',
                input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])


datagen = ImageDataGenerator(
        rotation_range=40,
        width_shift_range=0.2,
        height_shift_range=0.2,
        shear_range=0.2,
        zoom_range=0.2,
        fill_mode='nearest')


model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size),
                    steps_per_epoch=x_train.shape[0] // batch_size,
                    validation_data=(x_test, y_test),
                    epochs=epochs, verbose=2)

score = model.evaluate(x_test, y_test, verbose=2)
print('Test loss:', score[0])
print('Test accuracy:', score[1])


model.save('model.h5')



import cv2 as cv
import numpy as np
from tensorflow.keras.models import load_model


img_color = cv.imread('test3.jpg', cv.IMREAD_COLOR)
img_gray = cv.cvtColor(img_color, cv.COLOR_BGR2GRAY)


ret,img_binary = cv.threshold(img_gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)

kernel = cv.getStructuringElement( cv.MORPH_RECT, ( 5, 5 ) )
img_binary = cv.morphologyEx(img_binary, cv. MORPH_CLOSE, kernel)

cv.imshow('digit', img_binary)
cv.waitKey(0)

contours, hierarchy = cv.findContours(img_binary, cv.RETR_EXTERNAL,
                        cv.CHAIN_APPROX_SIMPLE)

for contour in contours:

    x, y, w, h = cv.boundingRect(contour)



    length = max(w, h) + 60
    img_digit = np.zeros((length, length, 1),np.uint8)

    new_x,new_y = x-(length - w)//2, y-(length - h)//2


    img_digit = img_binary[new_y:new_y+length, new_x:new_x+length]

    kernel = np.ones((5, 5), np.uint8)
    img_digit = cv.morphologyEx(img_digit, cv.MORPH_DILATE, kernel)

    cv.imshow('digit', img_digit)
    cv.waitKey(0)

    model = load_model('model.h5')

    img_digit = cv.resize(img_digit, (28, 28), interpolation=cv.INTER_AREA)

    img_digit = img_digit / 255.0

    img_input = img_digit.reshape(1, 28, 28, 1)
    predictions = model.predict(img_input)


    number = np.argmax(predictions)
    print(number)

    cv.rectangle(img_color, (x, y), (x+w, y+h), (255, 255, 0), 2)


    location = (x + int(w *0.5), y - 10)
    font = cv.FONT_HERSHEY_COMPLEX 
    fontScale = 1.2
    cv.putText(img_color, str(number), location, font, fontScale, (0,255,0), 2)
   

    cv.imshow('digit', img_digit)
    cv.waitKey(0)
   

cv.imshow('result', img_color)
cv.waitKey(0)



작성 2020. 3. 30




포스트 작성시에는 문제 없었지만 이후 문제가 생길 수 있습니다.
댓글로 알려주시면 빠른 시일내에 답변을 드리겠습니다.

여러분의 응원으로 좋은 컨텐츠가 만들어집니다.
지금 본 내용이 도움이 되었다면 유튜브 구독 부탁드립니다. 감사합니다 : )

유튜브 구독하기


후원계좌 카카오뱅크 3355112417955

  1. Favicon of https://fishpoint.tistory.com BlogIcon 지구빵집 2020.03.31 15:28 신고

    잘보고 갑니다. 구독해요~^^

  2. BlogIcon fourleg2020@naver.com 2020.04.23 15:04

    항상 잘보고있습니다!! 이런 에러가 뜨네요... 어떻게 처리하나요

    Traceback (most recent call last):
    File "C:/Users/wnsgh/PycharmProjects/TensorFlow_example/img test2.py", line 30, in <module>
    img_digit = cv.morphologyEx(img_digit, cv.MORPH_DILATE, kernel)
    cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\core\src\matrix.cpp:757: error: (-215:Assertion failed) dims <= 2 && step[0] > 0 in function 'cv::Mat::locateROI'

    • Favicon of https://webnautes.tistory.com BlogIcon webnautes 2020.04.23 21:24 신고

      에러난 함수 실행되기전에 img_digit에 이미지가 제대로 저장되있나 imshow로 확인해보세요.

  3. 서윤 2020.05.19 13:42

    파이썬 프롬프트로 해도 같은 방식으로 작성하면 되는건가요?}

  4. 2020.05.19 15:36

    model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size),
    ... steps_per_epoch=x_train.shape[0] // batch_size,
    ... validation_data=(x_test, y_test),
    ... epochs=epochs, verbose=2)
    WARNING:tensorflow:From <stdin>:4: Model.fit_generator (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
    Instructions for updating:
    Please use Model.fit, which supports generators.
    Epoch 1/12
    468/468 - 98s - loss: 1.1417 - accuracy: 0.6151 - val_loss: 0.2093 - val_accuracy: 0.9462
    Epoch 2/12
    라고 뜨는데 무시하고 계속 설치하면 되는건가요?

    • Favicon of https://webnautes.tistory.com BlogIcon webnautes 2020.05.19 19:55 신고

      당장 사용하는데 문제가 되지 않기 때문에 무시해도 됩니다.

      이후 버전에서 API가 변경될 수 있다는 의미입니다.

+ Recent posts