from tensorflow import keras
model = keras.models.load_model('asl_model')
model.summary()
새로운 이미지를 가지고 prediction을 해보자.
폴더를 확인해보면, 새로운 이미지가 기존 grayscale 28*28보다 더 높은 해상도에 컬러 이미지임을 알 수 있다.
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def show_image(image_path):
image=mpimg.imread(image_path)
plt.imshow(image, cmap='gray')
show_image('data/asl_images/b.png')
from tensorflow.keras.preprocessing import image as image_utils
def load_and_scale_image(image_path):
image= image_utils.load_img(image_path, color_mode="grayscale", target_size=(28,28))
return image
image = load_and_scale_image('data/asl_images/b.png')
plt.imshow(image, cmap='gray')
image = image_utils.img_to_array(image)
image = image.reshape(1, 28, 28, 1) # 이미지 1개 28*28 grayscale
image = image / 255
prediction = model.predict(image)
print(prediction)
import numpy as np
np.argmax(prediction)
우리의 알파벳 data는 j와 z를 제외하고 있다(Moving gesture를 포함하기 때문)
alphabet = "abcdefghiklmnopqrstuvwxy"
alphabet[np.argmax(prediction)]
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing import image as image_utils
from tensorflow import keras
import numpy as np
def predict_letter(file_path):
model = keras.models.load_model('asl_model')
image = mpimg.imread(file_path)
plt.imshow(image, cmap='gray')
#Load and scale image
image = image_utils.load_img(file_path, color_mode="grayscale", target_size=(28,28))
#convert to array
image=image_utils.img_to_array(image)
#reshape imge
image = image.reshape(1, 28, 28, 1)
#normalize
image = image/255
#prediction
prediction = model.predict(image)
#convert prediction to letter
alphabet = "abcdefghiklmnopqrstuvwxy"
predicted_letter = alphabet[np.argmax(prediction)]
return predicted_letter
위에서 만든 함수들을 그대로 쓴다면
def predict_letter(file_path):
show_image(file_path)
image = load_and_scale_image(file_path)
image = image_utils.img_to_array(image)
image = image.reshape(1, 28, 28, 1)
image = image/255
prediction = model.predict(image)
predicted_letter = alphabet[np.argmax(prediction)]
return predicted_letter
predict_letter("data/asl_images/b.png")
predict_letter("data/asl_images/a.png")
import IPython
app = IPython.Application.instance()
app.kernel.do_shutdown(True)